Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_color_painter.hpp
1#pragma once
2
3#include "structure/graphics/painter/spk_mesh_painter.hpp"
4#include "structure/graphics/spk_color.hpp"
5#include "structure/math/spk_vector2.hpp"
6#include "structure/opengl/spk_vertex_buffer_object.hpp"
7
8namespace spk
9{
19 {
23 Vector2Int pos;
28 };
29
40 class ColorMesh : public IMesh<ColorVertex>
41 {
42 public:
51
62 class Builder
63 {
64 private:
65 std::vector<Polygon> _polygons;
66
67 public:
71 Builder() = default;
72
78 {
79 _polygons.clear();
80 return *this;
81 }
82
91 Builder &insertTriangle(const Vector2Int &p_a, const Vector2Int &p_b, const Vector2Int &p_c, const Color &p_color)
92 {
93 _polygons.push_back(Polygon::makeTriangle(ColorVertex{p_a, p_color}, ColorVertex{p_b, p_color}, ColorVertex{p_c, p_color}));
94 return *this;
95 }
96
107 Builder &insertQuad(const Vector2Int &p_a, const Vector2Int &p_b, const Vector2Int &p_c, const Vector2Int &p_d, const Color &p_color, PolygonOrder p_order = PolygonOrder::TriangleFanFromFirst)
108 {
109 _polygons.push_back(Polygon::makeQuad(ColorVertex{p_a, p_color}, ColorVertex{p_b, p_color}, ColorVertex{p_c, p_color}, ColorVertex{p_d, p_color}, p_order));
110 return *this;
111 }
112
120 Builder &insertRectangle(const Vector2Int &p_anchor, const Vector2UInt &p_size, const Color &p_color)
121 {
122 const Vector2Int a = p_anchor;
123 const Vector2Int b = {p_anchor.x, p_anchor.y + static_cast<int>(p_size.y)};
124 const Vector2Int c = {p_anchor.x + static_cast<int>(p_size.x), p_anchor.y + static_cast<int>(p_size.y)};
125 const Vector2Int d = {p_anchor.x + static_cast<int>(p_size.x), p_anchor.y};
126
127 return insertQuad(a, b, c, d, p_color, PolygonOrder::TriangleFanFromFirst);
128 }
129
135 {
136 ColorMesh result;
137 for (const auto &poly : _polygons)
138 {
139 result.append(poly);
140 }
141 return result;
142 }
143 };
144
145 protected:
149 void _configureBufferSet() const override
150 {
151 auto &layout = bufferSet()->vbo().layout();
153 layout.addAttribute({0, Attribute::Type::Vector2Int});
154 layout.addAttribute({1, Attribute::Type::Color});
155 }
156 };
157
168 class ColorPainter : public MeshPainter<ColorMesh>
169 {
170 private:
171 static Lumina::Pipeline &_colorPipeline();
172 OpenGL::UBO &_modelData;
173 float &_layerRef;
174
175 public:
179 ColorPainter();
180
185 void setLayer(const float &p_layer);
190 const float &layer() const;
191 };
192}
Builder & insertQuad(const Vector2Int &p_a, const Vector2Int &p_b, const Vector2Int &p_c, const Vector2Int &p_d, const Color &p_color, PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Inserts a colored quad.
Definition spk_color_painter.hpp:107
ColorMesh construct()
Builds a ColorMesh from stored polygons.
Definition spk_color_painter.hpp:134
Builder()=default
Builds an empty builder.
Builder & insertRectangle(const Vector2Int &p_anchor, const Vector2UInt &p_size, const Color &p_color)
Inserts an axis-aligned rectangle.
Definition spk_color_painter.hpp:120
Builder & insertTriangle(const Vector2Int &p_a, const Vector2Int &p_b, const Vector2Int &p_c, const Color &p_color)
Inserts a colored triangle.
Definition spk_color_painter.hpp:91
Builder & clear()
Clears all stored polygons.
Definition spk_color_painter.hpp:77
Mesh specialization for colored vertices.
Definition spk_color_painter.hpp:41
void _configureBufferSet() const override
Configures the vertex buffer layout for colored vertices.
Definition spk_color_painter.hpp:149
IMesh< ColorVertex > Base
Base mesh type.
Definition spk_color_painter.hpp:46
Base::Polygon Polygon
Polygon type for colored vertices.
Definition spk_color_painter.hpp:50
const float & layer() const
Returns the current layer value.
Definition spk_color_painter.cpp:28
ColorPainter()
Builds a color painter with its rendering pipeline.
Definition spk_color_painter.cpp:14
void setLayer(const float &p_layer)
Sets the layer value used during rendering.
Definition spk_color_painter.cpp:22
const std::shared_ptr< spk::OpenGL::BufferSetObject > & bufferSet() const
Definition spk_mesh.hpp:338
IMesh()
Definition spk_mesh.hpp:170
void append(const Polygon &p_shape)
Appends a polygon and notifies subscribers.
Definition spk_mesh.hpp:270
IPolygon< ColorVertex > Polygon
Definition spk_mesh.hpp:34
Loads shader artifacts and orchestrates draw calls with strongly typed constants and attributes.
Definition spk_pipeline.hpp:38
MeshPainter(Lumina::Pipeline &p_pipeline)
Definition spk_mesh_painter.hpp:41
VertexBufferObject & vbo()
Access mutable VBO.
Definition spk_buffer_set_object.cpp:76
Layout & layout()
Access layout descriptor.
Definition spk_vertex_buffer_object.cpp:253
RGBA color utility storing normalized floating components.
Definition spk_color.hpp:18
Vertex containing a position and color.
Definition spk_color_painter.hpp:19
Vector2Int pos
Vertex position in integer coordinates.
Definition spk_color_painter.hpp:23
Color color
Vertex color value.
Definition spk_color_painter.hpp:27
static IPolygon< TVertex > makeTriangle(const TVertex &p_a, const TVertex &p_b, const TVertex &p_c)
Creates a triangle polygon.
Definition spk_polygon.hpp:46
static IPolygon< TVertex > makeQuad(const TVertex &p_a, const TVertex &p_b, const TVertex &p_c, const TVertex &p_d, const PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Creates a quad polygon.
Definition spk_polygon.hpp:60
TType x
X component.
Definition spk_vector2.hpp:44
TType y
Y component.
Definition spk_vector2.hpp:48
Describes a vertex attribute entry in the layout.
Definition spk_vertex_buffer_object.hpp:38