Skip to content

Commit 6113783

Browse files
committed
Améliore la gestion des maillages dans Chunk
Modifie la méthode `DeleteChunk` pour supprimer un maillage si `m_mesh_id` est valide. Ajoute `compute_mesh` pour calculer la géométrie des voxels actifs et `render_mesh` pour charger le maillage dans le monde. Met à jour `Chunk.h` avec les nouvelles déclarations de méthode. Dans `ChunkManager.cpp`, remplace l'appel à `BuildChunk` par `compute_mesh` et `render_mesh` pour une meilleure séparation des responsabilités.
1 parent ee0e3b5 commit 6113783

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

R3DVoxel/VoxelEngine/Chunk.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,95 @@ void Chunk::DeleteChunk(std::shared_ptr<GameObject> world)
167167
world->RemoveMesh(m_mesh_id);
168168
}
169169

170+
std::optional<Geometry> Chunk::compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map)
171+
{
172+
if (m_active_voxel == false)
173+
return {};
174+
175+
m_visible_voxel = { true };
176+
177+
Geometry mesh;
178+
for (uint32_t x = 0; x < Voxel::CHUNK_SIZE; x++)
179+
{
180+
for (uint32_t y = 0; y < Voxel::CHUNK_SIZE; y++)
181+
{
182+
for (uint32_t z = 0; z < Voxel::CHUNK_SIZE; z++)
183+
{
184+
const uint32_t voxel = x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR;
185+
if (m_active_voxel[voxel])
186+
{
187+
if (x > 0)
188+
{
189+
m_visible_voxel[voxel].xneg = m_active_voxel[(x - 1) + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
190+
}
191+
else if (chunk_map.find({ static_cast<int32_t>(m_position.x) - 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) }) != chunk_map.end())
192+
{
193+
// check if face is hidden by other chunk
194+
m_visible_voxel[voxel].xneg =
195+
chunk_map.at({ static_cast<int32_t>(m_position.x) - 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
196+
->GetVoxel(x + Voxel::CHUNK_SIZE - 1, y, z);
197+
}
198+
199+
if (x < Voxel::CHUNK_SIZE - 1)
200+
{
201+
m_visible_voxel[voxel].xpos = m_active_voxel[(x + 1) + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
202+
}
203+
else if (chunk_map.find({ static_cast<int32_t>(m_position.x) + 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) }) != chunk_map.end())
204+
{
205+
m_visible_voxel[voxel].xpos =
206+
chunk_map.at({ static_cast<int32_t>(m_position.x) + 1, static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) })
207+
->GetVoxel(x - Voxel::CHUNK_SIZE + 1, y, z);
208+
}
209+
210+
if (y > 0)
211+
{
212+
m_visible_voxel[voxel].yneg = m_active_voxel[x + (y - 1) * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
213+
}
214+
215+
if (y < Voxel::CHUNK_SIZE - 1)
216+
{
217+
m_visible_voxel[voxel].ypos = m_active_voxel[x + (y + 1) * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR];
218+
}
219+
220+
if (z > 0)
221+
{
222+
m_visible_voxel[voxel].zneg = m_active_voxel[x + y * Voxel::CHUNK_SIZE + (z - 1) * Voxel::CHUNK_SIZE_SQR];
223+
}
224+
else if (chunk_map.find({ static_cast<int32_t>(m_position.x), static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) - 1 }) != chunk_map.end())
225+
{
226+
// check if face is hidden by other chunk
227+
m_visible_voxel[voxel].zneg =
228+
chunk_map.at({ static_cast<int32_t>(m_position.x), static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) - 1 })
229+
->GetVoxel(x, y, z + Voxel::CHUNK_SIZE - 1);
230+
}
231+
232+
if (z < Voxel::CHUNK_SIZE - 1)
233+
{
234+
m_visible_voxel[voxel].zpos = m_active_voxel[x + y * Voxel::CHUNK_SIZE + (z + 1) * Voxel::CHUNK_SIZE_SQR];
235+
}
236+
else if (chunk_map.find({ static_cast<int32_t>(m_position.x), static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) + 1 }) != chunk_map.end())
237+
{
238+
// check if face is hidden by other chunk
239+
m_visible_voxel[voxel].zpos =
240+
chunk_map.at({ static_cast<int32_t>(m_position.x), static_cast<int32_t>(m_position.y), static_cast<int32_t>(m_position.z) + 1 })
241+
->GetVoxel(x, y, z - Voxel::CHUNK_SIZE + 1); // faces are invisible because they are removed before, should make 2 lists
242+
}
243+
244+
CreateCube(mesh, x, y, z);
245+
}
246+
}
247+
}
248+
}
249+
250+
return std::move(mesh);
251+
}
252+
253+
void Chunk::render_mesh(const Geometry& mesh, GameObject& world, std::shared_ptr<Material> mat)
254+
{
255+
m_mesh_id = world.LoadMesh(mesh.vertices, mesh.indices);
256+
world.bindMatToMesh(m_mesh_id, mat);
257+
}
258+
170259
void Chunk::SetVoxel(const uint32_t x, const uint32_t y, const uint32_t z)
171260
{
172261
m_active_voxel.set(x + y * Voxel::CHUNK_SIZE + z * Voxel::CHUNK_SIZE_SQR, true);

R3DVoxel/VoxelEngine/Chunk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class Chunk
4242
void UpdateChunk(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map, GameObject& world);
4343
void DeleteChunk(std::shared_ptr<GameObject> world);
4444

45+
std::optional<Geometry> compute_mesh(const std::map<ChunkKey, std::unique_ptr<Chunk>>& chunk_map);
46+
void render_mesh(const Geometry& mesh, GameObject& world, std::shared_ptr<Material> mat);
47+
48+
49+
4550
void SetVoxel(const uint32_t x, const uint32_t y, const uint32_t z);
4651
void SetBlockType(const uint32_t x, const uint32_t y, const uint32_t z, const TBlock type);
4752

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ void ChunkManager::update_world_x(int32_t create_x, int32_t update_xplus, int32_
336336
{
337337
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
338338
{
339-
m_chunk_map.at({ create_x, y, z })->BuildChunk(m_chunk_map, mp_world, mp_world_mat);
339+
// m_chunk_map.at({ create_x, y, z })->BuildChunk(m_chunk_map, mp_world, mp_world_mat);
340+
auto mesh = m_chunk_map.at({ create_x, y, z })->compute_mesh(m_chunk_map);
341+
if (mesh.has_value())
342+
m_chunk_map.at({ create_x, y, z })->render_mesh(mesh.value(), *mp_world, mp_world_mat);
343+
340344
m_chunk_map.at({ update_xplus, y, z })->UpdateChunk(m_chunk_map, *mp_world);
341345
m_chunk_map.at({ update_xmin, y, z })->UpdateChunk(m_chunk_map, *mp_world);
342346
}

0 commit comments

Comments
 (0)