Add ui::painter::draw_triangle with per-vertex colors

This commit is contained in:
Nikita Lisitsa 2022-05-25 22:53:20 +03:00
parent fd8bec44ab
commit ebdd90c520
3 changed files with 12 additions and 6 deletions

View file

@ -33,7 +33,13 @@ namespace psemek::ui
struct painter struct painter
{ {
virtual void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0; virtual void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) = 0;
virtual void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color) = 0;
virtual void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color)
{
draw_triangle(tri, color, color, color);
}
virtual void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) = 0;
using color_mode = detail::color_mode; using color_mode = detail::color_mode;
using image_options = detail::image_options; using image_options = detail::image_options;

View file

@ -16,7 +16,7 @@ namespace psemek::ui
~painter_impl(); ~painter_impl();
void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) override; void draw_rect(geom::box<float, 2> const & rect, gfx::color_rgba const & color) override;
void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color) override; void draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) override;
void draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, image_options const & opts) override; void draw_image(geom::box<float, 2> const & rect, gfx::texture_view_2d const & tex, image_options const & opts) override;

View file

@ -245,16 +245,16 @@ void main()
impl().draw_bbox |= rect; impl().draw_bbox |= rect;
} }
void painter_impl::draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & color) void painter_impl::draw_triangle(geom::triangle<geom::point<float, 2>> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2)
{ {
auto & batch = impl().batch<colored_batch>({}); auto & batch = impl().batch<colored_batch>({});
std::uint32_t const depth = impl().depth++; std::uint32_t const depth = impl().depth++;
std::uint32_t const base = batch.vertices.size(); std::uint32_t const base = batch.vertices.size();
batch.vertices.push_back({tri[0], depth, color}); batch.vertices.push_back({tri[0], depth, c0});
batch.vertices.push_back({tri[1], depth, color}); batch.vertices.push_back({tri[1], depth, c1});
batch.vertices.push_back({tri[2], depth, color}); batch.vertices.push_back({tri[2], depth, c2});
batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2}); batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2});