Refactor gfx::painter methods

This commit is contained in:
Nikita Lisitsa 2020-08-30 21:23:33 +03:00
parent ac505229ae
commit 3d3e6c583b
2 changed files with 21 additions and 8 deletions

View file

@ -4,7 +4,7 @@
#include <psemek/geom/vector.hpp> #include <psemek/geom/vector.hpp>
#include <psemek/geom/point.hpp> #include <psemek/geom/point.hpp>
#include <psemek/geom/matrix.hpp> #include <psemek/geom/matrix.hpp>
#include <psemek/geom/simplex.hpp> #include <psemek/geom/box.hpp>
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -20,7 +20,8 @@ namespace psemek::gfx
~painter(); ~painter();
// 2D // 2D
void quad(geom::point<float, 2> const & center, float width, color const & c); void triangle(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, geom::point<float, 2> const & p2, color const & c);
void rect(geom::box<float, 2> const & box, color const & c);
void circle(geom::point<float, 2> const & center, float radius, color const & c); void circle(geom::point<float, 2> const & center, float radius, color const & c);
void line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c, bool smooth = true); void line(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, float width, color const & c, bool smooth = true);

View file

@ -62,15 +62,27 @@ namespace psemek::gfx
painter::~painter() = default; painter::~painter() = default;
void painter::quad(geom::point<float, 2> const & p, float width, color const & c) void painter::triangle(geom::point<float, 2> const & p0, geom::point<float, 2> const & p1, geom::point<float, 2> const & p2, color const & c)
{ {
std::uint32_t const base = impl().vertices.size(); std::uint32_t const base = impl().vertices.size();
float const r = width / 2.f;
impl().vertices.push_back({{p[0] - r, p[1] - r, 0.f}, c}); impl().vertices.push_back({{p0[0], p0[1], 0.f}, c});
impl().vertices.push_back({{p[0] + r, p[1] - r, 0.f}, c}); impl().vertices.push_back({{p1[0], p1[1], 0.f}, c});
impl().vertices.push_back({{p[0] - r, p[1] + r, 0.f}, c}); impl().vertices.push_back({{p2[0], p2[1], 0.f}, c});
impl().vertices.push_back({{p[0] + r, p[1] + r, 0.f}, c});
impl().indices.push_back(base + 0);
impl().indices.push_back(base + 1);
impl().indices.push_back(base + 2);
}
void painter::rect(geom::box<float, 2> const & box, color const & c)
{
std::uint32_t const base = impl().vertices.size();
impl().vertices.push_back({{box[0].min, box[1].min, 0.f}, c});
impl().vertices.push_back({{box[0].max, box[1].min, 0.f}, c});
impl().vertices.push_back({{box[0].min, box[1].max, 0.f}, c});
impl().vertices.push_back({{box[0].max, box[1].max, 0.f}, c});
impl().indices.push_back(base + 0); impl().indices.push_back(base + 0);
impl().indices.push_back(base + 1); impl().indices.push_back(base + 1);