Skip to content

Commit 6b271f3

Browse files
committed
src/chunk-walls: factor out common formulas
1 parent 2f9498d commit 6b271f3

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

src/chunk-walls.cpp

+50-35
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ using Wall::Frame;
4545
template<typename T> using Vec2_ = VectorTypeFor<2, T>;
4646
template<typename T> using Vec3_ = VectorTypeFor<3, T>;
4747

48+
template<typename F, uint32_t N>
49+
struct minmax_v
50+
{
51+
VectorTypeFor<N, F> min, max;
52+
};
53+
4854
template<Group_ G, bool IsWest, typename F = float>
49-
constexpr std::array<Vec3_<F>, 4> get_quad(F depth)
55+
constexpr std::array<Vec3_<F>, 4> get_quadʹ(minmax_v<F, 3> bounds, F d)
5056
{
5157
static_assert(G < Group_::COUNT);
5258

53-
constexpr auto half_tile = Vec2_<F>(TILE_SIZE2*.5f);
54-
constexpr auto X = half_tile.x(), Y = half_tile.y(), Z = F(TILE_SIZE.z());
59+
const auto x0 = bounds.min.x(), y0 = bounds.min.y(), z0 = bounds.min.z(),
60+
x1 = bounds.max.x(), y1 = bounds.max.y(), z1 = bounds.max.z();
5561

5662
switch (G)
5763
{
@@ -60,67 +66,76 @@ constexpr std::array<Vec3_<F>, 4> get_quad(F depth)
6066
case wall:
6167
if (!IsWest)
6268
return {{
63-
{ X, -Y, 0 },
64-
{ X, -Y, Z },
65-
{-X, -Y, 0 },
66-
{-X, -Y, Z },
69+
{ x1, y0, z0 },
70+
{ x1, y0, z1 },
71+
{ x0, y0, z0 },
72+
{ x0, y0, z1 },
6773
}};
6874
else
6975
return {{
70-
{-X, -Y, 0 },
71-
{-X, -Y, Z },
72-
{-X, Y, 0 },
73-
{-X, Y, Z },
76+
{ x0, y0, z0 },
77+
{ x0, y0, z1 },
78+
{ x0, y1, z0 },
79+
{ x0, y1, z1 },
7480
}};
7581
case side:
7682
if (!IsWest)
7783
return {{
78-
{ X, -Y - depth, 0 },
79-
{ X, -Y - depth, Z },
80-
{ X, -Y, 0 },
81-
{ X, -Y, Z },
84+
{ x1, y0 - d, z0 },
85+
{ x1, y0 - d, z1 },
86+
{ x1, y0, z0 },
87+
{ x1, y0, z1 },
8288
}};
8389
else
8490
return {{
85-
{ -X, Y, 0 },
86-
{ -X, Y, Z },
87-
{ -X - depth, Y, 0 },
88-
{ -X - depth, Y, Z },
91+
{ x0, y1, z0 },
92+
{ x0, y1, z1 },
93+
{ x0 - d, y1, z0 },
94+
{ x0 - d, y1, z1 },
8995
}};
9096
case top:
9197
if (!IsWest)
9298
return {{
93-
{ -X, -Y - depth, Z },
94-
{ X, -Y - depth, Z },
95-
{ -X, -Y, Z },
96-
{ X, -Y, Z },
99+
{ x0, y0 - d, z1 },
100+
{ x1, y0 - d, z1 },
101+
{ x0, y0, z1 },
102+
{ x1, y0, z1 },
97103
}};
98104
else
99105
return {{
100-
{ -X, -Y, Z },
101-
{ -X, Y, Z },
102-
{ -X - depth, -Y, Z },
103-
{ -X - depth, Y, Z },
106+
{ x0, y0, z1 },
107+
{ x0, y1, z1 },
108+
{ x0 - d, y0, z1 },
109+
{ x0 - d, y1, z1 },
104110
}};
105111
case corner:
106112
if (!IsWest)
107113
return {{
108-
{-X, -Y, 0 },
109-
{-X, -Y, Z },
110-
{-X - depth, -Y, 0 },
111-
{-X - depth, -Y, Z },
114+
{ x0, y0, z0 },
115+
{ x0, y0, z1 },
116+
{ x0 - d, y0, z0 },
117+
{ x0 - d, y0, z1 },
112118
}};
113119
else
114120
return {{
115-
{-X, -Y - depth, 0 },
116-
{-X, -Y - depth, Z },
117-
{-X, -Y, 0 },
118-
{-X, -Y, Z },
121+
{ x0, y0 - d, z0 },
122+
{ x0, y0 - d, z1 },
123+
{ x0, y0, z0 },
124+
{ x0, y0, z1 },
119125
}};
120126
}
121127
std::unreachable();
122128
}
123129

130+
template<Group_ G, bool IsWest, typename F = float>
131+
constexpr std::array<Vec3_<F>, 4> get_quad(F d)
132+
{
133+
constexpr auto half_tile = Vec2_<F>(TILE_SIZE2*.5f);
134+
constexpr auto X = half_tile.x(), Y = half_tile.y(), Z = F(TILE_SIZE.z());
135+
136+
return get_quadʹ<G, IsWest, F>({ { -X, -Y, 0, }, { X, Y, Z, }, }, d);
137+
}
138+
124139
template<bool IsWest>
125140
CutResult<Int>::rect get_wall_rect(local_coords tile)
126141
{

0 commit comments

Comments
 (0)