mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
perf: optimize multi-chunk rendering logic
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
#include <Cubed/gameplay/chuck.hpp>
|
||||
|
||||
Chuck::Chuck() {
|
||||
init_chuck();
|
||||
}
|
||||
|
||||
Chuck::~Chuck() {
|
||||
|
||||
}
|
||||
|
||||
const std::vector<uint8_t>& Chuck::get_chuck_blocks() const{
|
||||
return m_blocks;
|
||||
}
|
||||
|
||||
void Chuck::init_chuck() {
|
||||
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0);
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
m_blocks[get_index(x, y, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Chuck::get_index(int x, int y, int z) {
|
||||
return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z;
|
||||
}
|
||||
83
src/gameplay/chunk.cpp
Normal file
83
src/gameplay/chunk.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <Cubed/gameplay/chunk.hpp>
|
||||
#include <Cubed/gameplay/world.hpp>
|
||||
Chunk::Chunk(World& world, ChunkPos chunk_pos) :
|
||||
m_world(world),
|
||||
m_chunk_pos(chunk_pos)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Chunk::~Chunk() {
|
||||
|
||||
}
|
||||
|
||||
const std::vector<uint8_t>& Chunk::get_chunk_blocks() const{
|
||||
return m_blocks;
|
||||
}
|
||||
|
||||
|
||||
int Chunk::get_index(int x, int y, int z) {
|
||||
return x * CHUCK_SIZE * CHUCK_SIZE + y * CHUCK_SIZE + z;
|
||||
}
|
||||
|
||||
void Chunk::gen_vertex_data() {
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
for (int y = 0; y < CHUCK_SIZE; y++) {
|
||||
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
|
||||
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
|
||||
int world_y = y;
|
||||
const auto& block_render_data = m_world.get_block_render_data(world_x, world_y, world_z);
|
||||
// air
|
||||
if (block_render_data.block_id == 0) {
|
||||
continue;
|
||||
}
|
||||
for (int face = 0; face < 6; face++) {
|
||||
if (!block_render_data.draw_face[face]) {
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
Vertex vex = {
|
||||
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
|
||||
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
|
||||
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
|
||||
TEX_COORDS[face][i][0],
|
||||
TEX_COORDS[face][i][1],
|
||||
static_cast<float>(block_render_data.block_id * 6 + face)
|
||||
|
||||
};
|
||||
m_vertexs_data.emplace_back(vex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GLuint Chunk::get_vbo() const{
|
||||
return m_vbo;
|
||||
}
|
||||
|
||||
const std::vector<Vertex>& Chunk::get_vertex_data() const{
|
||||
return m_vertexs_data;
|
||||
}
|
||||
|
||||
void Chunk::init_chunk() {
|
||||
m_blocks.assign(CHUCK_SIZE * CHUCK_SIZE * CHUCK_SIZE, 0);
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
m_blocks[get_index(x, y, z)] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,70 +7,161 @@ World::World() {
|
||||
World::~World() {
|
||||
|
||||
}
|
||||
static int chuck_x, chuck_z;
|
||||
static int chunk_x, chunk_z;
|
||||
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
|
||||
|
||||
chuck_x = world_x / CHUCK_SIZE;
|
||||
chuck_z = world_z / CHUCK_SIZE;
|
||||
|
||||
auto it = m_chucks.find((ChuckPos){chuck_x, chuck_z});
|
||||
CUBED_ASSERT_MSG(it != m_chucks.end(), "Chuck not find");
|
||||
chunk_x = world_x / CHUCK_SIZE;
|
||||
chunk_z = world_z / CHUCK_SIZE;
|
||||
//LOG::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z);
|
||||
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
|
||||
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find");
|
||||
|
||||
const auto& chuck_blocks = it->second.get_chuck_blocks();
|
||||
const auto& chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - chuck_x * CHUCK_SIZE;
|
||||
z = world_z - chuck_z * CHUCK_SIZE;
|
||||
x = world_x - chunk_x * CHUCK_SIZE;
|
||||
z = world_z - chunk_z * CHUCK_SIZE;
|
||||
// block id
|
||||
m_block_render_data.block_id = chuck_blocks[Chuck::get_index(x, y, z)];
|
||||
m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)];
|
||||
// draw_face
|
||||
m_block_render_data.draw_face.assign(6, true);
|
||||
if (x > 0 ) {
|
||||
if (x > 0 && chuck_blocks[Chuck::get_index(x - 1, y, z)]) {
|
||||
if (x > 0 && chunk_blocks[Chunk::get_index(x - 1, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
if (x < CHUCK_SIZE - 1) {
|
||||
if (x < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x + 1, y, z)]) {
|
||||
if (x < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x + 1, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
if (z > 0 ) {
|
||||
if (z > 0 && chuck_blocks[Chuck::get_index(x, y, z - 1)]) {
|
||||
if (z > 0 && chunk_blocks[Chunk::get_index(x, y, z - 1)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
if (z < CHUCK_SIZE - 1) {
|
||||
if (z < DISTANCE * CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y, z + 1)]) {
|
||||
if (z < DISTANCE * CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y, z + 1)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
}
|
||||
if (y > 0 ) {
|
||||
if (y > 0 && chuck_blocks[Chuck::get_index(x, y - 1, z)]) {
|
||||
if (y > 0 && chunk_blocks[Chunk::get_index(x, y - 1, z)]) {
|
||||
m_block_render_data.draw_face[5] = false;
|
||||
}
|
||||
}
|
||||
if (y < CHUCK_SIZE - 1) {
|
||||
if (y < CHUCK_SIZE - 1 && chuck_blocks[Chuck::get_index(x, y + 1, z)]) {
|
||||
if (y < CHUCK_SIZE - 1 && chunk_blocks[Chunk::get_index(x, y + 1, z)]) {
|
||||
m_block_render_data.draw_face[4] = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (x == 0 && world_x - 1 > 0) {
|
||||
int adjacent_chunk_x = (world_x - 1) / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = world_z / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[3] = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (x == CHUCK_SIZE - 1) {
|
||||
int adjacent_chunk_x = (world_x + 1) / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = world_z / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x + 1 - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[1] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == 0 && world_z - 1 > 0) {
|
||||
int adjacent_chunk_x = world_x / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = (world_z - 1) / CHUCK_SIZE;
|
||||
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z - 1 - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[2] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (z == CHUCK_SIZE - 1) {
|
||||
int adjacent_chunk_x = world_x / CHUCK_SIZE;
|
||||
int adjacet_chunk_z = (world_z + 1) / CHUCK_SIZE;
|
||||
auto adjacent = m_chunks.find(ChunkPos{adjacent_chunk_x, adjacet_chunk_z});
|
||||
if (adjacent != m_chunks.end()) {
|
||||
int adjacent_x, adjacent_z;
|
||||
const auto& adjacent_chunk_blocks = it->second.get_chunk_blocks();
|
||||
int x, y, z;
|
||||
y = world_y;
|
||||
x = world_x - adjacent_chunk_x * CHUCK_SIZE;
|
||||
z = world_z + 1 - adjacet_chunk_z * CHUCK_SIZE;
|
||||
if (adjacent_chunk_blocks[Chunk::get_index(x, y, z)]) {
|
||||
m_block_render_data.draw_face[0] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m_block_render_data;
|
||||
}
|
||||
|
||||
|
||||
void World::init_world() {
|
||||
for (int s = 0; s < DISTANCE; s++) {
|
||||
for (int t = 0; t < DISTANCE; t++) {
|
||||
ChuckPos pos{s, t};
|
||||
Chuck chuck;
|
||||
m_chucks[pos] = chuck;
|
||||
ChunkPos pos{s, t};
|
||||
LOG::info("Chunk Pos in init_world X: {} Z: {}", pos.x, pos.z);
|
||||
m_chunks.emplace(pos, Chunk(*this, pos));
|
||||
}
|
||||
}
|
||||
|
||||
LOG::info("World init finfish");
|
||||
|
||||
for (auto& chunk_map : m_chunks) {
|
||||
auto& [chunk_pos, chunk] = chunk_map;
|
||||
chunk.init_chunk();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void World::render() {
|
||||
|
||||
for (const auto& chunk_map : m_chunks) {
|
||||
const auto& [pos, chunk] = chunk_map;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, chunk.get_vbo());
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, s));
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, layer));
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glEnableVertexAttribArray(2);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, chunk.get_vertex_data().size() * 3);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
//LOG::info("Chunk {} {} render finished", pos.x, pos.z);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user