optimizing chunk mesh

This commit is contained in:
2025-05-20 16:15:23 +02:00
parent 84e5e437d0
commit 6fea3147ee
9 changed files with 199 additions and 125 deletions

View File

@@ -6,42 +6,36 @@
#include <ScopGraphics.h>
static const std::vector<Scop::Vertex> BLOCK_MESH = {
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 1 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 0 } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0, 1 } }
//Front face
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } },
//Back face
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 0.0f, -1.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } },
//Top face
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } },
//Bottom face
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 0.0f, -1.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } },
//Right face
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 1.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ 1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } },
//Left face
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 0.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 0.0f, 1.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 0.0f, 1.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 0.0f } },
Scop::Vertex{ Scop::Vec4f{ 0.0f, 1.0f, 1.0f, 1.0f }, Scop::Vec4f{ -1.0f, 0.0f, 0.0f, 1.0f }, Scop::Vec2f{ 1.0f, 1.0f } }
};
#endif

View File

@@ -2,26 +2,35 @@
#include <Block.h>
#include <World.h>
#define CHUNK_POS_TO_INDEX(px, py, pz) (px * CHUNK_SIZE.x * CHUNK_SIZE.z + pz * CHUNK_SIZE.z + py)
Chunk::Chunk(World& world, Scop::Vec2i offset) : m_data(CHUNK_VOLUME, 0), m_offset(offset), m_position(std::move(offset) * Scop::Vec2i{ CHUNK_SIZE.x, CHUNK_SIZE.z }), m_world(world)
Chunk::Chunk(World& world, Scop::Vec2i offset) : m_offset(offset), m_position(std::move(offset) * Scop::Vec2i{ CHUNK_SIZE.x, CHUNK_SIZE.z }), m_world(world)
{
m_data.resize(CHUNK_SIZE.x);
for(auto& z: m_data)
{
z.resize(CHUNK_SIZE.z);
for(auto& y: z)
y.resize(CHUNK_SIZE.y);
}
}
void Chunk::GenerateChunk()
{
if(p_actor)
return;
std::memset(m_data.data(), 0, m_data.size() * sizeof(std::uint32_t));
for(auto& z: m_data)
{
for(auto& y: z)
std::fill(y.begin(), y.end(), 0);
}
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
{
// Implement noise here
std::uint32_t height = std::min(static_cast<std::uint32_t>(4 + (std::sin(x) + std::cos(z)) * 2), CHUNK_SIZE.y);
std::uint32_t height = std::min(static_cast<std::uint32_t>(4 + (std::sin(m_position.x) * std::sin(m_position.y)) * 10 + x), CHUNK_SIZE.y);
for(std::uint32_t y = 0; y < height; y++)
m_data[CHUNK_POS_TO_INDEX(x, y, z)] = 1;
m_data[x][z][y] = 1;
}
}
}
@@ -34,6 +43,8 @@ void Chunk::GenerateMesh()
m_mesh_data.clear();
m_mesh_index_data.clear();
std::size_t offset = 0;
for(std::uint32_t x = 0; x < CHUNK_SIZE.x; x++)
{
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
@@ -42,77 +53,111 @@ void Chunk::GenerateMesh()
{
if(!GetBlock(Scop::Vec3i(x, y, z)))
continue;
if(!GetBlock(Scop::Vec3i(x, y, static_cast<std::int32_t>(z) - 1)))
{
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[0].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[0].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[1].position + Scop::Vec3f(x, y, z), BLOCK_MESH[1].normal, BLOCK_MESH[1].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[2].position + Scop::Vec3f(x, y, z), BLOCK_MESH[2].normal, BLOCK_MESH[2].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[3].position + Scop::Vec3f(x, y, z), BLOCK_MESH[3].normal, BLOCK_MESH[3].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[4].position + Scop::Vec3f(x, y, z), BLOCK_MESH[4].normal, BLOCK_MESH[4].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[5].position + Scop::Vec3f(x, y, z), BLOCK_MESH[5].normal, BLOCK_MESH[5].uv));
}
if(!GetBlock(Scop::Vec3i(x, y, z + 1)))
{
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset + 1);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[0].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[0].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[1].position + Scop::Vec3f(x, y, z), BLOCK_MESH[1].normal, BLOCK_MESH[1].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[2].position + Scop::Vec3f(x, y, z), BLOCK_MESH[2].normal, BLOCK_MESH[2].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[3].position + Scop::Vec3f(x, y, z), BLOCK_MESH[3].normal, BLOCK_MESH[3].uv));
offset += 4;
}
if(!GetBlock(Scop::Vec3i(x, y, static_cast<std::int32_t>(z) - 1)))
{
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[4].position + Scop::Vec3f(x, y, z), BLOCK_MESH[4].normal, BLOCK_MESH[4].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[5].position + Scop::Vec3f(x, y, z), BLOCK_MESH[5].normal, BLOCK_MESH[5].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[6].position + Scop::Vec3f(x, y, z), BLOCK_MESH[6].normal, BLOCK_MESH[6].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[7].position + Scop::Vec3f(x, y, z), BLOCK_MESH[7].normal, BLOCK_MESH[7].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[8].normal, BLOCK_MESH[8].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[10].position + Scop::Vec3f(x, y, z), BLOCK_MESH[10].normal, BLOCK_MESH[10].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[11].position + Scop::Vec3f(x, y, z), BLOCK_MESH[11].normal, BLOCK_MESH[11].uv));
}
if(!GetBlock(Scop::Vec3i(static_cast<std::int32_t>(x) - 1, y, z)))
{
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[12].position + Scop::Vec3f(x, y, z), BLOCK_MESH[12].normal, BLOCK_MESH[12].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[13].position + Scop::Vec3f(x, y, z), BLOCK_MESH[13].normal, BLOCK_MESH[13].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[14].position + Scop::Vec3f(x, y, z), BLOCK_MESH[14].normal, BLOCK_MESH[14].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[15].position + Scop::Vec3f(x, y, z), BLOCK_MESH[15].normal, BLOCK_MESH[15].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[16].position + Scop::Vec3f(x, y, z), BLOCK_MESH[16].normal, BLOCK_MESH[16].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[17].position + Scop::Vec3f(x, y, z), BLOCK_MESH[17].normal, BLOCK_MESH[17].uv));
}
if(!GetBlock(Scop::Vec3i(x + 1, y, z)))
{
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[18].position + Scop::Vec3f(x, y, z), BLOCK_MESH[18].normal, BLOCK_MESH[18].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[19].position + Scop::Vec3f(x, y, z), BLOCK_MESH[19].normal, BLOCK_MESH[19].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[20].position + Scop::Vec3f(x, y, z), BLOCK_MESH[20].normal, BLOCK_MESH[20].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[21].position + Scop::Vec3f(x, y, z), BLOCK_MESH[21].normal, BLOCK_MESH[21].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[22].position + Scop::Vec3f(x, y, z), BLOCK_MESH[22].normal, BLOCK_MESH[22].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[23].position + Scop::Vec3f(x, y, z), BLOCK_MESH[23].normal, BLOCK_MESH[23].uv));
}
if(!GetBlock(Scop::Vec3i(x, static_cast<std::int32_t>(y) - 1, z)))
{
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[24].position + Scop::Vec3f(x, y, z), BLOCK_MESH[24].normal, BLOCK_MESH[24].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[25].position + Scop::Vec3f(x, y, z), BLOCK_MESH[25].normal, BLOCK_MESH[25].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[26].position + Scop::Vec3f(x, y, z), BLOCK_MESH[26].normal, BLOCK_MESH[26].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[27].position + Scop::Vec3f(x, y, z), BLOCK_MESH[27].normal, BLOCK_MESH[27].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[28].position + Scop::Vec3f(x, y, z), BLOCK_MESH[28].normal, BLOCK_MESH[28].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[29].position + Scop::Vec3f(x, y, z), BLOCK_MESH[29].normal, BLOCK_MESH[29].uv));
offset += 4;
}
if(!GetBlock(Scop::Vec3i(x, y + 1, z)))
{
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[30].position + Scop::Vec3f(x, y, z), BLOCK_MESH[30].normal, BLOCK_MESH[30].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[31].position + Scop::Vec3f(x, y, z), BLOCK_MESH[31].normal, BLOCK_MESH[31].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[32].position + Scop::Vec3f(x, y, z), BLOCK_MESH[32].normal, BLOCK_MESH[32].uv));
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 3);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[33].position + Scop::Vec3f(x, y, z), BLOCK_MESH[33].normal, BLOCK_MESH[33].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[34].position + Scop::Vec3f(x, y, z), BLOCK_MESH[34].normal, BLOCK_MESH[34].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[35].position + Scop::Vec3f(x, y, z), BLOCK_MESH[35].normal, BLOCK_MESH[35].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[8].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[10].position + Scop::Vec3f(x, y, z), BLOCK_MESH[10].normal, BLOCK_MESH[10].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[11].position + Scop::Vec3f(x, y, z), BLOCK_MESH[11].normal, BLOCK_MESH[11].uv));
offset += 4;
}
if(!GetBlock(Scop::Vec3i(x, static_cast<std::int32_t>(y) - 1, z)))
{
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 2);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[12].position + Scop::Vec3f(x, y, z), BLOCK_MESH[12].normal, BLOCK_MESH[12].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[13].position + Scop::Vec3f(x, y, z), BLOCK_MESH[13].normal, BLOCK_MESH[13].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[14].position + Scop::Vec3f(x, y, z), BLOCK_MESH[14].normal, BLOCK_MESH[14].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[15].position + Scop::Vec3f(x, y, z), BLOCK_MESH[15].normal, BLOCK_MESH[15].uv));
offset += 4;
}
if(!GetBlock(Scop::Vec3i(x + 1, y, z)))
{
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 1);
m_mesh_index_data.push_back(offset);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[16].position + Scop::Vec3f(x, y, z), BLOCK_MESH[16].normal, BLOCK_MESH[16].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[17].position + Scop::Vec3f(x, y, z), BLOCK_MESH[17].normal, BLOCK_MESH[17].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[18].position + Scop::Vec3f(x, y, z), BLOCK_MESH[18].normal, BLOCK_MESH[18].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[19].position + Scop::Vec3f(x, y, z), BLOCK_MESH[19].normal, BLOCK_MESH[19].uv));
offset += 4;
}
if(!GetBlock(Scop::Vec3i(static_cast<std::int32_t>(x) - 1, y, z)))
{
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 2);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset);
m_mesh_index_data.push_back(offset + 3);
m_mesh_index_data.push_back(offset + 1);
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[20].position + Scop::Vec3f(x, y, z), BLOCK_MESH[20].normal, BLOCK_MESH[20].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[21].position + Scop::Vec3f(x, y, z), BLOCK_MESH[21].normal, BLOCK_MESH[21].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[22].position + Scop::Vec3f(x, y, z), BLOCK_MESH[22].normal, BLOCK_MESH[22].uv));
m_mesh_data.push_back(Scop::Vertex(BLOCK_MESH[23].position + Scop::Vec3f(x, y, z), BLOCK_MESH[23].normal, BLOCK_MESH[23].uv));
offset += 4;
}
}
}
}
for(std::uint32_t i = 0; i < m_mesh_data.size(); i++)
m_mesh_index_data.push_back(i);
}
void Chunk::UploadMesh()
@@ -131,10 +176,7 @@ void Chunk::UploadMesh()
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept
{
if(position.x < 0 || position.x >= CHUNK_SIZE.x || position.z < 0 || position.z >= CHUNK_SIZE.z || position.y < 0)
return 1;
std::uint32_t index = CHUNK_POS_TO_INDEX(position.x, position.y, position.z);
if(index < m_data.size())
return m_data[index];
if(position.x < m_data.size() && position.z < m_data[position.x].size() && position.y < m_data[position.x][position.z].size())
return m_data[position.x][position.z][position.y];
return 0;
}

