Store texcoords as float in texture_view

This commit is contained in:
Nikita Lisitsa 2022-05-19 10:49:30 +03:00
parent 77d3e6e3f2
commit f21fa3c2d6
5 changed files with 18 additions and 18 deletions

View file

@ -3,6 +3,8 @@
#include <psemek/gfx/texture.hpp> #include <psemek/gfx/texture.hpp>
#include <psemek/geom/box.hpp> #include <psemek/geom/box.hpp>
#include <cstdint>
namespace psemek::gfx namespace psemek::gfx
{ {
@ -10,19 +12,19 @@ namespace psemek::gfx
struct basic_texture_view struct basic_texture_view
{ {
basic_texture<D, Target> const * texture = nullptr; basic_texture<D, Target> const * texture = nullptr;
geom::box<std::size_t, D> part; geom::box<float, D> part;
basic_texture_view(basic_texture<D, Target> const * texture = nullptr); basic_texture_view(basic_texture<D, Target> const * texture = nullptr);
basic_texture_view(basic_texture<D, Target> const * texture, geom::box<std::size_t, D> const & part); basic_texture_view(basic_texture<D, Target> const * texture, geom::box<float, D> const & part);
geom::vector<std::size_t, D> size() const { return part.dimensions(); } geom::vector<float, D> size() const { return part.dimensions(); }
std::size_t width() const float width() const
{ {
return part[0].length(); return part[0].length();
} }
std::size_t height() const float height() const
{ {
if constexpr (D >= 2) if constexpr (D >= 2)
return part[1].length(); return part[1].length();
@ -30,7 +32,7 @@ namespace psemek::gfx
return 1; return 1;
} }
std::size_t depth() const float depth() const
{ {
if constexpr (D >= 3) if constexpr (D >= 3)
return part[2].length(); return part[2].length();
@ -47,13 +49,13 @@ namespace psemek::gfx
{ {
if (texture) if (texture)
{ {
static const auto zero = geom::point<std::size_t, D>::zero(); static const auto zero = geom::point<float, D>::zero();
part = geom::span(zero, zero + texture->size()); part = geom::span(zero, zero + geom::cast<float>(texture->size()));
} }
} }
template <std::size_t D, GLenum Target> template <std::size_t D, GLenum Target>
basic_texture_view<D, Target>::basic_texture_view(basic_texture<D, Target> const * texture, geom::box<std::size_t, D> const & part) basic_texture_view<D, Target>::basic_texture_view(basic_texture<D, Target> const * texture, geom::box<float, D> const & part)
: texture(texture) : texture(texture)
, part(part) , part(part)
{} {}

View file

@ -97,7 +97,7 @@ namespace psemek::ui
{ {
struct image struct image
{ {
geom::box<std::size_t, 2> texcoords; geom::box<float, 2> texcoords;
geom::box<float, 2> position; geom::box<float, 2> position;
}; };

View file

@ -317,7 +317,7 @@ namespace psemek::ui
image.position = g.position; image.position = g.position;
image.position[0] += (std::round(image.position[0].min) - image.position[0].min); image.position[0] += (std::round(image.position[0].min) - image.position[0].min);
image.position[1] += (std::round(image.position[1].min) - image.position[1].min); image.position[1] += (std::round(image.position[1].min) - image.position[1].min);
image.texcoords = geom::cast<std::size_t>(*tc); image.texcoords = *tc;
} }
} }

View file

@ -364,12 +364,10 @@ void main()
p11 = c + geom::rotate(p11 - c, rotation); p11 = c + geom::rotate(p11 - c, rotation);
} }
auto part = geom::cast<float>(tex.part); batch.vertices.push_back({p00, depth, color, tex.part.corner(0.f, 0.f)});
batch.vertices.push_back({p10, depth, color, tex.part.corner(1.f, 0.f)});
batch.vertices.push_back({p00, depth, color, part.corner(0.f, 0.f)}); batch.vertices.push_back({p01, depth, color, tex.part.corner(0.f, 1.f)});
batch.vertices.push_back({p10, depth, color, part.corner(1.f, 0.f)}); batch.vertices.push_back({p11, depth, color, tex.part.corner(1.f, 1.f)});
batch.vertices.push_back({p01, depth, color, part.corner(0.f, 1.f)});
batch.vertices.push_back({p11, depth, color, part.corner(1.f, 1.f)});
batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2, base + 2, base + 1, base + 3}); batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2, base + 2, base + 1, base + 3});

View file

@ -177,7 +177,7 @@ namespace psemek::ui
if (*st->shadow_offset != geom::vector{0, 0}) if (*st->shadow_offset != geom::vector{0, 0})
p.draw_rect(box + geom::cast<float>(*st->shadow_offset), *st->shadow_color); p.draw_rect(box + geom::cast<float>(*st->shadow_offset), *st->shadow_color);
p.draw_image(box, {image_.get(), geom::cast<std::size_t>(reg)}, color_); p.draw_image(box, {image_.get(), reg}, color_);
} }
} }