diff options
Diffstat (limited to 'engine-ocean/Game/Environment')
-rw-r--r-- | engine-ocean/Game/Environment/Environment.cpp | 6 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/Environment.h | 12 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/environmentsystem.cpp | 40 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/environmentsystem.h | 41 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/grassenvironment.cpp | 85 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/grassenvironment.h | 37 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/skyboxenvironment.cpp | 64 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/skyboxenvironment.h | 103 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/water.cpp | 48 | ||||
-rw-r--r-- | engine-ocean/Game/Environment/water.h | 38 |
10 files changed, 474 insertions, 0 deletions
diff --git a/engine-ocean/Game/Environment/Environment.cpp b/engine-ocean/Game/Environment/Environment.cpp new file mode 100644 index 0000000..2782194 --- /dev/null +++ b/engine-ocean/Game/Environment/Environment.cpp @@ -0,0 +1,6 @@ +#include "Environment.h" + +Environment::Environment() +{ + +} diff --git a/engine-ocean/Game/Environment/Environment.h b/engine-ocean/Game/Environment/Environment.h new file mode 100644 index 0000000..90a5409 --- /dev/null +++ b/engine-ocean/Game/Environment/Environment.h @@ -0,0 +1,12 @@ +#ifndef ENVIRONMENT_H +#define ENVIRONMENT_H + +class Environment +{ +public: + Environment(); + virtual void draw() = 0; + virtual void update(double deltaTime) = 0; +}; + +#endif // ENVIRONMENT_H diff --git a/engine-ocean/Game/Environment/environmentsystem.cpp b/engine-ocean/Game/Environment/environmentsystem.cpp new file mode 100644 index 0000000..f94b804 --- /dev/null +++ b/engine-ocean/Game/Environment/environmentsystem.cpp @@ -0,0 +1,40 @@ +#include "environmentsystem.h" +#include "Game/Environment/grassenvironment.h" +#include "Game/Environment/skyboxenvironment.h" +#include "Game/Environment/water.h" + +EnvironmentSystem::EnvironmentSystem(std::map<std::string, std::shared_ptr<GameObject>>& rigid_gameobjects, + std::map<std::string, std::shared_ptr<GameObject>>& dynamic_gameobjects, + std::shared_ptr<Camera> camera) +{ + addEnviron<GrassEnvironment>(std::make_shared<GrassEnvironment>(rigid_gameobjects, dynamic_gameobjects, camera)); + addEnviron<SkyboxEnvironment>(std::make_shared<SkyboxEnvironment>(camera)); + addEnviron<Water>(std::make_shared<Water>(camera)); + + + +} + +void EnvironmentSystem::draw(){ + for (auto &environ : m_environs){ + environ.second->draw(); + } + +} + +void EnvironmentSystem::drawNonWater(){ + getEnviron<GrassEnvironment>()->draw(); + getEnviron<SkyboxEnvironment>()->draw(); + + +} + +void EnvironmentSystem::update(double deltaTime){ + for (auto &environ : m_environs){ + environ.second->update(deltaTime); + } + +} + +void EnvironmentSystem::scrollEvent(double distance){} +void EnvironmentSystem::mousePosEvent(double xpos, double ypos) {} diff --git a/engine-ocean/Game/Environment/environmentsystem.h b/engine-ocean/Game/Environment/environmentsystem.h new file mode 100644 index 0000000..240ecb7 --- /dev/null +++ b/engine-ocean/Game/Environment/environmentsystem.h @@ -0,0 +1,41 @@ +#ifndef ENVIRONMENTSYSTEM_H +#define ENVIRONMENTSYSTEM_H +#include "Game/Environment/Environment.h" +#include "Game/Systems/system.h" + +class EnvironmentSystem : public System +{ +public: + EnvironmentSystem(std::map<std::string, std::shared_ptr<GameObject>>& rigid_gameobjects, + std::map<std::string, std::shared_ptr<GameObject>>& dynamic_gameobjects, + std::shared_ptr<Camera> camera); + void draw() override; + void update(double deltaTime) override; + void scrollEvent(double distance) override; + void mousePosEvent(double xpos, double ypos) override; + void drawNonWater(); + + + template <typename T> + void addEnviron(std::shared_ptr<T> &&environ){ + m_environs.put<T>(std::forward<std::shared_ptr<T>>(environ)); + } + + + template <class T> + T* getEnviron(){ + auto comp = m_environs.find<T>(); + assert(comp != m_environs.end()); + return static_cast<T*>(comp->second.get()); + } + + template <class T> + void removeEnviron(){ + m_environs.remove<T>(); + } +private: + TypeMap<std::shared_ptr<Environment>> m_environs; + +}; + +#endif // ENVIRONMENTSYSTEM_H diff --git a/engine-ocean/Game/Environment/grassenvironment.cpp b/engine-ocean/Game/Environment/grassenvironment.cpp new file mode 100644 index 0000000..1d1ed9b --- /dev/null +++ b/engine-ocean/Game/Environment/grassenvironment.cpp @@ -0,0 +1,85 @@ +#include "grassenvironment.h" +#include "Game/Components/CollisionComponents/BoundingTriangle.h" +#include "Game/Components/CollisionComponents/CollisionComponent.h" +#include "Game/Components/TransformComponent.h" +#include "Game/GameObjects/GameObject.h" +#include "Graphics/global.h" + +GrassEnvironment::GrassEnvironment(std::map<std::string, std::shared_ptr<GameObject>>& rigid_gameobjects, + std::map<std::string, std::shared_ptr<GameObject>>& dynamic_gameobjects, + std::shared_ptr<Camera> camera): + m_rigid_gameobjects(rigid_gameobjects), + m_dynamic_gameobjects(dynamic_gameobjects), + m_camera(camera) +{ + + initializeGrassVBO(); +} + +void GrassEnvironment::initializeGrassVBO(){ +// grass_tex = Global::graphics.loadTextureFromFile("/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/meadow_texture_atlas.png").textureID; +// wind_tex = Global::graphics.loadTextureFromFile("/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/wind_flowmap1.png").textureID; +// meadow_tex = Global::graphics.loadTextureFromFile("/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/meadow_flower_map.png").textureID; + + +// std::vector<glm::vec3> surface_points = m_rigid_gameobjects["ground"]->getComponent<CollisionComponent>()->getCollisionShape<BoundingTriangle>()->getSurfacePoints(); +// positions.insert(std::end(positions), std::begin(surface_points), std::end(surface_points)); + + +// glGenVertexArrays(1, &VAO); +// glGenBuffers(1, &VBO); +// glBindVertexArray(VAO); + +// glBindBuffer(GL_ARRAY_BUFFER, VBO); +// glBufferData(GL_ARRAY_BUFFER, positions.size()*sizeof(glm::vec3), positions.data(), GL_STATIC_DRAW); + +// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); + +// // unbind +// glEnableVertexAttribArray(0); +// glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +glm::vec2 GrassEnvironment::getRandomOffset(int ub, int numRows){ + int index = floor(Global::graphics.generateRandomNumbers(0, ub)); + int column = index % numRows; + float xoffset = static_cast<float>(column)/static_cast<float>(numRows); + + int row = floor(index/numRows); + float yOffset = static_cast<float>(row)/static_cast<float>(numRows); + return glm::vec2(xoffset, yOffset); +} + +void GrassEnvironment::draw(){ + // draw grass + glPointSize(8.f); + Global::graphics.bindShader("grass"); + + // activate texture + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, grass_tex); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, wind_tex); + + glActiveTexture(GL_TEXTURE7); + glBindTexture(GL_TEXTURE_2D, meadow_tex); + + // update time + glUniform1f(glGetUniformLocation(Global::graphics.getShaderID("grass"), "u_time"), glfwGetTime()*.1f); + // update player pos + glm::vec3 playerPos = m_dynamic_gameobjects.at("player")->getComponent<TransformComponent>()->getPos(); + glUniform3f(glGetUniformLocation(Global::graphics.getShaderID("grass"), "playerPos"), playerPos.x, playerPos.y, playerPos.z); + + //glm::vec2 offset = getRandomOffset(4, 2); + // update offset + //glUniform2f(glGetUniformLocation(Global::graphics.getShaderID("grass"), "atlas_offset"), offset.x, offset.y); + + + Global::graphics.setCameraData(m_camera); + Global::graphics.setGlobalData(glm::vec3(.5f)); + glBindVertexArray(VAO); + glDrawArrays(GL_POINTS, 0, positions.size()); +}; + +void GrassEnvironment::update(double deltaTime){}; diff --git a/engine-ocean/Game/Environment/grassenvironment.h b/engine-ocean/Game/Environment/grassenvironment.h new file mode 100644 index 0000000..f92b6c3 --- /dev/null +++ b/engine-ocean/Game/Environment/grassenvironment.h @@ -0,0 +1,37 @@ +#ifndef GRASSENVIRONMENT_H +#define GRASSENVIRONMENT_H +#include "Game/GameObjects/GameObject.h" +#include "Graphics/global.h" +#include "Graphics/camera.h" +#include <GLFW/glfw3.h> +#include "Environment.h" + +class GrassEnvironment : public Environment +{ +public: + GrassEnvironment(std::map<std::string, std::shared_ptr<GameObject>>& rigid_gameobjects, + std::map<std::string, std::shared_ptr<GameObject>>& dynamic_gameobjects, + std::shared_ptr<Camera> camera); + + void draw() override; + void update(double deltaTime) override; +private: + GLuint VAO, VBO; + GLuint grass_tex, wind_tex, meadow_tex; + std::vector<glm::vec3> positions; + + + + std::map<std::string, std::shared_ptr<GameObject>>& m_rigid_gameobjects; + std::map<std::string, std::shared_ptr<GameObject>>& m_dynamic_gameobjects; + std::shared_ptr<Camera> m_camera; + + + void initializeGrassVBO(); + glm::vec2 getRandomOffset(int ub, int numRows); + + + +}; + +#endif // GRASSENVIRONMENT_H diff --git a/engine-ocean/Game/Environment/skyboxenvironment.cpp b/engine-ocean/Game/Environment/skyboxenvironment.cpp new file mode 100644 index 0000000..f0ad41f --- /dev/null +++ b/engine-ocean/Game/Environment/skyboxenvironment.cpp @@ -0,0 +1,64 @@ +#include "skyboxenvironment.h" +#include "Graphics/global.h" + + +SkyboxEnvironment::SkyboxEnvironment(std::shared_ptr<Camera> camera): + m_camera(camera), + m_rotation_mat(std::make_shared<ModelTransform>()) +{ + initializeVAO(); +} + +void SkyboxEnvironment::initializeVAO(){ + skybox_tex = Global::graphics.loadCubeMap(m_skyboxTextureFiles); + + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, m_vertices.size()*sizeof(float), m_vertices.data(), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0); + + // unbind + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + + +void SkyboxEnvironment::draw(){ + glDepthFunc(GL_LEQUAL); + glDisable(GL_CULL_FACE); + + Global::graphics.bindShader("skybox"); + //Global::graphics.setCameraData(m_camera); + + // activate texture + glActiveTexture(GL_TEXTURE3); + glBindTexture(GL_TEXTURE_CUBE_MAP, skybox_tex); + + // manually set view and projection, for non-translating view + glm::mat4 projection = m_camera->getProjection(); + glm::mat4 view = m_camera->getView(); + view[3] = glm::vec4(0.f); + + glUniformMatrix4fv(glGetUniformLocation(Global::graphics.getShaderID("skybox"), "view"), 1, GL_FALSE, glm::value_ptr(view[0])); + glUniformMatrix4fv(glGetUniformLocation(Global::graphics.getShaderID("skybox"), "projection"), 1, GL_FALSE, glm::value_ptr(projection[0])); + + // apply rotation matrix + glUniformMatrix4fv(glGetUniformLocation(Global::graphics.getShaderID("skybox"), "rotation"), 1, GL_FALSE, glm::value_ptr(m_rotation_mat->getRotation()[0])); + + //Global::graphics.setGlobalData(glm::vec3(.5f)); + glBindVertexArray(VAO); + glDrawArrays(GL_TRIANGLES, 0, m_vertices.size()); + + glDepthFunc(GL_LESS); +} + +void SkyboxEnvironment::update(double deltaTime){ + m_rotation += ROTATE_SPEED * deltaTime; + m_rotation_mat->rotate(glm::radians(ROTATE_SPEED), glm::vec3(0.f, 1.f, 0.f)); + + +} diff --git a/engine-ocean/Game/Environment/skyboxenvironment.h b/engine-ocean/Game/Environment/skyboxenvironment.h new file mode 100644 index 0000000..aa86c0c --- /dev/null +++ b/engine-ocean/Game/Environment/skyboxenvironment.h @@ -0,0 +1,103 @@ +#ifndef SKYBOXENVIRONMENT_H +#define SKYBOXENVIRONMENT_H +#include "Graphics/global.h" +#include "Graphics/camera.h" +#include <GLFW/glfw3.h> +#include "Environment.h" + + +class SkyboxEnvironment : public Environment +{ +public: + SkyboxEnvironment(std::shared_ptr<Camera> camera); + void draw() override; + void update(double deltaTime) override; +private: + void initializeVAO(); + + GLuint VAO; + GLuint VBO; + GLuint EBO; + GLuint skybox_tex; + std::shared_ptr<Camera> m_camera; + + float SIZE = 500.f; + + std::vector<int> m_vertex_indices = { + // Right + 1, 2, 6, + 6, 5, 1, + // Left + 0, 4, 7, + 7, 3, 0, + // Top + 4, 5, 6, + 6, 7, 4, + // Bottom + 0, 3, 2, + 2, 1, 0, + // Back + 0, 1, 5, + 5, 4, 0, + // Front + 3, 7, 6, + 6, 2, 3 + }; + std::vector<GLfloat> m_vertices = { + -SIZE, SIZE, -SIZE, + -SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + SIZE, SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + + -SIZE, -SIZE, SIZE, + -SIZE, -SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + -SIZE, SIZE, -SIZE, + -SIZE, SIZE, SIZE, + -SIZE, -SIZE, SIZE, + + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + + -SIZE, -SIZE, SIZE, + -SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + SIZE, -SIZE, SIZE, + -SIZE, -SIZE, SIZE, + + -SIZE, SIZE, -SIZE, + SIZE, SIZE, -SIZE, + SIZE, SIZE, SIZE, + SIZE, SIZE, SIZE, + -SIZE, SIZE, SIZE, + -SIZE, SIZE, -SIZE, + + -SIZE, -SIZE, -SIZE, + -SIZE, -SIZE, SIZE, + SIZE, -SIZE, -SIZE, + SIZE, -SIZE, -SIZE, + -SIZE, -SIZE, SIZE, + SIZE, -SIZE, SIZE + }; + + std::vector<const char*> m_skyboxTextureFiles = {"/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/right.png", + "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/left.png", + "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/up.png", + "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/bottom.png", + "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/back.png", + "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/skybox/hills2/front.png"}; + + float ROTATE_SPEED = .1f; // 1 degree per sec + float m_rotation = 0.f; + std::shared_ptr<ModelTransform> m_rotation_mat; + +}; + +#endif // SKYBOXENVIRONMENT_H diff --git a/engine-ocean/Game/Environment/water.cpp b/engine-ocean/Game/Environment/water.cpp new file mode 100644 index 0000000..537bfbd --- /dev/null +++ b/engine-ocean/Game/Environment/water.cpp @@ -0,0 +1,48 @@ +#include "water.h" +#include "GLFW/glfw3.h" + +Water::Water(std::shared_ptr<Camera> camera): + m_camera(camera) +{ + + initializeQuad(); + +} + + +void Water::initializeQuad(){ + m_waterPlane = Global::graphics.addShape("water", "/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Meshes/howl_field_water.obj"); + m_waterMT = std::make_shared<ModelTransform>(); + m_waterMT->setScale(glm::vec3(1.f)); + m_waterMT->setPos(glm::vec3(0.f)); + Global::graphics.setWaterHeight(m_waterMT->getPos().y); + + du_dv_map = Global::graphics.loadTextureFromFile_Repeat("/Users/jesswan/Desktop/cs1950u/cs1950u-jjesswan/Resources/Images/waterDUDV.png").textureID; + + +} + +void Water::draw(){ + Global::graphics.bindShader("water"); + + glActiveTexture(GL_TEXTURE8); + glBindTexture(GL_TEXTURE_2D, Global::graphics.getReflectionTexture()); + glActiveTexture(GL_TEXTURE9); + glBindTexture(GL_TEXTURE_2D, Global::graphics.getRefractionTexture()); + glActiveTexture(GL_TEXTURE10); + glBindTexture(GL_TEXTURE_2D, du_dv_map); + glUniform1f(glGetUniformLocation(Global::graphics.getShaderID("water"), "moveFactor"), m_moveFactor); + + Global::graphics.setCameraData(m_camera); + + Global::graphics.drawShape(Global::graphics.getShape("water"), m_waterMT); + +} + +void Water::update(double deltaTime){ + m_moveFactor += WAVE_SPEED*deltaTime*.01f; + // loop back to 0 if it gets to one + if (m_moveFactor == 1) m_moveFactor = 0; +} + + diff --git a/engine-ocean/Game/Environment/water.h b/engine-ocean/Game/Environment/water.h new file mode 100644 index 0000000..c0df0da --- /dev/null +++ b/engine-ocean/Game/Environment/water.h @@ -0,0 +1,38 @@ +#ifndef WATER_H +#define WATER_H +#include "Graphics/global.h" + + +#include "Environment.h" + +class Water : public Environment +{ +public: + Water(std::shared_ptr<Camera> camera); + void draw() override; + void update(double deltaTime) override; + + + +private: + + GLuint du_dv_map; + void initializeQuad(); + + + + + + std::shared_ptr<Camera> m_camera; + std::vector<glm::vec3> m_waterPlane; + std::shared_ptr<ModelTransform> m_waterMT; + + const float WAVE_SPEED = .008f; + float m_moveFactor = 0.f; + + + + +}; + +#endif // WATER_H |