chunk: migration to intervalmaps

intervalmaps-array-y
EmaMaker 2022-11-20 19:14:11 +01:00 committed by emamaker
parent 5dd924abf7
commit 8eb15f4725
4 changed files with 40 additions and 25 deletions

View File

@ -9,6 +9,8 @@
#include <bitset> #include <bitset>
#include "block.hpp" #include "block.hpp"
#include "spacefilling.hpp"
#include "intervalmap.hpp"
#define CHUNK_SIZE 16 #define CHUNK_SIZE 16
#define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) #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); void setBlock(Block b, int x, int y, int z);
Block getBlock(int x, int y, int z); Block getBlock(int x, int y, int z);
std::array<Block, CHUNK_VOLUME> *getBlocks() { return &(this->blocks); } IntervalMap<Block>& getBlocks() { return (this->blocks); }
Block* getBlocksArray(int* len) { return (this->blocks.toArray(len)); }
private: private:
glm::vec3 position{}; glm::vec3 position{};
std::array<Block, CHUNK_VOLUME> blocks{}; IntervalMap<Block> blocks{};
std::bitset<8> state{0}; std::bitset<8> state{0};
}; };

View File

@ -3,6 +3,8 @@
#include "chunk.hpp" #include "chunk.hpp"
#include "block.hpp" #include "block.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "intervalmap.hpp"
#include "globals.hpp"
namespace Chunk namespace Chunk
{ {
@ -15,6 +17,7 @@ namespace Chunk
Chunk::Chunk(glm::vec3 pos) Chunk::Chunk(glm::vec3 pos)
{ {
this->position = pos; this->position = pos;
this->blocks.insert(0, CHUNK_VOLUME, Block::AIR);
// std::cout << "CHUNK" << std::endl; // std::cout << "CHUNK" << std::endl;
} }
@ -24,12 +27,15 @@ namespace Chunk
Block Chunk::getBlock(int x, int y, int z) 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) 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) void Chunk::setState(uint8_t nstate, bool value)

View File

@ -27,8 +27,8 @@ void generateChunk(Chunk::Chunk *chunk)
Block block; Block block;
std::mt19937 mt; std::mt19937 mt;
OpenSimplexNoise::Noise noiseGen1(mt()); OpenSimplexNoise::Noise noiseGen1(1234);
OpenSimplexNoise::Noise noiseGen2(mt()); OpenSimplexNoise::Noise noiseGen2(12345);
std::array<int, CHUNK_SIZE * CHUNK_SIZE> grassNoiseLUT; std::array<int, CHUNK_SIZE * CHUNK_SIZE> grassNoiseLUT;
std::array<int, CHUNK_SIZE * CHUNK_SIZE> dirtNoiseLUT; std::array<int, CHUNK_SIZE * CHUNK_SIZE> dirtNoiseLUT;
@ -79,5 +79,5 @@ void generatePyramid(Chunk::Chunk *chunk)
for (int j = 0; j < CHUNK_SIZE; j++) for (int j = 0; j < CHUNK_SIZE; j++)
for (int k = 0; k < CHUNK_SIZE; k++) 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; // 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);
} }

View File

@ -1,4 +1,5 @@
#include <array> #include <array>
#include <memory>
#include "block.hpp" #include "block.hpp"
#include "chunk.hpp" #include "chunk.hpp"
@ -7,7 +8,8 @@
#include "spacefilling.hpp" #include "spacefilling.hpp"
#include "utils.hpp" #include "utils.hpp"
ChunkMesh::~ChunkMesh(){ ChunkMesh::~ChunkMesh()
{
delete this->chunk; delete this->chunk;
delete (this->theShader); delete (this->theShader);
} }
@ -56,9 +58,10 @@ void ChunkMesh::mesh()
vIndex = 0; vIndex = 0;
// convert tree to array since it is easier to work with it // convert tree to array since it is easier to work with it
std::array<Block, CHUNK_VOLUME> blocks = *(this->chunk->getBlocks()); int length{0};
Block *blocks = this->chunk->getBlocksArray(&length);
if (blocks.size() == 0) if (length == 0)
return; return;
int k, l, u, v, w, h, n, j, i; 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]++) 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) 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]]]*/ : 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 // This is the original line taken from rob's code, readapted (replace voxelFace
@ -119,9 +122,7 @@ void ChunkMesh::mesh()
x[dim]++; x[dim]++;
n = 0; n = 0;
// Actually generate the mesh from the mask. This is the // Actually generate the mesh from the mask. This is the same thing I used in my old crappy voxel engine
// same
// thing I used in my old crappy voxel engine
for (j = 0; j < CHUNK_SIZE; j++) for (j = 0; j < CHUNK_SIZE; j++)
{ {
for (i = 0; i < CHUNK_SIZE;) for (i = 0; i < CHUNK_SIZE;)
@ -198,6 +199,9 @@ void ChunkMesh::mesh()
} }
} }
if (blocks)
delete blocks;
if (vertices.size() > 0) if (vertices.size() > 0)
{ {
@ -227,7 +231,8 @@ void ChunkMesh::draw()
{ {
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // wireframe mode
if (vertices.size() > 0)
{
theShader->use(); theShader->use();
theShader->setMat4("model", this->model); theShader->setMat4("model", this->model);
theShader->setMat4("view", theCamera.getView()); theShader->setMat4("view", theCamera.getView());
@ -237,6 +242,7 @@ void ChunkMesh::draw()
glDrawElements(GL_TRIANGLES, static_cast<GLuint>(indices.size()), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, static_cast<GLuint>(indices.size()), GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }
}
void ChunkMesh::quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace) void ChunkMesh::quad(glm::vec3 bottomLeft, glm::vec3 topLeft, glm::vec3 topRight, glm::vec3 bottomRight, Block block, bool backFace)
{ {