From be0e3a88b1bf8b0376fad611840f9648d97af511 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sun, 5 Jun 2022 11:16:22 +0200 Subject: [PATCH] Refactor scene, disable shadows, disable exceptions --- .gitignore | 1 + data/shaders/basic.frag | 102 +++++++------- imgui.ini | 15 -- lib/CMakeLists.txt | 3 + src/CMakeLists.txt | 2 +- src/Controller.cpp | 50 +++---- src/Controller.h | 13 +- src/Entity.cpp | 46 ------- src/Entity.h | 25 +--- src/Scene.cpp | 297 ++++++++++++++++------------------------ src/Scene.h | 57 ++++---- 11 files changed, 239 insertions(+), 372 deletions(-) delete mode 100644 imgui.ini diff --git a/.gitignore b/.gitignore index 81822a4..cbdbfb4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ res/models res/textures .kdev4 .cache +imgui.ini diff --git a/data/shaders/basic.frag b/data/shaders/basic.frag index c7f271b..e8b0e88 100644 --- a/data/shaders/basic.frag +++ b/data/shaders/basic.frag @@ -60,8 +60,8 @@ uniform SpotLight u_spotLight;*/ uniform mat3 u_normalMatrix; -uniform sampler2D u_texture_directionalShadowMap; -uniform samplerCube u_texture_pointShadowMap0; +// uniform sampler2D u_texture_directionalShadowMap; +// uniform samplerCube u_texture_pointShadowMap0; //uniform samplerCube u_texture_pointShadowMap1; //uniform samplerCube u_texture_pointShadowMap2; //uniform samplerCube u_texture_pointShadowMap3; @@ -74,9 +74,9 @@ vec3 sampleOffsetDirections[20] = vec3[] ( vec3( 0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1) ); -uniform bool b_drawShadows; +// uniform bool b_drawShadows; -uniform float pointShadowDepthMapFarPlane; +// uniform float pointShadowDepthMapFarPlane; vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 viewDir); vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir); @@ -90,8 +90,8 @@ void computeShading( float computeAttenuation(vec3 lightPos, vec3 fragPos, float K_q); -float computeDirectionalShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir); -float computePointShadows(vec3 fragPos, vec3 lightPos); +// float computeDirectionalShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir); +// float computePointShadows(vec3 fragPos, vec3 lightPos); void main() { @@ -131,8 +131,8 @@ vec3 directionalLightContribution(DirectionalLight light, vec3 normal, vec3 view computeShading(ambientColor, diffuseColor, specularColor, lightDir, viewDir, normal, ambient, diffuse, specular); float shadows = 0.0f; - if(b_drawShadows) - shadows = computeDirectionalShadows(v_fragmentPositionDirectionalLightSpace, normal, lightDir); + // if(b_drawShadows) + // shadows = computeDirectionalShadows(v_fragmentPositionDirectionalLightSpace, normal, lightDir); return (ambient + (1.0f - shadows) * (diffuse + specular)); } @@ -158,8 +158,8 @@ vec3 pointLightContribution(PointLight light, vec3 normal, vec3 fragPos, vec3 vi //specular *= attenuation; float shadows = 0.0f; - if(b_drawShadows) - shadows = computePointShadows(v_fragmentPosition, light.position); + // if(b_drawShadows) + // shadows = computePointShadows(v_fragmentPosition, light.position); return (ambient + (1.0f - shadows) * (diffuse + specular)); } @@ -219,57 +219,57 @@ float computeAttenuation(vec3 lightPos, vec3 fragPos, float K_q) { } -float computeDirectionalShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir) { +// float computeDirectionalShadows(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir) { - // Perspective divide - vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; +// // Perspective divide +// vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; - // Transform from [-1,1] to [0,1] - projCoords *= 0.5f; - projCoords += 0.5f; +// // Transform from [-1,1] to [0,1] +// projCoords *= 0.5f; +// projCoords += 0.5f; - if(projCoords.z > 1.0f) return 0.0f; +// if(projCoords.z > 1.0f) return 0.0f; - float closestDepth = texture(u_texture_directionalShadowMap, projCoords.xy).r; - float currentDepth = projCoords.z; +// float closestDepth = texture(u_texture_directionalShadowMap, projCoords.xy).r; +// float currentDepth = projCoords.z; - float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); - bias *= 0.25f; +// float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); +// bias *= 0.25f; - float shadow = 0.0; - vec2 texelSize = 1.0 / textureSize(u_texture_directionalShadowMap, 0); - for(int x = -1; x <= 1; x++) { - for(int y = -1; y <= 1; y++) { - float pcfDepth = texture(u_texture_directionalShadowMap, projCoords.xy + vec2(x, y) * texelSize).r; - shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; - } - } - shadow /= 9.0f; +// float shadow = 0.0; +// vec2 texelSize = 1.0 / textureSize(u_texture_directionalShadowMap, 0); +// for(int x = -1; x <= 1; x++) { +// for(int y = -1; y <= 1; y++) { +// float pcfDepth = texture(u_texture_directionalShadowMap, projCoords.xy + vec2(x, y) * texelSize).r; +// shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; +// } +// } +// shadow /= 9.0f; - return shadow; -} +// return shadow; +// } -float computePointShadows(vec3 fragPos, vec3 lightPos) { +// float computePointShadows(vec3 fragPos, vec3 lightPos) { - // get vector between fragment position and light position - vec3 fragToLight = fragPos - lightPos; +// // get vector between fragment position and light position +// vec3 fragToLight = fragPos - lightPos; - // now get current linear depth as the length between the fragment and light position - float currentDepth = length(fragToLight); +// // now get current linear depth as the length between the fragment and light position +// float currentDepth = length(fragToLight); - float shadow = 0.0; - float bias = 0.05; - int samples = 20; - float viewDistance = length(v_viewPositionTangent - fragPos); - float diskRadius = 0.05; +// float shadow = 0.0; +// float bias = 0.05; +// int samples = 20; +// float viewDistance = length(v_viewPositionTangent - fragPos); +// float diskRadius = 0.05; - for(int i = 0; i < samples; ++i) { - float closestDepth = texture(u_texture_pointShadowMap0, fragToLight + sampleOffsetDirections[i] * diskRadius).r; - closestDepth *= pointShadowDepthMapFarPlane; - if(currentDepth - bias > closestDepth) - shadow += 1.0; - } +// for(int i = 0; i < samples; ++i) { +// float closestDepth = texture(u_texture_pointShadowMap0, fragToLight + sampleOffsetDirections[i] * diskRadius).r; +// closestDepth *= pointShadowDepthMapFarPlane; +// if(currentDepth - bias > closestDepth) +// shadow += 1.0; +// } - shadow /= float(samples); - return shadow; -} +// shadow /= float(samples); +// return shadow; +// } diff --git a/imgui.ini b/imgui.ini deleted file mode 100644 index b108402..0000000 --- a/imgui.ini +++ /dev/null @@ -1,15 +0,0 @@ -[Window][Debug##Default] -Pos=60,60 -Size=400,400 -Collapsed=0 - -[Window][Debug Utils] -Pos=9,24 -Size=598,357 -Collapsed=0 - -[Window][Entities] -Pos=1062,24 -Size=225,279 -Collapsed=0 - diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index d4a6aab..94a3490 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,6 +1,9 @@ +option(SPDLOG_NO_EXCEPTIONS "" ON) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/stb) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glm) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/imgui) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spdlog) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/entt) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f230690..d7b4104 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,4 +38,4 @@ target_link_libraries( spdlog ) -target_compile_options(Fall-Fever PRIVATE -Wall -Wextra -pedantic) +target_compile_options(Fall-Fever PRIVATE -Wall -Wextra -pedantic -fno-exceptions) diff --git a/src/Controller.cpp b/src/Controller.cpp index 05cbfec..24883e2 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -38,33 +38,34 @@ Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) }; for (auto &prototype : shaderProgramPrototypes) { - m_shaderPrograms.push_back(new ShaderProgram(prototype)); + m_shaderPrograms.push_back(std::make_shared(prototype)); Log::logger().info("Loaded shaderprogram \"{}\"", prototype.name); } - m_postProcessFrameBuffer = new FrameBuffer(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight(), - getShaderProgramByName("postProcessingProgram")); + m_postProcessFrameBuffer = + std::make_shared(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight(), + getShaderProgramByName("postProcessingProgram").get()); - m_scene = new Scene(m_shaderPrograms); + m_scene = std::make_shared(m_shaderPrograms); } Controller::~Controller() { - for (auto program : m_shaderPrograms) { - delete program; - } + // for (auto program : m_shaderPrograms) { + // delete program; + // } - delete m_scene; + // delete m_scene; delete m_camera; - delete m_postProcessFrameBuffer; + // delete m_postProcessFrameBuffer; delete m_gameEventHandler; } void Controller::run() { - updateExposure(getShaderProgramByName("postProcessingProgram")); + updateExposure(*getShaderProgramByName("postProcessingProgram")); - ModelEntity *lightSource = m_scene->getEntityByName("light"); + auto lightSource = m_scene->getEntityByName("light"); lightSource->setScale(0.1f); lightSource->setRotation(glm::vec3(0.f)); lightSource->setPosition(glm::vec3(-2.f, 1.5f, 2.f)); @@ -91,15 +92,15 @@ void Controller::run() // --- Render and buffer swap --- // Calc shadows - static bool firstRun = true; getShaderProgramByName("defaultProgram")->bind(); getShaderProgramByName("defaultProgram")->setUniform("b_drawShadows", (int)drawShadows); getShaderProgramByName("defaultProgram")->unbind(); - if (drawShadows || firstRun) { - firstRun = false; - m_scene->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"), - getShaderProgramByName("pointShadowDepthProgram")); - } + // static bool firstRun = true; + // if (drawShadows || firstRun) { + // firstRun = false; + // m_scene->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"), + // getShaderProgramByName("pointShadowDepthProgram")); + // } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -160,19 +161,20 @@ void Controller::updateWindowDimensions() m_postProcessFrameBuffer->changeDimensions(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight()); } -void Controller::updateExposure(ShaderProgram *shaderProgram) +void Controller::updateExposure(ShaderProgram &shaderProgram) { - shaderProgram->bind(); - shaderProgram->setUniform("u_exposure", m_exposure); - shaderProgram->unbind(); + shaderProgram.bind(); + shaderProgram.setUniform("u_exposure", m_exposure); + shaderProgram.unbind(); } -ShaderProgram *Controller::getShaderProgramByName(const std::string &name) +std::shared_ptr Controller::getShaderProgramByName(const std::string &name) { return getShaderProgramByName(name, m_shaderPrograms); } -ShaderProgram *Controller::getShaderProgramByName(const std::string &name, std::vector shaderPrograms) +std::shared_ptr +Controller::getShaderProgramByName(const std::string &name, std::vector> shaderPrograms) { for (auto program : shaderPrograms) { if (program->getUniqueName() == name) { @@ -180,7 +182,7 @@ ShaderProgram *Controller::getShaderProgramByName(const std::string &name, std:: } } Log::logger().critical("Shaderprogram could not be found by name \"{}\"", name); - return nullptr; + return {}; } void Controller::setMaxFps(uint16_t fps) diff --git a/src/Controller.h b/src/Controller.h index 5b98b66..addb114 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -27,10 +27,11 @@ public: void setMaxFps(uint16_t fps); // TODO remove... - ShaderProgram *getShaderProgramByName(const std::string &name); - static ShaderProgram *getShaderProgramByName(const std::string &name, std::vector shaderPrograms); + std::shared_ptr getShaderProgramByName(const std::string &name); + static std::shared_ptr + getShaderProgramByName(const std::string &name, std::vector> shaderPrograms); - void updateExposure(ShaderProgram *shaderProgram); + void updateExposure(ShaderProgram &shaderProgram); private: void limit_framerate(); @@ -43,13 +44,13 @@ private: std::unique_ptr m_gameWindow; EventHandler *m_gameEventHandler; - Scene *m_scene; + std::shared_ptr m_scene; Camera *m_camera; - std::vector m_shaderPrograms; + std::vector> m_shaderPrograms; - FrameBuffer *m_postProcessFrameBuffer; + std::shared_ptr m_postProcessFrameBuffer; uint16_t m_MAX_FPS = 60; double m_deltaTime; diff --git a/src/Entity.cpp b/src/Entity.cpp index e202a78..2bb0bfc 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -27,22 +27,8 @@ const std::string &Entity::getUniqueName() const return m_uniqueName; } -void Entity::setParent(Entity *parent) -{ - m_parent = parent; -} - -void Entity::addChild(Entity *child) -{ - m_children.push_back(child); -} - void Entity::translate(glm::vec3 vector) { - for (auto &child : m_children) { - child->translate(vector); - } - m_position += vector; updateModelMatrix(); @@ -50,10 +36,6 @@ void Entity::translate(glm::vec3 vector) void Entity::rotate(glm::vec3 axis, float radians) { - for (auto &child : m_children) { - child->rotate(axis, radians); - } - glm::quat rotation = glm::angleAxis(radians, axis); m_quaternion = rotation * m_quaternion; @@ -62,40 +44,24 @@ void Entity::rotate(glm::vec3 axis, float radians) void Entity::setPosition(glm::vec3 position) { - for (auto &child : m_children) { - child->setPosition(child->getPosition() - m_position + position); - } - m_position = position; updateModelMatrix(); } void Entity::setRotation(glm::vec3 eulerAngles) { - for (auto &child : m_children) { - child->setRotation(eulerAngles); - } - m_quaternion = glm::quat(eulerAngles); updateModelMatrix(); } void Entity::setRotation(glm::vec3 axis, float radians) { - for (auto &child : m_children) { - child->setRotation(axis, radians); - } - m_quaternion = glm::angleAxis(radians, axis); updateModelMatrix(); } void Entity::setScale(float scale) { - for (auto &child : m_children) { - child->setScale(scale); - } - m_scale = scale; updateModelMatrix(); } @@ -137,10 +103,6 @@ ModelEntity::ModelEntity(Prototype prototype, const Model *model, ShaderProgram void ModelEntity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) { - for (auto &child : m_children) - if (auto childModel = dynamic_cast(child)) - childModel->draw(viewProjMatrix, viewPosition); - m_shaderProgram->bind(); glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix; @@ -161,10 +123,6 @@ void ModelEntity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) void ModelEntity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *shaderProgram) { - for (auto &child : m_children) - if (auto childModel = dynamic_cast(child)) - childModel->drawDirectionalShadows(viewProjMatrix, shaderProgram); - shaderProgram->bind(); glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix; @@ -178,10 +136,6 @@ void ModelEntity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram void ModelEntity::drawPointShadows(ShaderProgram *shaderProgram) { - for (auto &child : m_children) - if (auto childModel = dynamic_cast(child)) - childModel->drawPointShadows(shaderProgram); - shaderProgram->bind(); shaderProgram->setUniform("u_modelMatrix", m_modelMatrix); diff --git a/src/Entity.h b/src/Entity.h index 1c081d6..b0bc6d2 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -18,14 +18,12 @@ class Entity public: struct Prototype { - Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 rotation, - float scale) - : name(name), parent(parent), position(position), rotation(rotation), scale(scale) + 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 parent; glm::vec3 position; glm::vec3 rotation; float scale; @@ -37,13 +35,6 @@ public: uint32_t getId() const; const std::string &getUniqueName() const; - void setParent(Entity *parent); - void addChild(Entity *child); - const std::vector &getChildren() const - { - return m_children; - } - void translate(glm::vec3 vector); void rotate(glm::vec3 axis, float radians); @@ -61,12 +52,6 @@ protected: const uint32_t m_id; static uint32_t s_idCounter; - // TODO - // std::weak_ptr m_parent; - Entity *m_parent = nullptr; - // std::vector> m_children; - std::vector m_children; - std::string m_uniqueName; glm::mat4 m_modelMatrix = glm::mat4(1.0f); @@ -81,9 +66,9 @@ class ModelEntity : public Entity public: struct Prototype : public Entity::Prototype { - Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 rotation, - float scale, const std::string &modelName, const std::string &shaderProgramName) - : Entity::Prototype(name, parent, position, rotation, scale), modelName(modelName), + 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; diff --git a/src/Scene.cpp b/src/Scene.cpp index 6ba92b9..8fda284 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -14,20 +14,20 @@ #include #include -Scene::Scene(std::vector shaderPrograms) - : m_shaderProgram(Controller::getShaderProgramByName("defaultProgram", shaderPrograms)), - m_depthMapDirectionalFBO(SHADOW_RES) +Scene::Scene(std::vector> shaderPrograms) + : m_shaderProgram(*Controller::getShaderProgramByName("defaultProgram", shaderPrograms)) +// m_depthMapDirectionalFBO(SHADOW_RES) { - // Create 4 depthMaps - for (int i = 0; i < 4; i++) { - DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES); - m_depthMapPointFBO.push_back(temp_depthMap); - } + // // Create 4 depthMaps + // for (int i = 0; i < 4; i++) { + // DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES); + // m_depthMapPointFBO.push_back(temp_depthMap); + // } // This will be removed in future when gloss maps are implemented - m_shaderProgram->bind(); - m_shaderProgram->setUniform("u_material.shininess", 100.0f); - m_shaderProgram->unbind(); + m_shaderProgram.bind(); + m_shaderProgram.setUniform("u_material.shininess", 100.0f); + m_shaderProgram.unbind(); std::array modelDescriptors{ ModelDescriptor{"fallback", "data/res/models/fallback.ffo"}, @@ -58,19 +58,18 @@ Scene::Scene(std::vector shaderPrograms) // TODO: use geometry shader instead of model and load skybox before models. Skybox::Prototype skyboxPrototype{"data/res/textures/skybox/"}; - m_skybox = - new Skybox(skyboxPrototype, std::static_pointer_cast(ResourceHandler::instance().resource("cube")).get(), - Controller::getShaderProgramByName("skyboxProgram", shaderPrograms)); + m_skybox = std::make_shared( + skyboxPrototype, std::static_pointer_cast(ResourceHandler::instance().resource("cube")).get(), + Controller::getShaderProgramByName("skyboxProgram", shaderPrograms).get()); Log::logger().info("Loaded skybox: {}", skyboxPrototype.texturePath); m_skybox->initializeOnGPU(); std::array entityPrototypes{ - ModelEntity::Prototype{"backpack", "", {0., 1., 0.}, {}, 0.6, "backpack", "defaultProgram"}, - ModelEntity::Prototype{ - "container", "backpack", {10., 1., 0.}, {45., 45., 45.}, 1., "container", "defaultProgram"}, - ModelEntity::Prototype{"ground", "", {}, {}, 1., "ground", "defaultProgram"}, - ModelEntity::Prototype{"light", "", {}, {}, 1., "cube", "lightProgram"}, + ModelEntity::Prototype{"backpack", {0., 1., 0.}, {}, 0.6, "backpack", "defaultProgram"}, + ModelEntity::Prototype{"container", {10., 1., 0.}, {45., 45., 45.}, 1., "container", "defaultProgram"}, + ModelEntity::Prototype{"ground", {}, {}, 1., "ground", "defaultProgram"}, + ModelEntity::Prototype{"light", {}, {}, 1., "cube", "lightProgram"}, }; { @@ -88,25 +87,15 @@ Scene::Scene(std::vector shaderPrograms) } // Get shaderprogram - ShaderProgram *currentProgram = - Controller::getShaderProgramByName(prototype.shaderProgramName, shaderPrograms); + auto currentProgram = Controller::getShaderProgramByName(prototype.shaderProgramName, shaderPrograms); if (!currentProgram) { currentProgram = Controller::getShaderProgramByName("basic", shaderPrograms); } - ModelEntity *currentEntity = new ModelEntity(prototype, currentModel, currentProgram); + m_entities.push_back(std::make_shared(prototype, currentModel, currentProgram.get())); Log::logger().info("Loaded entity \"{}\" with model \"{}\"", prototype.name, prototype.modelName); - - if (!prototype.parent.empty()) { - Entity *parent = getEntityByName(prototype.parent); - if (parent) { - parent->addChild(currentEntity); - currentEntity->setParent(parent); - } - } else - m_entities.push_back(currentEntity); } } @@ -118,27 +107,19 @@ Scene::Scene(std::vector shaderPrograms) new PointLight::Prototype{"pointLight0", "light", {0., 1., 0.}, {1., 1., 1.}, 7.5}), }; - std::vector lights; + std::vector> lights; { for (auto &prototype : lightPrototypes) { - Light *currentLight; + std::shared_ptr currentLight; auto directionalPrototype = dynamic_cast(prototype.get()); if (directionalPrototype) { - currentLight = new DirectionalLight(*directionalPrototype, m_shaderProgram); + currentLight = std::make_shared(*directionalPrototype, &m_shaderProgram); } auto pointPrototype = dynamic_cast(prototype.get()); if (pointPrototype) { - currentLight = new PointLight(*pointPrototype, m_shaderProgram); - if (!pointPrototype->parent.empty()) { - Entity *parent = getEntityByName(pointPrototype->parent); - if (parent) { - parent->addChild(currentLight); - currentLight->setParent(parent); - } else { - Log::logger().warn("Could not find entity \"{}\"", pointPrototype->parent); - } + currentLight = std::make_shared(*pointPrototype, &m_shaderProgram); } - } + lights.push_back(currentLight); Log::logger().info("Loaded light \"{}\"", prototype->name); } @@ -146,50 +127,9 @@ Scene::Scene(std::vector shaderPrograms) m_lights = lights; } -Scene::~Scene() -{ - // Iterate over depthMapPointFBO vector and delete all items - for (auto it = m_depthMapPointFBO.begin(); it != m_depthMapPointFBO.end(); it++) { - delete (*it); - } - // Iterate over models and entities and delete all items - // for (auto it = m_models.begin(); it != m_models.end(); it++) { - // delete (*it); - // } - for (auto it = m_entities.begin(); it != m_entities.end(); it++) { - delete (*it); - } - - delete m_skybox; -} - -void Scene::addEntity(ModelEntity *entity) -{ - m_entities.push_back(entity); -} - -void Scene::removeEntityByName(const std::string &name) -{ - for (auto it = m_entities.begin(); it != m_entities.end(); it++) { - if ((*it)->getUniqueName() == name) { - m_entities.erase(it); - return; - } - } - - Log::logger().warn("Entity \"{}\" could not be removed", name); -} - -void Scene::clearEntities() -{ - for (auto it = m_entities.begin(); it != m_entities.end(); it++) { - m_entities.erase(it); - } -} - void Scene::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity) { - std::vector pointLights = getPointLights(); + auto pointLights = getPointLights(); pointLights[lightId]->setActive(active); pointLights[lightId]->setPosition(position); pointLights[lightId]->setIntensity(intensity); @@ -198,7 +138,7 @@ void Scene::updatePointLight(unsigned int lightId, bool active, glm::vec3 positi void Scene::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color) { - DirectionalLight *directionalLight = getDirectionalLight(); + auto directionalLight = getDirectionalLight(); directionalLight->setActive(active); directionalLight->setDirection(direction); directionalLight->setColor(color); @@ -215,113 +155,114 @@ void Scene::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) // calculateShadows(); } -void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram) -{ - // Get old viewport dimensions to reset them later... - GLint VIEWPORT[4]; - glGetIntegerv(GL_VIEWPORT, VIEWPORT); +// void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram) +// { +// // Get old viewport dimensions to reset them later... +// GLint VIEWPORT[4]; +// glGetIntegerv(GL_VIEWPORT, VIEWPORT); - glViewport(0, 0, SHADOW_RES, SHADOW_RES); - // Switch face culling (Peter panning) - glCullFace(GL_BACK); +// glViewport(0, 0, SHADOW_RES, SHADOW_RES); +// // Switch face culling (Peter panning) +// glCullFace(GL_BACK); - m_depthMapDirectionalFBO.bind(); +// m_depthMapDirectionalFBO.bind(); - glClear(GL_DEPTH_BUFFER_BIT); +// glClear(GL_DEPTH_BUFFER_BIT); - // --- Directional shadows --- - glm::mat4 directionalLightView = - glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - glm::mat4 directionalLightViewProjectionMatrix = m_directionalLightProjection * directionalLightView; +// // --- Directional shadows --- +// glm::mat4 directionalLightView = +// glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, +// 0.0f)); +// glm::mat4 directionalLightViewProjectionMatrix = m_directionalLightProjection * directionalLightView; - // Draw scene from light perspective - // Draw all entities - for (auto it = m_entities.begin(); it != m_entities.end(); it++) { - (*it)->drawDirectionalShadows(directionalLightViewProjectionMatrix, directionalShaderProgram); - } +// // Draw scene from light perspective +// // Draw all entities +// for (auto it = m_entities.begin(); it != m_entities.end(); it++) { +// (*it)->drawDirectionalShadows(directionalLightViewProjectionMatrix, directionalShaderProgram); +// } - m_depthMapDirectionalFBO.unbind(); +// m_depthMapDirectionalFBO.unbind(); - m_shaderProgram->bind(); +// m_shaderProgram->bind(); - // Send lightViewProjMatrix to basic shader - m_shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix); +// // Send lightViewProjMatrix to basic shader +// m_shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix); - // Send shadowMap to basic shader - int textureUnit = static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2; - m_shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit); - glActiveTexture(GL_TEXTURE0 + textureUnit); - glBindTexture(GL_TEXTURE_2D, m_depthMapDirectionalFBO.getDepthMap()); +// // Send shadowMap to basic shader +// int textureUnit = static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2; +// m_shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit); +// glActiveTexture(GL_TEXTURE0 + textureUnit); +// glBindTexture(GL_TEXTURE_2D, m_depthMapDirectionalFBO.getDepthMap()); - m_shaderProgram->unbind(); +// m_shaderProgram->unbind(); - // --- Point shadows --- - std::vector pointLights = getPointLights(); +// // --- Point shadows --- +// std::vector pointLights = getPointLights(); - // 4 depthMaps for 4 point lights - for (int i = 0; i < 1; i++) { - m_depthMapPointFBO[i]->bind(); +// // 4 depthMaps for 4 point lights +// for (int i = 0; i < 1; i++) { +// m_depthMapPointFBO[i]->bind(); - glClear(GL_DEPTH_BUFFER_BIT); +// glClear(GL_DEPTH_BUFFER_BIT); - // Create 6 view matrices for every face of the cubeMap - std::vector viewProjMatrices; - glm::vec3 lightPos = pointLights[i]->getPosition(); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(1.0f, 0.0f, 0.0f), - glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), - glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(0.0f, 1.0f, 0.0f), - glm::vec3(0.0f, 0.0f, 1.0f))); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(0.0f, -1.0f, 0.0f), - glm::vec3(0.0f, 0.0f, -1.0f))); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(0.0f, 0.0f, 1.0f), - glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, - lightPos + glm::vec3(0.0f, 0.0f, -1.0f), - glm::vec3(0.0f, -1.0f, 0.0f))); +// // Create 6 view matrices for every face of the cubeMap +// std::vector viewProjMatrices; +// glm::vec3 lightPos = pointLights[i]->getPosition(); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(1.0f, 0.0f, 0.0f), +// glm::vec3(0.0f, -1.0f, 0.0f))); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), +// glm::vec3(0.0f, -1.0f, 0.0f))); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(0.0f, 1.0f, 0.0f), +// glm::vec3(0.0f, 0.0f, 1.0f))); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(0.0f, -1.0f, 0.0f), +// glm::vec3(0.0f, 0.0f, -1.0f))); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(0.0f, 0.0f, 1.0f), +// glm::vec3(0.0f, -1.0f, 0.0f))); +// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, +// lightPos + glm::vec3(0.0f, 0.0f, -1.0f), +// glm::vec3(0.0f, -1.0f, 0.0f))); - pointShaderProgram->bind(); +// pointShaderProgram->bind(); - for (int i = 0; i < 6; i++) { - pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(), - viewProjMatrices[i]); - } +// for (int i = 0; i < 6; i++) { +// pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(), +// viewProjMatrices[i]); +// } - pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint); - pointShaderProgram->setUniform("v_lightPos", lightPos); +// pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint); +// pointShaderProgram->setUniform("v_lightPos", lightPos); - // Draw scene from light perspective - // Draw all entities - for (auto it = m_entities.begin(); it != m_entities.end(); it++) { - (*it)->drawPointShadows(pointShaderProgram); - } +// // Draw scene from light perspective +// // Draw all entities +// for (auto it = m_entities.begin(); it != m_entities.end(); it++) { +// (*it)->drawPointShadows(pointShaderProgram); +// } - m_depthMapPointFBO[i]->unbind(); +// m_depthMapPointFBO[i]->unbind(); - m_shaderProgram->bind(); +// m_shaderProgram->bind(); - m_shaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint); +// m_shaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint); - 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]->getCubeMapTextureId()); +// 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]->getCubeMapTextureId()); - m_shaderProgram->unbind(); - } +// m_shaderProgram->unbind(); +// } - // Reset viewport size - glViewport(VIEWPORT[0], VIEWPORT[1], VIEWPORT[2], VIEWPORT[3]); - glCullFace(GL_FRONT); -} +// // Reset viewport size +// glViewport(VIEWPORT[0], VIEWPORT[1], VIEWPORT[2], VIEWPORT[3]); +// glCullFace(GL_FRONT); +// } -ModelEntity *Scene::getEntityByName(const std::string &name) +std::shared_ptr Scene::getEntityByName(const std::string &name) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { if ((*it)->getUniqueName() == name) { @@ -332,7 +273,7 @@ ModelEntity *Scene::getEntityByName(const std::string &name) return nullptr; } -ModelEntity *Scene::getEntityById(uint32_t id) +std::shared_ptr Scene::getEntityById(uint32_t id) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { if ((*it)->getId() == id) { @@ -343,12 +284,12 @@ ModelEntity *Scene::getEntityById(uint32_t id) return nullptr; } -std::vector Scene::getPointLights() +std::vector> Scene::getPointLights() { - std::vector temp_pointLights; + std::vector> temp_pointLights; for (auto it = m_lights.begin(); it != m_lights.end(); it++) { - PointLight *temp_pointLight = dynamic_cast(*it); + auto temp_pointLight = std::dynamic_pointer_cast(*it); if (temp_pointLight) { temp_pointLights.push_back(temp_pointLight); } @@ -357,12 +298,12 @@ std::vector Scene::getPointLights() return temp_pointLights; } -DirectionalLight *Scene::getDirectionalLight() +std::shared_ptr Scene::getDirectionalLight() { - DirectionalLight *temp_directionalLight = nullptr; + std::shared_ptr temp_directionalLight; for (auto it = m_lights.begin(); it != m_lights.end(); it++) { - temp_directionalLight = dynamic_cast(*it); + temp_directionalLight = std::dynamic_pointer_cast(*it); if (temp_directionalLight) break; } @@ -370,12 +311,12 @@ DirectionalLight *Scene::getDirectionalLight() return temp_directionalLight; } -std::vector Scene::getEntities() +std::vector> Scene::getEntities() { return m_entities; } -Skybox *Scene::getSkybox() +std::shared_ptr Scene::getSkybox() { return m_skybox; } diff --git a/src/Scene.h b/src/Scene.h index dd1a683..6592609 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -21,48 +21,43 @@ class Model; class Scene { public: - Scene(std::vector shaderPrograms); - ~Scene(); - - void addEntity(ModelEntity *entity); - void removeEntityByName(const std::string &name); - void clearEntities(); + Scene(std::vector> shaderPrograms); 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 getPointLights(); - DirectionalLight *getDirectionalLight(); - Skybox *getSkybox(); - ModelEntity *getEntityByName(const std::string &name); - ModelEntity *getEntityById(uint32_t id); + std::vector> getEntities(); + std::vector> getPointLights(); + std::shared_ptr getDirectionalLight(); + std::shared_ptr getSkybox(); + std::shared_ptr getEntityByName(const std::string &name); + std::shared_ptr getEntityById(uint32_t id); void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); - void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram); + // void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram); private: - ShaderProgram *m_shaderProgram; + ShaderProgram &m_shaderProgram; std::vector m_models; - std::vector m_entities; - Skybox *m_skybox; + std::vector> m_entities; + std::shared_ptr m_skybox; // Lights - std::vector m_lights; + std::vector> m_lights; - // Shadows - const int SHADOW_RES = 4096 / 4; - DepthMap m_depthMapDirectionalFBO; - std::vector m_depthMapPointFBO; - // Shadow projection matrices - const float m_nearPlaneDirectional = 1.0f; - const float m_farPlaneDirectional = 15.0f; - glm::mat4 m_directionalLightProjection = - glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, m_nearPlaneDirectional, m_farPlaneDirectional); - const float m_aspectRatioPoint = 1.0f; - const float m_nearPlanePoint = 1.0f; - const float m_farPlanePoint = 25.0f; - glm::mat4 m_pointLightProjection = - glm::perspective(glm::radians(90.0f), m_aspectRatioPoint, m_nearPlanePoint, m_farPlanePoint); + // // Shadows + // const int SHADOW_RES = 4096 / 4; + // DepthMap m_depthMapDirectionalFBO; + // std::vector m_depthMapPointFBO; + // // Shadow projection matrices + // const float m_nearPlaneDirectional = 1.0f; + // const float m_farPlaneDirectional = 15.0f; + // glm::mat4 m_directionalLightProjection = + // glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, m_nearPlaneDirectional, m_farPlaneDirectional); + // const float m_aspectRatioPoint = 1.0f; + // const float m_nearPlanePoint = 1.0f; + // const float m_farPlanePoint = 25.0f; + // glm::mat4 m_pointLightProjection = + // glm::perspective(glm::radians(90.0f), m_aspectRatioPoint, m_nearPlanePoint, m_farPlanePoint); };