From 573ad69ee4118a00588b6fc43d660d64499fea92 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Tue, 5 Nov 2019 12:39:18 +0000 Subject: [PATCH] Terrain Generation --- src/game.cpp | 2 +- src/world/chunk/chunk.cpp | 45 +++++++++++++++++++------- src/world/chunk/chunk.hpp | 12 ++++--- src/world/generator/chunkgenerator.cpp | 14 ++++++++ src/world/world.cpp | 14 ++++++-- src/world/world.hpp | 5 +++ 6 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 src/world/generator/chunkgenerator.cpp diff --git a/src/game.cpp b/src/game.cpp index 8182bcc..5f9610c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -84,7 +84,7 @@ void Game::Setup(int w, int h) { m_cameras["Default"] = std::make_shared(w, h); m_activeCamera = m_cameras["Default"]; - m_activeCamera->Position = { 0, 40, 0 }; + m_activeCamera->Position = { 0, 64, 0 }; m_activeCamera->UpdateView(); std::shared_ptr BlockDictionary = CBlockDictionary::GetInstance(); diff --git a/src/world/chunk/chunk.cpp b/src/world/chunk/chunk.cpp index 247895a..09b28c3 100644 --- a/src/world/chunk/chunk.cpp +++ b/src/world/chunk/chunk.cpp @@ -1,12 +1,13 @@ #include "chunk.hpp" +#include "voxel.hpp" #include "../../renderer/shader.hpp" #include "../../renderer/camera.hpp" -#include "voxel.hpp" - #include "../block.hpp" +#include "../../util/fastnoise.hpp" + #include static std::default_random_engine generator; @@ -17,7 +18,7 @@ Chunk::Chunk() { Chunk::Chunk(int x, int z) { - m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH }); + X = x, Z = z; Load(); @@ -25,7 +26,7 @@ Chunk::Chunk(int x, int z) { Chunk::Chunk(int x, int z, std::vector voxels) { - m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH }); + X = x, Z = z; Voxels = voxels; @@ -33,16 +34,44 @@ Chunk::Chunk(int x, int z, std::vector voxels) { } +Chunk::Chunk(int x, int z, std::shared_ptr terrainGenerator) { + + X = x, Z = z; + + for (x = 0; x < CHUNK_WIDTH; x++) + for (int y = 0; y < CHUNK_HEIGHT; y++) + for (z = 0; z < CHUNK_DEPTH; z++) { + + if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetValueFractal(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.3f) { + + Voxels.push_back((uint8_t)EBlockType::Grass); + + } else { + + Voxels.push_back((uint8_t)EBlockType::Air); + + } + + } + + Load(); + +} + void Chunk::Load() { if (Loaded) return; + m_model = glm::translate(glm::mat4(1.0f), { X * CHUNK_WIDTH, 0, Z * CHUNK_DEPTH }); + if (!Voxels.empty()) { m_mesh(); + Loaded = true; return; } + // Generate a superflat chunk if nothing is there // [x + WIDTH * (y + HEIGHT * z)] for (int x = 0; x < CHUNK_WIDTH; x++) for (int y = 0; y < CHUNK_HEIGHT; y++) @@ -53,14 +82,6 @@ void Chunk::Load() { continue; } - std::uniform_real_distribution distribution(0, 1); - float r = distribution(generator); - - if (r > 0.9f) { - Voxels.push_back((uint8_t)EBlockType::Air); - continue; - } - if (y == 0) Voxels.push_back((uint8_t)EBlockType::Bedrock); else if (y < 28) diff --git a/src/world/chunk/chunk.hpp b/src/world/chunk/chunk.hpp index 16ae9b3..2588a2c 100644 --- a/src/world/chunk/chunk.hpp +++ b/src/world/chunk/chunk.hpp @@ -7,6 +7,8 @@ #define CHUNK_WIDTH 16 #define CHUNK_DEPTH 16 +class FastNoise; + class Camera; class Shader; @@ -18,20 +20,17 @@ public: Chunk(); Chunk(int x, int z); Chunk(int x, int z, std::vector voxels); + Chunk(int x, int z, std::shared_ptr terrainGenerator); void Load(); - bool MeshReady = false; void UploadMesh(); - + bool MeshReady = false; void Render(std::shared_ptr camera, std::shared_ptr shader); void Update(std::vector voxels); - //bool Loaded = false; - //bool Render = false; - uint8_t BlockAt(int x, int y, int z); // Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxelID @@ -44,6 +43,9 @@ public: // To only be changed by render components bool ShouldRender = false; + // Chunk World pos + int X,Z; + ~Chunk(); private: diff --git a/src/world/generator/chunkgenerator.cpp b/src/world/generator/chunkgenerator.cpp new file mode 100644 index 0000000..435dc81 --- /dev/null +++ b/src/world/generator/chunkgenerator.cpp @@ -0,0 +1,14 @@ +#include "../../util/fastnoise.hpp" + + +void dp() { + + FastNoise noise; + noise.SetSeed(121212); + + noise.SetNoiseType(FastNoise::SimplexFractal); + + noise.SetFractalOctaves(3); + +} + diff --git a/src/world/world.cpp b/src/world/world.cpp index 7c0b10c..f3519c5 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -5,6 +5,7 @@ #include "../renderer/shader.hpp" #include "../config.hpp" +#include "../util/fastnoise.hpp" World::World() { @@ -16,13 +17,21 @@ void World::LoadWorld() { m_shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple"); m_shaders["Basic"]->Link(); - for (int x = -4; x < 4; x++) + m_noiseGenerator = std::make_shared(); + m_noiseGenerator->SetSeed(rand()); + + m_noiseGenerator->SetNoiseType(FastNoise::SimplexFractal); + + m_noiseGenerator->SetFractalOctaves(3); + + for (int x = -4; x < 10; x++) for (int y = -4; y < 4; y++) { m_chunkLoaderQueue.push({ x, y }); } + // for (int i = 0; i < 7; i++) { m_generatorThreads.push_back(std::thread([&]() { @@ -115,7 +124,7 @@ void World::m_loadChunks() { m_chunkMutex.unlock(); - std::shared_ptr loadingChunk = std::make_shared(coords.x, coords.y); + std::shared_ptr loadingChunk = std::make_shared(coords.x, coords.y, m_noiseGenerator); std::cout << "Loaded chunk " << coords.x << ":" << coords.y << std::endl; @@ -130,6 +139,7 @@ void World::m_loadChunks() { while (m_chunkLoaderQueue.empty()) { if (!m_generatorRunning) break; + static std::chrono::milliseconds dura(1); std::this_thread::sleep_for(dura); diff --git a/src/world/world.hpp b/src/world/world.hpp index 49826d3..1f793f6 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -12,6 +12,8 @@ #include #include +class FastNoise; + class Shader; class World { @@ -61,6 +63,9 @@ private: std::mutex m_chunkMutex; + // Generator + std::shared_ptr m_noiseGenerator; + void m_loadChunks();