From f2b3972156c41062c1dfa21a1dbe940c092e6ca8 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 21 Nov 2023 18:45:43 +0100 Subject: add texcoords helper function --- src/tile-atlas.cpp | 25 ++++++++++++++++--------- src/tile-atlas.hpp | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index 1867189f..9cfb7998 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -34,20 +34,27 @@ std::array tile_atlas::texcoords_for_id(size_t i) const return texcoords_[i]; } -auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, size_t i) -> texcoords +auto tile_atlas::texcoords_at(Vector2ui pos, Vector2ui size, Vector2ui image_size) -> texcoords { - const auto sz = pixel_size/Vector2ui{tile_count}; - const Vector2ui id = { uint32_t(i % tile_count[0]), uint32_t(i / tile_count[0]) }; - const Vector2 p0(id * sz), p1(sz); - const auto x0 = p0.x()+.5f, x1 = p1.x()-1, y0 = p0.y()+.5f, y1 = p1.y()-1; + const auto x0 = (float)pos.x()+.5f, x1 = x0 + (float)size.x()-1, + y0 = (float)pos.y()+.5f, y1 = y0 + (float)size.y()-1; + const auto W = image_size.x(), H = image_size.y(); return {{ - { (x0+x1) / pixel_size[0], 1 - (y0+y1) / pixel_size[1] }, // bottom right - { (x0+x1) / pixel_size[0], 1 - y0 / pixel_size[1] }, // top right - { x0 / pixel_size[0], 1 - (y0+y1) / pixel_size[1] }, // bottom left - { x0 / pixel_size[0], 1 - y0 / pixel_size[1] }, // top left + { x1 / W, 1 - y1 / H }, // bottom right + { x1 / W, 1 - y0 / H }, // top right + { x0 / W, 1 - y1 / H }, // bottom left + { x0 / W, 1 - y0 / H }, // 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}; + const auto id = Vector2ui{ uint32_t(i % tile_count[0]), uint32_t(i / tile_count[0]) }; + const auto p0 = id * sz; + return texcoords_at(p0, sz, pixel_size); +} + auto tile_atlas::make_texcoords_array(Vector2ui pixel_size, Vector2ub tile_count) -> std::unique_ptr { const size_t N = Vector2ui{tile_count}.product(); diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp index edb18764..26d3fc17 100644 --- a/src/tile-atlas.hpp +++ b/src/tile-atlas.hpp @@ -17,6 +17,7 @@ struct tile_atlas final tile_atlas(StringView path, StringView name, const ImageView2D& img, Vector2ub tile_count, Optional 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); -- cgit v1.2.3