2 Commits

Author SHA1 Message Date
Lars
4587559030 [COMMENTS] added some comments for all the scene files
the only thing i could not find was the on key method, the other params are empty(thus no comments)
2021-06-04 16:44:10 +02:00
lars
2468d7fa7f [ADD] the pause menu can be accessed with the 'P' key. the real button are not implemented yet. 2021-06-01 15:18:07 +02:00
13 changed files with 271 additions and 113 deletions

2
.gitignore vendored
View File

@@ -428,6 +428,4 @@ 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

BIN
res/background_grey.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 294 KiB

View File

@@ -62,7 +62,7 @@ int main(void)
current_scene->onKey(window, key, scancode, action, mods); current_scene->onKey(window, key, scancode, action, mods);
}); });
bool window_open = true; bool window_open = true;
// Main game loop // Main game loop
while (!glfwWindowShouldClose(window) && window_open) while (!glfwWindowShouldClose(window) && window_open)

View File

@@ -10,99 +10,115 @@
#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::deque<entities::Entity> house_models; /**
std::deque<entities::Light> lights; * sets up the first things when the objects has been made
std::deque<entities::Entity> trees; */
models::RawModel raw_model;
models::ModelTexture texture;
shaders::EntityShader* shader;
shaders::GuiShader* gui_shader;
entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
std::vector<gui::GuiTexture*> guis;
models::TexturedModel model;
models::TexturedModel tree;
In_Game_Scene::In_Game_Scene() In_Game_Scene::In_Game_Scene()
{ {
camera = new entities::Camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
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. * deletes certain veriables when the object will be deleted, prevents memory leaks
*
* @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) In_Game_Scene::~In_Game_Scene()
{ {
std::cout << "loading model chunk" << std::endl; delete camera;
if (house_models.size() >= MAX_MODEL_DEQUE_SIZE) delete shader;
{ delete gui_shader;
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));
} }
/**
* starts the game scene, calls the render and update methods in a while loop
*/
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;
model = { raw_model, texture }; models::TexturedModel 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 };
int z = 0;
// load the first few house models for (int i = 0; i < 5; ++i)
for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++)
{ {
load_chunk(i); entities_to_render.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20));
z += (raw_model.model_size.x * 20);
} }
lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); // sun 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, -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)));
// GUI stuff // GUI stuff
gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f)); gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(1, 1));
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);
//guis for the pause menu
gui::GuiTexture background(render_engine::loader::LoadTexture("res/background_grey.png"), glm::vec2(0, 0), glm::vec2(1, 1));
pause_guis.push_back(&background);
gui::Button pause_button_resume(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0, 0), glm::vec2(0.25f, 0.25f));
pause_button_resume.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
pause_button_resume.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
pause_button_resume.SetOnClickAction([]()
{
std::cout << "I got clicked on the resume button!" << std::endl;
});
pause_guis.push_back(&pause_button_resume);
gui::Button pause_button_quit(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.3f, 0.0f), glm::vec2(0.25f, 0.25f));
pause_button_quit.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
pause_button_quit.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
pause_button_quit.SetOnClickAction([]()
{
std::cout << "I got clicked on the quit button!" << std::endl;
});
pause_guis.push_back(&pause_button_quit);
//the scene loop, this while loop represent the scene
while (return_value == scene::Scenes::INGAME) while (return_value == scene::Scenes::INGAME)
{ {
update(window); //checks the current game state, so it can render the correct models for each state
button.Update(window); switch (game_state)
render(); {
/*case scene::Game_State::IDLE:
break;*/
case scene::Game_State::PAUSED:
render();
render_pause_menu();
break;
case scene::Game_State::RUNNING:
update(window);
button.Update(window);
render();
break;
default:
std::cout << "Game state unknown" << std::endl;
break;
}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
@@ -113,56 +129,58 @@ namespace scene
return return_value; return return_value;
} }
/**
* renders the game models
*/
void scene::In_Game_Scene::render() void scene::In_Game_Scene::render()
{ {
// Render // Render
render_engine::renderer::Prepare(); render_engine::renderer::Prepare();
//starts the shader and begins to render
shader->Start(); shader->Start();
shader->LoadSkyColor(render_engine::renderer::SKY_COLOR); shader->LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader->LoadLightsDeque(lights); shader->LoadLights(lights);
shader->LoadViewMatrix(camera); shader->LoadViewMatrix(*camera);
for (entities::Entity& model_entity : house_models) // Renders each entity in the entities list
for (entities::Entity& entity : entities_to_render)
{ {
render_engine::renderer::Render(model_entity, *shader); render_engine::renderer::Render(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);
// Stop rendering the entities // Stop rendering the entities
shader->Stop(); shader->Stop();
} }
//updates certain variables
void scene::In_Game_Scene::update(GLFWwindow* window) void scene::In_Game_Scene::update(GLFWwindow* window)
{ {
camera.Move(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);
}
// remember the position at which the new model was added
last_model_pos = model_pos;
} }
//renders the models for the pause menu
void In_Game_Scene::render_pause_menu()
{
render_engine::renderer::Render(pause_guis, *gui_shader);
}
//manages the key input in the game scene
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_SPACE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{ {
return_value = scene::Scenes::STOP; return_value = scene::Scenes::STOP;
} }
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS)
{
game_state = scene::Game_State::PAUSED;
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS)
{
game_state = scene::Game_State::RUNNING;
}
} }
} }

