GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Model2D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_2D_MODEL_2D_HPP
5#define GREM_GRAPHICS_2D_MODEL_2D_HPP
6
8
14#include <GREM/core/math.hpp>
19
20#include <utility> // std::move
21
22namespace grem::graphics {
23
24class Device; // Forward declaration, to avoid including Device.hpp.
25class Renderer2D; // Forward declaration, to avoid a circular include of Renderer2D.hpp.
26
30class Model2D {
31public:
41
55
60
65
74
79
86
93
97 using TextureBuffer = UniformBuffer<TextureParameters, "Model2DTexture">;
98
105
110
121 template <typename Constants, typename Outputs, typename... ExtraBuffers>
122 using VertexShaderBase = graphics::VertexShader<Mesh, Constants, Outputs, Camera2D::ParameterBuffer, ExtraBuffers...>;
123
128
141 template <typename Inputs, typename Constants, typename Outputs, typename... ExtraBuffers>
142 using FragmentShaderBase = graphics::FragmentShader<Mesh, Inputs, Constants, Outputs, Camera2D::ParameterBuffer, ExtraBuffers..., TextureBuffer>;
143
148
153
167
172
177
182 .depthBufferMode = DepthBufferMode::NONE,
183 .primitiveType = PrimitiveType::TRIANGLE_STRIP,
184 .faceCullingMode = FaceCullingMode::NONE,
186 };
187
191 Model2D() noexcept = default;
192
201 Model2D(Device& device, Renderer2D& renderer2D, ArrayList<Node> nodes) noexcept
202 : nodes(std::move(nodes)) {
203 GREM_ASSERT(!this->nodes.empty());
204
205 (void)device;
206 (void)renderer2D;
207
208 boundingBox = this->nodes.front().boundingBox;
209 boundingRadius = this->nodes.front().boundingRadius;
210 for (const Node& node : Span{nodes}.subspan(1)) {
211 boundingBox.min = min(boundingBox.min, node.boundingBox.min);
212 boundingBox.max = max(boundingBox.max, node.boundingBox.max);
213 boundingRadius = max(boundingRadius, node.boundingRadius);
214 }
215 }
216
230 Model2D(Device& device, Renderer2D& renderer2D, Span<const Vertex> vertices) {
231 (void)renderer2D;
232
233 Box<2, float> meshBoundingBox{};
234 float meshBoundingRadius = 0.0f;
235 if (!vertices.empty()) {
236 meshBoundingBox = {.min = vertices.front().vertexPosition, .max = vertices.front().vertexPosition};
237 float meshBoundingRadiusSquared = length2(vertices.front().vertexPosition);
238 for (const Vertex& vertex : vertices.subspan(1)) {
239 meshBoundingBox.min = min(meshBoundingBox.min, vertex.vertexPosition);
240 meshBoundingBox.max = max(meshBoundingBox.max, vertex.vertexPosition);
241 meshBoundingRadiusSquared = max(meshBoundingRadiusSquared, length2(vertex.vertexPosition));
242 }
243 meshBoundingRadius = sqrt(meshBoundingRadiusSquared);
244 }
245
246 nodes.push_back(Node{.mesh = Mesh{device, vertices}, .boundingBox = meshBoundingBox, .boundingRadius = meshBoundingRadius});
247 boundingBox = meshBoundingBox;
248 boundingRadius = meshBoundingRadius;
249 }
250
256 [[nodiscard]] Span<const Node> getNodes() const noexcept {
257 return nodes;
258 }
259
266 [[nodiscard]] Box<2, float> getBoundingBox() const noexcept {
267 return boundingBox;
268 }
269
275 [[nodiscard]] float getBoundingRadius() const noexcept {
276 return boundingRadius;
277 }
278
279private:
280 ArrayList<Node> nodes{};
281 Box<2, float> boundingBox{};
282 float boundingRadius = 0.0f;
283};
284
285} // namespace grem::graphics
286
287#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition ArrayList.hpp:27
Definition Span.hpp:146
constexpr reference front() const noexcept
Definition Span.hpp:261
constexpr bool empty() const noexcept
Definition Span.hpp:257
constexpr detail::subspan_type_t< T, EXTENT, Offset, N > subspan() const
Definition Span.hpp:284
UniformBuffer< Parameters, "Camera2DParameters"> ParameterBuffer
Shader buffer for 2D camera parameters.
Definition Camera2D.hpp:125
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Compiled GPU fragment shader stage.
Definition shaders.hpp:1274
Generic abstraction of a GPU vertex array object and its associated buffers.
Definition Mesh.hpp:209
UniformBuffer< TextureParameters, "Model2DTexture"> TextureBuffer
Shader buffer for 2D model texture parameters.
Definition Model2D.hpp:97
graphics::Mesh< Vertex, NoIndex, NoParameters, Instance > Mesh
Type of a single mesh of a 2D model.
Definition Model2D.hpp:59
graphics::VertexShader< Mesh, Constants, Outputs, Camera2D::ParameterBuffer, ExtraBuffers... > VertexShaderBase
Vertex shader for drawing 2D models.
Definition Model2D.hpp:122
static constexpr FragmentShaderConstants DEFAULT_FRAGMENT_SHADER_CONSTANTS
Default 2D model fragment shader constants.
Definition Model2D.hpp:176
Box< 2, float > getBoundingBox() const noexcept
Get the local bounding box of the model.
Definition Model2D.hpp:266
static constexpr VertexShaderConstants DEFAULT_VERTEX_SHADER_CONSTANTS
Default 2D model vertex shader constants.
Definition Model2D.hpp:171
graphics::FragmentShader< Mesh, Inputs, Constants, Outputs, Camera2D::ParameterBuffer, ExtraBuffers..., TextureBuffer > FragmentShaderBase
Fragment shader for drawing 2D models.
Definition Model2D.hpp:142
UniformBuffer< Transformation3DParameters, "Model2DTransformation3D"> Transformation3DBuffer
Shader buffer for 2D model 3D transformation parameters.
Definition Model2D.hpp:109
graphics::ShaderPipeline< Mesh > ShaderPipeline
Shader pipeline for drawing 2D models.
Definition Model2D.hpp:152
static constexpr ShaderPipelineOptions DEFAULT_SHADER_PIPELINE_OPTIONS
Default 2D model graphics pipeline configuration.
Definition Model2D.hpp:181
Span< const Node > getNodes() const noexcept
Get the nodes of the model.
Definition Model2D.hpp:256
float getBoundingRadius() const noexcept
Get the local bounding radius of the model.
Definition Model2D.hpp:275
Model2D(Device &device, Renderer2D &renderer2D, Span< const Vertex > vertices)
Construct a model with a single mesh from a list of vertices.
Definition Model2D.hpp:230
VertexShaderBase< VertexShaderConstants, VertexShaderOutputs > VertexShader
Default vertex shader for drawing 2D models.
Definition Model2D.hpp:127
FragmentShaderBase< VertexShaderOutputs, FragmentShaderConstants, FragmentShaderOutputs > FragmentShader
Default fragment shader type for drawing 2D models.
Definition Model2D.hpp:147
Model2D() noexcept=default
Construct an invalid model that must be reassigned before use.
Persistent system that provides 2D rendering capabilities.
Definition Renderer2D.hpp:46
Linked GPU shader pipeline.
Definition shaders.hpp:1508
GPU memory buffer containing a fixed-size struct of uniform parameters to be read by a shader program...
Definition buffers.hpp:426
Compiled GPU vertex shader stage.
Definition shaders.hpp:1060
Definition buffer_layouts.hpp:20
@ TRIANGLE_STRIP
Each point, except the first two, forms a filled triangle with the previous two points.
Definition shaders.hpp:296
@ NONE
Ignore the depth buffer.
Definition shaders.hpp:59
@ NONE
Ignore facing, don't cull primitives based on their winding order.
Definition shaders.hpp:319
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length2(T a)
Definition math.hpp:1969
vec< 4, float > vec4
Definition math.hpp:440
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sqrt(T a)
Get the square root of a value.
Definition math.hpp:1114
vec< 2, float > vec2
Definition math.hpp:438
vec< 3, float > vec3
Definition math.hpp:439
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
mat< 2, 2, float > mat2
Definition math.hpp:804
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
mat< 4, 4, float > mat4
Definition math.hpp:806
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
Point< N, T > max
Position with the maximum coordinates of the box extents on each coordinate axis.
Definition geometry.hpp:154
Point< N, T > min
Position with the minimum coordinates of the box extents on each coordinate axis.
Definition geometry.hpp:153
static const BlendState ALPHA_BLENDING_PREMULTIPLIED
Blend state to use for pre-multiplied alpha blending using the standard "over" operator.
Definition shaders.hpp:391
Struct of default fragment shader constants.
Definition Model2D.hpp:78
Struct of fields output by the default fragment shader.
Definition Model2D.hpp:83
vec4 outputColor
Final fragment color.
Definition Model2D.hpp:84
Data layout for the attributes of a single instance of a 2D model mesh.
Definition Model2D.hpp:47
vec3 instanceEmissiveColor
Additive emissive color to use when rendering.
Definition Model2D.hpp:53
mat2 instanceTextureBasis
Basis transformation to apply to the texture coordinates before sampling the texture.
Definition Model2D.hpp:51
vec4 instanceTintColor
Multiplicative tint color to use when rendering.
Definition Model2D.hpp:52
vec2 instanceTextureOffset
Offset to apply to the texture coordinates before sampling the texture.
Definition Model2D.hpp:50
vec2 instancePosition
Offset to apply to the vertex positions.
Definition Model2D.hpp:48
mat2 instanceBasis
Basis transformation to apply to the vertex positions.
Definition Model2D.hpp:49
Node of a 2D model.
Definition Model2D.hpp:157
Mesh mesh
Mesh primitive of the node.
Definition Model2D.hpp:159
Box< 2, float > boundingBox
Local bounding box of the node.
Definition Model2D.hpp:162
float boundingRadius
Local bounding radius of the node.
Definition Model2D.hpp:165
Struct of shader parameters representing the texture of a 2D model.
Definition Model2D.hpp:90
sampler2D mainTexture
Texture to use when rendering the model.
Definition Model2D.hpp:91
Struct of shader fields representing the 3D transformation of a 2D model.
Definition Model2D.hpp:102
mat4 transformation3DTransformation
Transformation of the model instance.
Definition Model2D.hpp:103
Struct of default vertex shader constants.
Definition Model2D.hpp:64
Struct of fields output by the default vertex shader.
Definition Model2D.hpp:69
vec3 fragmentEmissiveColor
Emissive color of the vertex.
Definition Model2D.hpp:72
vec2 fragmentTextureCoordinates
Texture coordinates of the vertex.
Definition Model2D.hpp:70
vec4 fragmentTintColor
Tint color of the vertex.
Definition Model2D.hpp:71
Data layout for the attributes of a single vertex of a 2D model mesh.
Definition Model2D.hpp:37
vec2 vertexTextureCoordinates
Texture UV coordinates that map to this vertex.
Definition Model2D.hpp:39
vec2 vertexPosition
Position relative to the model origin.
Definition Model2D.hpp:38
Configuration of a graphics pipeline.
Definition shaders.hpp:428
Reference to a sampler2D Texture that may be used as a parameter value type in a shader-read buffer.
Definition ParameterDescription.hpp:131