ADD: Multiple Tree Types
This commit is contained in:
parent
d4489f5631
commit
f6beaf48ea
@ -71,6 +71,8 @@ add_executable(Dicewars_Siedler src/main.cpp
|
||||
src/engine/renderer/TerrainRenderer.h
|
||||
src/engine/renderer/shaders/TerrainShader.cpp
|
||||
src/engine/renderer/shaders/TerrainShader.h
|
||||
src/engine/renderer/model/AssetManager.cpp
|
||||
src/engine/renderer/model/AssetManager.h
|
||||
)
|
||||
|
||||
target_include_directories(Dicewars_Siedler PRIVATE
|
||||
|
||||
BIN
assets/trees/tree.png
Normal file
BIN
assets/trees/tree.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.3 KiB |
25
src/engine/renderer/model/AssetManager.cpp
Normal file
25
src/engine/renderer/model/AssetManager.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by sebastian on 08.02.26.
|
||||
//
|
||||
|
||||
#include "AssetManager.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "../loader/OBJLoader.h"
|
||||
|
||||
|
||||
std::shared_ptr<TexturedModel> AssetManager::loadModel(const std::string &name, const std::string &objPath, const std::string &texturePath, Loader &loader) {
|
||||
if (models.contains(name)) {
|
||||
return models[name];
|
||||
}
|
||||
|
||||
auto model = OBJLoader::loadModel(objPath, texturePath, loader);
|
||||
models[name] = model;
|
||||
return model;
|
||||
}
|
||||
|
||||
std::shared_ptr<TexturedModel> AssetManager::getModel(const std::string &name) {
|
||||
assert(models.contains(name) && "Model not found!");
|
||||
return models.at(name);
|
||||
}
|
||||
24
src/engine/renderer/model/AssetManager.h
Normal file
24
src/engine/renderer/model/AssetManager.h
Normal file
@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by sebastian on 08.02.26.
|
||||
//
|
||||
|
||||
#ifndef ASSETMANAGER_H
|
||||
#define ASSETMANAGER_H
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "TexturedModel.h"
|
||||
#include "../loader/Loader.h"
|
||||
|
||||
|
||||
class AssetManager {
|
||||
public:
|
||||
static std::shared_ptr<TexturedModel> loadModel(const std::string& name, const std::string& objPath, const std::string& texturePath, Loader& loader);
|
||||
static std::shared_ptr<TexturedModel> getModel(const std::string& name);
|
||||
private:
|
||||
static inline std::unordered_map<std::string, std::shared_ptr<TexturedModel>> models;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //ASSETMANAGER_H
|
||||
@ -11,6 +11,7 @@
|
||||
#include "../engine/renderer/textures/ModelTexture.h"
|
||||
#include "../engine/toolbox/MathUtils.h"
|
||||
#include "../engine/layer/entities/Light.h"
|
||||
#include "../engine/renderer/model/AssetManager.h"
|
||||
#include "hexWorld/HexModelFactory.h"
|
||||
#include "hexWorld/MapGenerator.h"
|
||||
|
||||
@ -31,9 +32,12 @@ void GameLayer::onAttach()
|
||||
hexModelWood = std::make_shared<TexturedModel>(HexModelFactory::createTexturedHexModel(loader, 10.0f, RessourceType::WOOD));
|
||||
map = std::make_unique<Map>();
|
||||
|
||||
treeModel = std::make_unique<TexturedModel>(*OBJLoader::loadModel("assets/trees/lowPolyTree.obj", "assets/trees/lowPolyTree.png", loader));
|
||||
treeEntity = std::make_unique<Entity>(Entity(treeModel, glm::vec3(0,0,0), 0,0,0, 0.5f));
|
||||
MapGenerator::generateHexMap(*map, 10,10,10.f, mapEntities, treeModel);
|
||||
AssetManager::loadModel("lowPolyTree", "assets/trees/lowPolyTree.obj", "assets/trees/lowPolyTree.png", loader);
|
||||
AssetManager::loadModel("lowPolyTree2", "assets/trees/tree.obj", "assets/trees/tree.png", loader);
|
||||
|
||||
//treeModel = std::make_unique<TexturedModel>(*OBJLoader::loadModel("assets/trees/lowPolyTree.obj", "assets/trees/lowPolyTree.png", loader));
|
||||
//treeEntity = std::make_unique<Entity>(Entity(treeModel, glm::vec3(0,0,0), 0,0,0, 0.5f));
|
||||
MapGenerator::generateHexMap(*map, 10,10,10.f, mapEntities);
|
||||
printf("Generated Terrain with %lu Tiles!\n", map->tiles.size());
|
||||
}
|
||||
|
||||
|
||||
@ -36,8 +36,6 @@ private:
|
||||
std::shared_ptr<TexturedModel> hexModelWood;
|
||||
|
||||
std::vector<std::shared_ptr<Entity>> tileEntities;
|
||||
std::shared_ptr<TexturedModel> treeModel;
|
||||
std::unique_ptr<Entity> treeEntity;
|
||||
|
||||
std::vector<std::shared_ptr<Entity>> mapEntities;
|
||||
std::unique_ptr<Map> map;
|
||||
|
||||
@ -9,10 +9,11 @@
|
||||
|
||||
#include "HexTile.h"
|
||||
#include "Map.h"
|
||||
#include "../../engine/renderer/model/AssetManager.h"
|
||||
|
||||
class MapGenerator {
|
||||
public:
|
||||
static void generateHexMap(Map& map, int width, int height, float hexRadius, std::vector<std::shared_ptr<Entity>>& mapEntities, const std::shared_ptr<TexturedModel>& treeModel) {
|
||||
static void generateHexMap(Map& map, int width, int height, float hexRadius, std::vector<std::shared_ptr<Entity>>& mapEntities) {
|
||||
// Zufallsgenerator initialisieren (einmal)
|
||||
std::random_device rd; // Zufälliger Seed
|
||||
std::mt19937 gen(rd()); // Mersenne Twister Generator
|
||||
@ -24,6 +25,12 @@ public:
|
||||
float xOffset = hexRadius * std::sqrt(3.0f);
|
||||
float zOffset = hexRadius * 1.5f;
|
||||
|
||||
std::shared_ptr<TexturedModel> treeModel = AssetManager::getModel("lowPolyTree");
|
||||
std::shared_ptr<TexturedModel> treeModel2 = AssetManager::getModel("lowPolyTree2");
|
||||
std::vector<std::shared_ptr<TexturedModel>> treeModels = {treeModel, treeModel2};
|
||||
|
||||
std::uniform_int_distribution<int> treeModelDistribution(0, treeModels.size() - 1);
|
||||
|
||||
for (int r = 0; r < height; ++r) {
|
||||
for (int q = 0; q < width; ++q) {
|
||||
HexTile tile;
|
||||
@ -43,17 +50,40 @@ public:
|
||||
int treeCount = 3 + (int) (dis(gen) * 3);
|
||||
std::uniform_real_distribution<float> offsetDis(-hexRadius * 0.5f, hexRadius * 0.5f);
|
||||
|
||||
float minDistance = 3.0f; // Minimaler Abstand zwischen Bäumen
|
||||
|
||||
for (int i = 0; i < treeCount; ++i) {
|
||||
glm::vec3 treePos = tile.worldPos;
|
||||
treePos.x += offsetDis(gen);
|
||||
treePos.z += offsetDis(gen);
|
||||
glm::vec3 treePos;
|
||||
bool validPos = false;
|
||||
|
||||
std::shared_ptr<Entity> treeEntity = std::make_shared<Entity>(Entity(treeModel, treePos, 0,0,0,.4f));
|
||||
// versuche zufällige Position, bis Abstand stimmt oder max Versuche erreicht
|
||||
int attempts = 0;
|
||||
const int maxAttempts = 10;
|
||||
|
||||
while (!validPos && attempts < maxAttempts) {
|
||||
treePos = tile.worldPos;
|
||||
treePos.x += offsetDis(gen);
|
||||
treePos.z += offsetDis(gen);
|
||||
|
||||
validPos = true;
|
||||
for (const auto& otherTree : tile.entitiesOnTile) {
|
||||
if (glm::distance(treePos, otherTree->getPosition()) < minDistance) {
|
||||
validPos = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
attempts++;
|
||||
}
|
||||
|
||||
if (!validPos) continue; // zu viele Versuche, überspringen
|
||||
|
||||
int treeModelIndex = treeModelDistribution(gen);
|
||||
auto treeEntity = std::make_shared<Entity>(treeModels[treeModelIndex], treePos, 0, 0, 0, 1.f);
|
||||
mapEntities.push_back(treeEntity);
|
||||
tile.entitiesOnTile.push_back(treeEntity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
map.tiles.push_back(tile);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user