From f03cc485cd2fbefbcda2e07abe5d6c84fc1ea0e3 Mon Sep 17 00:00:00 2001 From: Menno Date: Tue, 1 Jun 2021 11:41:21 +0200 Subject: [PATCH 1/8] [ADDED] timer class --- src/main.cpp | 5 +++++ src/toolbox/Timer.h | 46 +++++++++++++++++++++++++++++++++++++++++ wk2_fps.vcxproj | 13 ++++++------ wk2_fps.vcxproj.filters | 3 +++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/toolbox/Timer.h diff --git a/src/main.cpp b/src/main.cpp index 1ea8768..cf20294 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,6 +55,11 @@ int main(void) glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_ESCAPE) + { + glfwSetWindowShouldClose(window, true); + } + current_scene->onKey(window, key, scancode, action, mods); }); diff --git a/src/toolbox/Timer.h b/src/toolbox/Timer.h new file mode 100644 index 0000000..80fd164 --- /dev/null +++ b/src/toolbox/Timer.h @@ -0,0 +1,46 @@ +#pragma once + +namespace toolbox +{ + /* + * This class represents a timer which needs to be updated + * every frame to work correctly. + */ + class Timer + { + private: + float current_time; + float final_time; + bool has_finished; + + public: + /* + * @brief: Constructor to make the timer + * + * @param final_time: The time which the timer needs to count to + */ + Timer(float final_time): current_time(0), final_time(final_time), has_finished(false) {} + + /* + * @brief: Updates the timer. Call this method once every iteration in the game loop + * + * @param delta: The deltatime of the game + */ + void UpdateTimer(const double delta) + { + current_time += delta; + + if (current_time >= final_time) + { + has_finished = true; + } + } + + /* + * @brief: Returns if the timer has finished + * + * @return: True if the timer has finished + */ + bool HasFinished() const { return has_finished; } + }; +} \ No newline at end of file diff --git a/wk2_fps.vcxproj b/wk2_fps.vcxproj index 527b982..555ce4b 100644 --- a/wk2_fps.vcxproj +++ b/wk2_fps.vcxproj @@ -64,6 +64,7 @@ + @@ -128,16 +129,16 @@ true - C:\opencv\build\include;$(IncludePath);C:\opencv\opencv\build\include - C:\opencv\build\x64\vc15\lib;$(LibraryPath);C:\opencv\opencv\build\x64\vc15\lib + C:\opencv\build\include;$(IncludePath);C:\opencv\opencv\build\include;C:\opencv\build\include + C:\opencv\build\x64\vc15\lib;$(LibraryPath);C:\opencv\opencv\build\x64\vc15\lib;C:\opencv\build\x64\vc15\lib false false - $(VC_IncludePath);$(WindowsSDK_IncludePath);;C:\opencv\opencv\build\include - $(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);C:\opencv\opencv\build\x64\vc15\lib + $(VC_IncludePath);$(WindowsSDK_IncludePath);;C:\opencv\opencv\build\include;C:\opencv\build\include + $(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);C:\opencv\opencv\build\x64\vc15\lib;C:\opencv\build\x64\vc15\lib @@ -169,7 +170,7 @@ Console true $(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories) - opencv_world452d.lib;%(AdditionalDependencies); opencv_world452.lib + opencv_world452d.lib;%(AdditionalDependencies); opencv_world452.lib;opencv_world452d.lib @@ -210,7 +211,7 @@ true true $(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories) - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies); opencv_world452.lib + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies); opencv_world452.lib;opencv_world452d.lib diff --git a/wk2_fps.vcxproj.filters b/wk2_fps.vcxproj.filters index 4092abd..3dc1142 100644 --- a/wk2_fps.vcxproj.filters +++ b/wk2_fps.vcxproj.filters @@ -152,6 +152,9 @@ Header Files + + Header Files + From 658b809ef2f9721eac71b19c5288df8a8cd117ff Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 1 Jun 2021 13:57:45 +0200 Subject: [PATCH 2/8] [ADD] automatic loading of models --- src/scenes/in_Game_Scene.cpp | 73 ++++++++++++++++++++++++++++------- src/shaders/entity_shader.cpp | 20 ++++++++++ src/shaders/entity_shader.h | 7 ++++ 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 0e7c268..9816336 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -10,46 +10,73 @@ #include "../renderEngine/renderer.h" #include "../shaders/entity_shader.h" #include "../toolbox/toolbox.h" +#include + +#define MAX_MODEL_DEQUE_SIZE 6 // max amount of models to load at the same time +#define UPCOMING_MODEL_AMOUNT 4 // how much models should be loaded in front of us namespace scene { - std::vector entities; - std::vector lights; + std::deque entites; + std::deque lights; + entities::Light sun; + models::RawModel raw_model; models::ModelTexture texture; - shaders::EntityShader *shader; - shaders::GuiShader *gui_shader; + shaders::EntityShader* shader; + shaders::GuiShader* gui_shader; entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); std::vector guis; - + models::TexturedModel model; + + In_Game_Scene::In_Game_Scene() { shader = new shaders::EntityShader; - shader->Init(); + shader->Init(); render_engine::renderer::Init(*shader); gui_shader = new shaders::GuiShader(); gui_shader->Init(); } + void load_chunk(int model_pos) + { + std::cout << "loading model chunk" << std::endl; + if (entites.size() >= MAX_MODEL_DEQUE_SIZE) + { + entites.pop_back(); + } + int z_offset = model_pos * (model.raw_model.model_size.x * 20); // how much "in the distance" we should load the model + entites.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); + + + } + + scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window) { raw_model = render_engine::LoadObjModel("res/House.obj"); texture = { render_engine::loader::LoadTexture("res/Texture.png") }; texture.shine_damper = 10; texture.reflectivity = 0; - models::TexturedModel model = { raw_model, texture }; - - int z = 0; + model = { raw_model, texture }; + + /*int z = 0; + for (int i = 0; i < 5; ++i) { entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20)); z += (raw_model.model_size.x * 20); + }*/ + for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++) + { + load_chunk(i); } - lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); + sun = entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5)); lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f))); @@ -58,11 +85,11 @@ namespace scene button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png")); button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png")); button.SetOnClickAction([]() - { - std::cout << "I got clicked on!" << std::endl; - }); + { + std::cout << "I got clicked on!" << std::endl; + }); guis.push_back(&button); - + while (return_value == scene::Scenes::INGAME) { @@ -90,9 +117,14 @@ namespace scene shader->LoadViewMatrix(camera); // Renders each entity in the entities list - for (entities::Entity& entity : entities) + /*for (entities::Entity& entity : entities) { render_engine::renderer::Render(entity, *shader); + }*/ + + for (entities::Entity& model_entity : entites) + { + render_engine::renderer::Render(model_entity, *shader); } // Render GUI items @@ -105,6 +137,17 @@ namespace scene void scene::In_Game_Scene::update(GLFWwindow* window) { camera.Move(window); + + // calculate where the next house model should be loaded + static int last_model_pos = 0; + int model_pos = -round(camera.GetPosition().z / (model.raw_model.model_size.x * 20)); // how much models we have passed, minus because we are moving in the negative z axis + + // if we have passed a model, load a new one and delete the one behind us + if (last_model_pos != model_pos) + { + load_chunk(model_pos + UPCOMING_MODEL_AMOUNT); + } + last_model_pos = model_pos; } void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods) diff --git a/src/shaders/entity_shader.cpp b/src/shaders/entity_shader.cpp index 3a5a0c0..0d1b8db 100644 --- a/src/shaders/entity_shader.cpp +++ b/src/shaders/entity_shader.cpp @@ -1,5 +1,6 @@ #include "entity_shader.h" #include "../toolbox/toolbox.h" +#include namespace shaders { @@ -160,6 +161,25 @@ namespace shaders } } + void EntityShader::LoadLights(std::deque& lights) const + { + for (int i = 0; i < MAX_LIGHTS; ++i) + { + if (i < lights.size()) + { + LoadVector(location_light_position[i], lights[i].GetPosition()); + LoadVector(location_light_color[i], lights[i].GetColor()); + LoadVector(location_light_attenuation[i], lights[i].GetAttenuation()); + } + else + { + LoadVector(location_light_position[i], glm::vec3(0, 0, 0)); + LoadVector(location_light_color[i], glm::vec3(0, 0, 0)); + LoadVector(location_light_attenuation[i], glm::vec3(1, 0, 0)); + } + } + } + void EntityShader::LoadShineVariables(float shine_damper, float reflectivity) const { LoadFloat(location_shine_damper, shine_damper); diff --git a/src/shaders/entity_shader.h b/src/shaders/entity_shader.h index 514bbb4..7e73037 100644 --- a/src/shaders/entity_shader.h +++ b/src/shaders/entity_shader.h @@ -58,6 +58,13 @@ namespace shaders */ void LoadLights(std::vector& lights) const; + /** + * @brief loads some lights contained in a deque. + * + * @param lights the deque containing the lights to load + */ + void LoadLights(std::deque& lights) const; + /* * @brief: A method to load the the shine variables from a model into the shader * From 8d52703297092252a7a838a79d8b8430049825ae Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 1 Jun 2021 14:06:28 +0200 Subject: [PATCH 3/8] [ADD] added deque support for light loading --- src/scenes/in_Game_Scene.cpp | 11 +---------- src/shaders/entity_shader.h | 1 + 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 9816336..a809258 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -20,7 +20,6 @@ namespace scene { std::deque entites; std::deque lights; - entities::Light sun; models::RawModel raw_model; models::ModelTexture texture; @@ -51,8 +50,6 @@ namespace scene } int z_offset = model_pos * (model.raw_model.model_size.x * 20); // how much "in the distance" we should load the model entites.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); - - } @@ -76,7 +73,7 @@ namespace scene load_chunk(i); } - sun = entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5)); + lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f))); @@ -116,12 +113,6 @@ namespace scene shader->LoadLights(lights); shader->LoadViewMatrix(camera); - // Renders each entity in the entities list - /*for (entities::Entity& entity : entities) - { - render_engine::renderer::Render(entity, *shader); - }*/ - for (entities::Entity& model_entity : entites) { render_engine::renderer::Render(model_entity, *shader); diff --git a/src/shaders/entity_shader.h b/src/shaders/entity_shader.h index 7e73037..6cc1e8a 100644 --- a/src/shaders/entity_shader.h +++ b/src/shaders/entity_shader.h @@ -2,6 +2,7 @@ #include #include +#include #include "shader_program.h" #include "../entities/camera.h" #include "../entities/light.h" From f69e104a8de71e6e0789a2e375fc26d74ed61ebf Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 1 Jun 2021 14:24:16 +0200 Subject: [PATCH 4/8] [ADD] comments --- src/scenes/in_Game_Scene.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index a809258..c75f743 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -18,7 +18,7 @@ namespace scene { - std::deque entites; + std::deque house_models; std::deque lights; models::RawModel raw_model; @@ -41,15 +41,21 @@ namespace scene gui_shader->Init(); } + /** + * @brief loads a new chunk in front of the camera, and deletes the chunk behind the camera. + * + * @param model_pos the amount of models the camera has passed already. This is the rounded result of (z position of camera) / (size of model) + * + */ void load_chunk(int model_pos) { std::cout << "loading model chunk" << std::endl; - if (entites.size() >= MAX_MODEL_DEQUE_SIZE) + if (house_models.size() >= MAX_MODEL_DEQUE_SIZE) { - entites.pop_back(); + house_models.pop_back(); } int z_offset = model_pos * (model.raw_model.model_size.x * 20); // how much "in the distance" we should load the model - entites.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); + house_models.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); } @@ -61,13 +67,7 @@ namespace scene texture.reflectivity = 0; model = { raw_model, texture }; - /*int z = 0; - - for (int i = 0; i < 5; ++i) - { - entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20)); - z += (raw_model.model_size.x * 20); - }*/ + // load the first few house models for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++) { load_chunk(i); @@ -113,7 +113,7 @@ namespace scene shader->LoadLights(lights); shader->LoadViewMatrix(camera); - for (entities::Entity& model_entity : entites) + for (entities::Entity& model_entity : house_models) { render_engine::renderer::Render(model_entity, *shader); } From ad35e14bfcfd7edd5adcf0b83a19ba38b026f6c4 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 2 Jun 2021 09:37:13 +0200 Subject: [PATCH 5/8] [ADD] (test) trees at every chunk --- src/scenes/in_Game_Scene.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index c75f743..11a309a 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -20,6 +20,7 @@ namespace scene { std::deque house_models; std::deque lights; + std::deque trees; models::RawModel raw_model; models::ModelTexture texture; @@ -29,6 +30,7 @@ namespace scene std::vector guis; models::TexturedModel model; + models::TexturedModel tree; In_Game_Scene::In_Game_Scene() @@ -53,9 +55,12 @@ namespace scene if (house_models.size() >= MAX_MODEL_DEQUE_SIZE) { house_models.pop_back(); + trees.pop_back(); } int z_offset = model_pos * (model.raw_model.model_size.x * 20); // how much "in the distance" we should load the model house_models.push_front(entities::Entity(model, glm::vec3(0, -50, -50 - z_offset), glm::vec3(0, 90, 0), 20)); + + trees.push_front(entities::Entity(tree, glm::vec3(0, 0, -50 - z_offset), glm::vec3(0, 90, 0), 3)); } @@ -67,13 +72,18 @@ namespace scene texture.reflectivity = 0; model = { raw_model, texture }; + models::RawModel raw_tree_model = render_engine::LoadObjModel("res/Tree.obj"); + models::ModelTexture tree_texture = { render_engine::loader::LoadTexture("res/TreeTexture.png") }; + tree = { raw_tree_model, tree_texture }; + + // load the first few house models for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++) { load_chunk(i); } - lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); + lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); // sun lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f))); @@ -118,6 +128,11 @@ namespace scene render_engine::renderer::Render(model_entity, *shader); } + for (entities::Entity& tree_entity : trees) + { + render_engine::renderer::Render(tree_entity, *shader); + } + // Render GUI items render_engine::renderer::Render(guis, *gui_shader); @@ -138,6 +153,7 @@ namespace scene { load_chunk(model_pos + UPCOMING_MODEL_AMOUNT); } + // remember the position at which the new model was added last_model_pos = model_pos; } From e166c1f988fccc9477aa0dafd110aa03c77bbbdc Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 2 Jun 2021 10:51:59 +0200 Subject: [PATCH 6/8] [EDIT] small fix --- src/scenes/in_Game_Scene.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scenes/in_Game_Scene.cpp b/src/scenes/in_Game_Scene.cpp index 11a309a..281df17 100644 --- a/src/scenes/in_Game_Scene.cpp +++ b/src/scenes/in_Game_Scene.cpp @@ -120,7 +120,7 @@ namespace scene shader->Start(); shader->LoadSkyColor(render_engine::renderer::SKY_COLOR); - shader->LoadLights(lights); + shader->LoadLightsDeque(lights); shader->LoadViewMatrix(camera); for (entities::Entity& model_entity : house_models) From 6f546fdd8bebb1332ea8d7f885a8132d392d193e Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 2 Jun 2021 10:54:10 +0200 Subject: [PATCH 7/8] [EDIT] caffemodel in gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 9232336..488f5fe 100644 --- a/.gitignore +++ b/.gitignore @@ -428,4 +428,6 @@ FodyWeavers.xsd **/docs/* **/doc/* +**/pose_iter_160000.caffemodel + # End of https://www.toptal.com/developers/gitignore/api/c++,visualstudio,visualstudiocode,opencv From 75c745427cd2bd03e34a29ff4dc1cf77fc60a73d Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 4 Jun 2021 10:22:35 +0200 Subject: [PATCH 8/8] [ADD] deque method --- src/shaders/entity_shader.cpp | 2 +- src/shaders/entity_shader.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shaders/entity_shader.cpp b/src/shaders/entity_shader.cpp index 0d1b8db..973d7c4 100644 --- a/src/shaders/entity_shader.cpp +++ b/src/shaders/entity_shader.cpp @@ -161,7 +161,7 @@ namespace shaders } } - void EntityShader::LoadLights(std::deque& lights) const + void EntityShader::LoadLightsDeque(std::deque& lights) const { for (int i = 0; i < MAX_LIGHTS; ++i) { diff --git a/src/shaders/entity_shader.h b/src/shaders/entity_shader.h index 6cc1e8a..101fec1 100644 --- a/src/shaders/entity_shader.h +++ b/src/shaders/entity_shader.h @@ -64,7 +64,7 @@ namespace shaders * * @param lights the deque containing the lights to load */ - void LoadLights(std::deque& lights) const; + void LoadLightsDeque(std::deque& lights) const; /* * @brief: A method to load the the shine variables from a model into the shader