Skip to content

Commit f96cba9

Browse files
committed
Refactor rendering and chunk management methods
Modifications apportées à `Application::Start()` pour remplacer l'appel à `update_world_x` par `render_meshes`, permettant ainsi de rendre les maillages calculés. La méthode `compute_world_update_x` a été ajustée pour utiliser des clés de chunk différentes et insérer des copies de chunks dans `_setup_list`. Ajout de nouvelles méthodes `compute_meshes` et `render_meshes` dans `ChunkManager` pour générer et rendre les maillages des chunks actifs. La méthode `copy_to_render` a été mise à jour pour éviter l'insertion de chunks nuls, et les déclarations de méthode dans `ChunkManager.h` ont été modifiées en conséquence.
1 parent e79e2f3 commit f96cba9

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

R3DVoxel/Application.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ void Application::Start()
6969
const auto update_x = chunk_manager.compute_world_update_x(*mp_engine->GetMainCamera());
7070
if (update_x.updated)
7171
{
72+
auto meshes = chunk_manager.compute_meshes(update_x.created, update_x.update_plus, update_x.update_min);
7273
chunk_manager.copy_to_render();
73-
chunk_manager.update_world_x(update_x.created, update_x.update_plus, update_x.update_min);
74+
chunk_manager.render_meshes(meshes, update_x.created, update_x.update_plus, update_x.update_min);
75+
76+
// chunk_manager.update_world_x(update_x.created, update_x.update_plus, update_x.update_min);
7477
}
7578

