summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Common/Sizes.hpp5
-rw-r--r--src/GFX/Util/MeshBuilder.hpp65
-rw-r--r--src/World/Generation/ChunkMeshing.cpp14
-rw-r--r--src/World/Generation/ChunkMeshing.hpp45
4 files changed, 96 insertions, 33 deletions
diff --git a/src/Common/Sizes.hpp b/src/Common/Sizes.hpp
index 91422e5..de49d3d 100644
--- a/src/Common/Sizes.hpp
+++ b/src/Common/Sizes.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
+#include <sys/types.h>
using I8 = int8_t;
using U8 = uint8_t;
@@ -14,8 +15,8 @@ using U32 = uint32_t;
using I64 = int64_t;
using U64 = uint64_t;
-using ISize = I64;
-using USize = U64;
+using ISize = ssize_t;
+using USize = size_t;
using F32 = float;
using F64 = double;
diff --git a/src/GFX/Util/MeshBuilder.hpp b/src/GFX/Util/MeshBuilder.hpp
new file mode 100644
index 0000000..e3caa8c
--- /dev/null
+++ b/src/GFX/Util/MeshBuilder.hpp
@@ -0,0 +1,65 @@
+#pragma once
+
+#include "../../Common/Sizes.hpp"
+#include "../../Math/Common.hpp"
+#include <vector>
+
+namespace MC::GFX::Util {
+
+template <typename... Attributes>
+class MeshBuilder {
+ using AttributeTuple = std::tuple<std::vector<Attributes>...>;
+
+ template <int N, typename AttributeContainer>
+ using EnableIfCorrectAttributeIndex =
+ std::enable_if_t<std::is_same_v<
+ typename std::tuple_element_t<N, AttributeTuple>::value_type,
+ typename AttributeContainer::value_type>, Bool>;
+
+ template <int N, typename A>
+ std::vector<A>& get() {
+ return std::get<N>(m_attributes);
+ }
+
+ template <USize... Sequence>
+ Mesh mesh(std::index_sequence<Sequence...>) {
+ return {
+ {m_positions, std::get<Sequence>(m_attributes)...},
+ m_indices,
+ };
+ }
+
+ std::vector<Vector<3, F32>> m_positions{};
+ std::vector<U32> m_indices{};
+ AttributeTuple m_attributes;
+public:
+ static constexpr UInt AttributesN = sizeof...(Attributes);
+
+ MeshBuilder() = default;
+
+ template<int N, typename AC, EnableIfCorrectAttributeIndex<N, AC> = true>
+ void attributes(const AC& from) {
+ auto& attribute_list = get<N, typename AC::value_type>();
+ attribute_list.insert(attribute_list.end(), from.begin(), from.end());
+ }
+
+ template<typename AC>
+ void positions(const AC& from) {
+ m_positions.insert(m_positions.end(), from.begin(), from.end());
+ }
+
+ template<typename AC>
+ void indices(const AC& from) {
+ m_indices.insert(m_indices.end(), from.begin(), from.end());
+ }
+
+ Mesh mesh() {
+ return mesh(std::make_index_sequence<AttributesN>{});
+ }
+
+ USize vertex_count() const {
+ return m_positions.size();
+ }
+};
+
+}
diff --git a/src/World/Generation/ChunkMeshing.cpp b/src/World/Generation/ChunkMeshing.cpp
index 7158734..2e9f191 100644
--- a/src/World/Generation/ChunkMeshing.cpp
+++ b/src/World/Generation/ChunkMeshing.cpp
@@ -13,10 +13,10 @@ ChunkMesh mesh_chunk(Chunk& chunk, const ChunkNeighbors& neighbors) {
namespace Detail {
-std::array<Vector<3, F32>, 4> DefaultMeshDecisions::face_positions(BlockSide side, U32 x, U32 y, U32 z) {
+Face<Vertex> DefaultMeshDecisions::face_positions(BlockSide side, U32 x, U32 y, U32 z) {
// Winding order: (0, 1, 2) (2, 3, 0)
// Note: OpenGL Coordinate system has a flipped z axis.
- std::array<Vector<3, F32>, 4> face{};
+ Face<Vertex> face{};
switch (side) {
case BlockSide::Front:
face = {{{0, 1, 1}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
@@ -44,14 +44,14 @@ std::array<Vector<3, F32>, 4> DefaultMeshDecisions::face_positions(BlockSide sid
return face;
}
-std::array<Vector<2, F32>, 4> DefaultMeshDecisions::face_tex_coords(BlockType type, BlockSide side) {
+Face<TexCoord> DefaultMeshDecisions::face_tex_coords(BlockType type, BlockSide side) {
U8 atlas_width = 4;
U8 atlas_height = 4;
Real width_step = 1.0f / atlas_width;
Real height_step = 1.0f / atlas_height;
- auto block_coords = [=](U8 x, U8 y) -> std::array<Vector<2, F32>, 4> {
+ auto block_coords = [=](U8 x, U8 y) -> Face<TexCoord> {
auto t = y * height_step;
auto l = x * width_step;
auto b = t + height_step;
@@ -119,12 +119,12 @@ std::array<Vector<2, F32>, 4> DefaultMeshDecisions::face_tex_coords(BlockType ty
}
}
-std::array<Vector<3, F32>, 4> DefaultMeshDecisions::face_normals(BlockSide side) {
+Face<Normal> DefaultMeshDecisions::face_normals(BlockSide side) {
Vector<3, F32> normal{get_face_normal(side)};
return {normal, normal, normal, normal};
}
-std::array<F32, 4> DefaultMeshDecisions::face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) {
+Face<AO> DefaultMeshDecisions::face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) {
std::array<Vector<3, I32>, 8> offsets{};
// Given a block position, these offsets can be added to it to get the 8 blocks necessary to calculate AO.
// There are 4 corners and 4 sides, corners are visually distinguished by the lack of spaces and
@@ -151,7 +151,7 @@ std::array<F32, 4> DefaultMeshDecisions::face_ao_values(Chunk& chunk, const Chun
break;
}
- std::array<F32, 4> vertex_ao{};
+ Face<AO> vertex_ao{};
UInt offset_index = 0;
for (UInt vertex = 0; vertex < 4; vertex++) {
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);