diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk-render.cpp | 9 | ||||
-rw-r--r-- | src/chunk-scenery.cpp | 6 | ||||
-rw-r--r-- | src/chunk.cpp | 3 | ||||
-rw-r--r-- | src/quads.cpp | 60 | ||||
-rw-r--r-- | src/quads.hpp | 17 | ||||
-rw-r--r-- | src/tile-atlas.cpp | 17 | ||||
-rw-r--r-- | src/tile-atlas.hpp | 46 |
7 files changed, 92 insertions, 66 deletions
diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index b4129489..173a8f8b 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -1,5 +1,6 @@ #include "chunk.hpp" #include "tile-atlas.hpp" +#include "quads.hpp" #include "shaders/shader.hpp" #include "compat/defs.hpp" #include <algorithm> @@ -8,12 +9,14 @@ namespace floormat { +using namespace floormat::Quads; + template<size_t N = 1> static auto make_index_array(size_t max) { std::array<std::array<UnsignedShort, 6>, N*TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init) for (auto i = 0uz; i < max; i++) - array[i] = tile_atlas::indices(i); + array[i] = quad_indexes(i); return array; } @@ -56,7 +59,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple const uint8_t i = _ground->ground_indexes[k]; const auto& atlas = _ground->_ground_atlases[i]; const local_coords pos{i}; - const auto quad = atlas->floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2); + const auto quad = floor_quad(Vector3(pos) * TILE_SIZE, TILE_SIZE2); const auto texcoords = atlas->texcoords_for_id(_ground->_ground_variants[i]); const float depth = tile_shader::depth_value(pos, tile_shader::ground_depth_offset + hack_offset); auto& v = vertexes[k]; @@ -89,7 +92,7 @@ GL::Mesh chunk::make_wall_mesh(size_t count) const auto& variant = _walls->_wall_variants[i]; const local_coords pos{i / 2u}; const auto center = Vector3(pos) * TILE_SIZE; - const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); + const auto quad = i & 1 ? wall_quad_W(center, TILE_SIZE) : wall_quad_N(center, TILE_SIZE); const float depth = tile_shader::depth_value(pos, tile_shader::wall_depth_offset); const auto texcoords = atlas->texcoords_for_id(variant); auto& v = vertexes[k]; diff --git a/src/chunk-scenery.cpp b/src/chunk-scenery.cpp index 946f835a..d167168e 100644 --- a/src/chunk-scenery.cpp +++ b/src/chunk-scenery.cpp @@ -2,13 +2,15 @@ #include "shaders/shader.hpp" #include "object.hpp" #include "anim-atlas.hpp" -#include "tile-atlas.hpp" +#include "quads.hpp" #include <bit> #include <Corrade/Containers/ArrayViewStl.h> #include <Magnum/GL/Buffer.h> namespace floormat { +using namespace floormat::Quads; + auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple { Array<object_draw_order> array; @@ -164,7 +166,7 @@ auto chunk::ensure_scenery_mesh(scenery_scratch_buffers buffers) noexcept -> sce for (auto j = 0uz; j < 4; j++) scenery_vertexes[i][j] = { quad[j], texcoords[j], depth }; - scenery_indexes[i] = tile_atlas::indices(i); + scenery_indexes[i] = quad_indexes(i); i++; } diff --git a/src/chunk.cpp b/src/chunk.cpp index 52321aa1..0b7c05b0 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -1,6 +1,5 @@ #include "chunk.hpp" -#include "src/tile-atlas.hpp" -#include "anim-atlas.hpp" +#include "object.hpp" #include "tile-iterator.hpp" #include <algorithm> #include <Magnum/GL/Context.h> diff --git a/src/quads.cpp b/src/quads.cpp new file mode 100644 index 00000000..a02654b0 --- /dev/null +++ b/src/quads.cpp @@ -0,0 +1,60 @@ +#include "quads.hpp" + +namespace floormat::Quads { + +std::array<UnsignedShort, 6> quad_indexes(size_t N) +{ + using u16 = UnsignedShort; + return { /* 3--1 1 */ + (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */ + (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */ + }; /* 2 2--0 */ +} + +quad floor_quad(const Vector3 center, const Vector2 size) +{ + float x = size[0]*.5f, y = size[1]*.5f; + return {{ + { x + center[0], -y + center[1], center[2]}, + { x + center[0], y + center[1], center[2]}, + {-x + center[0], -y + center[1], center[2]}, + {-x + center[0], y + center[1], center[2]}, + }}; +} + +quad wall_quad_W(const Vector3 center, const Vector3 size) +{ + float x = size[0]*.5f, y = size[1]*.5f, z = size[2]; + return {{ + {-x + center[0], y + center[1], z + center[2] }, + {-x + center[0], y + center[1], center[2] }, + {-x + center[0], -y + center[1], z + center[2] }, + {-x + center[0], -y + center[1], center[2] }, + }}; +} + +quad wall_quad_N(const Vector3 center, const Vector3 size) +{ + float x = size[0]*.5f, y = size[1]*.5f, z = size[2]; + return {{ + { x + center[0], -y + center[1], z + center[2] }, + { x + center[0], -y + center[1], center[2] }, + {-x + center[0], -y + center[1], z + center[2] }, + {-x + center[0], -y + center[1], center[2] }, + }}; +} + +texcoords texcoords_at(Vector2ui pos_, Vector2ui size_, Vector2ui image_size_) +{ + auto pos = Vector2(pos_), size = Vector2(size_), image_size = Vector2(image_size_); + auto offset = pos + Vector2(.5f), end = offset + size - Vector2(1); + auto x0 = offset / image_size, x1 = end / image_size; + return {{ + { x1.x(), 1.f - x1.y() }, // bottom right + { x1.x(), 1.f - x0.y() }, // top right + { x0.x(), 1.f - x1.y() }, // bottom left + { x0.x(), 1.f - x0.y() }, // top left + }}; +} + +} // namespace floormat::Quads diff --git a/src/quads.hpp b/src/quads.hpp new file mode 100644 index 00000000..57492f35 --- /dev/null +++ b/src/quads.hpp @@ -0,0 +1,17 @@ +#pragma once +#include <array> +#include <Magnum/Math/Vector2.h> +#include <Magnum/Math/Vector3.h> + +namespace floormat::Quads { + +using quad = std::array<Vector3, 4>; +using texcoords = std::array<Vector2, 4>; + +quad floor_quad(Vector3 center, Vector2 size); +quad wall_quad_N(Vector3 center, Vector3 size); +quad wall_quad_W(Vector3 center, Vector3 size); +std::array<UnsignedShort, 6> quad_indexes(size_t N); +texcoords texcoords_at(Vector2ui pos, Vector2ui size, Vector2ui image_size); + +} // namespace floormat::Quads diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index b16de6e8..1596247f 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -1,5 +1,5 @@ - #include "tile-atlas.hpp" +#include "quads.hpp" #include "compat/assert.hpp" #include "tile-image.hpp" #include "compat/exception.hpp" @@ -10,6 +10,8 @@ namespace floormat { +using namespace floormat::Quads; + tile_atlas::tile_atlas(StringView path, StringView name, const ImageView2D& image, Vector2ub tile_count, Optional<enum pass_mode> p) : texcoords_{make_texcoords_array(Vector2ui(image.size()), tile_count)}, path_{path}, name_{name}, size_{image.size()}, dims_{tile_count}, passability{std::move(p)} @@ -34,19 +36,6 @@ std::array<Vector2, 4> tile_atlas::texcoords_for_id(size_t i) const return texcoords_[i]; } -auto tile_atlas::texcoords_at(Vector2ui pos_, Vector2ui size_, Vector2ui image_size_) -> texcoords -{ - auto pos = Vector2(pos_), size = Vector2(size_), image_size = Vector2(image_size_); - auto offset = pos + Vector2(.5f), end = offset + size - Vector2(1); - auto x0 = offset / image_size, x1 = end / image_size; - return {{ - { x1.x(), 1.f - x1.y() }, // bottom right - { x1.x(), 1.f - x0.y() }, // top right - { x0.x(), 1.f - x1.y() }, // bottom left - { x0.x(), 1.f - x0.y() }, // top left - }}; -} - auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, size_t i) -> texcoords { const auto sz = pixel_size/Vector2ui{tile_count}; diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp index 26d3fc17..205da68f 100644 --- a/src/tile-atlas.hpp +++ b/src/tile-atlas.hpp @@ -17,12 +17,8 @@ struct tile_atlas final tile_atlas(StringView path, StringView name, const ImageView2D& img, Vector2ub tile_count, Optional<enum pass_mode> pass_mode); - static texcoords texcoords_at(Vector2ui pos, Vector2ui size, Vector2ui image_size); texcoords texcoords_for_id(size_t id) const; - static constexpr quad floor_quad(Vector3 center, Vector2 size); - static constexpr quad wall_quad_N(Vector3 center, Vector3 size); - static constexpr quad wall_quad_W(Vector3 center, Vector3 size); - static constexpr std::array<UnsignedShort, 6> indices(size_t N); + [[maybe_unused]] Vector2ui pixel_size() const { return size_; } size_t num_tiles() const; Vector2ub num_tiles2() const { return dims_; } @@ -44,46 +40,6 @@ private: Optional<enum pass_mode> passability; }; -constexpr std::array<UnsignedShort, 6> tile_atlas::indices(size_t N) -{ - using u16 = UnsignedShort; - return { /* 3--1 1 */ - (u16)(0+N*4), (u16)(1+N*4), (u16)(2+N*4), /* | / /| */ - (u16)(2+N*4), (u16)(1+N*4), (u16)(3+N*4), /* |/ / | */ - }; /* 2 2--0 */ -} - -constexpr tile_atlas::quad tile_atlas::floor_quad(const Vector3 center, const Vector2 size) -{ - float x = size[0]*.5f, y = size[1]*.5f; - return {{ - { x + center[0], -y + center[1], center[2]}, - { x + center[0], y + center[1], center[2]}, - {-x + center[0], -y + center[1], center[2]}, - {-x + center[0], y + center[1], center[2]}, - }}; -} -constexpr tile_atlas::quad tile_atlas::wall_quad_W(const Vector3 center, const Vector3 size) -{ - float x = size[0]*.5f, y = size[1]*.5f, z = size[2]; - return {{ - {-x + center[0], y + center[1], z + center[2] }, - {-x + center[0], y + center[1], center[2] }, - {-x + center[0], -y + center[1], z + center[2] }, - {-x + center[0], -y + center[1], center[2] }, - }}; -} - -constexpr tile_atlas::quad tile_atlas::wall_quad_N(const Vector3 center, const Vector3 size) -{ - float x = size[0]*.5f, y = size[1]*.5f, z = size[2]; - return {{ - { x + center[0], -y + center[1], z + center[2] }, - { x + center[0], -y + center[1], center[2] }, - {-x + center[0], -y + center[1], z + center[2] }, - {-x + center[0], -y + center[1], center[2] }, - }}; -} } // namespace floormat |