Cogs.Core
SpriteRenderSystem.h
1#pragma once
2
3#include "Scene/RayPick.h"
4#include "Systems/ComponentSystem.h"
5
6#include "Components/Core/SpriteRenderComponent.h"
7
8#include "Resources/Resources.h"
9#include "Resources/MaterialOptions.h"
10#include "Resources/VertexFormats.h"
11
12#include "Renderer/RenderList.h"
13
14#include "Foundation/Geometry/BoundingBox.hpp"
15
16#include <span>
17
18
19namespace Cogs::Core
20{
21 using SpriteBatchIndex = size_t;
22 const SpriteBatchIndex NoBatchIndex = static_cast<SpriteBatchIndex>(-1);
23
24 using SpriteIndex = size_t;
25 const SpriteIndex NoSpriteIndex = static_cast<SpriteIndex>(-1);
26
28 {
29 SpriteBatchIndex batchIndex = NoBatchIndex;
30 SpriteIndex spriteIndex = NoSpriteIndex;
31
32 glm::vec3 position;
33 glm::vec2 offset;
34 glm::vec2 size;
35
36 Geometry::BoundingBox boundingBox;
37 };
38
40 {
41 MaterialHandle material;
42 VariableKey colorKey = NoProperty;
43 VariableKey alwaysOnTopKey = NoProperty;
44 VariableKey textureKey = NoProperty;
45
46 std::vector<MaterialInstanceHandle> freeInstances;
47 std::vector<MaterialInstanceHandle> frameInstances;
48 };
49
50 using SpriteMaterialKey = size_t;
51 const SpriteMaterialKey NoSpriteMaterial = static_cast<SpriteMaterialKey>(-1);
52 const SpriteMaterialKey DefaultSpriteMaterial = 0;
53
55 {
56 SpriteBatchIndex index = NoBatchIndex;
59 BlendMode blendMode = BlendMode::Blend;
60 glm::mat4 transform = glm::mat4(1.0f);
61 glm::vec3 worldPosition = glm::vec3(0, 0, 0);
62 Geometry::BoundingBox bbox;
63 PositionMode positionMode = PositionMode::World;
64 SizeMode sizeMode = SizeMode::Pixels;
66 size_t start = 0;
67 size_t count = 0;
68 float depth = -1;
69 RenderItemFlags flags = RenderItemFlags::Sprite;
70 bool expanded = false;
71 };
72
74 {
75 glm::vec4 position;
76 glm::vec4 parameters;
77 glm::vec4 texCoords;
78
79 static VertexFormatHandle getVertexFormat()
80 {
81 return VertexFormats::Pos4fNorm4fTex4f;
82 }
83 };
84
85 class SpritePicker;
86
87 const size_t kMinBatchSize = 256 / sizeof(SpriteVertex);
88
89 class COGSCORE_DLL_API SpriteRenderSystem : public ComponentSystemWithDataPool<SpriteRenderComponent, SpriteRenderData>
90 {
92
93 public:
94 SpriteRenderSystem(Memory::Allocator * allocator, SizeType capacity);
95
96 void initialize(Context * context) override;
97 void preUpdate(Context * context) override;
98 void postUpdate(Context * context) override;
99 ComponentHandle createComponent() override;
100 void destroyComponent(ComponentHandle component) override;
101
102 SpriteMaterialKey registerSpriteMaterial(MaterialHandle material);
103
104 SpriteBatch * begin(SpriteMaterialKey material, const StringView & permutation, TextureHandle texture, glm::vec4 color, bool alwaysOnTop = false);
105
106 SpriteBatch * begin(SpriteMaterialKey material,
107 const StringView & postfix,
108 PositionMode positionMode,
109 SizeMode sizeMode,
110 TextureHandle texture,
111 glm::vec4 color,
112 bool alwaysOnTop = false);
113
114 SpriteBatch * begin(SpriteMaterialKey material, TextureHandle texture, glm::vec4 color);
115 SpriteBatch * begin(TextureHandle texture, glm::vec4 color);
116
117 void addSprite(SpriteRenderComponent * spriteRenderer, glm::vec3 position, glm::vec2 size, glm::vec4 texCoords);
118 void addSprite(SpriteRenderComponent * spriteRenderer, glm::vec3 position, glm::vec2 offset, glm::vec2 size, glm::vec4 texCoords);
119
120 const SpriteBatch * getSpriteBatch(SpriteBatchIndex index) const;
121 std::span<const SpriteBatch> getSpriteBatches() const;
122
123 static StringView getPermutation(PositionMode positionMode, SizeMode sizeMode);
124
125 private:
126 void updateBatchMesh();
127
128 std::vector<SpriteBatch> spriteBatches;
129
130 MaterialHandle spriteMaterial;
131 std::vector<SpriteMaterial> materials;
132
133 SpriteBatch * currentBatch = nullptr;
134
135 std::vector<MeshHandle> frameMeshes;
136 std::vector<MeshHandle> freeMeshes;
137
138 bool expandSpriteGeometry = true;
139
140 SpritePicker* picker = nullptr;
141 };
142
143
145 {
146 public:
147 explicit SpritePicker(SpriteRenderSystem* spriteSystem);
148
149 bool pickCamera(Context* context,
150 const CameraComponent& camera,
151 const glm::vec2& normPosition,
152 float /*rayLength*/,
153 float /*radius*/,
154 PickingFlags pickingFlags,
155 PicksReturned returnFlag,
156 const RayPicking::RayPickFilter& filter,
157 std::vector<RayPicking::RayPickHit>& hits) override;
158
159 SpriteRenderSystem* spriteSystem = nullptr;
160 };
161}
Component system with parallel data per component stored in a pool similar to how the components them...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Interface for modules implementing custom picking.
Definition: RayPick.h:140
bool pickCamera(Context *context, const CameraComponent &camera, const glm::vec2 &normPosition, float, float, PickingFlags pickingFlags, PicksReturned returnFlag, const RayPicking::RayPickFilter &filter, std::vector< RayPicking::RayPickHit > &hits) override
Do a ray pick from a normalized screen space position in the camera direction and return all hits.
Base allocator implementation.
Definition: Allocator.h:30
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
PositionMode
Positioning mode.
@ World
Position given in world space coordinates.
PicksReturned
  * Options for returning picking hits.
Definition: PickingFlags.h:40
PickingFlags
Options for COGS picking.
Definition: PickingFlags.h:12
BlendMode
Defines blending modes for rendering.
@ Blend
Render with regular alpha blending.
SizeMode
Sizing mode.
@ Pixels
Size given in screen pixels.
uint16_t VariableKey
Used to lookup material properties.
Definition: Resources.h:46
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
Lod data holding the LoD state of the entity this render component belongs to.
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.