diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-18 15:20:28 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-18 16:43:40 +0200 |
commit | b7d111e8a669c45d2cee50ff152b47025f62ace8 (patch) | |
tree | 5b0df11b8f11a0a07b6086a909988d2c0b64f0b7 /src | |
parent | 29fdfcd56a57586ecee50485326b916909380f44 (diff) |
precompute texcoords
Diffstat (limited to 'src')
-rw-r--r-- | src/tile-atlas.cpp | 40 | ||||
-rw-r--r-- | src/tile-atlas.hpp | 10 |
2 files changed, 36 insertions, 14 deletions
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index 50f28cd0..0b09b493 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -8,12 +8,11 @@ namespace floormat { tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Vector2ui dims) : - name_{name}, - size_{image.size()}, - dims_{dims} + texcoords_{make_texcoords_array(Vector2ui(image.size()), dims)}, + name_{name}, size_{image.size()}, dims_{dims} { CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0); - CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2ui{}); + CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2ui()); CORRADE_INTERNAL_ASSERT(dims_.product() < 256); tex_.setWrapping(GL::SamplerWrapping::ClampToEdge) .setMagnificationFilter(GL::SamplerFilter::Linear) @@ -24,19 +23,36 @@ tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Ve .setSubImage(0, {}, image); } -std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t id_) const +std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t i) const { - const auto sz = size_/dims_; - ASSERT(id_ < sz.product()); - const Vector2ui id = { (UnsignedInt)id_ % dims_[0], (UnsignedInt)id_ / dims_[0] }; + ASSERT(i < (size_/dims_).product()); + return texcoords_[i]; +} + +auto tile_atlas::make_texcoords(Vector2ui size, Vector2ui dims, std::uint_fast16_t i) -> texcoords +{ + const auto sz = size/dims; + const Vector2ui id = { (UnsignedInt)i % dims[0], (UnsignedInt)i / dims[0] }; const Vector2 p0(id * sz), p1(sz); const auto x0 = p0.x(), x1 = p1.x()-1, y0 = p0.y(), y1 = p1.y()-1; return {{ - { (x0+x1)/size_[0], (y0+y1)/size_[1] }, // bottom right - { (x0+x1)/size_[0], y0/size_[1] }, // top right - { x0/size_[0], (y0+y1)/size_[1] }, // bottom left - { x0/size_[0], y0/size_[1] } // top left + { (x0+x1)/size[0], (y0+y1)/size[1] }, // bottom right + { (x0+x1)/size[0], y0 /size[1] }, // top right + { x0 /size[0], (y0+y1)/size[1] }, // bottom left + { x0 /size[0], y0 /size[1] } // top left }}; } +auto tile_atlas::make_texcoords_array(Vector2ui size, Vector2ui dims) -> std::unique_ptr<const texcoords[]> +{ + const auto sz = size/dims; + const std::uint32_t max = sz.product(); + auto ptr = std::make_unique<std::array<Vector2, 4>[]>(max); + for (std::uint_fast16_t i = 0; i < max; i++) + { + ptr[i] = make_texcoords(size, dims, i); + } + return ptr; +} + } // namespace floormat diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp index 08a090a4..5fb0f899 100644 --- a/src/tile-atlas.hpp +++ b/src/tile-atlas.hpp @@ -3,6 +3,7 @@ #include <Magnum/GL/Texture.h> #include <array> #include <string> +#include <memory> namespace std::filesystem { class path; } @@ -11,20 +12,25 @@ namespace floormat { struct tile_atlas final { using quad = std::array<Vector3, 4>; + using texcoords = std::array<Vector2, 4>; tile_atlas(Containers::StringView name, const ImageView2D& img, Vector2ui dimensions); - std::array<Vector2, 4> texcoords_for_id(std::size_t id) const; + texcoords texcoords_for_id(std::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(std::size_t N); - Vector2ui pixel_size() const { return size_; } + [[maybe_unused]] Vector2ui pixel_size() const { return size_; } Vector2ui num_tiles() const { return dims_; } GL::Texture2D& texture() { return tex_; } Containers::StringView name() const { return name_; } private: + static std::unique_ptr<const texcoords[]> make_texcoords_array(Vector2ui size, Vector2ui dims); + static texcoords make_texcoords(Vector2ui size, Vector2ui dims, std::uint_fast16_t i); + + std::unique_ptr<const texcoords[]> texcoords_; GL::Texture2D tex_; std::string name_; Vector2ui size_, dims_; |