Merge branch 'feature/automatic-rendering' into feature/MovementCharacter

* feature/automatic-rendering:
  [ADD] deque method
  [EDIT] caffemodel in gitignore
  [EDIT] small fix
  [ADD] (test) trees at every chunk
  [ADD] comments
  [ADD] added deque support for light loading
  [ADD] automatic loading of models
  [ADDED] timer class

# Conflicts:
#	src/scenes/in_Game_Scene.cpp
#	wk2_fps.vcxproj.filters
This commit is contained in:
Nathalie Seen
2021-06-04 12:57:51 +02:00
8 changed files with 163 additions and 49 deletions

2
.gitignore vendored
View File

@@ -428,4 +428,6 @@ FodyWeavers.xsd
**/docs/* **/docs/*
**/doc/* **/doc/*
**/pose_iter_160000.caffemodel
# End of https://www.toptal.com/developers/gitignore/api/c++,visualstudio,visualstudiocode,opencv # End of https://www.toptal.com/developers/gitignore/api/c++,visualstudio,visualstudiocode,opencv

View File

@@ -55,6 +55,11 @@ int main(void)
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) 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); current_scene->onKey(window, key, scancode, action, mods);
}); });

View File

@@ -3,7 +3,6 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "in_Game_Scene.h" #include "in_Game_Scene.h"
#include "startup_Scene.h" #include "startup_Scene.h"
#include "../entities/main_character.h"
#include "../gui/gui_interactable.h" #include "../gui/gui_interactable.h"
#include "../models/model.h" #include "../models/model.h"
#include "../renderEngine/loader.h" #include "../renderEngine/loader.h"
@@ -11,53 +10,80 @@
#include "../renderEngine/renderer.h" #include "../renderEngine/renderer.h"
#include "../shaders/entity_shader.h" #include "../shaders/entity_shader.h"
#include "../toolbox/toolbox.h" #include "../toolbox/toolbox.h"
#include <deque>
#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 namespace scene
{ {
std::vector<entities::Entity*> entities; std::deque<entities::Entity> house_models;
std::vector<entities::main_character*> mainCharacter; std::deque<entities::Light> lights;
std::vector<entities::Light> lights; std::deque<entities::Entity> trees;
models::RawModel raw_model, raw_model_char;
models::RawModel raw_model;
models::ModelTexture texture; models::ModelTexture texture;
shaders::EntityShader *shader; shaders::EntityShader* shader;
shaders::GuiShader *gui_shader; shaders::GuiShader* gui_shader;
entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
std::vector<gui::GuiTexture*> guis; std::vector<gui::GuiTexture*> guis;
models::TexturedModel model;
models::TexturedModel tree;
In_Game_Scene::In_Game_Scene() In_Game_Scene::In_Game_Scene()
{ {
shader = new shaders::EntityShader; shader = new shaders::EntityShader;
shader->Init(); shader->Init();
render_engine::renderer::Init(*shader); render_engine::renderer::Init(*shader);
gui_shader = new shaders::GuiShader(); gui_shader = new shaders::GuiShader();
gui_shader->Init(); 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 (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));
}
scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window) scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window)
{ {
raw_model = render_engine::LoadObjModel("res/House.obj"); raw_model = render_engine::LoadObjModel("res/House.obj");
texture = { render_engine::loader::LoadTexture("res/Texture.png") }; texture = { render_engine::loader::LoadTexture("res/Texture.png") };
texture.shine_damper = 10; texture.shine_damper = 10;
texture.reflectivity = 0; texture.reflectivity = 0;
models::TexturedModel model = { raw_model, texture }; model = { raw_model, texture };
raw_model_char = render_engine::LoadObjModel("res/beeTwo.obj"); models::RawModel raw_tree_model = render_engine::LoadObjModel("res/Tree.obj");
models::TexturedModel model_char = { raw_model_char, texture }; models::ModelTexture tree_texture = { render_engine::loader::LoadTexture("res/TreeTexture.png") };
tree = { raw_tree_model, tree_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);
}
entities::main_character character{ model_char, glm::vec3(0, -50, -100), glm::vec3(0, 90, 0), 5,collision::Box() };
entities.push_back(&character);
mainCharacter.push_back(&character);
lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); // 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))); // 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, -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))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
@@ -66,11 +92,11 @@ namespace scene
button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png")); button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png")); button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
button.SetOnClickAction([]() button.SetOnClickAction([]()
{ {
std::cout << "I got clicked on!" << std::endl; std::cout << "I got clicked on!" << std::endl;
}); });
guis.push_back(&button); guis.push_back(&button);
while (return_value == scene::Scenes::INGAME) while (return_value == scene::Scenes::INGAME)
{ {
@@ -94,15 +120,18 @@ namespace scene
shader->Start(); shader->Start();
shader->LoadSkyColor(render_engine::renderer::SKY_COLOR); shader->LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader->LoadLights(lights); shader->LoadLightsDeque(lights);
shader->LoadViewMatrix(camera); shader->LoadViewMatrix(camera);
// Renders each entity in the entities list for (entities::Entity& model_entity : house_models)
for (entities::Entity* entity : entities)
{ {
render_engine::renderer::Render(*entity, *shader); render_engine::renderer::Render(model_entity, *shader);
}
for (entities::Entity& tree_entity : trees)
{
render_engine::renderer::Render(tree_entity, *shader);
} }
// Render GUI items // Render GUI items
render_engine::renderer::Render(guis, *gui_shader); render_engine::renderer::Render(guis, *gui_shader);
@@ -113,18 +142,24 @@ namespace scene
void scene::In_Game_Scene::update(GLFWwindow* window) void scene::In_Game_Scene::update(GLFWwindow* window)
{ {
//camera.Move(window); camera.Move(window);
entities::main_character *character = mainCharacter[0]; // calculate where the next house model should be loaded
glm::vec3 movement = character->move(window); static int last_model_pos = 0;
//character->IncreasePosition(movement); 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
std::cout <<"x: "<< character->GetPosition().x << "\ny: " << character->GetPosition().y << "\nz: " << character->GetPosition().z << "\n";
std::cout <<"x get: "<< movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n"; // 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);
}
// remember the position at which the new model was added
last_model_pos = model_pos;
} }
void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods) void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{ {
return_value = scene::Scenes::STOP; return_value = scene::Scenes::STOP;
} }

