1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#pragma once
#include "../../GFX/Mesh.hpp"
#include "../../GFX/Util/MeshBuilder.hpp"
#include "../BlockSide.hpp"
#include "../Chunk.hpp"
#include "ChunkNeighbors.hpp"
#include "../ChunkRegistry.hpp"
namespace MC::World::Generation::ChunkMeshing {
// Literally just the blocks squarely surrounding a chunk.
// Example context for a 2x1x2 chunk:
// c c c c
// c x x c
// c x x c
// c c c c
class SurroundingContext {
public:
struct Block { Bool does_exist; Chunk::BlockData block; };
Block& at(Position::BlockLocalOffset p);
const Block& at(Position::BlockLocalOffset p) const;
private:
static USize pos(Position::BlockLocalOffset p);
static constexpr USize surrounding_block_count = Chunk::Width * 4 + 4;
Block m_blocks[surrounding_block_count * Chunk::Height] = {};
};
SurroundingContext create_meshing_context(const Chunk& chunk, ChunkNeighbors& neighbors);
struct ChunkMesh { GFX::Mesh land_mesh, water_mesh; };
ChunkMesh mesh_chunk(Chunk& chunk, const SurroundingContext& context);
namespace Detail {
using Vertex = Vector<3, F32>;
using Normal = Vector<3, F32>;
using TexCoord = Vector<2, F32>;
using Light = F32;
using AO = F32;
template <typename T>
using Face = std::array<T, 4>;
template <typename Decisions>
GFX::Mesh create_mesh(Chunk& chunk, const SurroundingContext& context) {
GFX::Util::MeshBuilder<Normal, TexCoord, Light, AO> builder;
for (Int x = 0; x < Chunk::Width; x++) {
for (Int y = 0; y < Chunk::Height; y++) {
for (Int z = 0; z < Chunk::Width; z++) {
auto block = chunk.at(x, y, z);
if (Decisions::should_ignore_block(block))
continue;
for (auto side: BlockSide::all()) {
if (!Decisions::is_face_visible(chunk, context, x, y, z, side))
continue;
U32 s = builder.vertex_count();
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_light(chunk, context, x, y, z, side));
builder.attributes<3>(Decisions::face_ao_values(chunk, context, x, y, z, side));
builder.indices(std::array{ s + 0, s + 1, s + 2, s + 2, s + 3, s + 0 });
}
}
}
}
return builder.mesh();
}
class DefaultMeshDecisions {
public:
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<Light> face_light(Chunk& chunk, const SurroundingContext& context, U32 x, U32 y, U32 z, BlockSide side);
static Face<AO> face_ao_values(Chunk& chunk, const SurroundingContext& context, U32 x, U32 y, U32 z, BlockSide side);
static Vector<3, I32> get_face_normal(BlockSide side);
static SurroundingContext::Block get_block_from_chunk_or_context(const Chunk& chunk, const SurroundingContext& context, Position::BlockLocalOffset pos);
static SurroundingContext::Block get_opposing_neighbor(const Chunk& chunk, const SurroundingContext& context, U32 x, U32 y, U32 z, BlockSide side);
static Bool is_face_visible(Chunk& chunk, const SurroundingContext& context, U32 x, U32 y, U32 z, BlockSide side);
static Bool should_ignore_block(Chunk::BlockData block);
};
class WaterMeshDecisions final : public DefaultMeshDecisions {
public:
static Bool is_face_visible(Chunk& chunk, const SurroundingContext& context, U32 x, U32 y, U32 z, BlockSide side);
static Bool should_ignore_block(Chunk::BlockData block);
};
}
}
|