[ADDED] simple house generation

This commit is contained in:
Menno
2021-06-04 14:32:16 +02:00
parent 75c745427c
commit cfd2d00d08
12 changed files with 116 additions and 4526 deletions

2
.gitignore vendored
View File

@@ -2,6 +2,8 @@
# Created by https://www.toptal.com/developers/gitignore/api/c++,visualstudio,visualstudiocode,opencv
# Edit at https://www.toptal.com/developers/gitignore?templates=c++,visualstudio,visualstudiocode,opencv
res/**
### C++ ###
# Prerequisites
*.d

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,35 @@
#include "HouseGenerator.h"
#include "../renderEngine/obj_loader.h"
#include "../renderEngine/Loader.h"
namespace entities
{
HouseGenerator::HouseGenerator()
{
models::RawModel raw_model = render_engine::LoadObjModel("res/HouseNew.obj");
models::ModelTexture texture = { render_engine::loader::LoadTexture("res/Texture.png") };
texture.shine_damper = 10;
house_model = { raw_model, texture };
GenerateFurnitureModels();
}
std::deque<std::shared_ptr<Entity>> HouseGenerator::GenerateHouse(const glm::vec3& position, float y_rotation)
{
std::deque<std::shared_ptr<Entity>> furniture;
// Add house
furniture.push_front(std::make_shared<Entity>(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE));
// Add furniture
return furniture;
}
void HouseGenerator::GenerateFurnitureModels()
{
}
}

View File

@@ -0,0 +1,37 @@
#pragma once
#include <deque>
#include <memory>
#include "../models/Model.h"
#include "../collision/collision.h"
namespace entities
{
class HouseGenerator
{
private:
const float HOUSE_SIZE = 30;
models::TexturedModel house_model;
std::deque<models::TexturedModel> furniture_models;
public:
HouseGenerator();
/*
* @brief: This function generates a house with furniture at the given position and rotation
*
* @param position: The position of the house to render
* @param y_rotation: The y rotation the house needs to be rendered with
*
* @return: A list with all the entities of the generated house (the furniture)
*/
std::deque<std::shared_ptr<Entity>> GenerateHouse(const glm::vec3& position, float y_rotation);
float GetHouseDepth() const { return house_model.raw_model.model_size.x * HOUSE_SIZE; }
private:
void GenerateFurnitureModels();
};
}

View File

