Merge remote-tracking branch 'origin/feature/adding_scenes' into feature/collision

This commit is contained in:
Menno
2021-05-28 16:08:35 +02:00
12 changed files with 304 additions and 82 deletions

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>

View File

@@ -1,3 +1,4 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "gui_interactable.h" #include "gui_interactable.h"

View File

@@ -3,6 +3,7 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <iostream> #include <iostream>
#include <map>
#include "stb_image.h" #include "stb_image.h"
#include <ostream> #include <ostream>
@@ -16,9 +17,9 @@
#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 "entities/collision_entity.h" #include "scenes/scene.h"
#include "entities/player.h" #include "scenes/in_Game_Scene.h"
#include "collision/collision_handler.h" #include "scenes/startup_Scene.h"
#pragma comment(lib, "glfw3.lib") #pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib") #pragma comment(lib, "glew32s.lib")
@@ -27,7 +28,7 @@
static double UpdateDelta(); static double UpdateDelta();
static GLFWwindow* window; static GLFWwindow* window;
scene::Scene* current_scene;
int main(void) int main(void)
{ {
@@ -45,96 +46,51 @@ int main(void)
glGetError(); glGetError();
#pragma endregion #pragma endregion
current_scene = new scene::Startup_Scene();
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
current_scene->onKey(window, key, scancode, action, mods);
if (key == GLFW_KEY_ESCAPE) if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(window, true);
}); });
bool window_open = true;
models::RawModel raw_model = render_engine::LoadObjModel("res/House.obj");
models::ModelTexture texture = { render_engine::loader::LoadTexture("res/Texture.png") };
texture.shine_damper = 10;
texture.reflectivity = 0;
models::TexturedModel model = { raw_model, texture };
// load and add some models (in this case some level sections) to the entities list.
std::vector<entities::Entity*> entities;
std::vector<entities::CollisionEntity*> collision_entities;
// 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);
// }
std::vector<entities::Light> lights;
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)));
// Collision testing
entities::Player player(model, glm::vec3(0, 0, 0), glm::vec3(0, 0, 0), 1, { {0, 0, 0}, raw_model.model_size });
entities.push_back(&player);
collision_entities.push_back(&player);
entities::Player2 player2(model, glm::vec3(50, 0, 0), glm::vec3(0, 0, 0), 1, { {50, 0, 0}, raw_model.model_size });
entities.push_back(&player2);
collision_entities.push_back(&player2);
shaders::EntityShader shader;
shader.Init();
render_engine::renderer::Init(shader);
entities::Camera camera(glm::vec3(40, 10, 80), glm::vec3(0, 0, 0));
// Main game loop // Main game loop
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window) && window_open)
{ {
//Update //Update
const double delta = UpdateDelta(); const double delta = UpdateDelta();
camera.Move(window);
scene::Scenes return_value = current_scene->start(window);
delete current_scene;
player.Update(); switch (return_value) {
player2.Update(); case scene::Scenes::STOP:
window_open = false;
break;
collision::CheckCollisions(collision_entities); case scene::Scenes::STARTUP:
current_scene = new scene::Startup_Scene();
break;
case scene::Scenes::INGAME:
current_scene = new scene::In_Game_Scene();
break;
// Render default:
render_engine::renderer::Prepare(); std::cout << "Wrong return value!!! ->" << std::endl;
break;
// Start rendering the entities }
shader.Start();
shader.LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader.LoadLights(lights);
shader.LoadViewMatrix(camera);
// Renders each entity in the entities list
for (entities::Entity* entity : entities)
{
render_engine::renderer::Render(*entity, shader);
} }
// Stop rendering the entities // Clean up -> preventing memory leaks!!!
shader.Stop(); std::cout << "ending..." << std::endl;
delete current_scene;
// Finish up
glfwSwapBuffers(window);
glfwPollEvents();
}
// Clean up
shader.CleanUp();
render_engine::loader::CleanUp();
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }

View File