View File

@@ -1,20 +1,100 @@
#pragma once #pragma once
#include <iostream>
#include <ostream>
#include <vector>
#include "scene.h" #include "scene.h"
#include "../gui/gui_interactable.h"
#include "../models/model.h"
#include "../renderEngine/loader.h"
#include "../renderEngine/obj_loader.h"
#include "../renderEngine/renderer.h"
#include "../shaders/entity_shader.h"
#include "../toolbox/toolbox.h"
namespace scene namespace scene
{ {
/**
* This enum is for managing the game scene state.
* for example: when pressed on a specific button, the game will be in a paused state and nothing about the player or the speed of the game will be updated
* and the pause screen will show up.
**/
enum class Game_State
{
//IDLE,
RUNNING,
PAUSED
};
class In_Game_Scene : public scene::Scene class In_Game_Scene : public scene::Scene
{ {
private: private:
//return_value is an enum that is necessary for the scene switching. Whenever this changes, the scene will change to a different scene.
scene::Scenes return_value = scene::Scenes::INGAME; scene::Scenes return_value = scene::Scenes::INGAME;
//game_state is an enum that keeps track of the current game state. For example: is the game running(thus the user is playing the game) of is the game paused.
scene::Game_State game_state = scene::Game_State::RUNNING;
//entities_to_render is a list of entities, those entities will be rendered in the 3D environment.
std::vector<entities::Entity> entities_to_render;
//lights is a lost of light points in the game, for example the sun or it can be used to attach light effects to lamps.
std::vector<entities::Light> lights;
models::RawModel raw_model;
models::ModelTexture texture;
//the shader that is used for rendering the models.
shaders::EntityShader* shader;
//the gui_shader is used of rendering the gui models (for example the pause buttons).
shaders::GuiShader* gui_shader;
//camera is the camera view of the game scene, this camera will be behind the main character.
entities::Camera *camera;
//guis is a list of all the gui components that needs to be load in the scene.
std::vector<gui::GuiTexture*> guis;
//pause_guis is a list of components that will be rendered when the game is paused.
std::vector<gui::GuiTexture*> pause_guis;
/**
* @brief renders the objects/gui models
* @param
* @return void
*/
void render_pause_menu();
public: public:
In_Game_Scene(); In_Game_Scene();
~In_Game_Scene();
/**
* @brief the method start is the start of the scene where a while loop runs, this runs the scene.
* @param window the main window of the application
* @return Scene value that indicates in which scene the application is
*/
Scenes start(GLFWwindow* window) override; Scenes start(GLFWwindow* window) override;
/**
* @brief this method renders the models for the game scene
* @param
* @return void
*/
void render() override; void render() override;
/**
* @brief this method updates the models/variables for the game scene
* @param window the main window of the application
* @return void
*/
void update(GLFWwindow* window) override; void update(GLFWwindow* window) override;
/**
* @brief this method updates the models/variables for the game scene
* @param window the main window of the application
* @param key this is the keycode on which key has been pressed
* @param scancode -
* @param action-
* @param mods -
* @return void
*/
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override; void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
}; };
} }

7
src/scenes/scene.cpp Normal file
View File

@@ -0,0 +1,7 @@
#include <iostream>
#include "scene.h"
scene::Scene::~Scene()
{
std::cout << "Main scene class gone!" << std::endl;
}

View File