7679
/* const auto update_z = chunk_manager.compute_world_update_z(*mp_engine->GetMainCamera());

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,16 @@ render_update_t ChunkManager::compute_world_update_x(const Camera& camera)
213213
{
214214
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
215215
{
216-
const ChunkKey key = { .x = m_render_max.x, .y = y, .z = z };
217-
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, nullptr));
216+
const ChunkKey new_key = { .x = m_render_max.x, .y = y, .z = z };
217+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(new_key, nullptr));
218+
219+
const ChunkKey front_key = { .x = m_render_max.x - 1, .y = y, .z = z };
220+
auto copy_front = std::make_unique<Chunk>(*m_chunk_map.at(front_key));
221+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(front_key, std::move(copy_front)));
222+
223+
const ChunkKey back_key = { .x = m_render_min.x + 1, .y = y, .z = z };
224+
auto copy_back = std::make_unique<Chunk>(*m_chunk_map.at(back_key));
225+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(back_key, std::move(copy_back)));
218226

219227
const ChunkKey destroy_key = { .x = m_render_min.x, .y = y, .z = z };
220228
_destroy_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(destroy_key, nullptr));
@@ -249,8 +257,16 @@ render_update_t ChunkManager::compute_world_update_x(const Camera& camera)
249257
{
250258
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
251259
{
252-
const ChunkKey key = { .x = m_render_min.x, .y = y, .z = z };
253-
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, nullptr));
260+
const ChunkKey new_key = { .x = m_render_min.x, .y = y, .z = z };
261+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(new_key, nullptr));
262+
263+
const ChunkKey front_key = { .x = m_render_min.x + 1, .y = y, .z = z };
264+
auto copy_front = std::make_unique<Chunk>(*m_chunk_map.at(front_key));
265+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(front_key, std::move(copy_front)));
266+
267+
const ChunkKey back_key = { .x = m_render_max.x - 1, .y = y, .z = z };
268+
auto copy_back = std::make_unique<Chunk>(*m_chunk_map.at(back_key));
269+
_setup_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(back_key, std::move(copy_back)));
254270

255271
const ChunkKey destroy_key = { .x = m_render_max.x, .y = y, .z = z };
256272
_destroy_list.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(destroy_key, nullptr));
@@ -330,6 +346,72 @@ render_update_t ChunkManager::compute_world_update_z(const Camera& camera)
330346
return render_update;
331347
}
332348

349+
std::map<ChunkKey, Geometry> ChunkManager::compute_meshes(int32_t create_x, int32_t update_xplus, int32_t update_xmin)
350+
{
351+
std::map<ChunkKey, Geometry> meshes;
352+
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
353+
{
354+
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
355+
{
356+
Chunk* new_chunk = _setup_list.at({ create_x, y, z }).get();
357+
if (new_chunk->is_active())
358+
{
359+
const ChunkKey key = { create_x, y, z };
360+
meshes.emplace(std::pair<ChunkKey, Geometry>(key, new_chunk->compute_mesh(_setup_list)));
361+
}
362+
363+
Chunk* front_chunk = _setup_list.at({ update_xplus, y, z }).get();
364+
if (front_chunk->is_active() || front_chunk->mesh_id() != -1)
365+
{
366+
const ChunkKey key = { update_xplus, y, z };
367+
meshes.emplace(std::pair<ChunkKey, Geometry>(key, front_chunk->compute_mesh(_setup_list)));
368+
}
369+
370+
Chunk* back_chunk = _setup_list.at({ update_xmin, y, z }).get();
371+
if (back_chunk->is_active() || back_chunk->mesh_id() != -1)
372+
{
373+
const ChunkKey key = { update_xmin, y, z };
374+
meshes.emplace(std::pair<ChunkKey, Geometry>(key, back_chunk->compute_mesh(_setup_list)));
375+
}
376+
}
377+
}
378+
379+
return meshes;
380+
}
381+
382+
void ChunkManager::render_meshes(const std::map<ChunkKey, Geometry>& meshes, int32_t create_x, int32_t update_xplus, int32_t update_xmin)
383+
{
384+
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
385+
{
386+
for (int32_t y = m_render_min.y; y <= m_render_max.y; y++)
387+
{
388+
const ChunkKey create_key = { create_x, y, z };
389+
Chunk* new_chunk = m_chunk_map.at(create_key).get();
390+
if (new_chunk->is_active())
391+
{
392+
auto mesh = meshes.at(create_key);
393+
new_chunk->render_mesh(mesh, *mp_world, mp_world_mat);
394+
}
395+
396+
const ChunkKey front_key = { update_xplus, y, z };
397+
Chunk* front_chunk = m_chunk_map.at(front_key).get();
398+
if (front_chunk->is_active() || front_chunk->mesh_id() != -1)
399+
{
400+
auto mesh = meshes.at(front_key);
401+
front_chunk->update_mesh(mesh, *mp_world);
402+
}
403+
404+
const ChunkKey back_key = { update_xmin, y, z };
405+
Chunk* back_chunk = m_chunk_map.at(back_key).get();
406+
if (back_chunk->is_active() || back_chunk->mesh_id() != -1)
407+
{
408+
auto mesh = meshes.at(back_key);
409+
back_chunk->update_mesh(mesh, *mp_world);
410+
}
411+
}
412+
}
413+
}
414+
333415
void ChunkManager::update_world_x(int32_t create_x, int32_t update_xplus, int32_t update_xmin) const
334416
{
335417
for (int32_t z = m_render_min.z; z <= m_render_max.z; z++)
@@ -380,7 +462,7 @@ void ChunkManager::copy_to_render()
380462
{
381463
for (auto& [key, chunk] : _setup_list)
382464
{
383-
if (chunk)
465+
if (chunk && !m_chunk_map.contains(key))
384466
{
385467
m_chunk_map.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, std::move(chunk)));
386468
}

R3DVoxel/VoxelEngine/ChunkManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class ChunkManager
3131
render_update_t compute_world_update_x(const Camera& camera);
3232
render_update_t compute_world_update_z(const Camera& camera);
3333

34+
std::map<ChunkKey, Geometry> compute_meshes(int32_t create_x, int32_t update_xplus, int32_t update_xmin);
35+
void render_meshes(const std::map<ChunkKey, Geometry>& meshes, int32_t create_x, int32_t update_xplus, int32_t update_xmin);
36+
3437
void update_world_x(int32_t create_x, int32_t update_xplus, int32_t update_xmin) const;
3538
void update_world_z(int32_t create_z, int32_t update_zplus, int32_t update_zmin) const;
3639

0 commit comments

Comments
 (0)