feat: add support for negative chunk coordinates

This commit is contained in:
2026-03-12 22:30:22 +08:00
parent 27b51d1a6a
commit 11324ba1b2
3 changed files with 132 additions and 40 deletions

View File

@@ -4,7 +4,7 @@ constexpr int WORLD_SIZE_Z = 32;
constexpr int WORLD_SIZE_Y = 16; constexpr int WORLD_SIZE_Y = 16;
constexpr int MAX_BLOCK_NUM = 2; constexpr int MAX_BLOCK_NUM = 2;
constexpr int CHUCK_SIZE = 16; constexpr int CHUCK_SIZE = 16;
constexpr int DISTANCE = 2; constexpr int DISTANCE = 8;
constexpr float VERTICES_POS[6][6][3] = { constexpr float VERTICES_POS[6][6][3] = {

View File

@@ -53,6 +53,12 @@ void Chunk::gen_vertex_data() {
} }
} }
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
GLuint Chunk::get_vbo() const{ GLuint Chunk::get_vbo() const{
@@ -72,12 +78,7 @@ void Chunk::init_chunk() {
} }
} }
} }
gen_vertex_data();
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertexs_data.size() * sizeof(Vertex), m_vertexs_data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }

View File

@@ -7,11 +7,21 @@ World::World() {
World::~World() { World::~World() {
} }
static int chunk_x, chunk_z;
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) { const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
static int chunk_x, chunk_z;
chunk_x = world_x / CHUCK_SIZE; if (world_x < 0) {
chunk_z = world_z / CHUCK_SIZE; chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
}
if (world_x >= 0) {
chunk_x = world_x / CHUCK_SIZE;
}
if (world_z < 0) {
chunk_z = (world_z + 1) / CHUCK_SIZE - 1;
}
if (world_z >= 0) {
chunk_z = world_z / CHUCK_SIZE;
}
//LOG::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z); //LOG::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z);
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z}); auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find"); CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find");
@@ -26,47 +36,69 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
// draw_face // draw_face
m_block_render_data.draw_face.assign(6, true); m_block_render_data.draw_face.assign(6, true);
if (x > 0 ) { if (x > 0 ) {
if (x > 0 && chunk_blocks[Chunk::get_index(x - 1, y, z)]) { if (chunk_blocks[Chunk::get_index(x - 1, y, z)]) {
m_block_render_data.draw_face[3] = false; m_block_render_data.draw_face[3] = false;
} }
} }
if (x < CHUCK_SIZE - 1) { if (x < CHUCK_SIZE - 1) {
if (x < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x + 1, y, z)]) { if (chunk_blocks[Chunk::get_index(x + 1, y, z)]) {
m_block_render_data.draw_face[1] = false; m_block_render_data.draw_face[1] = false;
} }
} }
if (z > 0 ) { if (z > 0 ) {
if (z > 0 && chunk_blocks[Chunk::get_index(x, y, z - 1)]) { if (chunk_blocks[Chunk::get_index(x, y, z - 1)]) {
m_block_render_data.draw_face[2] = false; m_block_render_data.draw_face[2] = false;
} }
} }
if (z < CHUCK_SIZE - 1) { if (z < CHUCK_SIZE - 1) {
if (z < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y, z + 1)]) { if (chunk_blocks[Chunk::get_index(x, y, z + 1)]) {
m_block_render_data.draw_face[0] = false; m_block_render_data.draw_face[0] = false;
} }
} }
if (y > 0 ) { if (y > 0 ) {
if (y > 0 && chunk_blocks[Chunk::get_index(x, y - 1, z)]) { if (chunk_blocks[Chunk::get_index(x, y - 1, z)]) {
m_block_render_data.draw_face[5] = false; m_block_render_data.draw_face[5] = false;
} }
} }
if (y < CHUCK_SIZE - 1) { if (y < CHUCK_SIZE - 1) {
if (y < CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y + 1, z)]) { if (chunk_blocks[Chunk::get_index(x, y + 1, z)]) {
m_block_render_data.draw_face[4] = false; m_block_render_data.draw_face[4] = false;
} }
} }
int adjacent_chunk_x;
if (x == 0 && world_x - 1 > 0) { int adjacent_chunk_z;
int adjacent_chunk_x = (world_x - 1) / CHUCK_SIZE; int adj_world_x;
int adjacet_chunk_z = world_z / CHUCK_SIZE; int adj_world_z;
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); int adjacent_x, adjacent_z;
if (x == 0) {
adj_world_x = world_x - 1;
adj_world_z = world_z;
if (adj_world_x < 0) {
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
}
if (adj_world_x >= 0) {
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
}
if (adj_world_z < 0) {
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
}
if (adj_world_z >= 0) {
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
}
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
if (adjacent != m_chunks.end()) { if (adjacent != m_chunks.end()) {
int adjacent_x, adjacent_z;
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
int x, y, z; int x, y, z;
y = world_y; y = world_y;
x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE; x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE;
z = world_z - adjacet_chunk_z * CHUCK_SIZE; z = world_z - adjacent_chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
} else
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
m_block_render_data.draw_face[3] = false; m_block_render_data.draw_face[3] = false;
} }
@@ -75,34 +107,66 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
} }
if (x == CHUCK_SIZE - 1) { if (x == CHUCK_SIZE - 1) {
int adjacent_chunk_x = (world_x + 1) / CHUCK_SIZE; adj_world_x = world_x + 1;
int adjacet_chunk_z = world_z / CHUCK_SIZE; adj_world_z = world_z;
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); if (adj_world_x < 0) {
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
}
if (adj_world_x >= 0) {
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
}
if (adj_world_z < 0) {
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
}
if (adj_world_z >= 0) {
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
}
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
if (adjacent != m_chunks.end()) { if (adjacent != m_chunks.end()) {
int adjacent_x, adjacent_z; int adjacent_x, adjacent_z;
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
int x, y, z; int x, y, z;
y = world_y; y = world_y;
x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE; x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE;
z = world_z - adjacet_chunk_z * CHUCK_SIZE; z = world_z - adjacent_chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
} else
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
m_block_render_data.draw_face[1] = false; m_block_render_data.draw_face[1] = false;
} }
} }
} }
if (z == 0 && world_z - 1 > 0) { if (z == 0) {
int adjacent_chunk_x = world_x / CHUCK_SIZE; adj_world_x = world_x;
int adjacet_chunk_z = (world_z - 1) / CHUCK_SIZE; adj_world_z = world_z - 1;
if (adj_world_x < 0) {
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
}
if (adj_world_x >= 0) {
adjacent_chunk_x = adj_world_x / CHUCK_SIZE;
}
if (adj_world_z < 0) {
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
}
if (adj_world_z >= 0) {
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
}
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
if (adjacent != m_chunks.end()) { if (adjacent != m_chunks.end()) {
int adjacent_x, adjacent_z; int adjacent_x, adjacent_z;
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
int x, y, z; int x, y, z;
y = world_y; y = world_y;
x = world_x - adjacent_chunk_x * CHUCK_SIZE; x = world_x - adjacent_chunk_x * CHUCK_SIZE;
z = world_z - 1 - adjacet_chunk_z * CHUCK_SIZE; z = world_z - 1 - adjacent_chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
} else
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
m_block_render_data.draw_face[2] = false; m_block_render_data.draw_face[2] = false;
} }
@@ -110,16 +174,32 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
} }
if (z == CHUCK_SIZE - 1) { if (z == CHUCK_SIZE - 1) {
int adjacent_chunk_x = world_x / CHUCK_SIZE; adj_world_x = world_x;
int adjacet_chunk_z = (world_z + 1) / CHUCK_SIZE; adj_world_z = world_z + 1;
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z}); if (adj_world_x < 0) {
adjacent_chunk_x = (adj_world_x + 1) / CHUCK_SIZE - 1;
}
if (adj_world_x >= 0) {
adjacent_chunk_x = adj_world_x/ CHUCK_SIZE;
}
if (adj_world_z < 0) {
adjacent_chunk_z = (adj_world_z + 1) / CHUCK_SIZE - 1;
}
if (adj_world_z >= 0) {
adjacent_chunk_z = adj_world_z / CHUCK_SIZE;
}
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacent_chunk_z});
if (adjacent != m_chunks.end()) { if (adjacent != m_chunks.end()) {
int adjacent_x, adjacent_z; int adjacent_x, adjacent_z;
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks(); const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
int x, y, z; int x, y, z;
y = world_y; y = world_y;
x = world_x - adjacent_chunk_x * CHUCK_SIZE; x = world_x - adjacent_chunk_x * CHUCK_SIZE;
z = world_z + 1 - adjacet_chunk_z * CHUCK_SIZE; z = world_z + 1 - adjacent_chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || Chunk::get_index(x, y, z) >= 4096 || Chunk::get_index(x, y, z) < 0) {
LOG::info("adj x {} z {}", adjacent_chunk_x, adjacent_chunk_z);
LOG::info("x {} y {} z {}, world x {} world y {} world z {} chunk x {} chunk z {}", x, y, z ,world_x, world_y, world_z, chunk_x, chunk_z);
} else
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) { if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
m_block_render_data.draw_face[0] = false; m_block_render_data.draw_face[0] = false;
} }
@@ -133,8 +213,11 @@ const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,in
void World::init_world() { void World::init_world() {
for (int s = 0; s < DISTANCE; s++) { for (int s = 0; s < DISTANCE; s++) {
for (int t = 0; t < DISTANCE; t++) { for (int t = 0; t < DISTANCE; t++) {
ChunkPos pos{s, t}; int ns = s - DISTANCE / 2;
LOG::info("Chunk Pos in init_world X: {} Z: {}", pos.x, pos.z); int nt = t - DISTANCE / 2;
ChunkPos pos{ns, nt};
m_chunks.emplace(pos, Chunk(*this, pos)); m_chunks.emplace(pos, Chunk(*this, pos));
} }
} }
@@ -143,6 +226,14 @@ void World::init_world() {
for (auto& chunk_map : m_chunks) { for (auto& chunk_map : m_chunks) {
auto& [chunk_pos, chunk] = chunk_map; auto& [chunk_pos, chunk] = chunk_map;
chunk.init_chunk(); chunk.init_chunk();
}
// After block gen fininshed
for (auto& chunk_map : m_chunks) {
auto& [chunk_pos, chunk] = chunk_map;
chunk.gen_vertex_data();
} }
} }