View File

@@ -6,8 +6,8 @@
#include <ScopGraphics.h>
#include <ScopMaths.h>
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 32, 32 };
constexpr std::uint32_t CHUNK_VOLUME = CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z;
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 32 };
constexpr Scop::Vec3ui CHUNK_SIZE_HALF = CHUNK_SIZE / 2 ;
class Chunk
{
@@ -19,13 +19,14 @@ class Chunk
void UploadMesh();
[[nodiscard]] std::uint32_t GetBlock(Scop::Vec3i position) const noexcept;
[[nodiscard]] inline Scop::NonOwningPtr<Scop::Actor> GetActor() const noexcept { return p_actor; }
[[nodiscard]] inline Scop::Vec2i GetPosition() const noexcept { return m_position; }
~Chunk() = default;
private:
std::vector<Scop::Vertex> m_mesh_data;
std::vector<std::uint32_t> m_mesh_index_data;
std::vector<std::uint32_t> m_data;
std::vector<std::vector<std::vector<std::uint32_t>>> m_data;
Scop::Vec2i m_offset; // In chunks
Scop::Vec2i m_position; // In blocks
class World& m_world;

View File

@@ -14,27 +14,40 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_previous_chunk_position(-10
auto narrator_update = [this](Scop::NonOwningPtr<Scop::Scene> scene, Scop::Inputs& input, float delta)
{
static bool generate = true;
static bool debounce = false;
Scop::FirstPerson3D* camera = reinterpret_cast<Scop::FirstPerson3D*>(m_scene.GetCamera().get());
std::int32_t x_chunk = static_cast<std::int32_t>(camera->GetPosition().x) / static_cast<std::int32_t>(CHUNK_SIZE.x);
std::int32_t z_chunk = static_cast<std::int32_t>(camera->GetPosition().z) / static_cast<std::int32_t>(CHUNK_SIZE.z);
Scop::Vec2i current_chunk_position{ x_chunk, z_chunk };
if(m_generation_status != GenerationState::Ready || current_chunk_position != m_previous_chunk_position)
if(input.IsKeyPressed(SDL_SCANCODE_G))
debounce = true;
else if(debounce)
{
if(m_generation_status == GenerationState::Ready)
{
UnloadChunks(current_chunk_position);
auto _ = std::async(std::launch::async, &World::GenerateWorld, this, current_chunk_position);
m_generation_status = GenerationState::Working;
}
else if(m_generation_status == GenerationState::Finished)
{
m_generation_status = GenerationState::Ready;
m_previous_chunk_position = current_chunk_position;
}
generate = !generate;
debounce = false;
}
if(m_generation_status != GenerationState::Working)
if(generate)
{
if(m_generation_status != GenerationState::Ready || current_chunk_position != m_previous_chunk_position)
{
if(m_generation_status == GenerationState::Ready)
{
UnloadChunks(current_chunk_position);
auto _ = std::async(std::launch::async, &World::GenerateWorld, this, current_chunk_position);
m_generation_status = GenerationState::Working;
}
else if(m_generation_status == GenerationState::Finished)
{
m_generation_status = GenerationState::Ready;
m_previous_chunk_position = current_chunk_position;
}
}
Upload();
}
};
m_scene.CreateNarrator().AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
@@ -53,8 +66,8 @@ void World::UnloadChunks(Scop::Vec2i current_chunk_position)
for(auto it = m_chunks.begin(); it != m_chunks.end();)
{
Scop::Vec3i pos = it->first;
float x_dist = std::abs(pos.x - current_chunk_position.x);
float z_dist = std::abs(pos.z - current_chunk_position.y);
std::uint32_t x_dist = std::abs(pos.x - current_chunk_position.x);
std::uint32_t z_dist = std::abs(pos.z - current_chunk_position.y);
if(RENDER_DISTANCE < x_dist || RENDER_DISTANCE < z_dist)
{
if(it->second.GetActor())
@@ -76,9 +89,12 @@ void World::GenerateWorld(Scop::Vec2i current_chunk_position)
auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z });
if(res.second)
{
res.first->second.GenerateChunk();
if(!res.first->second.GetActor())
{
res.first->second.GenerateChunk();
res.first->second.GenerateMesh();
m_chunks_to_upload.Push(res.first->second);
}
}
}
}
@@ -93,7 +109,6 @@ void World::Upload()
for(std::size_t i = 0; i < CHUNKS_UPLOAD_PER_FRAME && !m_chunks_to_upload.IsEmpty(); i++)
{
auto chunk = m_chunks_to_upload.Pop();
chunk.get().GenerateMesh();
chunk.get().UploadMesh();
}
Scop::RenderCore::Get().WaitQueueIdle(KVF_GRAPHICS_QUEUE);

View File

@@ -9,7 +9,7 @@
#include <Chunk.h>
#include <Utils.h>
constexpr std::uint8_t RENDER_DISTANCE = 10;
constexpr std::uint8_t RENDER_DISTANCE = 5;
constexpr std::uint8_t CHUNKS_UPLOAD_PER_FRAME = 1;
enum class GenerationState: std::uint8_t

View File

@@ -14,7 +14,7 @@ int main(int ac, char** av)
Scop::SceneDescriptor main_scene_desc;
main_scene_desc.fragment_shader = Scop::RenderCore::Get().GetDefaultFragmentShader();
main_scene_desc.camera = std::make_shared<Scop::FirstPerson3D>(Scop::Vec3f{ 0.0f, 20.0f, 0.0f }, 80.f);
main_scene_desc.culling = Scop::CullMode::None;
main_scene_desc.culling = Scop::CullMode::Front;
Scop::Scene& main_scene = splash_scene->AddChildScene("main", std::move(main_scene_desc));
Scop::Vec2ui32 skybox_size;