UI painter: add draw_subimage & don't restrict to integer texcoords

This commit is contained in:
Nikita Lisitsa 2021-03-04 10:02:46 +03:00
parent 576bc8b91a
commit 486a8b24aa
3 changed files with 27 additions and 10 deletions

View file

@ -13,6 +13,7 @@ namespace psemek::ui
virtual void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0;
virtual void draw_glyph(font const & f, char32_t c, geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0;
virtual void draw_image(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0;
virtual void draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0;
virtual ~painter() {}
};

View file

@ -18,6 +18,7 @@ namespace psemek::ui
void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) override;
void draw_glyph(font const & f, char32_t c, geom::box<float, 2> const & rect, gfx::color_rgba const & color) override;
void draw_image(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color) override;
void draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) override;
void render(geom::matrix<float, 4, 4> const & transform);

View file

@ -139,19 +139,19 @@ void main()
return true;
}
struct textured_vertex
struct bitmap_text_vertex
{
geom::point<float, 2> position;
std::uint32_t depth;
gfx::color_rgba color;
geom::vector<std::uint16_t, 2> texcoords;
};
static_assert(sizeof(textured_vertex) == 20);
static_assert(sizeof(bitmap_text_vertex) == 20);
struct bitmap_text_batch
{
gfx::texture_2d const * atlas = nullptr;
std::vector<textured_vertex> vertices{};
std::vector<bitmap_text_vertex> vertices{};
std::vector<std::uint32_t> indices{};
};
@ -160,6 +160,15 @@ void main()
return b1.atlas == b2.atlas;
}
struct textured_vertex
{
geom::point<float, 2> position;
std::uint32_t depth;
gfx::color_rgba color;
geom::point<float, 2> texcoords;
};
static_assert(sizeof(textured_vertex) == 24);
struct textured_batch
{
gfx::texture_2d const * texture = nullptr;
@ -216,7 +225,7 @@ void main()
colored_mesh.setup<geom::point<float, 2>, std::uint32_t, gfx::normalized<gfx::color_rgba>>();
bitmap_text_mesh.setup<geom::point<float, 2>, std::uint32_t, gfx::normalized<gfx::color_rgba>, geom::vector<std::uint16_t, 2>>();
textured_mesh.setup<geom::point<float, 2>, std::uint32_t, gfx::normalized<gfx::color_rgba>, geom::vector<std::uint16_t, 2>>();
textured_mesh.setup<geom::point<float, 2>, std::uint32_t, gfx::normalized<gfx::color_rgba>, geom::vector<float, 2>>();
}
painter_impl::painter_impl()
@ -270,18 +279,24 @@ void main()
}
void painter_impl::draw_image(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color)
{
geom::box<float, 2> part;
part[0] = {0.f, tex.width()};
part[1] = {0.f, tex.height()};
draw_subimage(rect, tex, part, color);
}
void painter_impl::draw_subimage(geom::box<float, 2> const & rect, gfx::texture_2d const & tex, geom::box<float, 2> const & part, gfx::color_rgba const & color)
{
auto & batch = impl().batch<textured_batch>(textured_batch{&tex});
std::uint32_t const depth = impl().depth++;
std::uint32_t const base = batch.vertices.size();
auto const size = tex.size();
batch.vertices.push_back({rect.corner(0.f, 0.f), depth, color, {0, 0}});
batch.vertices.push_back({rect.corner(1.f, 0.f), depth, color, {size[0], 0}});
batch.vertices.push_back({rect.corner(0.f, 1.f), depth, color, {0, size[1]}});
batch.vertices.push_back({rect.corner(1.f, 1.f), depth, color, {size[0], size[1]}});
batch.vertices.push_back({rect.corner(0.f, 0.f), depth, color, part.corner(0.f, 0.f)});
batch.vertices.push_back({rect.corner(1.f, 0.f), depth, color, part.corner(1.f, 0.f)});
batch.vertices.push_back({rect.corner(0.f, 1.f), depth, color, part.corner(0.f, 1.f)});
batch.vertices.push_back({rect.corner(1.f, 1.f), 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});
}