guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
rectangle_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/rectangle_render.hpp"
24
25#include "guillaume/components/ring.hpp"
26#include "guillaume/mesh_renderable.hpp"
28
29#include <algorithm>
30#include <cmath>
31#include <memory>
32
33namespace guillaume::systems
34{
35 float RectangleRender::extractAverageRadius(
36 const components::Borders &borders) const
37 {
38 return (borders.getTopLeftRadius() + borders.getTopRightRadius()
39 + borders.getBottomRightRadius()
40 + borders.getBottomLeftRadius())
41 / 4.0f;
42 }
43
44 std::vector<utility::math::Vector2F>
45 RectangleRender::buildAxisAlignedRectVertices(
46 const float halfWidth, const float halfHeight) const
47 {
48 return {
49 utility::math::Vector2F({ -halfWidth, -halfHeight }),
50 utility::math::Vector2F({ halfWidth, -halfHeight }),
51 utility::math::Vector2F({ halfWidth, halfHeight }),
52 utility::math::Vector2F({ -halfWidth, halfHeight }),
53 };
54 }
55
56 void RectangleRender::appendRoundedCornerArc(
57 std::vector<utility::math::Vector2F> &localVertices,
58 const utility::math::Vector2F &arcCenter, const float startAngle,
59 const float endAngle, const float radius, const int arcSegments) const
60 {
61 for (int i = 0; i <= arcSegments; ++i) {
62 const float t =
63 static_cast<float>(i) / static_cast<float>(arcSegments);
64 const float angle = startAngle + (endAngle - startAngle) * t;
65 localVertices.push_back(
66 arcCenter
67 + utility::math::Vector2F(
68 { std::cos(angle) * radius, std::sin(angle) * radius }));
69 }
70 }
71
72 std::vector<utility::math::Vector2F>
73 RectangleRender::buildLocalRoundedRectVertices(
74 const float halfWidth, const float halfHeight, const float radius,
75 int arcSegments, const float epsilon) const
76 {
77 const float pi = std::acos(-1.0f);
78
79 if (arcSegments <= 0) {
80 arcSegments = 1;
81 }
82
83 const float cornerRadius =
84 std::max(0.0f, (std::min)({ radius, halfWidth, halfHeight }));
85
86 if (cornerRadius <= epsilon) {
87 return buildAxisAlignedRectVertices(halfWidth, halfHeight);
88 }
89
90 std::vector<utility::math::Vector2F> localVertices;
91 localVertices.reserve(static_cast<std::size_t>(4 * (arcSegments + 2)));
92
93 const utility::math::Vector2F topLeftCenter(
94 { -halfWidth + cornerRadius, -halfHeight + cornerRadius });
95 const utility::math::Vector2F topRightCenter(
96 { halfWidth - cornerRadius, -halfHeight + cornerRadius });
97 const utility::math::Vector2F bottomRightCenter(
98 { halfWidth - cornerRadius, halfHeight - cornerRadius });
99 const utility::math::Vector2F bottomLeftCenter(
100 { -halfWidth + cornerRadius, halfHeight - cornerRadius });
101
102 appendRoundedCornerArc(localVertices, topRightCenter, -pi / 2.0f, 0.0f,
103 cornerRadius, arcSegments);
104 appendRoundedCornerArc(localVertices, bottomRightCenter, 0.0f,
105 pi / 2.0f, cornerRadius, arcSegments);
106 appendRoundedCornerArc(localVertices, bottomLeftCenter, pi / 2.0f, pi,
107 cornerRadius, arcSegments);
108 appendRoundedCornerArc(localVertices, topLeftCenter, pi,
109 3.0f * pi / 2.0f, cornerRadius, arcSegments);
110
111 return localVertices;
112 }
113
114 std::vector<utility::graphic::PositionF>
115 RectangleRender::buildRoundedRectVertices(
116 const utility::graphic::PositionF &center,
117 const utility::graphic::OrientationF &orientation,
118 const utility::math::Vector2F &scale,
119 const utility::graphic::SizeF &size, float radius, int arcSegments,
120 float epsilon)
121 {
122 const float halfWidth = (size.getWidth() / 2.0f) * std::abs(scale[0]);
123 const float halfHeight = (size.getHeight() / 2.0f) * std::abs(scale[1]);
124 const auto localVertices = buildLocalRoundedRectVertices(
125 halfWidth, halfHeight, radius, arcSegments, epsilon);
126 return shape::transformToWorldVertices(localVertices, center,
127 orientation);
128 }
129
130 RectangleRender::RectangleRender(utility::Engine &engine)
131 : ecs::SystemFiller<components::Transform, components::Bound,
132 components::Color, components::Borders>(
133 ecs::Phase::Render)
134 , _engine(engine)
135 {
136 }
137
139 {
140 clear();
141 }
142
144 {
146 }
147
149 {
150 removeUnused(_engine);
151 }
152
153 void
155 {
156 getLogger().debug() << "Updating RectangleRender system for entity "
157 << entityIdentifier;
158
159 if (!requireComponent<components::Bound>(entityIdentifier)
160 || !requireComponent<components::Transform>(entityIdentifier)
161 || !requireComponent<components::Color>(entityIdentifier)
162 || !requireComponent<components::Borders>(entityIdentifier)) {
163 return;
164 }
165
166 // An entity with a Ring is drawn as that ring by systems::RingRender,
167 // with the same Color: filling its rectangle would cover the ring
168 // (a radio button would be a filled disc).
170 entityIdentifier)) {
171 return;
172 }
173
174 const auto &transformComponent =
175 getComponent<components::Transform>(entityIdentifier);
176 const auto &boundComponent =
177 getComponent<components::Bound>(entityIdentifier);
178 const auto &colorComponent =
179 getComponent<components::Color>(entityIdentifier);
180 const auto &bordersComponent =
181 getComponent<components::Borders>(entityIdentifier);
182
183 const auto &pose = transformComponent.getPose();
184 const auto &color = colorComponent.getColor();
185
186 getLogger().debug()
187 << "Rendering rectangle with pose: " << pose
188 << ", size: " << boundComponent.getWidth() << "x"
189 << boundComponent.getHeight() << ", color: " << color;
190
191 const utility::graphic::PositionF center(
192 pose.getPosition().x + boundComponent.getWidth() / 2.0f,
193 pose.getPosition().y - boundComponent.getHeight() / 2.0f,
194 pose.getPosition().z);
195
196 const auto roundedVertices = buildRoundedRectVertices(
197 center, pose.getOrientation(),
198 utility::math::Vector2F({ 1.0f, 1.0f }), boundComponent,
199 extractAverageRadius(bordersComponent), 16, 0.001f);
200
201 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
202 std::vector<uint32_t> {});
203 shape::buildTriangleFanVertices(mesh, center, roundedVertices, color);
204
205 std::vector<utility::graphic::Mesh> meshes;
206 if (!mesh.getVertices().empty()) {
207 meshes.push_back(mesh);
208 }
209
210 if (bordersComponent.hasStroke()) {
211 const utility::graphic::PositionF strokeCenter(
212 -pose.getOrientation().getForward() * StrokeLift);
213 utility::graphic::Mesh stroke(
214 std::vector<utility::graphic::VertexF> {},
215 std::vector<uint32_t> {});
217 stroke,
218 utility::graphic::PositionF(center[0] + strokeCenter[0],
219 center[1] + strokeCenter[1],
220 center[2] + strokeCenter[2]),
221 pose.getOrientation(), boundComponent.getWidth() / 2.0f,
222 boundComponent.getHeight() / 2.0f,
223 extractAverageRadius(bordersComponent),
224 bordersComponent.getWidth(), 16, bordersComponent.getColor());
225 if (!stroke.getVertices().empty()) {
226 meshes.push_back(stroke);
227 }
228 }
229
230 // Transparent fill and no visible stroke: submit nothing, so the
231 // object is removed at the end of the frame.
232 if (meshes.empty()) {
233 return;
234 }
235
236 auto renderable =
237 std::make_shared<MeshRenderable>(meshes, "mesh_material");
238
239 RenderHandle *handle = find(entityIdentifier);
240 if (handle == nullptr) {
241 size_t objectId = _engine.createObject(renderable);
242 insert(entityIdentifier,
243 RenderHandle { objectId, renderable, true });
244 } else {
245 if (!_engine.updateObject(renderable, handle->objectId)) {
246 handle->objectId = _engine.createObject(renderable);
247 }
248 handle->renderable = renderable;
249 handle->used = true;
250 }
251 }
252
253} // 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
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
Definition system.cpp:53
bool hasComponent(const ecs::Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component, in the bound registry.
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 prepare(void) override
Mark every engine object as not drawn yet. Called once per frame before the entities are visited.
~RectangleRender(void)
Forget the engine objects without removing them from the engine.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the fill and border meshes of one entity and upload them, creating the engine object on the f...
void cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame. Called once per frame after...
RectangleRender(utility::Engine &engine)
Create the system in the Render phase.
static constexpr float StrokeLift
Distance the border is moved towards the viewer, along the forward axis of the entity,...
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 clear(void)
Forget every handle without calling utility::Engine::removeObject. Use it only when the engine object...
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.
std::size_t size(void) const
Count the entities that have an engine object in this map.
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.
void buildRoundedStrokeMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation, float halfWidth, float halfHeight, float radius, float width, int arcSegments, const utility::graphic::Color32Bit &color)
Build the border of a rounded rectangle as a band of constant width.
void buildTriangleFanVertices(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const std::vector< utility::graphic::PositionF > &outline, const utility::graphic::Color32Bit &color)
Fill a closed outline with a fan of triangles around its center.
What a render system remembers about the engine object it created for one entity.
bool used
Whether the entity was drawn this frame.
std::shared_ptr< utility::graphic::Renderable > renderable
Renderable uploaded last for this object.
size_t objectId
Identifier given by the engine.