From 4927720c29adaefadfef6ff8359703444491dbf7 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sun, 25 Jul 2021 21:21:54 +0200 Subject: [PATCH] Refactoring... --- .clang-format | 1 - data/shaders/menu.frag | 1 - data/shaders/pointShadowDepth.vert | 1 - imgui.ini | 11 +- src/CMakeLists.txt | 3 +- src/Controller.cpp | 9 +- src/Controller.h | 6 +- src/Entity.cpp | 186 ++++++++++++++--------------- src/Entity.h | 67 +++++++---- src/FrameBuffer.cpp | 113 +++++++++--------- src/FrameBuffer.h | 59 +++++---- src/GLBucket.cpp | 14 --- src/GLBucket.h | 19 --- src/JsonParser.cpp | 17 +-- src/JsonParser.h | 2 +- src/Light.cpp | 2 +- src/Light.h | 4 +- src/Mesh.cpp | 11 +- src/Model.cpp | 6 +- src/{World.cpp => Scene.cpp} | 47 ++++---- src/{World.h => Scene.h} | 20 ++-- src/Screen.cpp | 5 +- src/Texture.cpp | 29 +++-- src/Texture.h | 10 +- src/VertexArray.cpp | 1 - src/imgui/GeneralInfoWindow.cpp | 6 +- src/imgui/GeneralInfoWindow.h | 6 +- 27 files changed, 320 insertions(+), 336 deletions(-) delete mode 100644 src/GLBucket.cpp delete mode 100644 src/GLBucket.h rename src/{World.cpp => Scene.cpp} (90%) rename src/{World.h => Scene.h} (81%) diff --git a/.clang-format b/.clang-format index 68d2527..9b156a1 100644 --- a/.clang-format +++ b/.clang-format @@ -7,7 +7,6 @@ AllowShortLoopsOnASingleLine: 'false' BreakBeforeBraces: Mozilla ColumnLimit: '120' IndentWidth: '4' -Language: Cpp PointerAlignment: Right ... diff --git a/data/shaders/menu.frag b/data/shaders/menu.frag index 59271de..08b96d9 100644 --- a/data/shaders/menu.frag +++ b/data/shaders/menu.frag @@ -13,5 +13,4 @@ uniform Material u_material; void main() { f_color = texture(u_material.texture_diffuse0, v_texCoord); - // f_color = vec4(1.0, 0.0, 0.0, 0.5); } diff --git a/data/shaders/pointShadowDepth.vert b/data/shaders/pointShadowDepth.vert index 12021bb..160b717 100644 --- a/data/shaders/pointShadowDepth.vert +++ b/data/shaders/pointShadowDepth.vert @@ -7,5 +7,4 @@ uniform mat4 u_modelMatrix; void main() { gl_Position = u_modelMatrix * vec4(a_position, 1.0f); - } diff --git a/imgui.ini b/imgui.ini index 609a38b..ecc3b9a 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,15 +1,10 @@ [Window][Debug##Default] -Pos=-18,59 +Pos=60,60 Size=400,400 Collapsed=0 -[Window][Object Modifier] -Pos=46,33 -Size=894,195 -Collapsed=0 - [Window][Debug Utils] -Pos=26,12 -Size=688,343 +Pos=9,24 +Size=689,372 Collapsed=0 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7f9791..8b93ef6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,14 +11,13 @@ add_executable(Fall-Fever Model.cpp Entity.cpp Light.cpp - World.cpp + Scene.cpp FrameBuffer.cpp Widget.cpp Screen.cpp Menu.cpp JsonParser.cpp Helper.cpp - GLBucket.cpp imgui/EntityWindow.cpp imgui/GeneralInfoWindow.cpp imgui/Handler.cpp diff --git a/src/Controller.cpp b/src/Controller.cpp index 4131750..d24c0c1 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -22,13 +22,13 @@ #include "Light.h" #include "Menu.h" #include "Model.h" +#include "Scene.h" #include "Screen.h" #include "ShaderProgram.h" #include "Texture.h" #include "VertexArray.h" #include "Widget.h" #include "Window.h" -#include "World.h" Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) { @@ -57,7 +57,7 @@ Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) // Show main menu when loading is finished... m_menu->showScreenByName("mainMenuScreen"); - m_world = new World(m_shaderPrograms); + m_world = new Scene(m_shaderPrograms); #ifdef _DEBUG m_imguiHandler = std::unique_ptr(new Imgui::Handler(m_gameWindow->getGLFWwindow())); @@ -87,11 +87,10 @@ void Controller::run() { updateExposure(getShaderProgramByName("postProcessingProgram")); - Entity *lightSource = m_world->getEntityByName("light"); + ModelEntity *lightSource = m_world->getEntityByName("light"); lightSource->setScale(0.1f); lightSource->setRotation(glm::vec3(0.f)); lightSource->setPosition(glm::vec3(-2.f, 1.5f, 2.f)); - lightSource->setIsLightSource(true); m_camera->translate(glm::vec3(0.0f, 1.5f, 5.0f)); @@ -158,7 +157,7 @@ void Controller::run() m_world->draw(m_camera->getViewProj(), m_camera->getPosition()); m_postProcessFrameBuffer->unbind(); - m_postProcessFrameBuffer->render(); + m_postProcessFrameBuffer->drawOnEntireScreen(); #ifdef _DEBUG m_imguiHandler->renderWindows(); diff --git a/src/Controller.h b/src/Controller.h index d813c69..30c9b05 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -7,7 +7,7 @@ class ShaderProgram; class Window; class EventHandler; -class World; +class Scene; class Camera; class Menu; class FrameBuffer; @@ -39,13 +39,13 @@ private: void updateWindowDimensions(); - void renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, + void renderImGui(Scene *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows); std::unique_ptr m_gameWindow; EventHandler *m_gameEventHandler; - World *m_world; + Scene *m_world; Camera *m_camera; Menu *m_menu; diff --git a/src/Entity.cpp b/src/Entity.cpp index b29e0aa..000558e 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -9,15 +9,95 @@ uint32_t Entity::s_idCounter = 0; -Entity::Entity(Prototype prototype, Model *model, ShaderProgram *shaderProgram) - : m_id(s_idCounter++), m_uniqueName(prototype.name), m_model(model), m_shaderProgram(shaderProgram) +Entity::Entity(const std::string &name) : m_id(s_idCounter++), m_uniqueName(name) +{} + +Entity::~Entity() +{} + +uint32_t Entity::getId() const +{ + return m_id; +} + +const std::string &Entity::getUniqueName() const +{ + return m_uniqueName; +} + +void Entity::translate(glm::vec3 vector) +{ + m_position += vector; + updateModelMatrix(); +} + +void Entity::rotate(glm::vec3 axis, float radians) +{ + glm::quat rotation = glm::angleAxis(radians, axis); + m_quaternion = rotation * m_quaternion; + updateModelMatrix(); +} + +void Entity::setPosition(glm::vec3 position) +{ + m_position = position; + updateModelMatrix(); +} + +void Entity::setRotation(glm::vec3 eulerAngles) +{ + m_quaternion = glm::quat(eulerAngles); + updateModelMatrix(); +} + +void Entity::setRotation(glm::vec3 axis, float radians) +{ + m_quaternion = glm::angleAxis(radians, axis); + updateModelMatrix(); +} + +void Entity::setScale(float scale) +{ + m_scale = scale; + updateModelMatrix(); +} + +void Entity::updateModelMatrix() +{ + // Translate * Rotate * Scale * vertex_vec; + // First scaling, then rotation, then translation + + // Translate + glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), m_position); + + // Rotate + glm::mat4 rotationMatrix = glm::toMat4(m_quaternion); + + // Scale + glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, m_scale)); + + m_modelMatrix = translationMatrix * rotationMatrix * scaleMatrix; +} + +glm::vec3 Entity::getPosition() const +{ + return m_position; +} + +glm::mat4 Entity::getModelMatrix() const +{ + return m_modelMatrix; +} + +ModelEntity::ModelEntity(Prototype prototype, Model *model, ShaderProgram *shaderProgram) + : Entity(prototype.name), m_model(model), m_shaderProgram(shaderProgram) { setPosition(prototype.position); setRotation(prototype.rotation); setScale(prototype.scale); } -void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) +void ModelEntity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) { m_shaderProgram->bind(); @@ -37,113 +117,29 @@ void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) m_shaderProgram->unbind(); } -void Entity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram) +void ModelEntity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *shaderProgram) { - p_shaderProgram->bind(); + shaderProgram->bind(); glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix; - p_shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj); + shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj); // Draw the model m_model->drawWithoutTextures(); - p_shaderProgram->unbind(); + shaderProgram->unbind(); } -void Entity::drawPointShadows(ShaderProgram *p_shaderProgram) +void ModelEntity::drawPointShadows(ShaderProgram *shaderProgram) { - p_shaderProgram->bind(); + shaderProgram->bind(); - p_shaderProgram->setUniform("u_modelMatrix", m_modelMatrix); + shaderProgram->setUniform("u_modelMatrix", m_modelMatrix); // Draw the model m_model->drawWithoutTextures(); - p_shaderProgram->unbind(); -} - -void Entity::translate(glm::vec3 vector) -{ - m_position += vector; - updateModelMatrix(); -} - -void Entity::rotate(glm::vec3 axis, float radians) -{ - glm::quat rotation = glm::angleAxis(radians, axis); - m_quaternion = rotation * m_quaternion; - updateModelMatrix(); -} - -void Entity::setPosition(glm::vec3 position) -{ - this->m_position = position; - updateModelMatrix(); -} - -void Entity::setRotation(glm::vec3 eulerAngles) -{ - m_quaternion = glm::quat(eulerAngles); - updateModelMatrix(); -} - -void Entity::setRotation(glm::vec3 axis, float radians) -{ - m_quaternion = glm::angleAxis(radians, axis); - updateModelMatrix(); -} - -void Entity::setScale(float scaleFactor) -{ - m_modelScale = scaleFactor; - updateModelMatrix(); -} - -void Entity::updateModelMatrix() -{ - // Translate * Rotate * Scale * vertex_vec; - // First scaling, then rotation, then translation - - // Translate - glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), m_position); - - // Rotate - glm::mat4 rotationMatrix = glm::toMat4(m_quaternion); - - // Scale - glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(m_modelScale, m_modelScale, m_modelScale)); - - m_modelMatrix = translationMatrix * rotationMatrix * scaleMatrix; -} - -void Entity::setIsLightSource(bool temp) -{ - m_isLightSource = temp; -} - -uint32_t Entity::getId() -{ - return m_id; -} - -std::string Entity::getUniqueName() -{ - return m_uniqueName; -} - -glm::vec3 Entity::getPosition() -{ - return m_position; -} - -glm::mat4 Entity::getModelMatrix() -{ - return m_modelMatrix; -} - -bool Entity::getIsLightSource() -{ - return m_isLightSource; + shaderProgram->unbind(); } Skybox::Skybox(Prototype prototype, Model *cubeModel, ShaderProgram *shaderProgram) diff --git a/src/Entity.h b/src/Entity.h index 3c1b98e..b310a68 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -4,6 +4,7 @@ #include #include +#include class VertexArray; class ShaderProgram; @@ -14,20 +15,22 @@ class Entity public: struct Prototype { + Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale) + : name(name), position(position), rotation(rotation), scale(scale) + {} + virtual ~Prototype() = default; + std::string name; - std::string modelName; - std::string shaderProgramName; glm::vec3 position; glm::vec3 rotation; float scale; }; - Entity(Prototype prototype, Model *model, ShaderProgram *shaderProgram); - ~Entity() = default; + Entity(const std::string &name); + virtual ~Entity() = 0; - void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); - void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram); - void drawPointShadows(ShaderProgram *p_shaderProgram); + uint32_t getId() const; + const std::string &getUniqueName() const; void translate(glm::vec3 vector); void rotate(glm::vec3 axis, float radians); @@ -35,34 +38,54 @@ public: void setPosition(glm::vec3 position); void setRotation(glm::vec3 eulerAngles); void setRotation(glm::vec3 axis, float radians); - void setScale(float scaleFactor); - void setIsLightSource(bool temp); + void setScale(float scale); - uint32_t getId(); - std::string getUniqueName(); - glm::vec3 getPosition(); - glm::mat4 getModelMatrix(); - bool getIsLightSource(); + glm::vec3 getPosition() const; + glm::mat4 getModelMatrix() const; -private: +protected: void updateModelMatrix(); const uint32_t m_id; static uint32_t s_idCounter; + + // TODO + std::weak_ptr m_parent; + std::vector> m_children; + std::string m_uniqueName; - Model *m_model; - ShaderProgram *m_shaderProgram; - - bool m_isLightSource = false; - glm::mat4 m_modelMatrix = glm::mat4(1.0f); - glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 m_velocity = glm::vec3(0.0f, 0.0f, 0.0f); glm::quat m_quaternion; + float m_scale = 1.0f; +}; - float m_modelScale = 1.0f; +class ModelEntity : public Entity +{ +public: + struct Prototype : public Entity::Prototype + { + Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale, + const std::string &modelName, const std::string &shaderProgramName) + : Entity::Prototype(name, position, rotation, scale), modelName(modelName), + shaderProgramName(shaderProgramName) + {} + std::string modelName; + std::string shaderProgramName; + }; + + ModelEntity(Prototype prototype, Model *model, ShaderProgram *shaderProgram); + ~ModelEntity() = default; + + void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); + void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram); + void drawPointShadows(ShaderProgram *p_shaderProgram); + +private: + Model *m_model; + ShaderProgram *m_shaderProgram; }; class Skybox diff --git a/src/FrameBuffer.cpp b/src/FrameBuffer.cpp index 39b1530..ae860b0 100644 --- a/src/FrameBuffer.cpp +++ b/src/FrameBuffer.cpp @@ -6,6 +6,24 @@ #include #include +AbstractFrameBuffer::~AbstractFrameBuffer() +{} + +void AbstractFrameBuffer::bind() const +{ + glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); +} + +void AbstractFrameBuffer::unbind() const +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +GLuint AbstractFrameBuffer::getFBO() const +{ + return m_FBO; +} + FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : m_shaderProgram(shaderProgram) { glGenFramebuffers(1, &m_FBO); @@ -21,17 +39,12 @@ FrameBuffer::~FrameBuffer() glDeleteRenderbuffers(1, &m_depthStencilBuffer); } -void FrameBuffer::bind() +GLuint FrameBuffer::getTextureId() const { - glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); + return m_colorBuffer; } -void FrameBuffer::unbind() -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} - -void FrameBuffer::render() +void FrameBuffer::drawOnEntireScreen() const { // Disable wireframe mode GLint wireframe; @@ -92,47 +105,34 @@ void FrameBuffer::generateTextures(uint32_t width, uint32_t height) unbind(); } -void FrameBuffer::setExposureCorrection(bool exposureCorrection) +void FrameBuffer::setExposureCorrection(bool exposureCorrection) const { m_shaderProgram->bind(); m_shaderProgram->setUniform("u_exposureCorrection", exposureCorrection); m_shaderProgram->unbind(); } -GLuint FrameBuffer::getTextureId() -{ - return m_colorBuffer; -} +AbstractDepthMap::~AbstractDepthMap() +{} -DepthMap::DepthMap(DepthMapType type, int RESOLUTION) +DepthMap::DepthMap(int RESOLUTION) { - glGenFramebuffers(1, &m_depthMapFBO); + glGenFramebuffers(1, &m_FBO); bind(); - if (type == DepthMapType::Normal) { + glGenTextures(1, &m_depthMap); + glBindTexture(GL_TEXTURE_2D, m_depthMap); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glGenTextures(1, &m_depthMap); - glBindTexture(GL_TEXTURE_2D, m_depthMap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, - NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glBindTexture(GL_TEXTURE_2D, 0); - glBindTexture(GL_TEXTURE_2D, 0); - - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthMap, 0); - glDrawBuffer(GL_NONE); - glReadBuffer(GL_NONE); - - } else if (type == DepthMapType::CubeMap) { - m_cubeMap = new CubeMap(RESOLUTION); - - glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_cubeMap->getTextureId(), 0); - glDrawBuffer(GL_NONE); - glReadBuffer(GL_NONE); - } + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthMap, 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "[Error] FrameBuffer is not complete!" << std::endl; @@ -146,27 +146,32 @@ DepthMap::~DepthMap() // delete m_cubeMap; } -void DepthMap::bind() -{ - glBindFramebuffer(GL_FRAMEBUFFER, m_depthMapFBO); -} - -void DepthMap::unbind() -{ - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} - -GLuint DepthMap::getFBO() -{ - return m_depthMapFBO; -} - -GLuint DepthMap::getDepthMap() +GLuint DepthMap::getDepthMap() const { return m_depthMap; } -GLuint DepthMap::getCubeMapId() +DepthMapCube::DepthMapCube(int RESOLUTION) +{ + glGenFramebuffers(1, &m_FBO); + bind(); + + m_cubeMap = new CubeMap(RESOLUTION); + + glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_cubeMap->getTextureId(), 0); + glDrawBuffer(GL_NONE); + glReadBuffer(GL_NONE); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + std::cout << "[Error] FrameBuffer is not complete!" << std::endl; + + unbind(); +} + +DepthMapCube::~DepthMapCube() +{} + +GLuint DepthMapCube::getCubeMapTextureId() { return m_cubeMap->getTextureId(); } diff --git a/src/FrameBuffer.h b/src/FrameBuffer.h index 9e891b6..849314e 100644 --- a/src/FrameBuffer.h +++ b/src/FrameBuffer.h @@ -5,58 +5,69 @@ class ShaderProgram; class CubeMap; -class FrameBuffer +class AbstractFrameBuffer +{ +public: + virtual ~AbstractFrameBuffer() = 0; + + void bind() const; + void unbind() const; + GLuint getFBO() const; + +protected: + GLuint m_FBO; +}; + +class FrameBuffer : public AbstractFrameBuffer { public: FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram); ~FrameBuffer(); - void bind(); - void unbind(); - - void render(); + void drawOnEntireScreen() const; void changeDimensions(uint32_t width, uint32_t height); - GLuint getTextureId(); - void setExposureCorrection(bool exposureCorrection); + void setExposureCorrection(bool exposureCorrection) const; + + GLuint getTextureId() const; private: void generateTextures(uint32_t width, uint32_t height); - GLuint m_FBO; GLuint m_colorBuffer; GLuint m_depthStencilBuffer; ShaderProgram *m_shaderProgram; }; -enum class DepthMapType +class AbstractDepthMap : public AbstractFrameBuffer { - Normal, - CubeMap +public: + virtual ~AbstractDepthMap() = 0; }; // FrameBuffer without color buffer. (Shadows) -class DepthMap +class DepthMap : public AbstractDepthMap { public: - // Normal depthMap with texture and point depthMap with cubeMap - DepthMap(DepthMapType type, int RESOLUTION); + DepthMap(int RESOLUTION); ~DepthMap(); - void bind(); - void unbind(); - - GLuint getFBO(); - GLuint getDepthMap(); - GLuint getCubeMapId(); + GLuint getDepthMap() const; private: - GLuint m_depthMapFBO; - - // Either a normal depthMap is used (Directional shadows) - // or a cubeMap is used (Point shadows) GLuint m_depthMap; +}; + +class DepthMapCube : public AbstractDepthMap +{ +public: + DepthMapCube(int RESOLUTION); + ~DepthMapCube(); + + GLuint getCubeMapTextureId(); + +private: CubeMap *m_cubeMap; }; diff --git a/src/GLBucket.cpp b/src/GLBucket.cpp deleted file mode 100644 index 762ddde..0000000 --- a/src/GLBucket.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "GLBucket.h" - -std::unique_ptr GLBucket::s_instance = std::unique_ptr(new GLBucket); - -GLBucket &GLBucket::instance() -{ - return *s_instance.get(); -} - -void GLBucket::runGlCall(const std::function &f) -{ - std::lock_guard lock(m_mutex); - f(); -} diff --git a/src/GLBucket.h b/src/GLBucket.h deleted file mode 100644 index 3e92051..0000000 --- a/src/GLBucket.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include -#include - -class GLBucket -{ -public: - static GLBucket &instance(); - - void runGlCall(const std::function &f); - -private: - GLBucket() = default; - - std::mutex m_mutex; - static std::unique_ptr s_instance; -}; \ No newline at end of file diff --git a/src/JsonParser.cpp b/src/JsonParser.cpp index ac12726..9f84e2a 100644 --- a/src/JsonParser.cpp +++ b/src/JsonParser.cpp @@ -41,9 +41,9 @@ std::vector JsonParser::getModelPrototypes() const return modelPrototypes; } -std::vector JsonParser::getEntityPrototypes() const +std::vector JsonParser::getEntityPrototypes() const { - std::vector entityPrototypes; + std::vector entityPrototypes; const Json::Value entitiesJson = m_root["entities"]; @@ -71,8 +71,8 @@ std::vector JsonParser::getEntityPrototypes() const entityScale = scaleJson.asFloat(); } - Entity::Prototype prototype{entityName, entityModel, entityShaderProgram, - entitiyPosition, entityRotation, entityScale}; + ModelEntity::Prototype prototype(entityName, entitiyPosition, entityRotation, entityScale, entityModel, + entityShaderProgram); entityPrototypes.push_back(prototype); } @@ -131,9 +131,6 @@ std::vector> JsonParser::getLightPrototypes() auto prototype = std::unique_ptr(new DirectionalLight::Prototype{direction, color, intensity}); - // DirectionalLight *current_directionalLight = new DirectionalLight(*prototype, shaderProgram); - // current_directionalLight->setActive(true); - prototypes.push_back(std::move(prototype)); // Pointlights @@ -161,9 +158,6 @@ std::vector> JsonParser::getLightPrototypes() auto prototype = std::unique_ptr(new PointLight::Prototype{position, color, intensity}); - // current_pointLight = new PointLight(*prototype, shaderProgram); - // current_pointLight->setActive(true); - prototypes.push_back(std::move(prototype)); } @@ -175,9 +169,6 @@ std::vector> JsonParser::getLightPrototypes() auto prototype = std::unique_ptr( new PointLight::Prototype{default_position, default_color, default_intensity}); - // PointLight *current_pointLight = new PointLight(*prototype, shaderProgram); - // current_pointLight->setActive(false); - prototypes.push_back(std::move(prototype)); } diff --git a/src/JsonParser.h b/src/JsonParser.h index 56634f9..aa14f3f 100644 --- a/src/JsonParser.h +++ b/src/JsonParser.h @@ -23,7 +23,7 @@ public: ~JsonParser(); std::vector getModelPrototypes() const; - std::vector getEntityPrototypes() const; + std::vector getEntityPrototypes() const; std::vector> getLightPrototypes() const; std::vector getScreenPrototypes() const; Skybox::Prototype getSkyboxPrototype() const; diff --git a/src/Light.cpp b/src/Light.cpp index 82f9cb3..91d46ea 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -6,7 +6,7 @@ uint32_t Light::s_idCounter = 0; Light::Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram) - : m_shaderProgram(shaderProgram), m_intensity(intensity) + : Entity("Light"), m_shaderProgram(shaderProgram), m_intensity(intensity) { m_id = s_idCounter++; m_lightColor = color * intensity; diff --git a/src/Light.h b/src/Light.h index dd2650c..39c30bb 100644 --- a/src/Light.h +++ b/src/Light.h @@ -1,5 +1,7 @@ #pragma once +#include "Entity.h" + #include #include @@ -7,7 +9,7 @@ class ShaderProgram; -class Light +class Light : public Entity { public: struct Prototype diff --git a/src/Mesh.cpp b/src/Mesh.cpp index e16a32d..b923547 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -25,14 +25,15 @@ void Mesh::draw(ShaderProgram *shaderProgram) uint8_t typeNumberCount[static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS)]{0}; glBindTexture(GL_TEXTURE_2D, 0); // Bind all textures in order to its texture unit - for (auto it = m_textures.begin(); it != m_textures.end(); it++) { - const int i = it - m_textures.begin(); + int i = 0; + for (auto it : m_textures) { + TextureType currentTextureType = it->getTextureType(); - TextureType currentTextureType = (*it)->getTextureType(); - - (*it)->bind(i, shaderProgram, typeNumberCount[static_cast(currentTextureType)]); + it->bind(i, shaderProgram, typeNumberCount[static_cast(currentTextureType)]); typeNumberCount[static_cast(currentTextureType)] += 1; + + i++; } // Draw elements diff --git a/src/Model.cpp b/src/Model.cpp index 848b72b..99d5ce1 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -1,5 +1,4 @@ #include "Model.h" -#include "GLBucket.h" #include "Mesh.h" #include "ShaderProgram.h" #include "Texture.h" @@ -97,7 +96,7 @@ void Model::loadModel(const std::string &pathToModel) std::string texturePath = m_workingPath + '/' + textureSources[i].c_str(); Texture::Prototype texturePrototype{texturePath, textureTypes[i]}; auto loadModel = [=, &mutex]() { - Texture *currentTex = new Texture(texturePrototype); + Texture *currentTex = new Texture(texturePrototype.texturePath, texturePrototype.textureType); std::lock_guard lock(mutex); m_textures.push_back(currentTex); @@ -115,8 +114,7 @@ void Model::loadModel(const std::string &pathToModel) } if (!hasNormalMap) { - Texture::Prototype texturePrototype{"data/res/models/tex/fallback_normal.png", TextureType::Normal}; - Texture *currentTex = new Texture(texturePrototype); + Texture *currentTex = new Texture("data/res/models/tex/fallback_normal.png", TextureType::Normal); m_textures.push_back(currentTex); } diff --git a/src/World.cpp b/src/Scene.cpp similarity index 90% rename from src/World.cpp rename to src/Scene.cpp index 3ad1b26..512a570 100644 --- a/src/World.cpp +++ b/src/Scene.cpp @@ -1,7 +1,8 @@ -#include "World.h" +#include "Scene.h" #include "Camera.h" #include "Controller.h" #include "Entity.h" +#include "FrameBuffer.h" #include "JsonParser.h" #include "Light.h" #include "Model.h" @@ -12,13 +13,13 @@ #include #include -World::World(std::vector shaderPrograms) +Scene::Scene(std::vector shaderPrograms) : m_shaderProgram(Controller::getShaderProgramByName("defaultProgram", shaderPrograms)), - m_depthMapDirectionalFBO(DepthMapType::Normal, SHADOW_RES) + m_depthMapDirectionalFBO(SHADOW_RES) { // Create 4 depthMaps for (int i = 0; i < 4; i++) { - DepthMap *temp_depthMap = new DepthMap(DepthMapType::CubeMap, SHADOW_RES); + DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES); m_depthMapPointFBO.push_back(temp_depthMap); } @@ -63,9 +64,9 @@ World::World(std::vector shaderPrograms) std::cout << "Loaded Skybox \"" << skyboxPrototype.texturePath << "\"" << std::endl; }); - std::vector entityPrototypes = modelParser.getEntityPrototypes(); + std::vector entityPrototypes = modelParser.getEntityPrototypes(); - std::vector entities; + std::vector entities; { for (auto &prototype : entityPrototypes) { // Get model @@ -86,7 +87,7 @@ World::World(std::vector shaderPrograms) currentProgram = Controller::getShaderProgramByName("basic", shaderPrograms); } - Entity *currentEntity = new Entity(prototype, currentModel, currentProgram); + ModelEntity *currentEntity = new ModelEntity(prototype, currentModel, currentProgram); std::cout << "Loaded Entity \"" << prototype.name << "\" with model \"" << prototype.modelName << "\"" << std::endl; @@ -120,7 +121,7 @@ World::World(std::vector shaderPrograms) m_skybox->initializeOnGPU(); } -World::~World() +Scene::~Scene() { // Iterate over depthMapPointFBO vector and delete all items for (auto it = m_depthMapPointFBO.begin(); it != m_depthMapPointFBO.end(); it++) { @@ -137,12 +138,12 @@ World::~World() delete m_skybox; } -void World::addEntity(Entity *entity) +void Scene::addEntity(ModelEntity *entity) { m_entities.push_back(entity); } -void World::removeEntityByName(std::string name) +void Scene::removeEntityByName(std::string name) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { if ((*it)->getUniqueName() == name) { @@ -154,14 +155,14 @@ void World::removeEntityByName(std::string name) std::cout << "[Warning] Entity with name " << name << " could not be removed." << std::endl; } -void World::clearEntities() +void Scene::clearEntities() { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { m_entities.erase(it); } } -void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity) +void Scene::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity) { std::vector pointLights = getPointLights(); pointLights[lightId]->setActive(active); @@ -170,7 +171,7 @@ void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 positi pointLights[lightId]->setColor(color); } -void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color) +void Scene::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color) { DirectionalLight *directionalLight = getDirectionalLight(); directionalLight->setActive(active); @@ -178,7 +179,7 @@ void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 c directionalLight->setColor(color); } -void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) +void Scene::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) { // Draw all entities for (auto it = m_entities.begin(); it != m_entities.end(); it++) { @@ -189,7 +190,7 @@ void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) // calculateShadows(); } -void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram) +void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram) { // Get old viewport dimensions to reset them later... GLint VIEWPORT[4]; @@ -285,7 +286,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg textureUnit = static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1; m_shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit); glActiveTexture(GL_TEXTURE0 + textureUnit); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapId()); + glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapTextureId()); m_shaderProgram->unbind(); } @@ -295,7 +296,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg glCullFace(GL_FRONT); } -Model *World::getModelByName(std::string name) +Model *Scene::getModelByName(std::string name) { for (auto it = m_models.begin(); it != m_models.end(); it++) { if ((*it)->getUniqueName() == name) { @@ -306,7 +307,7 @@ Model *World::getModelByName(std::string name) return nullptr; } -Entity *World::getEntityByName(std::string name) +ModelEntity *Scene::getEntityByName(std::string name) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { if ((*it)->getUniqueName() == name) { @@ -317,7 +318,7 @@ Entity *World::getEntityByName(std::string name) return nullptr; } -Entity *World::getEntityById(uint32_t id) +ModelEntity *Scene::getEntityById(uint32_t id) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { if ((*it)->getId() == id) { @@ -328,7 +329,7 @@ Entity *World::getEntityById(uint32_t id) return nullptr; } -std::vector World::getPointLights() +std::vector Scene::getPointLights() { std::vector temp_pointLights; @@ -342,7 +343,7 @@ std::vector World::getPointLights() return temp_pointLights; } -DirectionalLight *World::getDirectionalLight() +DirectionalLight *Scene::getDirectionalLight() { DirectionalLight *temp_directionalLight = nullptr; @@ -355,12 +356,12 @@ DirectionalLight *World::getDirectionalLight() return temp_directionalLight; } -std::vector World::getEntities() +std::vector Scene::getEntities() { return m_entities; } -Skybox *World::getSkybox() +Skybox *Scene::getSkybox() { return m_skybox; } diff --git a/src/World.h b/src/Scene.h similarity index 81% rename from src/World.h rename to src/Scene.h index a1d7693..f66e3f2 100644 --- a/src/World.h +++ b/src/Scene.h @@ -9,7 +9,7 @@ #include class Camera; -class Entity; +class ModelEntity; class Light; class PointLight; class DirectionalLight; @@ -17,25 +17,25 @@ class ShaderProgram; class Skybox; class Model; -class World +class Scene { public: - World(std::vector shaderPrograms); - ~World(); + Scene(std::vector shaderPrograms); + ~Scene(); - void addEntity(Entity *entity); + void addEntity(ModelEntity *entity); void removeEntityByName(std::string name); void clearEntities(); void updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity); void updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color); - std::vector getEntities(); + std::vector getEntities(); std::vector getPointLights(); DirectionalLight *getDirectionalLight(); Skybox *getSkybox(); - Entity *getEntityByName(std::string name); - Entity *getEntityById(uint32_t id); + ModelEntity *getEntityByName(std::string name); + ModelEntity *getEntityById(uint32_t id); Model *getModelByName(std::string name); void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); @@ -45,7 +45,7 @@ private: ShaderProgram *m_shaderProgram; std::vector m_models; - std::vector m_entities; + std::vector m_entities; Skybox *m_skybox; // Lights @@ -54,7 +54,7 @@ private: // Shadows const int SHADOW_RES = 4096 / 4; DepthMap m_depthMapDirectionalFBO; - std::vector m_depthMapPointFBO; + std::vector m_depthMapPointFBO; // Shadow projection matrices const float m_nearPlaneDirectional = 1.0f; const float m_farPlaneDirectional = 15.0f; diff --git a/src/Screen.cpp b/src/Screen.cpp index 716c04f..dac5aa6 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -12,7 +12,8 @@ Screen::Screen(Prototype prototype, FrameBuffer *framebuffer, ShaderProgram *sha : m_id(s_idCounter++), m_uniqueName(prototype.name), m_frameBuffer(framebuffer), m_shaderProgram(shaderProgram) { for (auto &prototype : prototype.widgetPrototypes) { - Texture *currentTexture = new Texture(prototype.texturePrototype); + auto texturePrototype = prototype.texturePrototype; + Texture *currentTexture = new Texture(texturePrototype.texturePath, texturePrototype.textureType); currentTexture->initializeOnGPU(); Widget *currentWidget = new Widget(prototype, currentTexture); @@ -70,6 +71,6 @@ void Screen::draw() const } m_frameBuffer->unbind(); - m_frameBuffer->render(); + m_frameBuffer->drawOnEntireScreen(); m_frameBuffer->setExposureCorrection(true); } diff --git a/src/Texture.cpp b/src/Texture.cpp index 10088d4..62692a2 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -3,8 +3,7 @@ #include -Texture::Texture(const Prototype &prototype) - : m_texturePath(prototype.texturePath), m_textureType(prototype.textureType) +Texture::Texture(const std::string &path, TextureType type) : m_texturePath(path), m_textureType(type) { stbi_set_flip_vertically_on_load(1); m_textureBuffer = stbi_load(m_texturePath.c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0); @@ -117,12 +116,12 @@ GLuint Texture::getTextureId() return m_textureId; } -CubeMap::CubeMap(const char *texturePseudoPath) +CubeMap::CubeMap(const std::string &texturePseudoPath) { // Reserve space in vector so that elements can be accessed explicitly. - m_texturePaths.resize(CUBEMAP_FACES_NUM_ITEMS); - m_textureBuffers.resize(CUBEMAP_FACES_NUM_ITEMS); - m_numComponents.resize(CUBEMAP_FACES_NUM_ITEMS); + m_texturePaths.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); + m_textureBuffers.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); + m_numComponents.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); fillTexturePathVector(texturePseudoPath); @@ -242,19 +241,19 @@ void CubeMap::unbind() glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } -void CubeMap::fillTexturePathVector(const char *texturePseudoPath) +void CubeMap::fillTexturePathVector(const std::string &texturePseudoPath) { - for (unsigned int i = 0; i < CUBEMAP_FACES_NUM_ITEMS; i++) { - m_texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png"; - m_texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png"; - m_texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png"; - m_texturePaths[cm_bottom] = std::string(texturePseudoPath) + "bottom.png"; - m_texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png"; - m_texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png"; + for (unsigned int i = 0; i < static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS); i++) { + m_texturePaths[static_cast(cubeMapFaces::cm_front)] = texturePseudoPath + "front.png"; + m_texturePaths[static_cast(cubeMapFaces::cm_back)] = texturePseudoPath + "back.png"; + m_texturePaths[static_cast(cubeMapFaces::cm_top)] = texturePseudoPath + "top.png"; + m_texturePaths[static_cast(cubeMapFaces::cm_bottom)] = texturePseudoPath + "bottom.png"; + m_texturePaths[static_cast(cubeMapFaces::cm_left)] = texturePseudoPath + "left.png"; + m_texturePaths[static_cast(cubeMapFaces::cm_right)] = texturePseudoPath + "right.png"; } } -GLuint CubeMap::getTextureId() +GLuint CubeMap::getTextureId() const { return m_textureId; } diff --git a/src/Texture.h b/src/Texture.h index 4d66451..dac97c5 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -11,7 +11,7 @@ class ShaderProgram; // Order is important! -enum cubeMapFaces +enum class cubeMapFaces { cm_right, cm_left, @@ -31,7 +31,7 @@ public: TextureType textureType; }; - Texture(const Prototype &prototype); + Texture(const std::string &path, TextureType type); ~Texture(); void initializeOnGPU(); @@ -62,7 +62,7 @@ private: class CubeMap { public: - CubeMap(const char *texturePseudoPath); + CubeMap(const std::string &texturePseudoPath); CubeMap(int RESOLUTION); void initializeOnGPU(); @@ -72,10 +72,10 @@ public: void bind(ShaderProgram *shaderProgram); void unbind(); - GLuint getTextureId(); + GLuint getTextureId() const; private: - void fillTexturePathVector(const char *texturePseudoPath); + void fillTexturePathVector(const std::string &texturePseudoPath); bool m_isInitialized = false; diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index d0d8eac..049e018 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -1,5 +1,4 @@ #include "VertexArray.h" -#include "GLBucket.h" #include "definitions/models.h" #include diff --git a/src/imgui/GeneralInfoWindow.cpp b/src/imgui/GeneralInfoWindow.cpp index 15c3221..2b9af19 100644 --- a/src/imgui/GeneralInfoWindow.cpp +++ b/src/imgui/GeneralInfoWindow.cpp @@ -1,13 +1,13 @@ #include "GeneralInfoWindow.h" #include "../Controller.h" #include "../Entity.h" +#include "../Scene.h" #include "../ShaderProgram.h" -#include "../World.h" #include #include -Imgui::GeneralInfoWindow::GeneralInfoWindow(Controller *controller, World *world, ShaderProgram *postProcessingProgram, +Imgui::GeneralInfoWindow::GeneralInfoWindow(Controller *controller, Scene *world, ShaderProgram *postProcessingProgram, bool *rotateEntity, bool *drawShadows, bool *rotateLightSource, glm::vec3 *lightColor, float *exposure, float *intensity) : m_controller(controller), m_world(world), m_postProcessingProgram(postProcessingProgram), @@ -26,7 +26,7 @@ void Imgui::GeneralInfoWindow::addWidgets() ImGui::Checkbox("Rotate Object", m_rotateEntity); - Entity *mainObject = m_world->getEntityById(0); + ModelEntity *mainObject = m_world->getEntityById(0); mainObject->setPosition(glm::vec3(m_translation[0], m_translation[1], m_translation[2])); if (!*m_rotateEntity) { mainObject->setRotation(glm::vec3(0.f, 1.0f, 0.f), m_rotation); diff --git a/src/imgui/GeneralInfoWindow.h b/src/imgui/GeneralInfoWindow.h index 53bc463..ca23519 100644 --- a/src/imgui/GeneralInfoWindow.h +++ b/src/imgui/GeneralInfoWindow.h @@ -4,7 +4,7 @@ #include -class World; +class Scene; class ShaderProgram; class Controller; @@ -13,7 +13,7 @@ namespace Imgui { class GeneralInfoWindow : public Window { public: - GeneralInfoWindow(Controller *controller, World *world, ShaderProgram *postProcessingProgram, bool *rotateEntity, + GeneralInfoWindow(Controller *controller, Scene *world, ShaderProgram *postProcessingProgram, bool *rotateEntity, bool *drawShadows, bool *rotateLightSource, glm::vec3 *lightColor, float *exposure, float *intensity); @@ -21,7 +21,7 @@ private: void addWidgets() override; Controller *m_controller; - World *m_world; + Scene *m_world; ShaderProgram *m_postProcessingProgram; bool *m_rotateEntity;