[FEATURE] simple house generator

This commit is contained in:
Menno
2021-06-04 15:49:56 +02:00
parent cfd2d00d08
commit 8c50792657
6 changed files with 78 additions and 13 deletions

View File

@@ -1,15 +1,20 @@
#include "HouseGenerator.h"
#include <functional>
#include <iostream>
#include "../renderEngine/obj_loader.h"
#include "../renderEngine/Loader.h"
#include "../toolbox/toolbox.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 };
default_texture = { render_engine::loader::LoadTexture("res/Texture.png") };
default_texture.shine_damper = 10;
house_model = { raw_model, default_texture };
GenerateFurnitureModels();
}
@@ -21,15 +26,37 @@ namespace entities
// Add house
furniture.push_front(std::make_shared<Entity>(house_model, position, glm::vec3(0, y_rotation, 0), HOUSE_SIZE));
// Add furniture
models::TexturedModel couch = GetFurnitureModel(Furniture::COUCH);
furniture.push_back(std::make_shared<Entity>(couch, glm::vec3(position.x, position.y + 20, position.z + 10), glm::vec3(0, 0, 0), 1));
return furniture;
}
models::TexturedModel HouseGenerator::GetFurnitureModel(Furniture furniture)
{
const auto found = furniture_models.find(furniture);
if (found == furniture_models.end())
{
std::cerr << "OH NEEEEEEEEEEEEEEE";
}
auto models = found->second;
const int modelNumber = toolbox::Random(0, models.size() - 1);
return models[modelNumber];
}
void HouseGenerator::GenerateFurnitureModels()
{
// Couches
std::deque<models::TexturedModel> couches;
models::RawModel couch_inside_model = render_engine::LoadObjModel("res/couchThree.obj");
models::TexturedModel couch_inside = { couch_inside_model, default_texture };
couches.push_back(couch_inside);
furniture_models.insert(std::pair<Furniture, std::deque<models::TexturedModel>>(Furniture::COUCH, couches));
}
}

View File

@@ -2,19 +2,26 @@
#include <deque>
#include <memory>
#include <map>
#include "../models/Model.h"
#include "../collision/collision.h"
namespace entities
{
enum class Furniture
{
COUCH
};
class HouseGenerator
{
private:
const float HOUSE_SIZE = 30;
models::TexturedModel house_model;
models::ModelTexture default_texture;
std::deque<models::TexturedModel> furniture_models;
std::map<Furniture, std::deque<models::TexturedModel>> furniture_models;
public:
HouseGenerator();
@@ -29,9 +36,14 @@ namespace entities
*/
std::deque<std::shared_ptr<Entity>> GenerateHouse(const glm::vec3& position, float y_rotation);
/*
* @brief: Returns the depth of the house (chunk)
*/
float GetHouseDepth() const { return house_model.raw_model.model_size.x * HOUSE_SIZE; }
private:
void GenerateFurnitureModels();
models::TexturedModel GetFurnitureModel(Furniture furniture);
};
}

View File

@@ -14,6 +14,7 @@
#include <deque>
#include <functional>
#include <memory>
#include <queue>
#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
@@ -60,18 +61,21 @@ namespace scene
static unsigned int furniture_count = 0;
std::cout << "loading model chunk" << std::endl;
if (house_models.size() >= MAX_MODEL_DEQUE_SIZE)
if (house_models.size() >= MAX_MODEL_DEQUE_SIZE * furniture_count)
{
for (int i = 0; i < furniture_count; i++)
{
house_models.pop_back();
house_models.pop_front();
}
}
int z_offset = model_pos * (house_generator->GetHouseDepth()); // how much "in the distance" we should load the model
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());
house_models.insert(house_models.end(), furniture.begin(), furniture.end());
std::cout << house_models.size() << std::endl;
}

View File

@@ -1,3 +1,4 @@
#include <ctime>
#include "toolbox.h"
namespace toolbox
@@ -31,4 +32,15 @@ namespace toolbox
matrix = glm::translate(matrix, negative_cam_pos);
return matrix;
}
int Random(const int min, const int max)
{
static bool first = true;
if (first)
{
srand(time(0));
first = false;
}
return min + rand() % ((max + 1) - min);
}
}

View File

@@ -46,4 +46,14 @@ namespace toolbox
* @return: The view matrix
*/
glm::mat4 CreateViewMatrix(entities::Camera& camera);
/*
* @brief: This function will return a value between min and max
*
* @param min: The min value
* @param max: The max value
*
* @return: The random number
*/
int Random(const int min, const int max);
}