@@ -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+
170259void 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 );
0 commit comments