134 lines
7.0 KiB
C++
134 lines
7.0 KiB
C++
#include "chunk.h"
|
|
#include "world.h"
|
|
|
|
Chunk::Chunk(int x, int y) : m_posX(x), m_posY(y) { m_blocks.Reset(BTYPE_AIR); }
|
|
|
|
Chunk::~Chunk() { }
|
|
|
|
void Chunk::RemoveBlock(int x, int y, int z, World* world) {
|
|
m_blocks.Set(x, y, z, BTYPE_AIR);
|
|
|
|
CheckNeighbors(x, y, world);
|
|
|
|
m_isDirty = true;
|
|
}
|
|
|
|
void Chunk::SetBlock(int x, int y, int z, BlockType type, World* world) {
|
|
m_blocks.Set(x, y, z, type);
|
|
|
|
CheckNeighbors(x, z, world);
|
|
|
|
m_isDirty = true;
|
|
}
|
|
|
|
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
|
|
|
|
void Chunk::CheckNeighbors(int x, int z, World* world) {
|
|
if (x == 0 && m_posX >= 0 &&
|
|
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z))
|
|
world->ChunkAt((m_posX - 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
|
else if (x == CHUNK_SIZE_X - 1 && m_posX < WORLD_SIZE_X &&
|
|
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z))
|
|
world->ChunkAt((m_posX + 1) * CHUNK_SIZE_X, 1, m_posY * CHUNK_SIZE_Z)->MakeDirty();
|
|
|
|
if (z == 0 && m_posY >= 0 &&
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z))
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY - 1) * CHUNK_SIZE_Z)->MakeDirty();
|
|
else if (z == CHUNK_SIZE_X - 1 && m_posY < WORLD_SIZE_Y &&
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z))
|
|
world->ChunkAt(m_posX * CHUNK_SIZE_X, 1, (m_posY + 1) * CHUNK_SIZE_Z)->MakeDirty();
|
|
}
|
|
|
|
int Chunk::GetPosX() const { return m_posX; }
|
|
|
|
int Chunk::GetPosY() const { return m_posY; }
|
|
|
|
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST], World* world) {
|
|
float u, v, s;
|
|
// Update mesh
|
|
if (m_isDirty) {
|
|
int maxVertexCount = (CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z) * (6 * 4);
|
|
VertexBuffer::VertexData* vd = new VertexBuffer::VertexData[maxVertexCount];
|
|
int count = 0;
|
|
for (int x = 0; x < CHUNK_SIZE_X; ++x) {
|
|
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
for (int y = 0; y < CHUNK_SIZE_Y; ++y) {
|
|
if (count > USHRT_MAX)
|
|
break;
|
|
|
|
BlockType bt = GetBlock(x, y, z);
|
|
|
|
if (bt != BTYPE_AIR) {
|
|
blockinfo[bt]->GetTexture(u, v, s);
|
|
AddBlockToMesh(vd, count, bt, x, y, z, u, v, s, world);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (count > USHRT_MAX) {
|
|
count = USHRT_MAX;
|
|
std::cout << "[ Chunk :: Update ] Chunk data truncaned , too much vertices to have a 16 bit index " << std::endl;
|
|
}
|
|
m_vertexBuffer.SetMeshData(vd, count);
|
|
delete[] vd;
|
|
}
|
|
m_isDirty = false;
|
|
}
|
|
|
|
void Chunk::AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt,
|
|
int x, int y, int z, float u, float v, float s, World* world) {
|
|
|
|
int cx = x + m_posX * CHUNK_SIZE_X, cy = z + m_posY * CHUNK_SIZE_Z;
|
|
|
|
if (y == CHUNK_SIZE_Y - 1 || GetBlock(x, y + 1, z) == BTYPE_AIR) { // y
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u + s, v);
|
|
}
|
|
|
|
if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, .8f, .8f, .8f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .8f, .8f, .8f, u + s, v);
|
|
}
|
|
|
|
if (world->BlockAt(cx + 1, y, cy) == BTYPE_AIR) { // x
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u + s, v);
|
|
}
|
|
|
|
if (world->BlockAt(cx - 1, y, cy) == BTYPE_AIR) { // -x
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, .9f, .9f, .9f, u + s, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z + 1.f, .9f, .9f, .9f, u, v);
|
|
}
|
|
|
|
if (world->BlockAt(cx, y, cy + 1) == BTYPE_AIR) { // z
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u + s, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z + 1.f, 1.f, 1.f, 1.f, u, v + s);
|
|
}
|
|
|
|
if (world->BlockAt(cx, y, cy - 1) == BTYPE_AIR) { // -z
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y + 1.f, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y + 1.f, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u + s, v + s);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X + 1.f, y, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u + s, v);
|
|
vd[count++] = VertexBuffer::VertexData(x + m_posX * CHUNK_SIZE_X, y, z + m_posY * CHUNK_SIZE_Z, 1.f, 1.f, 1.f, u, v);
|
|
}
|
|
}
|
|
|
|
void Chunk::Render() const { m_vertexBuffer.Render(); }
|
|
|
|
bool Chunk::IsDirty() const { return m_isDirty; }
|
|
|
|
void Chunk::MakeDirty() { m_isDirty = true; }
|
|
|
|
void Chunk::MakeModified() { m_isModified = true; }
|
|
|