summary refs log tree commit diff
path: root/src/World/Generation/ChunkMeshing.hpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2023-07-11 04:13:18 +0200
committerMel <einebeere@gmail.com>2023-07-11 04:13:18 +0200
commite6812d2df6bd8a0a71375096abe46f8039d8c570 (patch)
tree8fd1bb578f5a6e2edfc1b1dfffea32f9285c17c9 /src/World/Generation/ChunkMeshing.hpp
parent2c3ac8db3f539459166c801e61c5efdacf9150c3 (diff)
downloadmeowcraft-e6812d2df6bd8a0a71375096abe46f8039d8c570.tar.zst
meowcraft-e6812d2df6bd8a0a71375096abe46f8039d8c570.zip
Create MeshBuilder utility for comfy mesh building
Diffstat (limited to 'src/World/Generation/ChunkMeshing.hpp')
-rw-r--r--src/World/Generation/ChunkMeshing.hpp45
1 files changed, 21 insertions, 24 deletions
diff --git a/src/World/Generation/ChunkMeshing.hpp b/src/World/Generation/ChunkMeshing.hpp
index 75023f5..7e202ab 100644
--- a/src/World/Generation/ChunkMeshing.hpp
+++ b/src/World/Generation/ChunkMeshing.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "../../GFX/Mesh.hpp"
+#include "../../GFX/Util/MeshBuilder.hpp"
 #include "../Chunk.hpp"
 
 namespace MC::World::Generation::ChunkMeshing {
@@ -15,13 +16,17 @@ ChunkMesh mesh_chunk(Chunk& chunk, const ChunkNeighbors& neighbors);
 
 namespace Detail {
 
+using Vertex = Vector<3, F32>;
+using Normal = Vector<3, F32>;
+using TexCoord = Vector<2, F32>;
+using AO = F32;
+
+template <typename T>
+using Face = std::array<T, 4>;
+
 template <typename Decisions>
 GFX::Mesh create_mesh(Chunk& chunk, const ChunkNeighbors& neighbors) {
-    std::vector<Vector<3, F32>> positions{};
-    std::vector<Vector<3, F32>> normals{};
-    std::vector<Vector<2, F32>> tex_coords{};
-    std::vector<F32> ambient_occlusion_values{};
-    std::vector<U32> indices{};
+    GFX::Util::MeshBuilder<Normal, TexCoord, AO> builder;
 
     for (Int x = 0; x < Chunk::Width; x++) {
         for (Int y = 0; y < Chunk::Height; y++) {
@@ -34,35 +39,27 @@ GFX::Mesh create_mesh(Chunk& chunk, const ChunkNeighbors& neighbors) {
                     if (!Decisions::is_face_visible(chunk, neighbors, x, y, z, side))
                         continue;
 
-                    auto side_positions = Decisions::face_positions(side, x, y, z);
-                    auto side_normals = Decisions::face_normals(side);
-                    auto side_tex_coords = Decisions::face_tex_coords(block.type, side);
-                    auto side_ao = Decisions::face_ao_values(chunk, neighbors, x, y, z, side);
-
-                    U32 s = positions.size();
+                    U32 s = builder.vertex_count();
 
-                    positions.insert(positions.end(), side_positions.begin(), side_positions.end());
-                    normals.insert(normals.end(), side_normals.begin(), side_normals.end());
-                    tex_coords.insert(tex_coords.end(), side_tex_coords.begin(), side_tex_coords.end());
-                    ambient_occlusion_values.insert(ambient_occlusion_values.end(), side_ao.begin(), side_ao.end());
-                    indices.insert(indices.end(), {s + 0, s + 1, s + 2, s + 2, s + 3, s + 0 });
+                    builder.positions(Decisions::face_positions(side, x, y, z));
+                    builder.attributes<0>(Decisions::face_normals(side));
+                    builder.attributes<1>(Decisions::face_tex_coords(block.type, side));
+                    builder.attributes<2>(Decisions::face_ao_values(chunk, neighbors, x, y, z, side));
+                    builder.indices(std::array{ s + 0, s + 1, s + 2, s + 2, s + 3, s + 0 });
                 }
             }
         }
     }
 
-    return {
-        {positions, normals, tex_coords, ambient_occlusion_values},
-        indices,
-    };
+    return builder.mesh();
 }
 
 class DefaultMeshDecisions {
 public:
-    static std::array<Vector<3, F32>, 4> face_positions(BlockSide side, U32 x, U32 y, U32 z);
-    static std::array<Vector<2, F32>, 4> face_tex_coords(BlockType type, BlockSide side);
-    static std::array<Vector<3, F32>, 4> face_normals(BlockSide side);
-    static std::array<F32, 4> face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side);
+    static Face<Vertex> face_positions(BlockSide side, U32 x, U32 y, U32 z);
+    static Face<TexCoord> face_tex_coords(BlockType type, BlockSide side);
+    static Face<Normal> face_normals(BlockSide side);
+    static Face<AO> face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side);
 
     static Chunk::BlockData get_block_wrapping(const Chunk& chunk, const ChunkNeighbors& neighbors, Vector<3, I32> pos);
     static Vector<3, I32> get_face_normal(BlockSide side);