From b579882324b6f20e04220f6cea735481abf18d47 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 26 Sep 2022 11:03:27 -0400 Subject: [PATCH] multichunk meshing --- engine/xe_engine.cpp | 2 +- src/chunk.cpp | 107 ++++++++++++++------------- src/chunk.hpp | 1 + src/first_app.cpp | 22 ++++-- src/first_app.hpp | 1 + src/keyboard_movement_controller.hpp | 2 +- 6 files changed, 75 insertions(+), 60 deletions(-) diff --git a/engine/xe_engine.cpp b/engine/xe_engine.cpp index 37cba41..b138942 100644 --- a/engine/xe_engine.cpp +++ b/engine/xe_engine.cpp @@ -56,7 +56,7 @@ bool Engine::poll() { frameTime = std::chrono::duration(newTime - currentTime).count(); currentTime = newTime; float aspect = xeRenderer.getAspectRatio(); - xeCamera.setPerspectiveProjection(glm::radians(FOV), aspect, 0.1f, 100.f); + xeCamera.setPerspectiveProjection(glm::radians(FOV), aspect, 0.1f, 1000.f); return !xeWindow.shouldClose(); } diff --git a/src/chunk.cpp b/src/chunk.cpp index 9d646f8..3a71031 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -3,7 +3,7 @@ namespace app { -static std::map> chunks{}; +static std::map, Chunk*> chunks{}; Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed) : world_seed{world_seed}, @@ -14,13 +14,61 @@ Chunk::Chunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed) } Chunk* Chunk::newChunk(uint32_t gridX, uint32_t gridZ, uint32_t world_seed) { - std::unique_ptr chunk = std::make_unique(gridX, gridZ, world_seed); - std::string key = gridX + "." + gridZ; - chunks[key] = std::move(chunk); - return chunks[key].get(); + Chunk* chunk = new Chunk(gridX, gridZ, world_seed); + chunks[{gridX, gridZ}] = std::move(chunk); + return chunks[{gridX, gridZ}]; +} + +Chunk* Chunk::getChunk(uint32_t gridX, uint32_t gridZ) { + if(chunks.count({gridX, gridZ})) { + return chunks[{gridX, gridZ}]; + } else { + return NULL; + } +} + +uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) { + if(y > 256) return AIR; + if(y < 0) return INVALID; + int chunkX = gridX; + int chunkZ = gridZ; + if(x < 0) { + chunkX--; + x = 15; + } else if(x > 15) { + chunkX++; + x = 0; + } + if(z < 0) { + chunkZ--; + z = 15; + } else if(z > 15) { + chunkZ++; + z = 0; + } + if(chunkX == gridX && chunkZ == gridZ) { + int index = x + (z * 16) + (y * 256); + return blocks[index]; + } else { + Chunk* chunk = getChunk(chunkX, chunkZ); + if(chunk == nullptr) { + return INVALID; + } else { + int index = x + (z * 16) + (y * 256); + return chunk->blocks[index]; + } + } +} + +void Chunk::setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block) { + int index = x + (z * 16) + (y * 256); + blocks[index] = block; } void Chunk::reset() { + for(const auto &[key, chunk]: chunks) { + delete chunk; + } chunks.clear(); } @@ -35,51 +83,6 @@ std::shared_ptr Chunk::getMesh() { return chunkMesh; } -uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) { - if(y < 0 || y > 256) return AIR; - int chunkX = gridX; - int chunkZ = gridZ; - if(x < 0) { - chunkX--; - x = 15; - } else if(x > 16) { - chunkX ++; - x = 0; - } - if(z < 0) { - chunkZ--; - z = 15; - } else if(z > 16) { - chunkZ ++; - z = 0; - } - if(chunkX == gridX && chunkZ == gridZ) { - int index = (z * 16 * 256) + (y * 16) + x; - return blocks[index]; - } else { - Chunk* chunk = getChunk(chunkX, chunkZ); - if(chunk == nullptr) { - return AIR; - } else { - int index = (z * 16 * 256) + (y * 16) + x; - return chunk->blocks[index]; - } - } -} - -void Chunk::setBlock(uint32_t x, uint32_t y, uint32_t z, uint8_t block) { - int index = (z * 16 * 256) + (y * 16) + x; - blocks[index] = block; -} - -Chunk* Chunk::getChunk(uint32_t x, uint32_t z) { - std::string key = x + "." + z; - if(chunks.count(key)) - return chunks[key].get(); - else - return nullptr; -} - void Chunk::createMeshAsync() { if(working) return; // worker = std::thread(createMesh); @@ -134,11 +137,11 @@ void Chunk::addVerticies(uint32_t side, uint32_t x, uint32_t y, uint32_t z) { void Chunk::generate() { blocks.resize(16*16*256); - const PerlinNoise perlin{123}; + const PerlinNoise perlin{world_seed}; for(int x = 0; x < 16; x++) { for(int z = 0; z < 16; z++) { - int height = perlin.octave2D_01((x * 0.01), (z * 0.01), 4) * 5; + int height = perlin.octave2D_01((( x + gridX * 16) * 0.01), ((z + gridZ * 16) * 0.01), 4) * 10; for(int y = 0; y < 256; y++) { if(y <= height) setBlock(x, y, z, DIRT); diff --git a/src/chunk.hpp b/src/chunk.hpp index 2fe78f0..b2b80f8 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -11,6 +11,7 @@ #include #include +#define INVALID -1 #define AIR 0 #define DIRT 1 diff --git a/src/first_app.cpp b/src/first_app.cpp index 80ee78c..a16ecde 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -46,13 +46,23 @@ void FirstApp::run() { void FirstApp::loadGameObjects() { - Chunk* chunk = Chunk::newChunk(0, 0, 123); - chunk->createMesh(); + for(int x = 0; x < 10; x++) { + for(int z = 0; z < 10; z++) { + Chunk* chunk = Chunk::newChunk(x, z, 53463); + } + } - auto chunkObject = xe::GameObject::createGameObject(); - chunkObject.model = chunk->getMesh(); - chunkObject.transform.translation = {0.f, 0.f, 0.f}; - gameObjects.push_back(std::move(chunkObject)); + for(int x = 0; x < 10; x++) { + for(int z = 0; z < 10; z++) { + Chunk* chunk = Chunk::getChunk(x,z); + chunk->createMesh(); + + auto chunkObject = xe::GameObject::createGameObject(); + chunkObject.model = chunk->getMesh(); + chunkObject.transform.translation = {16.f*x, 0.f, 16.f*z}; + gameObjects.push_back(std::move(chunkObject)); + } + } } diff --git a/src/first_app.hpp b/src/first_app.hpp index 6fb63e1..87cedba 100755 --- a/src/first_app.hpp +++ b/src/first_app.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace app { class FirstApp { diff --git a/src/keyboard_movement_controller.hpp b/src/keyboard_movement_controller.hpp index 75264c0..1cc0456 100644 --- a/src/keyboard_movement_controller.hpp +++ b/src/keyboard_movement_controller.hpp @@ -36,7 +36,7 @@ namespace app { xe::GameObject &viewerObject; KeyMappings keys{}; - float moveSpeed{3.f}; + float moveSpeed{10.f}; float lookSpeed{1.5f}; };