SQCSimulator2023/SQCSim2021/chunk.cpp

99 lines
4.7 KiB
C++
Raw Normal View History

#include "chunk.h"
Chunk::Chunk() { m_blocks.Reset(BTYPE_AIR); }
2021-10-11 11:37:58 -04:00
Chunk::~Chunk() { }
2021-10-11 11:37:58 -04:00
void Chunk::RemoveBlock(int x, int y, int z) {
m_blocks.Set(x, y, z, BTYPE_AIR);
m_isDirty = true;
}
2021-10-11 11:37:58 -04:00
void Chunk::SetBlock(int x, int y, int z, BlockType type) {
m_blocks.Set(x, y, z, type);
m_isDirty = true;
}
2021-10-11 11:37:58 -04:00
BlockType Chunk::GetBlock(int x, int y, int z) { return m_blocks.Get(x, y, z); }
2021-10-31 00:31:08 -04:00
void Chunk::Update(BlockInfo* blockinfo[BTYPE_LAST]) {
float u, v, s;
2021-10-11 11:37:58 -04:00
// 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) {
2021-10-31 00:31:08 -04:00
blockinfo[bt]->GetTexture(u, v, s);
AddBlockToMesh(vd, count, bt, x, y, z, u, v, s);
2021-10-11 11:37:58 -04:00
}
}
}
}
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) {
if (x == CHUNK_SIZE_X - 1 || GetBlock(x + 1, y, z) == BTYPE_AIR) { // x
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, .9f, .9f, .9f, u, v);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, .9f, .9f, .9f, u + s, v);
}
2021-10-11 11:37:58 -04:00
if (x == 0 || GetBlock(x - 1, y, z) == BTYPE_AIR) { // -x
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, .9f, .9f, .9f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, .9f, .9f, .9f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, .9f, .9f, .9f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, .9f, .9f, .9f, u, v);
}
2021-10-11 11:37:58 -04:00
if (y == CHUNK_SIZE_Y - 1 || GetBlock(x, y + 1, z) == BTYPE_AIR) { // y
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, .8f, .8f, .8f, u, v);
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, .8f, .8f, .8f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, .8f, .8f, .8f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, .8f, .8f, .8f, u + s, v);
}
if (y == 0 || GetBlock(x, y - 1, z) == BTYPE_AIR) { // -y
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, .8f, .8f, .8f, u, v);
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, .8f, .8f, .8f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, .8f, .8f, .8f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, .8f, .8f, .8f, u + s, v);
}
2021-10-11 11:37:58 -04:00
if (z == CHUNK_SIZE_Z - 1 || GetBlock(x, y, z + 1) == BTYPE_AIR) { // z
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, u, v);
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z + .5f, 1.f, 1.f, 1.f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z + .5f, 1.f, 1.f, 1.f, u, v + s);
}
2021-10-11 11:37:58 -04:00
if (z == 0 || GetBlock(x, y, z - 1) == BTYPE_AIR) { // -z
2021-10-31 00:31:08 -04:00
vd[count++] = VertexBuffer::VertexData(x - .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, u, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y + .5f, z - .5f, 1.f, 1.f, 1.f, u + s, v + s);
vd[count++] = VertexBuffer::VertexData(x + .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, u + s, v);
vd[count++] = VertexBuffer::VertexData(x - .5f, y - .5f, z - .5f, 1.f, 1.f, 1.f, u, v);
}
}
2021-10-11 11:37:58 -04:00
void Chunk::Render() const { m_vertexBuffer.Render(); }
bool Chunk::IsDirty() const { return m_isDirty; }