guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
scrim_render.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include "guillaume/systems/scrim_render.hpp"
24
25#include <memory>
26#include <vector>
27
28#include "guillaume/mesh_renderable.hpp"
30
31namespace guillaume::systems
32{
33 ScrimRender::ScrimRender(utility::Engine &engine)
34 : ecs::SystemFiller<components::Transform, components::Bound,
35 components::Scrim>(ecs::Phase::Render)
36 , _engine(engine)
37 {
38 }
39
41 {
43 }
44
46 {
47 removeUnused(_engine);
48 }
49
50 void ScrimRender::update(const ecs::Entity::Identifier &entityIdentifier)
51 {
52 if (!requireComponent<components::Bound>(entityIdentifier)
53 || !requireComponent<components::Transform>(entityIdentifier)
54 || !requireComponent<components::Scrim>(entityIdentifier)) {
55 return;
56 }
57
58 const auto &pose =
59 getComponent<components::Transform>(entityIdentifier).getPose();
60 const auto &bound = getComponent<components::Bound>(entityIdentifier);
61 const auto &color =
62 getComponent<components::Scrim>(entityIdentifier).getColor();
63
64 const float x0 = pose.getPosition().x;
65 const float y0 = pose.getPosition().y;
66 const float z = pose.getPosition().z;
67
68 // The bound's position is its top-left corner: derive the center by
69 // going half the width right and half the height down (y is up),
70 // then rotate the local quad around it. This
71 // matches the other shape renderers (rectangle, ellipse, ring, ...).
72 const float halfWidth = bound.getWidth() / 2.0f;
73 const float halfHeight = bound.getHeight() / 2.0f;
74
75 const utility::graphic::PositionF center(x0 + halfWidth,
76 y0 - halfHeight, z);
77
78 const std::vector<utility::math::Vector2F> localVertices {
79 { -halfWidth, -halfHeight },
80 { halfWidth, -halfHeight },
81 { halfWidth, halfHeight },
82 { -halfWidth, halfHeight },
83 };
84
85 const auto worldVertices = shape::transformToWorldVertices(
86 localVertices, center, pose.getOrientation());
87
88 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
89 std::vector<uint32_t> {});
90
91 for (const auto &worldVertex: worldVertices) {
92 mesh.addVertex(shape::createVertex(worldVertex, color));
93 }
94
95 mesh.addIndex(0);
96 mesh.addIndex(1);
97 mesh.addIndex(2);
98 mesh.addIndex(0);
99 mesh.addIndex(2);
100 mesh.addIndex(3);
101
102 // A transparent shape adds no geometry: submit nothing, so the
103 // object is removed at the end of the frame.
104 if (mesh.getVertices().empty()) {
105 return;
106 }
107
108 auto renderable = std::make_shared<MeshRenderable>(
109 std::vector<utility::graphic::Mesh> { mesh }, "mesh_material");
110
111 RenderHandle *handle = find(entityIdentifier);
112 if (handle == nullptr) {
113 insert(entityIdentifier,
114 RenderHandle { _engine.createObject(renderable), renderable,
115 true });
116 } else {
117 if (!_engine.updateObject(renderable, handle->objectId)) {
118 handle->objectId = _engine.createObject(renderable);
119 }
120 handle->renderable = renderable;
121 handle->used = true;
122 }
123 }
124
125} // namespace guillaume::systems
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
void insert(const ecs::Entity::Identifier &identifier, RenderHandle handle)
Store the handle of an entity, replacing the one it had.
void markAllUnused(void)
Clear the used flag of every handle. Call it from the system's prepare(), before any entity is drawn.
void removeUnused(utility::Engine &engine)
Remove from the engine, then from the map, every handle whose used flag is still false....
RenderHandle * find(const ecs::Entity::Identifier &identifier)
Look up the handle of an entity.
ScrimRender(utility::Engine &engine)
Create the system in the Render phase.
void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the quad of one scrim and upload it, creating the engine object on the first frame.
void cleanup(void) override
Remove the scrims of the entities that were not drawn this frame. Called once per frame after the ent...
void prepare(void) override
Mark every scrim as not drawn yet. Called once per frame before the entities are visited.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.
Mesh builders shared by the shape render systems.
utility::graphic::VertexF createVertex(const utility::graphic::PositionF &position, const utility::graphic::Color32Bit &color)
Make a mesh vertex from a position and a color.
std::vector< utility::graphic::PositionF > transformToWorldVertices(const std::vector< utility::math::Vector2F > &localVertices, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation)
Move 2D points of a shape's plane into world space.
What a render system remembers about the engine object it created for one entity.