GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Instances2D.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_INSTANCES_2D_HPP
5#define GREM_GRAPHICS_2D_INSTANCES_2D_HPP
6
8
14#include <GREM/core/math.hpp>
24
25namespace grem::graphics {
26
27class Device; // Forward declaration, to avoid including Device.hpp.
28class Renderer2D; // Forward declaration, to avoid a circular include of Renderer2D.hpp.
29
41 const Texture* texture = nullptr;
42
49 vec2 position{0.0f, 0.0f};
50
57 mat2 basis{1.0f};
58
66 vec2 textureOffset{0.0f, 0.0f};
67
76
89
99};
100
113 const Texture* texture = nullptr;
114
115 vec2 pointA{0.0f, 0.0f};
116 vec2 pointB{1.0f, 0.0f};
117 vec2 pointC{0.0f, 1.0f};
118
126 vec2 textureOffset{0.0f, 0.0f};
127
136
149
159};
160
173 const Texture* texture = nullptr;
174
181 vec2 position{0.0f, 0.0f};
182
189 mat2 basis{1.0f};
190
198 vec2 textureOffset{0.0f, 0.0f};
199
208
221
231};
232
244 const Texture* texture = nullptr;
245
250 vec2 position{0.0f, 0.0f};
251
256 float angle = 0.0f;
257
261 vec2 size{1.0f, 1.0f};
262
268 vec2 origin{0.0f, 0.0f};
269
279 vec2 textureOffset{0.0f, 0.0f};
280
290 float textureAngle = 0.0f;
291
301 vec2 textureScale{1.0f, 1.0f};
302
315
325};
326
335 vec2 position{0.0f, 0.0f};
336
341 float angle = 0.0f;
342
349 vec2 scale{1.0f, 1.0f};
350
356 vec2 origin{0.0f, 0.0f};
357
367 vec2 textureOffset{0.0f, 0.0f};
368
378 float textureAngle = 0.0f;
379
389 vec2 textureScale{1.0f, 1.0f};
390
400
410};
411
420 vec2 position{0.0f, 0.0f};
421
426 float angle = 0.0f;
427
434 vec2 scale{1.0f, 1.0f};
435
441 vec2 origin{0.0f, 0.0f};
442
452
462};
463
472 vec2 position{0.0f, 0.0f};
473
478 float angle = 0.0f;
479
491 vec2 scale{1.0f, 1.0f};
492
497
503
508};
509
517 uint32_t characterSize = 16;
518
523 vec2 position{0.0f, 0.0f};
524
536 vec2 shapeScale{1.0f, 1.0f};
537
545 vec2 shapeOrigin{0.0f, 0.0f};
546
551 float angle = 0.0f;
552
564 vec2 scale{1.0f, 1.0f};
565
570
576
581};
582
587public:
600 Instances2D(Device& device, Renderer2D& renderer2D)
601 : device(&device)
602 , renderer2D(&renderer2D) {}
603
612 void clear() {
613 instanceBuffer.clear();
614 drawCommands.clear();
615 }
616
628 GREM_API(graphics_2d) void putModelInstance(const Model2D& model, const ModelInstance2D& instance);
629
643 GREM_API(graphics_2d) void putShadedModelInstance(const Model2D::ShaderPipeline& shaderPipeline, const Model2D& model, const ModelInstance2D& instance);
644
657 GREM_API(graphics_2d) void putTriangleInstance(const TriangleInstance2D& instance);
658
672 GREM_API(graphics_2d) void putShadedTriangleInstance(const Model2D::ShaderPipeline& shaderPipeline, const TriangleInstance2D& instance);
673
688 GREM_API(graphics_2d) void putQuadInstance(const QuadInstance2D& instance);
689
705 GREM_API(graphics_2d) void putShadedQuadInstance(const Model2D::ShaderPipeline& shaderPipeline, const QuadInstance2D& instance);
706
721 GREM_API(graphics_2d) void putRectangleInstance(const RectangleInstance2D& instance);
722
739 GREM_API(graphics_2d) void putShadedRectangleInstance(const Model2D::ShaderPipeline& shaderPipeline, const RectangleInstance2D& instance);
740
756 GREM_API(graphics_2d) void putImageInstance(const Texture& texture, const ImageInstance2D& instance = {});
757
774 GREM_API(graphics_2d) void putShadedImageInstance(const Model2D::ShaderPipeline& shaderPipeline, const Texture& texture, const ImageInstance2D& instance = {});
775
793 GREM_API(graphics_2d) void putSpriteInstance(const SpriteAtlas& spriteAtlas, SpriteID spriteID, const SpriteInstance2D& instance);
794
813 GREM_API(graphics_2d)
814 void putShadedSpriteInstance(const Model2D::ShaderPipeline& shaderPipeline, const SpriteAtlas& spriteAtlas, SpriteID spriteID, const SpriteInstance2D& instance);
815
829 GREM_API(graphics_2d) void putTextInstance(const Text2D& text, const TextInstance2D& instance);
830
845 GREM_API(graphics_2d) void putShadedTextInstance(const Model2D::ShaderPipeline& shaderPipeline, const Text2D& text, const TextInstance2D& instance);
846
869 GREM_API(graphics_2d) void putTextStringInstance(Font2D& font, UTF8StringView string, const TextStringInstance2D& instance);
870
894 GREM_API(graphics_2d)
895 void putShadedTextStringInstance(const Model2D::ShaderPipeline& shaderPipeline, Font2D& font, UTF8StringView string, const TextStringInstance2D& instance);
896
920 GREM_API(graphics_2d) void putTextStringInstance(Font2D& font, StringView string, const TextStringInstance2D& instance);
921
946 GREM_API(graphics_2d) void putShadedTextStringInstance(const Model2D::ShaderPipeline& shaderPipeline, Font2D& font, StringView string, const TextStringInstance2D& instance);
947
948private:
949 friend Renderer2D;
950
951 struct DrawCommand {
955 uint32_t instanceOffset;
956 };
957
958 GREM_API(graphics_2d)
959 void putShadedModelInstanceImplementation(SharedPointer<ShaderPipelineImplementation> shaderPipelineHandle, const Model2D& model, const ModelInstance2D& instance);
960
961 GREM_API(graphics_2d)
962 void putShadedTextInstanceImplementation(SharedPointer<ShaderPipelineImplementation> shaderPipelineHandle, const Text2D& text, const TextInstance2D& instance);
963
964 GREM_API(graphics_2d)
965 void pushDrawCommand(SharedPointer<ShaderPipelineImplementation> shaderPipelineHandle, SharedPointer<MeshImplementation> meshHandle,
966 SharedPointer<TextureImplementation> textureHandle, uint32_t instanceOffset);
967
968 Device* device;
969 Renderer2D* renderer2D;
970 InstanceBuffer<Model2D::Instance> instanceBuffer{*device};
971 ArrayList<DrawCommand> drawCommands{};
972};
973
978
1014
1015} // namespace grem::graphics
1016
1017#endif
Definition ArrayList.hpp:27
Floating-point linear RGBA color in the Rec.
Definition Color.hpp:60
static const Color INVISIBLE
Definition Color.hpp:70
static const Color WHITE
Definition Color.hpp:215
Definition SharedPointer.hpp:65
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
Typeface describing an assortment of character glyphs that may be rendered on-demand into an expandin...
Definition Font2D.hpp:66
Resource handle with shared ownership of a GPU memory buffer with copy-on-write value semantics conta...
Definition buffers.hpp:825
Batch of instances to be drawn in a Renderer2D frame.
Definition Instances2D.hpp:586
void putShadedQuadInstance(const Model2D::ShaderPipeline &shaderPipeline, const QuadInstance2D &instance)
Add a quad instance to be rendered using a specific shader pipeline.
void putRectangleInstance(const RectangleInstance2D &instance)
Add a rectangle instance to be rendered using a plain 2D model shader.
void putShadedModelInstance(const Model2D::ShaderPipeline &shaderPipeline, const Model2D &model, const ModelInstance2D &instance)
Add a 2D model instance to be rendered using a specific shader pipeline.
void putTextStringInstance(Font2D &font, UTF8StringView string, const TextStringInstance2D &instance)
Add a 2D text string instance to be rendered using a plain 2D text shader.
void putShadedTextStringInstance(const Model2D::ShaderPipeline &shaderPipeline, Font2D &font, UTF8StringView string, const TextStringInstance2D &instance)
Add a 2D text string instance to be rendered using a specific shader pipeline.
void putShadedSpriteInstance(const Model2D::ShaderPipeline &shaderPipeline, const SpriteAtlas &spriteAtlas, SpriteID spriteID, const SpriteInstance2D &instance)
Add a sprite instance to be rendered using a specific shader pipeline.
void clear()
Clear the batch of all instances.
Definition Instances2D.hpp:612
void putShadedRectangleInstance(const Model2D::ShaderPipeline &shaderPipeline, const RectangleInstance2D &instance)
Add a rectangle instance to be rendered using a specific shader pipeline.
void putSpriteInstance(const SpriteAtlas &spriteAtlas, SpriteID spriteID, const SpriteInstance2D &instance)
Add a sprite instance to be rendered using a plain 2D model shader.
void putShadedTextInstance(const Model2D::ShaderPipeline &shaderPipeline, const Text2D &text, const TextInstance2D &instance)
Add a text instance to be rendered using a specific shader pipeline.
void putModelInstance(const Model2D &model, const ModelInstance2D &instance)
Add a 2D model instance to be rendered using a plain 2D model shader.
void putShadedImageInstance(const Model2D::ShaderPipeline &shaderPipeline, const Texture &texture, const ImageInstance2D &instance={})
Add an image instance to be rendered using a specific shader pipeline.
Instances2D(Device &device, Renderer2D &renderer2D)
Construct a batch of instances.
Definition Instances2D.hpp:600
void putTextInstance(const Text2D &text, const TextInstance2D &instance)
Add a 2D text instance to be rendered using a plain 2D text shader.
void putImageInstance(const Texture &texture, const ImageInstance2D &instance={})
Add an image instance to be rendered using a plain 2D model shader.
void putShadedTriangleInstance(const Model2D::ShaderPipeline &shaderPipeline, const TriangleInstance2D &instance)
Add a triangle instance to be rendered using a specific shader pipeline.
void putTriangleInstance(const TriangleInstance2D &instance)
Add a triangle instance to be rendered using a plain 2D model shader.
void putQuadInstance(const QuadInstance2D &instance)
Add a quad instance to be rendered using a plain 2D model shader.
Container for a set of 2D triangle strip meshes stored on the GPU.
Definition Model2D.hpp:30
graphics::ShaderPipeline< Mesh > ShaderPipeline
Shader pipeline for drawing 2D models.
Definition Model2D.hpp:152
Persistent system that provides 2D rendering capabilities.
Definition Renderer2D.hpp:46
Linked GPU shader pipeline.
Definition shaders.hpp:1508
Expandable texture atlas for packing 2D images into a spritesheet to enable batch rendering.
Definition SpriteAtlas.hpp:173
Facility for shaping text, according to a Font2D, into renderable glyphs.
Definition Text2D.hpp:206
Storage for multidimensional data, such as 2D images, on the GPU, combined with a sampler configurati...
Definition Texture.hpp:700
Definition buffer_layouts.hpp:20
vec< 2, float > vec2
Definition math.hpp:438
GREM_ALWAYS_INLINE void lock(Lockable1 &lock1, Lockable2 &lock2, LockableN &... lockN)
Definition synchronization.hpp:1268
mat< 2, 2, float > mat2
Definition math.hpp:804
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
StringViewBase< char8_t, std::char_traits< char8_t > > UTF8StringView
Definition StringView.hpp:17
Configuration of a 2D image instance to draw.
Definition Instances2D.hpp:330
float angle
Angle, in radians, to rotate the image counter-clockwise by, around its ImageInstance2D::origin.
Definition Instances2D.hpp:341
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition Instances2D.hpp:367
vec2 position
Position, in world coordinates, to draw the image at, with respect to its ImageInstance2D::origin.
Definition Instances2D.hpp:335
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:409
vec2 origin
Offset, in local vertex coordinates, specifying the origin relative to the bottom left of the image.
Definition Instances2D.hpp:356
vec2 scale
Coefficients to scale the size of the image by.
Definition Instances2D.hpp:349
float textureAngle
Angle, in radians, to rotate the texture coordinates by before sampling the texture.
Definition Instances2D.hpp:378
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling the texture.
Definition Instances2D.hpp:389
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:399
Filter parameters that determine which instances in a 2D batch to draw.
Definition Instances2D.hpp:977
Instances2DView(const Instances2D &instances, const Instances2DFilter &filter={})
Construct an instance batch view.
Definition Instances2D.hpp:998
Instances2DView(const Instances2D &instances, const Model2D::ShaderPipeline &shaderPipelineOverride, const Instances2DFilter &filter={})
Construct an instance batch view with a shader override.
Definition Instances2D.hpp:1009
Instances2DFilter filter
Filter parameters that determine which instances to draw.
Definition Instances2D.hpp:990
SharedPointer< ShaderPipelineImplementation > shaderPipelineOverrideHandle
Shader pipeline override to use, or nullptr to not use a shader override.
Definition Instances2D.hpp:987
const Instances2D * instances
Non-owning read-only pointer to the batch of instances.
Definition Instances2D.hpp:984
Configuration of a Model2D instance to draw.
Definition Instances2D.hpp:33
vec2 position
Offset to apply to every vertex position of the model, in world space.
Definition Instances2D.hpp:49
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:98
mat2 basis
Basis to scale/rotate/shear the vertex positions of the model by.
Definition Instances2D.hpp:57
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:88
const Texture * texture
Non-owning pointer to a texture to apply to the model, or nullptr to use a fully white texture.
Definition Instances2D.hpp:41
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition Instances2D.hpp:66
mat2 textureBasis
Texture coordinate basis to scale/rotate/shear the texture coordinates by before sampling the texture...
Definition Instances2D.hpp:75
Configuration of an arbitrarily transformed 2D quadrilateral instance, optionally textured,...
Definition Instances2D.hpp:165
mat2 basis
Basis to scale/rotate/shear the vertex positions of the quad by.
Definition Instances2D.hpp:189
vec2 position
Offset to apply to every vertex position of the quad, in world space.
Definition Instances2D.hpp:181
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:220
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:230
const Texture * texture
Non-owning pointer to a texture to apply to the quad, or nullptr to use a fully white texture.
Definition Instances2D.hpp:173
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition Instances2D.hpp:198
mat2 textureBasis
Texture coordinate basis to scale/rotate/shear the texture coordinates by before sampling the texture...
Definition Instances2D.hpp:207
Configuration of a 2D rectangle instance, optionally textured, to draw.
Definition Instances2D.hpp:236
float textureAngle
Angle, in radians, to rotate the texture coordinates by before sampling the texture.
Definition Instances2D.hpp:290
vec2 position
Position, in world coordinates, to draw the rectangle at, with respect to its RectangleInstance2D::or...
Definition Instances2D.hpp:250
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:324
const Texture * texture
Non-owning pointer to a texture to apply to the rectangle, or nullptr to use a fully white texture.
Definition Instances2D.hpp:244
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:314
vec2 textureScale
Coefficients to scale the texture coordinates by before sampling the texture.
Definition Instances2D.hpp:301
vec2 origin
Offset, in local vertex coordinates, specifying the origin relative to the bottom left of the rectang...
Definition Instances2D.hpp:268
vec2 size
Size of the rectangle, in world coordinates.
Definition Instances2D.hpp:261
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition Instances2D.hpp:279
float angle
Angle, in radians, to rotate the rectangle counter-clockwise by, around its RectangleInstance2D::orig...
Definition Instances2D.hpp:256
Identifier for a specific image in a SpriteAtlas.
Definition SpriteAtlas.hpp:124
Configuration of a 2D sprite instance from a SpriteAtlas to draw.
Definition Instances2D.hpp:415
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:451
vec2 position
Position, in world coordinates, to draw the sprite at, with respect to its SpriteInstance2D::origin.
Definition Instances2D.hpp:420
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:461
vec2 origin
Offset, in local vertex coordinates, specifying the origin relative to the bottom left of the sprite.
Definition Instances2D.hpp:441
vec2 scale
Coefficients to scale the size of the sprite by.
Definition Instances2D.hpp:434
float angle
Angle, in radians, to rotate the sprite counter-clockwise by, around its SpriteInstance2D::origin.
Definition Instances2D.hpp:426
Text alignment mode.
Definition Text2D.hpp:53
static const TextAlign NONE
No alignment.
Definition Text2D.hpp:54
Configuration of a 2D instance of Text2D shaped from a Font2D to draw.
Definition Instances2D.hpp:467
Color color
Base text color.
Definition Instances2D.hpp:507
vec2 scale
Coefficients to scale the size of the text by.
Definition Instances2D.hpp:491
float angle
Angle, in radians, to rotate the text counter-clockwise by, around its TextInstance2D::origin.
Definition Instances2D.hpp:478
TextAlign alignment
Alignment mode of the text.
Definition Instances2D.hpp:496
vec2 alignmentOffset
Additional offset to apply to the text after applying the specified alignment.
Definition Instances2D.hpp:502
vec2 position
Starting position, in world coordinates, to draw the text at.
Definition Instances2D.hpp:472
Configuration of a 2D instance of a string of text to draw using a Font2D.
Definition Instances2D.hpp:513
float angle
Angle, in radians, to rotate the text counter-clockwise by, around its TextStringInstance2D::origin.
Definition Instances2D.hpp:551
vec2 shapeOrigin
Offset, in normalized coordinates, specifying the origin relative to the baseline of the first line o...
Definition Instances2D.hpp:545
vec2 alignmentOffset
Additional offset to apply to the text after applying the specified alignment.
Definition Instances2D.hpp:575
TextAlign alignment
Alignment mode of the text.
Definition Instances2D.hpp:569
vec2 position
Starting position, in world coordinates, to draw the text at.
Definition Instances2D.hpp:523
uint32_t characterSize
Character size to shape the glyphs at.
Definition Instances2D.hpp:517
Color color
Base text color.
Definition Instances2D.hpp:580
vec2 scale
Coefficients to scale the size of the text by.
Definition Instances2D.hpp:564
vec2 shapeScale
Scaling to apply to the size of the shaped glyphs.
Definition Instances2D.hpp:536
Configuration of an arbitrarily transformed 2D triangle instance, optionally textured,...
Definition Instances2D.hpp:105
const Texture * texture
Non-owning pointer to a texture to apply to the triangle, or nullptr to use a fully white texture.
Definition Instances2D.hpp:113
Color color
Tint color to use in the shader.
Definition Instances2D.hpp:148
vec2 textureOffset
Offset, in texture coordinates, to apply to the texture coordinates before sampling the texture.
Definition Instances2D.hpp:126
vec2 pointC
Position, in world coordinates, of the third vertex of the triangle to draw.
Definition Instances2D.hpp:117
mat2 textureBasis
Texture coordinate basis to scale/rotate/shear the texture coordinates by before sampling the texture...
Definition Instances2D.hpp:135
vec2 pointB
Position, in world coordinates, of the second vertex of the triangle to draw.
Definition Instances2D.hpp:116
Color emissiveColor
Emissive color to use in the shader.
Definition Instances2D.hpp:158
vec2 pointA
Position, in world coordinates, of the first vertex of the triangle to draw.
Definition Instances2D.hpp:115