View File

@@ -1,5 +1,6 @@
#include "entity_shader.h" #include "entity_shader.h"
#include "../toolbox/toolbox.h" #include "../toolbox/toolbox.h"
#include <deque>
namespace shaders namespace shaders
{ {
@@ -160,6 +161,25 @@ namespace shaders
} }
} }
void EntityShader::LoadLightsDeque(std::deque<entities::Light>& 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 void EntityShader::LoadShineVariables(float shine_damper, float reflectivity) const
{ {
LoadFloat(location_shine_damper, shine_damper); LoadFloat(location_shine_damper, shine_damper);

View File

@@ -2,6 +2,7 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <vector> #include <vector>
#include <deque>
#include "shader_program.h" #include "shader_program.h"
#include "../entities/camera.h" #include "../entities/camera.h"
#include "../entities/light.h" #include "../entities/light.h"
@@ -58,6 +59,13 @@ namespace shaders
*/ */
void LoadLights(std::vector<entities::Light>& lights) const; void LoadLights(std::vector<entities::Light>& lights) const;
/**
* @brief loads some lights contained in a deque.
*
* @param lights the deque containing the lights to load
*/
void LoadLightsDeque(std::deque<entities::Light>& lights) const;
/* /*
* @brief: A method to load the the shine variables from a model into the shader * @brief: A method to load the the shine variables from a model into the shader
* *

46
src/toolbox/Timer.h Normal file
View File

@@ -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; }
};
}

View File

@@ -66,6 +66,7 @@
<ClInclude Include="src\shaders\shader_program.h" /> <ClInclude Include="src\shaders\shader_program.h" />
<ClInclude Include="src\shaders\entity_shader.h" /> <ClInclude Include="src\shaders\entity_shader.h" />
<ClInclude Include="src\stb_image.h" /> <ClInclude Include="src\stb_image.h" />
<ClInclude Include="src\toolbox\Timer.h" />
<ClInclude Include="src\toolbox\toolbox.h" /> <ClInclude Include="src\toolbox\toolbox.h" />
<ClInclude Include="src\scenes\startup_Scene.h" /> <ClInclude Include="src\scenes\startup_Scene.h" />
</ItemGroup> </ItemGroup>
@@ -130,16 +131,16 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>C:\opencv\build\include;$(IncludePath);C:\opencv\opencv\build\include</IncludePath> <IncludePath>C:\opencv\build\include;$(IncludePath);C:\opencv\opencv\build\include;C:\opencv\build\include</IncludePath>
<LibraryPath>C:\opencv\build\x64\vc15\lib;$(LibraryPath);C:\opencv\opencv\build\x64\vc15\lib</LibraryPath> <LibraryPath>C:\opencv\build\x64\vc15\lib;$(LibraryPath);C:\opencv\opencv\build\x64\vc15\lib;C:\opencv\build\x64\vc15\lib</LibraryPath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);;C:\opencv\opencv\build\include</IncludePath> <IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);;C:\opencv\opencv\build\include;C:\opencv\build\include</IncludePath>
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);C:\opencv\opencv\build\x64\vc15\lib</LibraryPath> <LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);C:\opencv\opencv\build\x64\vc15\lib;C:\opencv\build\x64\vc15\lib</LibraryPath>
</PropertyGroup> </PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
@@ -171,7 +172,7 @@
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>opencv_world452d.lib;%(AdditionalDependencies); opencv_world452.lib</AdditionalDependencies> <AdditionalDependencies>opencv_world452d.lib;%(AdditionalDependencies); opencv_world452.lib;opencv_world452d.lib</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -212,7 +213,7 @@
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>$(SolutionDir)lib\glfw-3.3.2\$(Platform);$(SolutionDir)lib\glew-2.1.0\lib\Release\$(Platform);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>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</AdditionalDependencies> <AdditionalDependencies>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</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@@ -75,9 +75,6 @@
<ClCompile Include="src\computervision\BackgroundRemover.cpp"> <ClCompile Include="src\computervision\BackgroundRemover.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\entities\main_character.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\entities\Camera.h"> <ClInclude Include="src\entities\Camera.h">
@@ -155,7 +152,7 @@
<ClInclude Include="src\computervision\BackgroundRemover.h"> <ClInclude Include="src\computervision\BackgroundRemover.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\entities\main_character.h"> <ClInclude Include="src\toolbox\Timer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>