Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_texture_painter.hpp
1#pragma once
2
3#include "structure/graphics/painter/spk_mesh_painter.hpp"
4#include "structure/math/spk_vector2.hpp"
5#include "structure/opengl/spk_sampler_object.hpp"
6#include "structure/opengl/spk_texture_object.hpp"
7#include "structure/opengl/spk_vertex_buffer_object.hpp"
8
9namespace spk
10{
20 {
24 Vector2Int pos;
28 Vector2 uv;
29 };
30
41 class TextureMesh : public IMesh<TextureVertex>
42 {
43 public:
52
63 class Builder
64 {
65 private:
66 std::vector<Polygon> _polygons;
67
68 public:
72 Builder() = default;
73
79 {
80 _polygons.clear();
81 return *this;
82 }
83
91 Builder &insertTriangle(const TextureVertex &p_a, const TextureVertex &p_b, const TextureVertex &p_c)
92 {
93 _polygons.push_back(Polygon::makeTriangle(p_a, p_b, p_c));
94 return *this;
95 }
96
106 Builder &insertQuad(const TextureVertex &p_a, const TextureVertex &p_b, const TextureVertex &p_c, const TextureVertex &p_d, PolygonOrder p_order = PolygonOrder::TriangleFanFromFirst)
107 {
108 _polygons.push_back(Polygon::makeQuad(p_a, p_b, p_c, p_d, p_order));
109 return *this;
110 }
111
121 Builder &insertRectangle(const Vector2Int &p_anchor, const Vector2UInt &p_size, const Vector2 &p_uvAnchor, const Vector2 &p_uvSize, PolygonOrder p_order = PolygonOrder::TriangleFanFromFirst)
122 {
123 const Vector2Int posA{p_anchor.x, p_anchor.y};
124 const Vector2Int posB{p_anchor.x, p_anchor.y + static_cast<int>(p_size.y)};
125 const Vector2Int posC{p_anchor.x + static_cast<int>(p_size.x), p_anchor.y + static_cast<int>(p_size.y)};
126 const Vector2Int posD{p_anchor.x + static_cast<int>(p_size.x), p_anchor.y};
127
128 const Vector2 uvA = p_uvAnchor;
129 const Vector2 uvB{p_uvAnchor.x, p_uvAnchor.y + p_uvSize.y};
130 const Vector2 uvC{p_uvAnchor.x + p_uvSize.x, p_uvAnchor.y + p_uvSize.y};
131 const Vector2 uvD{p_uvAnchor.x + p_uvSize.x, p_uvAnchor.y};
132
133 TextureVertex a{posA, uvA};
134 TextureVertex b{posB, uvB};
135 TextureVertex c{posC, uvC};
136 TextureVertex d{posD, uvD};
137
138 return insertQuad(a, b, c, d, p_order);
139 }
140
148 Builder &insertRectangleFullUV(const Vector2Int &p_anchor, const Vector2UInt &p_size, PolygonOrder p_order = PolygonOrder::TriangleFanFromFirst)
149 {
150 return insertRectangle(p_anchor, p_size, {0.0f, 0.0f}, {1.0f, 1.0f}, p_order);
151 }
152
159 Builder &insertRectangleFullUV(const spk::Extend2D &p_extend, PolygonOrder p_order = PolygonOrder::TriangleFanFromFirst)
160 {
161 return insertRectangle(p_extend.anchor, p_extend.size, {0.0f, 0.0f}, {1.0f, 1.0f}, p_order);
162 }
163
169 {
170 TextureMesh result;
171 for (const auto &poly : _polygons)
172 {
173 result.append(poly);
174 }
175 return result;
176 }
177 };
178
179 protected:
183 void _configureBufferSet() const override
184 {
185 auto &layout = bufferSet()->vbo().layout();
187 layout.addAttribute({0, Attribute::Type::Vector2Int});
188 layout.addAttribute({1, Attribute::Type::Vector2});
189 }
190 };
191
203 class TexturePainter : public MeshPainter<TextureMesh>
204 {
205 private:
206 static Lumina::Pipeline &_texturePipeline();
207 OpenGL::SamplerObject &_textureSampler;
208 OpenGL::UBO &_modelData;
209 float &_layerRef;
210
211 public:
216
221 void setLayer(const float &p_layer);
226 const float &layer() const;
227
232 void setTexture(const OpenGL::TextureObject *p_texture);
237 const OpenGL::TextureObject *texture() const;
238 };
239}
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< TextureVertex > 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
Binds a TextureObject to a uniform sampler location and texture unit.
Definition spk_sampler_object.hpp:18
CPU-staged 2D texture with activation and parameter controls.
Definition spk_texture_object.hpp:25
Layout & layout()
Access layout descriptor.
Definition spk_vertex_buffer_object.cpp:253
Builder()=default
Builds an empty builder.
Builder & insertQuad(const TextureVertex &p_a, const TextureVertex &p_b, const TextureVertex &p_c, const TextureVertex &p_d, PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Inserts a textured quad.
Definition spk_texture_painter.hpp:106
Builder & insertRectangleFullUV(const Vector2Int &p_anchor, const Vector2UInt &p_size, PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Inserts a rectangle using full [0,1] UV range.
Definition spk_texture_painter.hpp:148
Builder & insertRectangle(const Vector2Int &p_anchor, const Vector2UInt &p_size, const Vector2 &p_uvAnchor, const Vector2 &p_uvSize, PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Inserts a textured rectangle with explicit UVs.
Definition spk_texture_painter.hpp:121
Builder & insertTriangle(const TextureVertex &p_a, const TextureVertex &p_b, const TextureVertex &p_c)
Inserts a textured triangle.
Definition spk_texture_painter.hpp:91
Builder & clear()
Clears all stored polygons.
Definition spk_texture_painter.hpp:78
TextureMesh construct()
Builds a TextureMesh from stored polygons.
Definition spk_texture_painter.hpp:168
Builder & insertRectangleFullUV(const spk::Extend2D &p_extend, PolygonOrder p_order=PolygonOrder::TriangleFanFromFirst)
Inserts a rectangle from an extend using full UV range.
Definition spk_texture_painter.hpp:159
Mesh specialization for textured vertices.
Definition spk_texture_painter.hpp:42
Base::Polygon Polygon
Polygon type for textured vertices.
Definition spk_texture_painter.hpp:51
IMesh< TextureVertex > Base
Base mesh type.
Definition spk_texture_painter.hpp:47
void _configureBufferSet() const override
Configures the vertex buffer layout for textured vertices.
Definition spk_texture_painter.hpp:183
void setLayer(const float &p_layer)
Sets the layer value used during rendering.
Definition spk_texture_painter.cpp:23
const OpenGL::TextureObject * texture() const
Returns the current texture.
Definition spk_texture_painter.cpp:39
TexturePainter()
Builds a texture painter with its rendering pipeline.
Definition spk_texture_painter.cpp:14
void setTexture(const OpenGL::TextureObject *p_texture)
Sets the texture used for rendering.
Definition spk_texture_painter.cpp:34
const float & layer() const
Returns the current layer value.
Definition spk_texture_painter.cpp:29
Axis-aligned rectangle defined by an anchor point and size.
Definition spk_extend_2d.hpp:24
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
Vertex containing a position and texture coordinates.
Definition spk_texture_painter.hpp:20
Vector2 uv
Texture coordinates.
Definition spk_texture_painter.hpp:28
Vector2Int pos
Vertex position in integer coordinates.
Definition spk_texture_painter.hpp:24