@@ -1,6 +1,7 @@
#pragma once
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
namespace models
{

View File

@@ -4,7 +4,6 @@
#include "loader.h"
#include "../toolbox/toolbox.h"
#include "renderer.h"
#include <iostream>
namespace render_engine
@@ -51,9 +50,9 @@ namespace render_engine
/*
This function will Render a Model on the screen.
*/
void Render(entities::Entity& entity, shaders::EntityShader& shader)
void Render(std::shared_ptr<entities::Entity> entity, shaders::EntityShader& shader)
{
const models::TexturedModel model = entity.GetModel();
const models::TexturedModel model = entity.get()->GetModel();
const models::RawModel raw_model = model.raw_model;
const models::ModelTexture texture = model.texture;
@@ -66,7 +65,7 @@ namespace render_engine
glEnableVertexAttribArray(2);
// Load the transformation of the model into the shader
const glm::mat4 modelMatrix = toolbox::CreateModelMatrix(entity.GetPosition(), entity.GetRotation(), entity.GetScale());
const glm::mat4 modelMatrix = toolbox::CreateModelMatrix(entity.get()->GetPosition(), entity.get()->GetRotation(), entity.get()->GetScale());
shader.LoadModelMatrix(modelMatrix);
shader.LoadShineVariables(texture.shine_damper, texture.reflectivity);

View File

@@ -1,5 +1,6 @@
#pragma once
#include <memory>
#include "../gui/gui_element.h"
#include "../entities/entity.h"
#include "../shaders/entity_shader.h"
@@ -30,7 +31,7 @@ namespace render_engine
@param entity: The entity which needs to be rendered
@param shader: The shader the entity needs to be rendered with
*/
void Render(entities::Entity& entity, shaders::EntityShader& shader);
void Render(std::shared_ptr<entities::Entity> entity, shaders::EntityShader& shader);
/*
@brief: Call this function to render gui_textures on the screen

View File

@@ -10,7 +10,10 @@
#include "../renderEngine/renderer.h"
#include "../shaders/entity_shader.h"
#include "../toolbox/toolbox.h"
#include "../entities/HouseGenerator.h"
#include <deque>
#include <functional>
#include <memory>
#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
@@ -18,9 +21,10 @@
namespace scene
{
std::deque<entities::Entity> house_models;
entities::HouseGenerator* house_generator;
std::deque<std::shared_ptr<entities::Entity>> house_models;
std::deque<entities::Light> lights;
std::deque<entities::Entity> trees;
std::deque<std::shared_ptr<entities::Entity>> trees;
models::RawModel raw_model;
models::ModelTexture texture;
@@ -29,9 +33,6 @@ namespace scene
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()
{
@@ -43,6 +44,11 @@ namespace scene
gui_shader->Init();
}
In_Game_Scene::~In_Game_Scene()
{
delete house_generator;
}
/**
* @brief loads a new chunk in front of the camera, and deletes the chunk behind the camera.
*
@@ -51,32 +57,27 @@ namespace scene
*/
void load_chunk(int model_pos)
{
static unsigned int furniture_count = 0;
std::cout << "loading model chunk" << std::endl;
if (house_models.size() >= MAX_MODEL_DEQUE_SIZE)
{
for (int i = 0; i < furniture_count; i++)
{
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));
}
int z_offset = model_pos * (house_generator->GetHouseDepth()); // how much "in the distance" we should load the model
trees.push_front(entities::Entity(tree, glm::vec3(0, 0, -50 - z_offset), glm::vec3(0, 90, 0), 3));
std::deque<std::shared_ptr<entities::Entity>> furniture = house_generator->GenerateHouse(glm::vec3(0, -75, -50 - z_offset), 90);
furniture_count = furniture.size();
house_models.insert(house_models.begin(), furniture.begin(), furniture.end());
}
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;
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 };
house_generator = new entities::HouseGenerator();
// load the first few house models
for (int i = 0; i <= UPCOMING_MODEL_AMOUNT; i++)
{
@@ -123,12 +124,12 @@ namespace scene
shader->LoadLightsDeque(lights);
shader->LoadViewMatrix(camera);
for (entities::Entity& model_entity : house_models)
for (std::shared_ptr<entities::Entity> model_entity : house_models)
{
render_engine::renderer::Render(model_entity, *shader);
}
for (entities::Entity& tree_entity : trees)
for (std::shared_ptr<entities::Entity> tree_entity : trees)
{
render_engine::renderer::Render(tree_entity, *shader);
}
@@ -146,7 +147,7 @@ namespace scene
// 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
int model_pos = -round(camera.GetPosition().z / (house_generator->GetHouseDepth())); // 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)

View File

@@ -11,6 +11,7 @@ namespace scene
public:
In_Game_Scene();
~In_Game_Scene();
Scenes start(GLFWwindow* window) override;
void render() override;

View File

@@ -29,7 +29,7 @@ namespace shaders
uniform vec3 light_position[4];
const float density = 0.0017;
const float gradient = 4;
const float gradient = 3;
void main(void)
{

View File

@@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\collision\collision_handler.cpp" />
<ClCompile Include="src\entities\HouseGenerator.cpp" />
<ClCompile Include="src\scenes\in_Game_Scene.cpp" />
<ClCompile Include="src\computervision\FaceDetector.cpp" />
<ClCompile Include="src\computervision\ObjectDetection.cpp" />
@@ -43,6 +44,7 @@
<ItemGroup>
<ClInclude Include="src\collision\collision.h" />
<ClInclude Include="src\collision\collision_handler.h" />
<ClInclude Include="src\entities\HouseGenerator.h" />
<ClInclude Include="src\scenes\in_Game_Scene.h" />
<ClInclude Include="src\scenes\scene.h" />
<ClInclude Include="src\computervision\FaceDetector.h" />

View File

@@ -75,6 +75,9 @@
<ClCompile Include="src\computervision\BackgroundRemover.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\entities\HouseGenerator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\entities\Camera.h">
@@ -155,6 +158,9 @@
<ClInclude Include="src\toolbox\Timer.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\entities\HouseGenerator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Xml Include="res\haarcascade_frontalface_alt.xml" />