optimizing chunk mesh

This commit is contained in:
Kbz-8
2025-05-20 16:15:23 +02:00
parent 05970359e2
commit 0dce511cd3
9 changed files with 199 additions and 125 deletions

View File

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

View File

@@ -2,26 +2,35 @@
#include <Block.h> #include <Block.h>
#include <World.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_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_data(CHUNK_VOLUME, 0), 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() void Chunk::GenerateChunk()
{ {
if(p_actor) if(p_actor)
return; 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 x = 0; x < CHUNK_SIZE.x; x++)
{ {
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++) for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++)
{ {
// Implement noise here // 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++) 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_data.clear();
m_mesh_index_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 x = 0; x < CHUNK_SIZE.x; x++)
{ {
for(std::uint32_t z = 0; z < CHUNK_SIZE.z; z++) 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))) if(!GetBlock(Scop::Vec3i(x, y, z)))
continue; 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))) 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[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[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)); offset += 4;
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));
} }
if(!GetBlock(Scop::Vec3i(x, y + 1, z))) 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_index_data.push_back(offset + 1);
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_index_data.push_back(offset);
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 + 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[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[8].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[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].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[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;
} }
for(std::uint32_t i = 0; i < m_mesh_data.size(); i++) if(!GetBlock(Scop::Vec3i(x, static_cast<std::int32_t>(y) - 1, z)))
m_mesh_index_data.push_back(i); {
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;
}
}
}
}
} }
void Chunk::UploadMesh() void Chunk::UploadMesh()
@@ -131,10 +176,7 @@ void Chunk::UploadMesh()
std::uint32_t Chunk::GetBlock(Scop::Vec3i position) const noexcept 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) if(position.x < m_data.size() && position.z < m_data[position.x].size() && position.y < m_data[position.x][position.z].size())
return 1; return m_data[position.x][position.z][position.y];
std::uint32_t index = CHUNK_POS_TO_INDEX(position.x, position.y, position.z);
if(index < m_data.size())
return m_data[index];
return 0; return 0;
} }

View File

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

View File

@@ -14,11 +14,24 @@ 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) 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()); 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 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); 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 }; Scop::Vec2i current_chunk_position{ x_chunk, z_chunk };
if(input.IsKeyPressed(SDL_SCANCODE_G))
debounce = true;
else if(debounce)
{
generate = !generate;
debounce = false;
}
if(generate)
{
if(m_generation_status != GenerationState::Ready || current_chunk_position != m_previous_chunk_position) if(m_generation_status != GenerationState::Ready || current_chunk_position != m_previous_chunk_position)
{ {
if(m_generation_status == GenerationState::Ready) if(m_generation_status == GenerationState::Ready)
@@ -33,8 +46,8 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_previous_chunk_position(-10
m_previous_chunk_position = current_chunk_position; m_previous_chunk_position = current_chunk_position;
} }
} }
if(m_generation_status != GenerationState::Working)
Upload(); Upload();
}
}; };
m_scene.CreateNarrator().AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{})); 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();) for(auto it = m_chunks.begin(); it != m_chunks.end();)
{ {
Scop::Vec3i pos = it->first; Scop::Vec3i pos = it->first;
float x_dist = std::abs(pos.x - current_chunk_position.x); std::uint32_t x_dist = std::abs(pos.x - current_chunk_position.x);
float z_dist = std::abs(pos.z - current_chunk_position.y); std::uint32_t z_dist = std::abs(pos.z - current_chunk_position.y);
if(RENDER_DISTANCE < x_dist || RENDER_DISTANCE < z_dist) if(RENDER_DISTANCE < x_dist || RENDER_DISTANCE < z_dist)
{ {
if(it->second.GetActor()) if(it->second.GetActor())
@@ -76,12 +89,15 @@ void World::GenerateWorld(Scop::Vec2i current_chunk_position)
auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z }); auto res = m_chunks.try_emplace(Scop::Vec2i{ x, z }, *this, Scop::Vec2i{ x, z });
if(res.second) if(res.second)
{ {
res.first->second.GenerateChunk();
if(!res.first->second.GetActor()) if(!res.first->second.GetActor())
{
res.first->second.GenerateChunk();
res.first->second.GenerateMesh();
m_chunks_to_upload.Push(res.first->second); m_chunks_to_upload.Push(res.first->second);
} }
} }
} }
}
m_generation_status = GenerationState::Finished; m_generation_status = GenerationState::Finished;
} }
@@ -93,7 +109,6 @@ void World::Upload()
for(std::size_t i = 0; i < CHUNKS_UPLOAD_PER_FRAME && !m_chunks_to_upload.IsEmpty(); i++) for(std::size_t i = 0; i < CHUNKS_UPLOAD_PER_FRAME && !m_chunks_to_upload.IsEmpty(); i++)
{ {
auto chunk = m_chunks_to_upload.Pop(); auto chunk = m_chunks_to_upload.Pop();
chunk.get().GenerateMesh();
chunk.get().UploadMesh(); chunk.get().UploadMesh();
} }
Scop::RenderCore::Get().WaitQueueIdle(KVF_GRAPHICS_QUEUE); Scop::RenderCore::Get().WaitQueueIdle(KVF_GRAPHICS_QUEUE);