@@ -0,0 +1,118 @@
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "in_Game_Scene.h"
#include "startup_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
{
std::vector<entities::Entity> entities;
std::vector<entities::Light> lights;
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;
In_Game_Scene::In_Game_Scene()
{
shader = new shaders::EntityShader;
shader->Init();
render_engine::renderer::Init(*shader);
gui_shader = new shaders::GuiShader();
gui_shader->Init();
}
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;
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);
}
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)));
// GUI stuff
gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f));
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;
});
guis.push_back(&button);
while (return_value == scene::Scenes::INGAME)
{
update(window);
button.Update(window);
render();
glfwSwapBuffers(window);
glfwPollEvents();
}
shader->CleanUp();
gui_shader->CleanUp();
render_engine::loader::CleanUp();
return return_value;
}
void scene::In_Game_Scene::render()
{
// Render
render_engine::renderer::Prepare();
shader->Start();
shader->LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader->LoadLights(lights);
shader->LoadViewMatrix(camera);
// Renders each entity in the entities list
for (entities::Entity& entity : entities)
{
render_engine::renderer::Render(entity, *shader);
}
// Render GUI items
render_engine::renderer::Render(guis, *gui_shader);
// Stop rendering the entities
shader->Stop();
}
void scene::In_Game_Scene::update(GLFWwindow* window)
{
camera.Move(window);
}
void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
return_value = scene::Scenes::STOP;
}
}
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "scene.h"
namespace scene
{
class In_Game_Scene : public scene::Scene
{
private:
scene::Scenes return_value = scene::Scenes::INGAME;
public:
In_Game_Scene();
Scenes start(GLFWwindow* window) override;
void render() override;
void update(GLFWwindow* window) 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 "scene.h"
namespace scene
{
}

31
src/scenes/scene.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <map>
namespace scene {
enum class Scenes
{
STARTUP,
INGAME,
GAMEOVER,
CALIBRATION,
STOP
};
class Scene
{
public:
virtual Scenes start(GLFWwindow* window) = 0;
virtual void render() = 0;
virtual void update(GLFWwindow* window) = 0;
virtual void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) {};
};
}

View File

@@ -0,0 +1,40 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <map>
#include "startup_Scene.h"
namespace scene
{
scene::Scenes scene::Startup_Scene::start(GLFWwindow *window)
{
while (return_value == scene::Scenes::STARTUP)
{
render();
update(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
return return_value;
}
void scene::Startup_Scene::render()
{
}
void scene::Startup_Scene::update(GLFWwindow* window)
{
}
void scene::Startup_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
return_value = scene::Scenes::INGAME;
}
}
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "scene.h"
#include <map>
namespace scene
{
extern GLFWwindow* window;
class Startup_Scene : public scene::Scene
{
private:
scene::Scenes return_value = scene::Scenes::STARTUP;
public:
Scenes start(GLFWwindow* window) override;
void render() override;
void update(GLFWwindow* window) override;
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
};
}

View File

@@ -1,4 +1,5 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>

View File

@@ -20,6 +20,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\collision\collision_handler.cpp" /> <ClCompile Include="src\collision\collision_handler.cpp" />
<ClCompile Include="src\scenes\in_Game_Scene.cpp" />
<ClCompile Include="src\scenes\scene.cpp" />
<ClCompile Include="src\entities\camera.cpp" /> <ClCompile Include="src\entities\camera.cpp" />
<ClCompile Include="src\entities\collision_entity.cpp" /> <ClCompile Include="src\entities\collision_entity.cpp" />
<ClCompile Include="src\entities\entity.cpp" /> <ClCompile Include="src\entities\entity.cpp" />
@@ -32,10 +34,13 @@
<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" />
<ClCompile Include="src\toolbox\toolbox.cpp" /> <ClCompile Include="src\toolbox\toolbox.cpp" />
<ClCompile Include="src\scenes\startup_Scene.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\collision\collision.h" /> <ClInclude Include="src\collision\collision.h" />
<ClInclude Include="src\collision\collision_handler.h" /> <ClInclude Include="src\collision\collision_handler.h" />
<ClInclude Include="src\scenes\in_Game_Scene.h" />
<ClInclude Include="src\scenes\scene.h" />
<ClInclude Include="src\entities\camera.h" /> <ClInclude Include="src\entities\camera.h" />
<ClInclude Include="src\entities\collision_entity.h" /> <ClInclude Include="src\entities\collision_entity.h" />
<ClInclude Include="src\entities\entity.h" /> <ClInclude Include="src\entities\entity.h" />
@@ -52,6 +57,7 @@
<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\toolbox.h" /> <ClInclude Include="src\toolbox\toolbox.h" />
<ClInclude Include="src\scenes\startup_Scene.h" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion> <VCProjectVersion>16.0</VCProjectVersion>

View File

@@ -48,6 +48,15 @@
<ClCompile Include="src\gui\gui_interactable.cpp"> <ClCompile Include="src\gui\gui_interactable.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\scenes\scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\scenes\in_Game_Scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\scenes\startup_Scene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\entities\collision_entity.cpp"> <ClCompile Include="src\entities\collision_entity.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
@@ -98,6 +107,15 @@
<ClInclude Include="src\gui\gui_interactable.h"> <ClInclude Include="src\gui\gui_interactable.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\scenes\scene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\scenes\in_Game_Scene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\scenes\startup_Scene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\entities\collision_entity.h"> <ClInclude Include="src\entities\collision_entity.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>