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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
#include "ChunkMeshing.hpp"
namespace MC::World::Generation::ChunkMeshing {
ChunkMesh mesh_chunk(Chunk& chunk, const ChunkNeighbors& neighbors) {
using namespace Detail;
return {
create_mesh<DefaultMeshDecisions>(chunk, neighbors),
create_mesh<WaterMeshDecisions>(chunk, neighbors)
};
}
namespace Detail {
std::array<Vector<3, F32>, 4> 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{};
switch (side) {
case BlockSide::Front:
face = {{{0, 1, 1}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1}}};
break;
case BlockSide::Back:
face = {{{0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}}};
break;
case BlockSide::Top:
face = {{{0, 1, 1}, {1, 1, 1}, {1, 1, 0}, {0, 1, 0}}};
break;
case BlockSide::Bottom:
face = {{{0, 0, 1}, {0, 0, 0}, {1, 0, 0}, {1, 0, 1}}};
break;
case BlockSide::Right:
face = {{{1, 1, 0}, {1, 1, 1}, {1, 0, 1}, {1, 0, 0}}};
break;
case BlockSide::Left:
face = {{{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}}};
break;
}
for (auto& p : face) {
p += {x, y, z};
}
return face;
}
std::array<Vector<2, F32>, 4> 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 t = y * height_step;
auto l = x * width_step;
auto b = t + height_step;
auto r = l + width_step;
// This is horrible and it was better before I restructured the vertex order...
// In the last version the front and back side pairs had the same structure in different winding,
// so a back_side check wasn't needed...
// Note: BlockSide::Front counts as a back side, because OpenGL has an inverted Z-axis compared to us.
Bool back_side = side == BlockSide::Front || side == BlockSide::Bottom || side == BlockSide::Left;
if (back_side) {
return {{{l, t}, {l, b}, {r, b}, {r, t}}};
}
return {{{r, t}, {l, t}, {l, b}, {r, b}}};
};
switch (type) {
case BlockType::Dirt:
return block_coords(1, 0);
case BlockType::Grass:
switch (side) {
case BlockSide::Front:
case BlockSide::Back:
case BlockSide::Left:
case BlockSide::Right:
return block_coords(2, 0);
case BlockSide::Bottom:
return block_coords(1, 0);
case BlockSide::Top:
return block_coords(0, 0);
}
case BlockType::Stone:
return block_coords(3, 0);
case BlockType::Sand:
return block_coords(1, 1);
case BlockType::Water:
return block_coords(0, 1);
case BlockType::Snow:
switch (side) {
case BlockSide::Front:
case BlockSide::Back:
case BlockSide::Left:
case BlockSide::Right:
return block_coords(3, 1);
case BlockSide::Bottom:
return block_coords(1, 0);
case BlockSide::Top:
return block_coords(2, 1);
}
case BlockType::Wood:
switch (side) {
case BlockSide::Front:
case BlockSide::Back:
case BlockSide::Right:
case BlockSide::Left:
return block_coords(0, 2);
case BlockSide::Bottom:
case BlockSide::Top:
return block_coords(1, 2);
}
case BlockType::Leaves:
return block_coords(2, 2);
case BlockType::Air:
return {};
}
}
std::array<Vector<3, F32>, 4> 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) {
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
// but you can recognize them by the fact that they have no 0 offsets.
// Note: Is there a way to compute these? I tried but I couldn't... If the vertex windings ever change again I don't want to hardcode these...
switch (side) {
case BlockSide::Front:
offsets = {{{0, 1, 1}, {-1,1,1}, {-1, 0, 1}, {-1,-1,1}, {0, -1, 1}, {1,-1,1}, {1, 0, 1}, {1,1,1}}};
break;
case BlockSide::Back:
offsets = {{{-1, 0, -1}, {-1,1,-1}, {0, 1, -1}, {1,1,-1}, {1, 0, -1}, {1,-1,-1}, {0, -1, -1}, {-1,-1,-1}}};
break;
case BlockSide::Top:
offsets = {{{-1, 1, 0}, {-1,1,1}, {0, 1, 1}, {1,1,1}, {1, 1, 0}, {1,1,-1}, {0, 1, -1}, {-1,1,-1}}};
break;
case BlockSide::Bottom:
offsets = {{{0, -1, 1}, {-1,-1,1}, {-1, -1, 0}, {-1,-1,-1}, {0, -1, -1}, {1,-1,-1}, {1, -1, 0}, {1,-1,1}}};
break;
case BlockSide::Right:
offsets = {{{1, 0, -1}, {1,1,-1}, {1, 1, 0}, {1,1,1}, {1, 0, 1}, {1,-1,1}, {1, -1, 0}, {1,-1,-1}}};
break;
case BlockSide::Left:
offsets = {{{-1, 1, 0}, {-1,1,-1}, {-1, 0, -1}, {-1,-1,-1}, {-1, -1, 0}, {-1,-1,1}, {-1, 0, 1}, {-1,1,1}}};
break;
}
std::array<F32, 4> vertex_ao{};
UInt offset_index = 0;
for (UInt vertex = 0; vertex < 4; vertex++) {
auto a = offsets[offset_index];
auto b = offsets[++offset_index]; // corner
auto c = offsets[++offset_index % 8];
auto block_a = get_block_wrapping(chunk, neighbors, {x + a.x(), y + a.y(), z + a.z()});
auto block_b = get_block_wrapping(chunk, neighbors, {x + b.x(), y + b.y(), z + b.z()});
auto block_c = get_block_wrapping(chunk, neighbors, {x + c.x(), y + c.y(), z + c.z()});
F32 occlusion_a = block_a.empty() ? 0 : 1;
F32 occlusion_b = block_b.empty() ? 0 : 1;
F32 occlusion_c = block_c.empty() ? 0 : 1;
if (occlusion_a + occlusion_c == 2.0f) {
vertex_ao[vertex] = 1.0f;
} else {
vertex_ao[vertex] = (occlusion_a + occlusion_b + occlusion_c) / 3;
}
}
return vertex_ao;
}
Chunk::BlockData DefaultMeshDecisions::get_block_wrapping(const Chunk& chunk, const ChunkNeighbors& neighbors, Vector<3, I32> pos) {
const Chunk* chunk_to_ask;
auto overflow = [](I32& c, I32 max) -> I8 {
if (c < 0) { c += max; return -1; }
if (c >= max) { c -= max; return 1; }
return 0;
};
auto xo = overflow(pos.x(), Chunk::Width);
auto yo = overflow(pos.y(), Chunk::Height);
auto zo = overflow(pos.z(), Chunk::Width);
// Blocks above and below a chunk are always Air.
if (yo != 0) return {};
if (xo == 1 && zo == 1) { chunk_to_ask = neighbors.south_east; }
else if (xo == 1 && zo == -1) { chunk_to_ask = neighbors.north_east; }
else if (xo == -1 && zo == 1) { chunk_to_ask = neighbors.south_west; }
else if (xo == -1 && zo == -1) { chunk_to_ask = neighbors.north_west; }
else if (xo == 1) { chunk_to_ask = neighbors.east; }
else if (xo == -1) { chunk_to_ask = neighbors.west; }
else if (zo == 1) { chunk_to_ask = neighbors.south; }
else if (zo == -1) { chunk_to_ask = neighbors.north; }
else { chunk_to_ask = &chunk; }
return chunk_to_ask->get(pos.x(), pos.y(), pos.z());
}
Vector<3, I32> DefaultMeshDecisions::get_face_normal(BlockSide side) {
auto is_side = [=](BlockSide s) -> I8 { return s == side; };
return {
is_side(BlockSide::Right) - is_side(BlockSide::Left),
is_side(BlockSide::Top) - is_side(BlockSide::Bottom),
is_side(BlockSide::Front) - is_side(BlockSide::Back),
};
}
Bool DefaultMeshDecisions::is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) {
Vector<3, I32> offset = get_face_normal(side);
auto neighbor_position = offset + Vector<3, I32>{x, y, z};
auto block = get_block_wrapping(chunk, neighbors, neighbor_position);
return block.type.is_transparent();
}
Bool DefaultMeshDecisions::should_ignore_block(Chunk::BlockData block) {
return block.empty() || block.type == BlockType::Water;
}
Bool WaterMeshDecisions::is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) {
Vector<3, I32> offset = get_face_normal(side);
auto neighbor_position = offset + Vector<3, I32>{x, y, z};
auto [neighbor] = get_block_wrapping(chunk, neighbors, neighbor_position);
return neighbor.is_transparent() && neighbor != BlockType::Water;
}
Bool WaterMeshDecisions::should_ignore_block(Chunk::BlockData block) {
return block.type != BlockType::Water;
}
}
}
|