From b579882324b6f20e04220f6cea735481abf18d47 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 26 Sep 2022 11:03:27 -0400 Subject: multichunk meshing --- src/chunk.cpp | 71 +++++++++++++++++++----------------- src/chunk.hpp | 1 + src/first_app.cpp | 22 ++++++++--- src/first_app.hpp | 1 + src/keyboard_movement_controller.hpp | 2 +- 5 files changed, 56 insertions(+), 41 deletions(-) (limited to 'src') 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,70 +14,73 @@ 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}]; } -void Chunk::reset() { - chunks.clear(); -} - -std::shared_ptr Chunk::getMesh() { - if(reloadRequired) { - delete chunkMesh.get(); - xe::Model::Builder builder{}; - builder.vertexData = vertexData; - builder.vertexSize = 32; - chunkMesh = std::make_shared(xe::Engine::getInstance()->getDevice(), builder); +Chunk* Chunk::getChunk(uint32_t gridX, uint32_t gridZ) { + if(chunks.count({gridX, gridZ})) { + return chunks[{gridX, gridZ}]; + } else { + return NULL; } - return chunkMesh; } uint8_t Chunk::getBlock(uint32_t x, uint32_t y, uint32_t z) { - if(y < 0 || y > 256) return AIR; + 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 > 16) { - chunkX ++; + } else if(x > 15) { + chunkX++; x = 0; } if(z < 0) { chunkZ--; z = 15; - } else if(z > 16) { - chunkZ ++; + } else if(z > 15) { + chunkZ++; z = 0; } if(chunkX == gridX && chunkZ == gridZ) { - int index = (z * 16 * 256) + (y * 16) + x; + int index = x + (z * 16) + (y * 256); return blocks[index]; } else { Chunk* chunk = getChunk(chunkX, chunkZ); if(chunk == nullptr) { - return AIR; + return INVALID; } else { - int index = (z * 16 * 256) + (y * 16) + x; + 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 = (z * 16 * 256) + (y * 16) + x; + int index = x + (z * 16) + (y * 256); 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::reset() { + for(const auto &[key, chunk]: chunks) { + delete chunk; + } + chunks.clear(); +} + +std::shared_ptr Chunk::getMesh() { + if(reloadRequired) { + delete chunkMesh.get(); + xe::Model::Builder builder{}; + builder.vertexData = vertexData; + builder.vertexSize = 32; + chunkMesh = std::make_shared(xe::Engine::getInstance()->getDevice(), builder); + } + return chunkMesh; } void Chunk::createMeshAsync() { @@ -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}; }; -- cgit v1.2.3-freya