From 6b271f37e740210e5c85324f88706ad5cf4487ba Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 15 Nov 2024 16:09:12 +0100 Subject: src/chunk-walls: factor out common formulas --- src/chunk-walls.cpp | 85 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 35 deletions(-) (limited to 'src/chunk-walls.cpp') diff --git a/src/chunk-walls.cpp b/src/chunk-walls.cpp index 59e0e915..3e62920e 100644 --- a/src/chunk-walls.cpp +++ b/src/chunk-walls.cpp @@ -45,13 +45,19 @@ using Wall::Frame; template using Vec2_ = VectorTypeFor<2, T>; template using Vec3_ = VectorTypeFor<3, T>; +template +struct minmax_v +{ + VectorTypeFor min, max; +}; + template -constexpr std::array, 4> get_quad(F depth) +constexpr std::array, 4> get_quadʹ(minmax_v bounds, F d) { static_assert(G < Group_::COUNT); - constexpr auto half_tile = Vec2_(TILE_SIZE2*.5f); - constexpr auto X = half_tile.x(), Y = half_tile.y(), Z = F(TILE_SIZE.z()); + const auto x0 = bounds.min.x(), y0 = bounds.min.y(), z0 = bounds.min.z(), + x1 = bounds.max.x(), y1 = bounds.max.y(), z1 = bounds.max.z(); switch (G) { @@ -60,67 +66,76 @@ constexpr std::array, 4> get_quad(F depth) case wall: if (!IsWest) return {{ - { X, -Y, 0 }, - { X, -Y, Z }, - {-X, -Y, 0 }, - {-X, -Y, Z }, + { x1, y0, z0 }, + { x1, y0, z1 }, + { x0, y0, z0 }, + { x0, y0, z1 }, }}; else return {{ - {-X, -Y, 0 }, - {-X, -Y, Z }, - {-X, Y, 0 }, - {-X, Y, Z }, + { x0, y0, z0 }, + { x0, y0, z1 }, + { x0, y1, z0 }, + { x0, y1, z1 }, }}; case side: if (!IsWest) return {{ - { X, -Y - depth, 0 }, - { X, -Y - depth, Z }, - { X, -Y, 0 }, - { X, -Y, Z }, + { x1, y0 - d, z0 }, + { x1, y0 - d, z1 }, + { x1, y0, z0 }, + { x1, y0, z1 }, }}; else return {{ - { -X, Y, 0 }, - { -X, Y, Z }, - { -X - depth, Y, 0 }, - { -X - depth, Y, Z }, + { x0, y1, z0 }, + { x0, y1, z1 }, + { x0 - d, y1, z0 }, + { x0 - d, y1, z1 }, }}; case top: if (!IsWest) return {{ - { -X, -Y - depth, Z }, - { X, -Y - depth, Z }, - { -X, -Y, Z }, - { X, -Y, Z }, + { x0, y0 - d, z1 }, + { x1, y0 - d, z1 }, + { x0, y0, z1 }, + { x1, y0, z1 }, }}; else return {{ - { -X, -Y, Z }, - { -X, Y, Z }, - { -X - depth, -Y, Z }, - { -X - depth, Y, Z }, + { x0, y0, z1 }, + { x0, y1, z1 }, + { x0 - d, y0, z1 }, + { x0 - d, y1, z1 }, }}; case corner: if (!IsWest) return {{ - {-X, -Y, 0 }, - {-X, -Y, Z }, - {-X - depth, -Y, 0 }, - {-X - depth, -Y, Z }, + { x0, y0, z0 }, + { x0, y0, z1 }, + { x0 - d, y0, z0 }, + { x0 - d, y0, z1 }, }}; else return {{ - {-X, -Y - depth, 0 }, - {-X, -Y - depth, Z }, - {-X, -Y, 0 }, - {-X, -Y, Z }, + { x0, y0 - d, z0 }, + { x0, y0 - d, z1 }, + { x0, y0, z0 }, + { x0, y0, z1 }, }}; } std::unreachable(); } +template +constexpr std::array, 4> get_quad(F d) +{ + constexpr auto half_tile = Vec2_(TILE_SIZE2*.5f); + constexpr auto X = half_tile.x(), Y = half_tile.y(), Z = F(TILE_SIZE.z()); + + return get_quadʹ({ { -X, -Y, 0, }, { X, Y, Z, }, }, d); +} + template CutResult::rect get_wall_rect(local_coords tile) { -- cgit v1.2.3