mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
working on worldgen
This commit is contained in:
@@ -4,26 +4,17 @@
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
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)
|
#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)
|
||||||
{
|
{
|
||||||
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;
|
||||||
for(auto& z: m_data)
|
std::memset(m_data.data(), 0, m_data.size() * sizeof(std::uint32_t));
|
||||||
{
|
|
||||||
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++)
|
||||||
{
|
{
|
||||||
@@ -32,7 +23,7 @@ void Chunk::GenerateChunk()
|
|||||||
// Implement noise here
|
// Implement noise here
|
||||||
std::uint32_t height = 4 + std::sin(x) + std::cos(z);
|
std::uint32_t height = 4 + std::sin(x) + std::cos(z);
|
||||||
for(std::uint32_t y = 0; y < height; y++)
|
for(std::uint32_t y = 0; y < height; y++)
|
||||||
m_data[x][z][y] = 1;
|
m_data[CHUNK_POS_TO_INDEX(x, y, z)] = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,10 +33,9 @@ void Chunk::GenerateMesh()
|
|||||||
if(p_actor)
|
if(p_actor)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::vector<Scop::Vertex> mesh_data(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
|
m_mesh_data.clear();
|
||||||
std::vector<std::uint32_t> indices(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
|
m_mesh_index_data.clear();
|
||||||
|
|
||||||
std::size_t index = 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++)
|
||||||
@@ -56,95 +46,97 @@ void Chunk::GenerateMesh()
|
|||||||
continue;
|
continue;
|
||||||
if(!GetBlock(Scop::Vec3i(x, y, static_cast<std::int32_t>(z) - 1)))
|
if(!GetBlock(Scop::Vec3i(x, y, static_cast<std::int32_t>(z) - 1)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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[0].position + Scop::Vec3f(x, y, z), BLOCK_MESH[0].normal, BLOCK_MESH[0].uv));
|
||||||
mesh_data[index++] = 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[1].position + Scop::Vec3f(x, y, z), BLOCK_MESH[1].normal, BLOCK_MESH[1].uv));
|
||||||
mesh_data[index++] = 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[2].position + Scop::Vec3f(x, y, z), BLOCK_MESH[2].normal, BLOCK_MESH[2].uv));
|
||||||
|
|
||||||
mesh_data[index++] = 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[3].position + Scop::Vec3f(x, y, z), BLOCK_MESH[3].normal, BLOCK_MESH[3].uv));
|
||||||
mesh_data[index++] = 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[4].position + Scop::Vec3f(x, y, z), BLOCK_MESH[4].normal, BLOCK_MESH[4].uv));
|
||||||
mesh_data[index++] = 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[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)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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));
|
||||||
mesh_data[index++] = 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));
|
||||||
mesh_data[index++] = 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[8].position + Scop::Vec3f(x, y, z), BLOCK_MESH[8].normal, BLOCK_MESH[8].uv));
|
||||||
|
|
||||||
mesh_data[index++] = 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[9].position + Scop::Vec3f(x, y, z), BLOCK_MESH[9].normal, BLOCK_MESH[9].uv));
|
||||||
mesh_data[index++] = 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[10].position + Scop::Vec3f(x, y, z), BLOCK_MESH[10].normal, BLOCK_MESH[10].uv));
|
||||||
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[11].position + Scop::Vec3f(x, y, z), BLOCK_MESH[11].normal, BLOCK_MESH[11].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)))
|
if(!GetBlock(Scop::Vec3i(static_cast<std::int32_t>(x) - 1, y, z)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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[12].position + Scop::Vec3f(x, y, z), BLOCK_MESH[12].normal, BLOCK_MESH[12].uv));
|
||||||
mesh_data[index++] = 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[13].position + Scop::Vec3f(x, y, z), BLOCK_MESH[13].normal, BLOCK_MESH[13].uv));
|
||||||
mesh_data[index++] = 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[14].position + Scop::Vec3f(x, y, z), BLOCK_MESH[14].normal, BLOCK_MESH[14].uv));
|
||||||
|
|
||||||
mesh_data[index++] = 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[15].position + Scop::Vec3f(x, y, z), BLOCK_MESH[15].normal, BLOCK_MESH[15].uv));
|
||||||
mesh_data[index++] = 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[16].position + Scop::Vec3f(x, y, z), BLOCK_MESH[16].normal, BLOCK_MESH[16].uv));
|
||||||
mesh_data[index++] = 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[17].position + Scop::Vec3f(x, y, z), BLOCK_MESH[17].normal, BLOCK_MESH[17].uv));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!GetBlock(Scop::Vec3i(x + 1, y, z)))
|
if(!GetBlock(Scop::Vec3i(x + 1, y, z)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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[18].position + Scop::Vec3f(x, y, z), BLOCK_MESH[18].normal, BLOCK_MESH[18].uv));
|
||||||
mesh_data[index++] = 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[19].position + Scop::Vec3f(x, y, z), BLOCK_MESH[19].normal, BLOCK_MESH[19].uv));
|
||||||
mesh_data[index++] = 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[20].position + Scop::Vec3f(x, y, z), BLOCK_MESH[20].normal, BLOCK_MESH[20].uv));
|
||||||
|
|
||||||
mesh_data[index++] = 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[21].position + Scop::Vec3f(x, y, z), BLOCK_MESH[21].normal, BLOCK_MESH[21].uv));
|
||||||
mesh_data[index++] = 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[22].position + Scop::Vec3f(x, y, z), BLOCK_MESH[22].normal, BLOCK_MESH[22].uv));
|
||||||
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[23].position + Scop::Vec3f(x, y, z), BLOCK_MESH[23].normal, BLOCK_MESH[23].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)))
|
if(!GetBlock(Scop::Vec3i(x, static_cast<std::int32_t>(y) - 1, z)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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[24].position + Scop::Vec3f(x, y, z), BLOCK_MESH[24].normal, BLOCK_MESH[24].uv));
|
||||||
mesh_data[index++] = 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[25].position + Scop::Vec3f(x, y, z), BLOCK_MESH[25].normal, BLOCK_MESH[25].uv));
|
||||||
mesh_data[index++] = 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[26].position + Scop::Vec3f(x, y, z), BLOCK_MESH[26].normal, BLOCK_MESH[26].uv));
|
||||||
|
|
||||||
mesh_data[index++] = 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[27].position + Scop::Vec3f(x, y, z), BLOCK_MESH[27].normal, BLOCK_MESH[27].uv));
|
||||||
mesh_data[index++] = 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[28].position + Scop::Vec3f(x, y, z), BLOCK_MESH[28].normal, BLOCK_MESH[28].uv));
|
||||||
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[29].position + Scop::Vec3f(x, y, z), BLOCK_MESH[29].normal, BLOCK_MESH[29].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)))
|
||||||
{
|
{
|
||||||
mesh_data[index++] = 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[30].position + Scop::Vec3f(x, y, z), BLOCK_MESH[30].normal, BLOCK_MESH[30].uv));
|
||||||
mesh_data[index++] = 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[31].position + Scop::Vec3f(x, y, z), BLOCK_MESH[31].normal, BLOCK_MESH[31].uv));
|
||||||
mesh_data[index++] = Scop::Vertex{ BLOCK_MESH[32].position + Scop::Vec3f(x, y, z), BLOCK_MESH[32].normal, BLOCK_MESH[32].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));
|
||||||
|
|
||||||
mesh_data[index++] = 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[33].position + Scop::Vec3f(x, y, z), BLOCK_MESH[33].normal, BLOCK_MESH[33].uv));
|
||||||
mesh_data[index++] = 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[34].position + Scop::Vec3f(x, y, z), BLOCK_MESH[34].normal, BLOCK_MESH[34].uv));
|
||||||
mesh_data[index++] = 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[35].position + Scop::Vec3f(x, y, z), BLOCK_MESH[35].normal, BLOCK_MESH[35].uv));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for(std::uint32_t i = 0; i < index; i++)
|
for(std::uint32_t i = 0; i < m_mesh_data.size(); i++)
|
||||||
indices[i] = i;
|
m_mesh_index_data.push_back(i);
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << index << std::endl;
|
void Chunk::UploadMesh()
|
||||||
|
{
|
||||||
|
if(p_actor)
|
||||||
|
return;
|
||||||
|
std::shared_ptr<Scop::Mesh> mesh = std::make_shared<Scop::Mesh>();
|
||||||
|
mesh->AddSubMesh({ std::move(m_mesh_data), std::move(m_mesh_index_data) });
|
||||||
|
|
||||||
p_actor = m_world.GetMeshPool().RequestActor(m_world.GetScene());
|
Scop::Actor& actor = m_world.GetScene().CreateActor(mesh);
|
||||||
p_actor->GetModelRef().GetMesh()->GetSubMesh(0).SetData(mesh_data, indices, index);
|
//actor.GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
|
||||||
p_actor->GetModelRef().SetMaterial(m_world.GetBlockMaterial(), 0);
|
actor.SetScale(Scop::Vec3f{ 2.0f });
|
||||||
p_actor->SetScale(Scop::Vec3f{ 2.0f });
|
actor.SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
|
||||||
p_actor->SetPosition(Scop::Vec3f(m_position.x, 0.0f, m_position.y));
|
p_actor = &actor;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 >= m_data.size() || position.z < 0 || position.z >= m_data[position.x >= m_data.size() ? m_data.size() - 1 : position.x].size() || position.y < 0)
|
if(position.x < 0 || position.x >= CHUNK_SIZE.x || position.z < 0 || position.z >= CHUNK_SIZE.z || position.y < 0)
|
||||||
return 1;
|
return 1;
|
||||||
if(position.x < m_data.size() && position.z < m_data[position.x].size() && position.y < m_data[position.x][position.z].size())
|
std::uint32_t index = CHUNK_POS_TO_INDEX(position.x, position.y, position.z);
|
||||||
return m_data[position.x][position.z][position.y];
|
if(index < m_data.size())
|
||||||
|
return m_data[index];
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::~Chunk()
|
|
||||||
{
|
|
||||||
m_world.GetMeshPool().ReturnToPool(p_actor);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,10 +2,13 @@
|
|||||||
#define CHUNK_H
|
#define CHUNK_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#include <ScopGraphics.h>
|
#include <ScopGraphics.h>
|
||||||
#include <ScopMaths.h>
|
#include <ScopMaths.h>
|
||||||
|
|
||||||
constexpr Scop::Vec3ui CHUNK_SIZE = Scop::Vec3ui{ 32, 256, 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;
|
||||||
|
|
||||||
class Chunk
|
class Chunk
|
||||||
{
|
{
|
||||||
@@ -14,13 +17,16 @@ class Chunk
|
|||||||
|
|
||||||
void GenerateChunk();
|
void GenerateChunk();
|
||||||
void GenerateMesh();
|
void GenerateMesh();
|
||||||
|
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; }
|
||||||
|
|
||||||
~Chunk();
|
~Chunk() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::vector<std::vector<std::uint32_t>>> m_data; // Should be a flat array but I cannot manage to flatten the 3D coordinates correctly
|
std::vector<Scop::Vertex> m_mesh_data;
|
||||||
|
std::vector<std::uint32_t> m_mesh_index_data;
|
||||||
|
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;
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include <MeshPool.h>
|
|
||||||
#include <World.h>
|
|
||||||
|
|
||||||
MeshPool::MeshPool() : m_actors(4 * (RENDER_DISTANCE * RENDER_DISTANCE)) {}
|
|
||||||
|
|
||||||
Scop::NonOwningPtr<Scop::Actor> MeshPool::RequestActor(Scop::Scene& scene)
|
|
||||||
{
|
|
||||||
for(auto& [free, actor] : m_actors)
|
|
||||||
{
|
|
||||||
if(free)
|
|
||||||
{
|
|
||||||
free = false;
|
|
||||||
actor->SetVisibility(true);
|
|
||||||
return actor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Scop::Vertex> mesh_data(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
|
|
||||||
std::vector<std::uint32_t> indices(CHUNK_SIZE.x * CHUNK_SIZE.y * CHUNK_SIZE.z);
|
|
||||||
std::shared_ptr<Scop::Mesh> mesh = std::make_shared<Scop::Mesh>();
|
|
||||||
mesh->AddSubMesh({ std::move(mesh_data), std::move(indices) });
|
|
||||||
m_actors.push_back(std::make_pair(false, &scene.CreateActor(mesh)));
|
|
||||||
return m_actors.back().second;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MeshPool::ReturnToPool(Scop::NonOwningPtr<Scop::Actor> actor)
|
|
||||||
{
|
|
||||||
auto it = std::find_if(m_actors.begin(), m_actors.end(), [actor](const auto& lhs) { return lhs.second == actor; });
|
|
||||||
if(it == m_actors.end())
|
|
||||||
return;
|
|
||||||
it->first = true;
|
|
||||||
it->second->SetVisibility(false);
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef MESH_POOL_H
|
|
||||||
#define MESH_POOL_H
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <ScopGraphics.h>
|
|
||||||
|
|
||||||
class MeshPool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
MeshPool();
|
|
||||||
Scop::NonOwningPtr<Scop::Actor> RequestActor(Scop::Scene& scene);
|
|
||||||
void ReturnToPool(Scop::NonOwningPtr<Scop::Actor> actor);
|
|
||||||
~MeshPool() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<std::pair<bool, Scop::NonOwningPtr<Scop::Actor>>> m_actors;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -13,6 +13,7 @@ World::World(Scop::Scene& scene) : m_scene(scene), m_narrator(scene.CreateNarrat
|
|||||||
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)
|
||||||
{
|
{
|
||||||
GenerateWorld();
|
GenerateWorld();
|
||||||
|
Upload();
|
||||||
};
|
};
|
||||||
|
|
||||||
m_narrator.AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
|
m_narrator.AttachScript(std::make_shared<Scop::NativeNarratorScript>(std::function<void()>{}, narrator_update, std::function<void()>{}));
|
||||||
@@ -36,7 +37,6 @@ void World::GenerateWorld()
|
|||||||
if(current_chunk_position == m_previous_chunk_position)
|
if(current_chunk_position == m_previous_chunk_position)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#pragma omp parallel for
|
|
||||||
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;
|
||||||
@@ -53,10 +53,8 @@ void World::GenerateWorld()
|
|||||||
++it;
|
++it;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma omp parallel for
|
|
||||||
for(std::int32_t x = x_chunk - RENDER_DISTANCE; x <= x_chunk + RENDER_DISTANCE; x++)
|
for(std::int32_t x = x_chunk - RENDER_DISTANCE; x <= x_chunk + RENDER_DISTANCE; x++)
|
||||||
{
|
{
|
||||||
#pragma omp parallel for
|
|
||||||
for(std::int32_t z = z_chunk - RENDER_DISTANCE; z <= z_chunk + RENDER_DISTANCE; z++)
|
for(std::int32_t z = z_chunk - RENDER_DISTANCE; z <= z_chunk + RENDER_DISTANCE; z++)
|
||||||
{
|
{
|
||||||
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 });
|
||||||
@@ -70,3 +68,9 @@ void World::GenerateWorld()
|
|||||||
|
|
||||||
m_previous_chunk_position = current_chunk_position;
|
m_previous_chunk_position = current_chunk_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void World::Upload()
|
||||||
|
{
|
||||||
|
for(auto& chunk : m_chunks)
|
||||||
|
chunk.second.UploadMesh();
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,9 +7,8 @@
|
|||||||
|
|
||||||
#include <Chunk.h>
|
#include <Chunk.h>
|
||||||
#include <Utils.h>
|
#include <Utils.h>
|
||||||
#include <MeshPool.h>
|
|
||||||
|
|
||||||
constexpr std::uint8_t RENDER_DISTANCE = 2;
|
constexpr std::uint8_t RENDER_DISTANCE = 1;
|
||||||
|
|
||||||
class World
|
class World
|
||||||
{
|
{
|
||||||
@@ -19,16 +18,15 @@ class World
|
|||||||
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
|
[[nodiscard]] inline Scop::Scene& GetScene() noexcept { return m_scene; }
|
||||||
[[nodiscard]] inline std::shared_ptr<Scop::Material> GetBlockMaterial() const { return p_block_material; }
|
[[nodiscard]] inline std::shared_ptr<Scop::Material> GetBlockMaterial() const { return p_block_material; }
|
||||||
[[nodiscard]] Scop::NonOwningPtr<Chunk> GetChunk(Scop::Vec2i position);
|
[[nodiscard]] Scop::NonOwningPtr<Chunk> GetChunk(Scop::Vec2i position);
|
||||||
[[nodiscard]] inline MeshPool& GetMeshPool() { return m_mesh_pool; }
|
|
||||||
|
|
||||||
~World() = default;
|
~World() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void GenerateWorld();
|
void GenerateWorld();
|
||||||
|
void Upload();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
|
std::unordered_map<Scop::Vec2i, Chunk> m_chunks;
|
||||||
MeshPool m_mesh_pool;
|
|
||||||
std::shared_ptr<Scop::Material> p_block_material;
|
std::shared_ptr<Scop::Material> p_block_material;
|
||||||
Scop::Narrator& m_narrator;
|
Scop::Narrator& m_narrator;
|
||||||
Scop::Scene& m_scene;
|
Scop::Scene& m_scene;
|
||||||
|
|||||||
Reference in New Issue
Block a user