Skip to content

Commit f9d84c3

Browse files
author
Dan Liebault
committed
Amélioration de la génération de chunks
Modification de `CreateNewChunk` pour utiliser `compute_world_chunk` à la place de `SetupWorld`. Ajout de la méthode `compute_world_chunk` dans `TerrainGenerator` pour générer des chunks avec des variations de hauteur basées sur le bruit de Perlin. Mise à jour de l'en-tête pour inclure la nouvelle méthode.
1 parent 2124466 commit f9d84c3

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

R3DVoxel/VoxelEngine/ChunkManager.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ void ChunkManager::CreateNewChunk(int32_t x, int32_t y, int32_t z)
176176
{
177177
const ChunkKey key = { x, y, z };
178178

179-
std::unique_ptr<Chunk> p_chunk = mp_terrain_generator->SetupWorld(x, y, z);
179+
// std::unique_ptr<Chunk> p_chunk = mp_terrain_generator->SetupWorld(x, y, z);
180+
auto p_chunk = mp_terrain_generator->compute_world_chunk(x, y, z);
180181
m_chunk_map.insert(std::pair<ChunkKey, std::unique_ptr<Chunk>>(key, std::move(p_chunk)));
181182
}
182183

R3DVoxel/VoxelEngine/TerrainGenerator.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,41 @@ std::unique_ptr<Chunk> TerrainGenerator::SetupWorld(const int32_t posx, const in
6363
}
6464

6565
return std::move(p_chunk);
66-
}
66+
}
67+
68+
std::unique_ptr<Chunk> TerrainGenerator::compute_world_chunk(const int32_t posx, const int32_t posy, const int32_t posz) const
69+
{
70+
auto p_chunk = std::make_unique<Chunk>(posx, posy, posz);
71+
72+
for (uint32_t x = 0; x < Voxel::CHUNK_SIZE; x++)
73+
{
74+
for (uint32_t y = 0; y < Voxel::CHUNK_SIZE; y++)
75+
{
76+
for (uint32_t z = 0; z < Voxel::CHUNK_SIZE; z++)
77+
{
78+
const int8_t x_sign = posx != 0 ? posx / std::abs(posx) : 1;
79+
const int8_t z_sign = posz != 0 ? posz / std::abs(posz) : 1;
80+
81+
const int32_t voxel_x_pos = static_cast<int32_t>(x) + posx * static_cast<int32_t>(Voxel::CHUNK_SIZE);
82+
const int32_t voxel_z_pos = static_cast<int32_t>(z) + posz * static_cast<int32_t>(Voxel::CHUNK_SIZE);
83+
const int32_t voxel_y_pos = static_cast<int32_t>(y) + posy * static_cast<int32_t>(Voxel::CHUNK_SIZE);
84+
85+
const float noise_value1 = m_perlin.perlin(static_cast<float>(voxel_x_pos * x_sign), static_cast<float>(voxel_z_pos * z_sign));
86+
const float noise_value2 = m_perlin.perlin(static_cast<float>(voxel_x_pos * x_sign * 0.5f), static_cast<float>(voxel_z_pos * z_sign * 0.5f));
87+
const float noise_value3 = m_perlin.perlin(static_cast<float>(voxel_x_pos * x_sign * 0.25f), static_cast<float>(voxel_z_pos * z_sign * 0.25f));
88+
89+
// Combinaison des valeurs de bruit pour créer des variations de hauteur plus détaillées
90+
const float combined_noise = (noise_value1 + noise_value2 * 0.5f + noise_value3 * 0.25f) / 1.75f;
91+
92+
if (voxel_y_pos < ((combined_noise + 1) / 2) * Voxel::CHUNK_SIZE)
93+
{
94+
p_chunk->SetVoxel(x, y, z);
95+
}
96+
97+
p_chunk->SetBlockType(x, y, z, TBlock::DEFAULT);
98+
}
99+
}
100+
}
101+
102+
return std::move(p_chunk);
103+
}

R3DVoxel/VoxelEngine/TerrainGenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TerrainGenerator
1414

1515
std::unique_ptr<Chunk> SetupSphere(const int32_t posx, const int32_t posy, const int32_t posz);
1616
std::unique_ptr<Chunk> SetupWorld(const int32_t posx, const int32_t posy, const int32_t posz);
17+
std::unique_ptr<Chunk> compute_world_chunk(const int32_t posx, const int32_t posy, const int32_t posz) const;
1718

1819
private:
1920
PerlinNoise m_perlin;

0 commit comments

Comments
 (0)