View File

@@ -9,7 +9,7 @@
#include <Chunk.h> #include <Chunk.h>
#include <Utils.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; constexpr std::uint8_t CHUNKS_UPLOAD_PER_FRAME = 1;
enum class GenerationState: std::uint8_t enum class GenerationState: std::uint8_t

View File

@@ -14,7 +14,7 @@ int main(int ac, char** av)
Scop::SceneDescriptor main_scene_desc; Scop::SceneDescriptor main_scene_desc;
main_scene_desc.fragment_shader = Scop::RenderCore::Get().GetDefaultFragmentShader(); 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.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::Scene& main_scene = splash_scene->AddChildScene("main", std::move(main_scene_desc));
Scop::Vec2ui32 skybox_size; Scop::Vec2ui32 skybox_size;

View File

@@ -16,6 +16,10 @@ namespace Scop
[[nodiscard]] inline constexpr std::string GetCameraType() override { return "FirstPerson3D"; } [[nodiscard]] inline constexpr std::string GetCameraType() override { return "FirstPerson3D"; }
[[nodiscard]] const Vec3f& GetPosition() const noexcept override { return m_position; } [[nodiscard]] const Vec3f& GetPosition() const noexcept override { return m_position; }
[[nodiscard]] const Vec3f& GetUp() const noexcept { return m_up; }
[[nodiscard]] const Vec3f& GetLeft() const noexcept { return m_left; }
[[nodiscard]] const Vec3f& GetTarget() const noexcept { return m_target; }
[[nodiscard]] const Vec3f& GetDirection() const noexcept { return m_direction; }
~FirstPerson3D() = default; ~FirstPerson3D() = default;

View File

@@ -27,6 +27,8 @@ namespace Scop
T r21, T r22, T r23, T r24, T r21, T r22, T r23, T r24,
T r31, T r32, T r33, T r34, T r31, T r32, T r33, T r34,
T r41, T r42, T r43, T r44); T r41, T r42, T r43, T r44);
constexpr Mat4(const T& col_1, const T& col_2, const T& col_3, const T& col_4);
constexpr Mat4(const Vec4<T>& col_1, const Vec4<T>& col_2, const Vec4<T>& col_3, const Vec4<T>& col_4);
constexpr Mat4(const T matrix[16]); constexpr Mat4(const T matrix[16]);
constexpr Mat4(const Mat4&) = default; constexpr Mat4(const Mat4&) = default;
constexpr Mat4(Mat4&&) = default; constexpr Mat4(Mat4&&) = default;

View File

@@ -33,6 +33,22 @@ namespace Scop
matrix[12], matrix[13], matrix[14], matrix[15]) matrix[12], matrix[13], matrix[14], matrix[15])
{} {}
template<typename T>
constexpr Mat4<T>::Mat4(const T& col_1, const T& col_2, const T& col_3, const T& col_4) :
m11(col_1), m12(col_2), m13(col_3), m14(col_4),
m21(col_1), m22(col_2), m23(col_3), m24(col_4),
m31(col_1), m32(col_2), m33(col_3), m34(col_4),
m41(col_1), m42(col_2), m43(col_3), m44(col_4)
{}
template<typename T>
constexpr Mat4<T>::Mat4(const Vec4<T>& col_1, const Vec4<T>& col_2, const Vec4<T>& col_3, const Vec4<T>& col_4) :
m11(col_1.x), m12(col_2.x), m13(col_3.x), m14(col_4.x),
m21(col_1.y), m22(col_2.y), m23(col_3.y), m24(col_4.y),
m31(col_1.z), m32(col_2.z), m33(col_3.z), m34(col_4.z),
m41(col_1.w), m42(col_2.w), m43(col_3.w), m44(col_4.w)
{}
template<typename T> template<typename T>
constexpr Mat4<T>& Mat4<T>::ApplyRotation(const Quat<T>& rotation) constexpr Mat4<T>& Mat4<T>::ApplyRotation(const Quat<T>& rotation)
{ {