diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2022-09-26 11:03:27 -0400 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2022-09-26 11:03:27 -0400 |
commit | b579882324b6f20e04220f6cea735481abf18d47 (patch) | |
tree | af2670c05b49d9ba95cbc8769e781c091bba94db | |
parent | MSAA (diff) | |
download | minecraftvulkan-b579882324b6f20e04220f6cea735481abf18d47.tar.gz minecraftvulkan-b579882324b6f20e04220f6cea735481abf18d47.tar.bz2 minecraftvulkan-b579882324b6f20e04220f6cea735481abf18d47.zip |
multichunk meshing
-rw-r--r-- | engine/xe_engine.cpp | 2 | ||||
-rw-r--r-- | src/chunk.cpp | 71 | ||||
-rw-r--r-- | src/chunk.hpp | 1 | ||||
-rwxr-xr-x | src/first_app.cpp | 22 | ||||
-rwxr-xr-x | src/first_app.hpp | 1 | ||||
-rw-r--r-- | src/keyboard_movement_controller.hpp | 2 |
6 files changed, 57 insertions, 42 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<float, std::chrono::seconds::period>(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<std::string, std::unique_ptr<Chunk>> chunks{}; +static std::map<std::pair<uint32_t, uint32_t>, 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> chunk = std::make_unique<Chunk>(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<xe::Model> Chunk::getMesh() { - if(reloadRequired) { - delete chunkMesh.get(); - xe::Model::Builder builder{}; - builder.vertexData = vertexData; - builder.vertexSize = 32; - chunkMesh = std::make_shared<xe::Model>(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<xe::Model> Chunk::getMesh() { + if(reloadRequired) { + delete chunkMesh.get(); + xe::Model::Builder builder{}; + builder.vertexData = vertexData; + builder.vertexSize = 32; + chunkMesh = std::make_shared<xe::Model>(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 <string> #include <map> +#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 <string> #include <memory> #include <vector> +#include <iostream> 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}; }; |