From 8eb15f4725d7b9f5a2b7e8791c7694aeffd8cb10 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 20 Nov 2022 19:14:11 +0100 Subject: [PATCH] chunk: migration to intervalmaps --- include/chunk.hpp | 7 +++++-- src/chunk.cpp | 10 ++++++++-- src/chunkgenerator.cpp | 6 +++--- src/chunkmesh.cpp | 42 ++++++++++++++++++++++++------------------ 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index e511470..fde267e 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -9,6 +9,8 @@ #include #include "block.hpp" +#include "spacefilling.hpp" +#include "intervalmap.hpp" #define CHUNK_SIZE 16 #define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) @@ -39,11 +41,12 @@ namespace Chunk void setBlock(Block b, int x, int y, int z); Block getBlock(int x, int y, int z); - std::array *getBlocks() { return &(this->blocks); } + IntervalMap& getBlocks() { return (this->blocks); } + Block* getBlocksArray(int* len) { return (this->blocks.toArray(len)); } private: glm::vec3 position{}; - std::array blocks{}; + IntervalMap blocks{}; std::bitset<8> state{0}; }; diff --git a/src/chunk.cpp b/src/chunk.cpp index cca99bf..8139fd5 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -3,6 +3,8 @@ #include "chunk.hpp" #include "block.hpp" #include "utils.hpp" +#include "intervalmap.hpp" +#include "globals.hpp" namespace Chunk { @@ -15,6 +17,7 @@ namespace Chunk Chunk::Chunk(glm::vec3 pos) { this->position = pos; + this->blocks.insert(0, CHUNK_VOLUME, Block::AIR); // std::cout << "CHUNK" << std::endl; } @@ -24,12 +27,15 @@ namespace Chunk Block Chunk::getBlock(int x, int y, int z) { - return blocks[utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)]; + return blocks.at(coord3DTo1D(x, y, z)); + // return blocks.at(HILBERT_XYZ_ENCODE[x][y][z]); } void Chunk::setBlock(Block b, int x, int y, int z) { - blocks[utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)] = b; + // return blocks.insert(HILBERT_XYZ_ENCODE[x][y][z], HILBERT_XYZ_ENCODE[x][y][z]+1, b); + int coord = coord3DTo1D(x, y, z); + blocks.insert(coord <= 0 ? 0 : coord, coord+1 >= CHUNK_VOLUME ? CHUNK_VOLUME : coord+1, b); } void Chunk::setState(uint8_t nstate, bool value) diff --git a/src/chunkgenerator.cpp b/src/chunkgenerator.cpp index e30333d..5fa4aea 100644 --- a/src/chunkgenerator.cpp +++ b/src/chunkgenerator.cpp @@ -27,8 +27,8 @@ void generateChunk(Chunk::Chunk *chunk) Block block; std::mt19937 mt; -OpenSimplexNoise::Noise noiseGen1(mt()); -OpenSimplexNoise::Noise noiseGen2(mt()); +OpenSimplexNoise::Noise noiseGen1(1234); +OpenSimplexNoise::Noise noiseGen2(12345); std::array grassNoiseLUT; std::array dirtNoiseLUT; @@ -79,5 +79,5 @@ void generatePyramid(Chunk::Chunk *chunk) for (int j = 0; j < CHUNK_SIZE; j++) for (int k = 0; k < CHUNK_SIZE; k++) // blocks[utils::coord3DTo1D(i, j, k, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)] = j == 0 ? Block::STONE : Block::AIR; - chunk->setBlock(i >= j && i < CHUNK_SIZE - j && k >= j && k < CHUNK_SIZE - j ? Block::STONE : Block::AIR, i, j, k); + chunk->setBlock(i >= j && i < CHUNK_SIZE - j && k >= j && k < CHUNK_SIZE - j ? (j & 1) == 0 ? Block::GRASS : Block::STONE : Block::AIR, i, j, k); } \ No newline at end of file diff --git a/src/chunkmesh.cpp b/src/chunkmesh.cpp index 898ae4e..d765f85 100755 --- a/src/chunkmesh.cpp +++ b/src/chunkmesh.cpp @@ -1,4 +1,5 @@ #include +#include #include "block.hpp" #include "chunk.hpp" @@ -7,7 +8,8 @@ #include "spacefilling.hpp" #include "utils.hpp" -ChunkMesh::~ChunkMesh(){ +ChunkMesh::~ChunkMesh() +{ delete this->chunk; delete (this->theShader); } @@ -56,9 +58,10 @@ void ChunkMesh::mesh() vIndex = 0; // convert tree to array since it is easier to work with it - std::array blocks = *(this->chunk->getBlocks()); + int length{0}; + Block *blocks = this->chunk->getBlocksArray(&length); - if (blocks.size() == 0) + if (length == 0) return; int k, l, u, v, w, h, n, j, i; @@ -96,9 +99,9 @@ void ChunkMesh::mesh() { for (x[u] = 0; x[u] < CHUNK_SIZE; x[u]++) { - Block b1 = (x[dim] >= 0) ? blocks[utils::coord3DTo1D(x[0], x[1], x[2], CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)] : Block::NULLBLK; /*blocks[SpaceFilling::HILBERT_XYZ_ENCODE[x[0]][x[1]][x[2]]]*/ + Block b1 = (x[dim] >= 0) ? blocks[Chunk::coord3DTo1D(x[0], x[1], x[2])] : Block::NULLBLK; /*blocks[SpaceFilling::HILBERT_XYZ_ENCODE[x[0]][x[1]][x[2]]]*/ Block b2 = (x[dim] < CHUNK_SIZE - 1) - ? blocks[utils::coord3DTo1D(x[0] + q[0], x[1] + q[1], x[2] + q[2], CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)] + ? blocks[Chunk::coord3DTo1D(x[0] + q[0], x[1] + q[1], x[2] + q[2])] : Block::NULLBLK; /*blocks[SpaceFilling::HILBERT_XYZ_ENCODE[x[0] + q[0]][x[1] + q[1]][x[2] + q[2]]]*/ // This is the original line taken from rob's code, readapted (replace voxelFace @@ -119,9 +122,7 @@ void ChunkMesh::mesh() x[dim]++; n = 0; - // Actually generate the mesh from the mask. This is the - // same - // thing I used in my old crappy voxel engine + // Actually generate the mesh from the mask. This is the same thing I used in my old crappy voxel engine for (j = 0; j < CHUNK_SIZE; j++) { for (i = 0; i < CHUNK_SIZE;) @@ -198,6 +199,9 @@ void ChunkMesh::mesh() } } + if (blocks) + delete blocks; + if (vertices.size() > 0) { @@ -227,15 +231,17 @@ void ChunkMesh::draw() { // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode + if (vertices.size() > 0) + { + theShader->use(); + theShader->setMat4("model", this->model); + theShader->setMat4("view", theCamera.getView()); + theShader->setMat4("projection", theCamera.getProjection()); - theShader->use(); - theShader->setMat4("model", this->model); - theShader->setMat4("view", theCamera.getView()); - theShader->setMat4("projection", theCamera.getProjection()); - - glBindVertexArray(VAO); - glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + glBindVertexArray(VAO); + glDrawElements(GL_TRIANGLES, static_cast(indices.size()), GL_UNSIGNED_INT, 0); + glBindVertexArray(0); + } } void ChunkMesh::quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace) @@ -248,7 +254,7 @@ void ChunkMesh::quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight vertices.push_back(bottomRight.x); vertices.push_back(bottomRight.y); vertices.push_back(bottomRight.z); - + vertices.push_back(topLeft.x); vertices.push_back(topLeft.y); vertices.push_back(topLeft.z); @@ -263,7 +269,7 @@ void ChunkMesh::quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight indices.push_back(vIndex + 1); indices.push_back(vIndex); indices.push_back(vIndex + 2); - vIndex+=4; + vIndex += 4; // } // else // {