@@ -6,6 +6,9 @@
namespace scene { namespace scene {
/**
* this enum represents the scenes in the game, those wil help to keep track in which scene the game is.
*/
enum class Scenes enum class Scenes
{ {
STARTUP, STARTUP,
@@ -18,14 +21,40 @@ namespace scene {
class Scene class Scene
{ {
public: public:
virtual ~Scene() = 0;
/**
* @brief the method start is the start of a scene where a while loop runs, this runs the scene.
* @param window the main window of the application
* @return Scene value that indicates in which scene the application is
*/
virtual Scenes start(GLFWwindow* window) = 0; virtual Scenes start(GLFWwindow* window) = 0;
/**
* @brief this method renders the models for a scene
* @param
* @return void
*/
virtual void render() = 0; virtual void render() = 0;
/**
* @brief this method updates the models/variables for a scene
* @param window the main window of the application
* @return void
*/
virtual void update(GLFWwindow* window) = 0; virtual void update(GLFWwindow* window) = 0;
/**
* @brief this method updates the models/variables for a scene
* @param window the main window of the application
* @param key this is the keycode on which key has been pressed
* @param scancode -
* @param action-
* @param mods -
* @return void
*/
virtual void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) {}; virtual void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) {};
}; };
} }

View File

@@ -1,3 +1,4 @@
#include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <map> #include <map>
@@ -5,7 +6,17 @@
namespace scene namespace scene
{ {
/**
* deletes certain variables to prevent memory leaks
*/
Startup_Scene::~Startup_Scene()
{
std::cout << "startup scene gone!" << std::endl;
}
/**
* starts the start-up scene, calls the render and update methods in a while loop
*/
scene::Scenes scene::Startup_Scene::start(GLFWwindow *window) scene::Scenes scene::Startup_Scene::start(GLFWwindow *window)
{ {
while (return_value == scene::Scenes::STARTUP) while (return_value == scene::Scenes::STARTUP)
@@ -20,16 +31,25 @@ namespace scene
return return_value; return return_value;
} }
/**
* renders the models in the start-up scene
*/
void scene::Startup_Scene::render() void scene::Startup_Scene::render()
{ {
} }
/**
* updates the variables for the start-up scene
*/
void scene::Startup_Scene::update(GLFWwindow* window) void scene::Startup_Scene::update(GLFWwindow* window)
{ {
} }
/**
* manages the key input in the start-up scene
*/
void scene::Startup_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods) void scene::Startup_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)

View File

@@ -9,12 +9,42 @@ namespace scene
class Startup_Scene : public scene::Scene class Startup_Scene : public scene::Scene
{ {
private: private:
//return_value is an enum that is necessary for the scene switching. Whenever this changes, the scene will change to a different scene.
scene::Scenes return_value = scene::Scenes::STARTUP; scene::Scenes return_value = scene::Scenes::STARTUP;
public: public:
~Startup_Scene();
/**
* @brief the method start is the start of the start-up scene where a while loop runs, this runs the scene.
* @param window the main window of the application
* @return Scene value that indicates in which scene the application is
*/
Scenes start(GLFWwindow* window) override; Scenes start(GLFWwindow* window) override;
/**
* @brief this method renders the models for the start-up scene
* @param
* @return void
*/
void render() override; void render() override;
/**
* @brief this method updates the models/variables for the start-up scene
* @param window the main window of the application
* @return void
*/
void update(GLFWwindow* window) override; void update(GLFWwindow* window) override;
/**
* @brief this method updates the models/variables for the start-up scene
* @param window the main window of the application
* @param key this is the keycode on which key has been pressed
* @param scancode -
* @param action-
* @param mods -
* @return void
*/
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override; void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
}; };
} }

View File

@@ -1,6 +1,5 @@
#include "entity_shader.h" #include "entity_shader.h"
#include "../toolbox/toolbox.h" #include "../toolbox/toolbox.h"
#include <deque>
namespace shaders namespace shaders
{ {
@@ -161,25 +160,6 @@ 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,7 +2,6 @@
#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"
@@ -59,13 +58,6 @@ 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
* *

View File

@@ -34,6 +34,7 @@
<ClCompile Include="src\renderEngine\loader.cpp" /> <ClCompile Include="src\renderEngine\loader.cpp" />
<ClCompile Include="src\renderEngine\obj_loader.cpp" /> <ClCompile Include="src\renderEngine\obj_loader.cpp" />
<ClCompile Include="src\renderEngine\renderer.cpp" /> <ClCompile Include="src\renderEngine\renderer.cpp" />
<ClCompile Include="src\scenes\scene.cpp" />
<ClCompile Include="src\shaders\gui_shader.cpp" /> <ClCompile Include="src\shaders\gui_shader.cpp" />
<ClCompile Include="src\shaders\shader_program.cpp" /> <ClCompile Include="src\shaders\shader_program.cpp" />
<ClCompile Include="src\shaders\entity_shader.cpp" /> <ClCompile Include="src\shaders\entity_shader.cpp" />

View File

@@ -75,6 +75,9 @@
<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\scenes\scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\entities\Camera.h"> <ClInclude Include="src\entities\Camera.h">