guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
elevation_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/elevation_render.hpp"
24
26
27#include <algorithm>
28#include <cmath>
29#include <cstdint>
30#include <memory>
31#include <vector>
32
33#include "guillaume/mesh_renderable.hpp"
34
35namespace guillaume::systems
36{
37 namespace
38 {
39 utility::graphic::PositionF rotateByOrientation(
40 const utility::graphic::PositionF &position,
41 const utility::graphic::OrientationF &orientation)
42 {
43 const auto normalized = orientation.normalized();
44 const float qx = normalized.x;
45 const float qy = normalized.y;
46 const float qz = normalized.z;
47 const float qw = normalized.w;
48
49 const float positionX = position[0];
50 const float positionY = position[1];
51 const float positionZ = position[2];
52
53 const float crossX = (qy * positionZ) - (qz * positionY);
54 const float crossY = (qz * positionX) - (qx * positionZ);
55 const float crossZ = (qx * positionY) - (qy * positionX);
56
57 const float tX = 2.0f * crossX;
58 const float tY = 2.0f * crossY;
59 const float tZ = 2.0f * crossZ;
60
61 const float crossTX = (qy * tZ) - (qz * tY);
62 const float crossTY = (qz * tX) - (qx * tZ);
63 const float crossTZ = (qx * tY) - (qy * tX);
64
65 return utility::graphic::PositionF(positionX + (qw * tX) + crossTX,
66 positionY + (qw * tY) + crossTY,
67 positionZ + (qw * tZ) + crossTZ);
68 }
69
70 std::vector<utility::math::Vector2F>
71 buildRoundedRectOutline(float halfWidth, float halfHeight,
72 float radius, int arcSegments)
73 {
74 const float pi = std::acos(-1.0f);
75
76 if (arcSegments <= 0) {
77 arcSegments = 1;
78 }
79
80 const float cornerRadius =
81 std::max(0.0f, (std::min)({ radius, halfWidth, halfHeight }));
82
83 std::vector<utility::math::Vector2F> localVertices;
84
85 if (cornerRadius <= 0.0f) {
86 localVertices = {
87 utility::math::Vector2F({ -halfWidth, -halfHeight }),
88 utility::math::Vector2F({ halfWidth, -halfHeight }),
89 utility::math::Vector2F({ halfWidth, halfHeight }),
90 utility::math::Vector2F({ -halfWidth, halfHeight }),
91 };
92 return localVertices;
93 }
94
95 auto appendArc = [&localVertices, arcSegments](
96 const utility::math::Vector2F &center,
97 float startAngle, float endAngle,
98 float arcRadius) {
99 for (int i = 0; i <= arcSegments; ++i) {
100 const float t =
101 static_cast<float>(i) / static_cast<float>(arcSegments);
102 const float angle =
103 startAngle + (endAngle - startAngle) * t;
104 localVertices.push_back(
105 center
106 + utility::math::Vector2F(
107 { std::cos(angle) * arcRadius,
108 std::sin(angle) * arcRadius }));
109 }
110 };
111
112 appendArc(utility::math::Vector2F({ halfWidth - cornerRadius,
113 -halfHeight + cornerRadius }),
114 -pi / 2.0f, 0.0f, cornerRadius);
115 appendArc(utility::math::Vector2F({ halfWidth - cornerRadius,
116 halfHeight - cornerRadius }),
117 0.0f, pi / 2.0f, cornerRadius);
118 appendArc(utility::math::Vector2F({ -halfWidth + cornerRadius,
119 halfHeight - cornerRadius }),
120 pi / 2.0f, pi, cornerRadius);
121 appendArc(utility::math::Vector2F({ -halfWidth + cornerRadius,
122 -halfHeight + cornerRadius }),
123 pi, 3.0f * pi / 2.0f, cornerRadius);
124
125 return localVertices;
126 }
127
128 void appendLayer(utility::graphic::Mesh &mesh,
129 const utility::graphic::PositionF &center,
130 const utility::graphic::OrientationF &orientation,
131 float halfWidth, float halfHeight, float radius,
132 const utility::graphic::Color32Bit &color)
133 {
134 if (shape::isInvisible(color)) {
135 return;
136 }
137 const auto outline =
138 buildRoundedRectOutline(halfWidth, halfHeight, radius, 8);
139
140 const uint32_t base =
141 static_cast<uint32_t>(mesh.getVertices().size());
142
143 utility::graphic::VertexF centerVertex;
144 centerVertex.setPosition(center);
145 centerVertex.setColor(shape::opaqueColor(color));
146 mesh.addVertex(centerVertex);
147
148 for (const auto &localVertex: outline) {
149 const utility::graphic::PositionF localPosition(
150 localVertex[0], localVertex[1], 0.0f);
151 const auto rotated =
152 rotateByOrientation(localPosition, orientation);
153
154 utility::graphic::VertexF vertex;
155 vertex.setPosition(utility::graphic::PositionF(
156 center[0] + rotated[0], center[1] + rotated[1],
157 center[2] + rotated[2]));
158 vertex.setColor(shape::opaqueColor(color));
159 mesh.addVertex(vertex);
160 }
161
162 for (std::size_t i = 1; i < outline.size(); ++i) {
163 mesh.addIndex(base);
164 mesh.addIndex(base + static_cast<uint32_t>(i));
165 mesh.addIndex(base + static_cast<uint32_t>(i + 1));
166 }
167 mesh.addIndex(base);
168 mesh.addIndex(base + static_cast<uint32_t>(outline.size()));
169 mesh.addIndex(base + 1u);
170 }
171 } // namespace
172
173 ElevationRender::ElevationRender(utility::Engine &engine)
174 : ecs::SystemFiller<components::Transform, components::Bound,
175 components::Elevation>(ecs::Phase::Render)
176 , _engine(engine)
177 {
178 }
179
181 {
183 }
184
186 {
187 removeUnused(_engine);
188 }
189
190 void
192 {
193 if (!requireComponent<components::Bound>(entityIdentifier)
194 || !requireComponent<components::Transform>(entityIdentifier)
195 || !requireComponent<components::Elevation>(entityIdentifier)) {
196 return;
197 }
198
199 const auto &pose =
200 getComponent<components::Transform>(entityIdentifier).getPose();
201 const auto &bound = getComponent<components::Bound>(entityIdentifier);
202 const auto &elevation =
203 getComponent<components::Elevation>(entityIdentifier);
204
205 // A flat surface paints no shadow; leaving the handle unused lets
206 // cleanup() remove a shadow drawn before the level dropped to 0.
207 if (!elevation.hasShadow()) {
208 return;
209 }
210
211 const utility::graphic::PositionF center(
212 pose.getPosition().x + bound.getWidth() / 2.0f,
213 pose.getPosition().y - bound.getHeight() / 2.0f,
214 pose.getPosition().z);
215
216 const float halfWidth = bound.getWidth() / 2.0f;
217 const float halfHeight = bound.getHeight() / 2.0f;
218
219 const int layers = std::max(1, elevation.getLayers());
220 const auto &orientation = pose.getOrientation();
221 const auto &color = elevation.getColor();
222
223 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
224 std::vector<uint32_t> {});
225
226 for (int i = 0; i < layers; ++i) {
227 const float t =
228 static_cast<float>(i + 1) / static_cast<float>(layers);
229 const float expansion =
230 elevation.getSpread() * elevation.getLevel() * t;
231 const float alpha =
232 static_cast<float>(color.getAlpha()) * (1.0f - t);
233
234 appendLayer(mesh, center, orientation, halfWidth + expansion,
235 halfHeight + expansion,
236 (std::min)(halfWidth, halfHeight) + expansion,
237 color.withAlpha(static_cast<std::uint8_t>(alpha)));
238 }
239
240 // A transparent shape adds no geometry: submit nothing, so the
241 // object is removed at the end of the frame.
242 if (mesh.getVertices().empty()) {
243 return;
244 }
245
246 auto renderable = std::make_shared<MeshRenderable>(
247 std::vector<utility::graphic::Mesh> { mesh }, "mesh_material");
248
249 RenderHandle *handle = find(entityIdentifier);
250 if (handle == nullptr) {
251 insert(entityIdentifier,
252 RenderHandle { _engine.createObject(renderable), renderable,
253 true });
254 } else {
255 if (!_engine.updateObject(renderable, handle->objectId)) {
256 handle->objectId = _engine.createObject(renderable);
257 }
258 handle->renderable = renderable;
259 handle->used = true;
260 }
261 }
262
263} // 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...
ElevationRender(utility::Engine &engine)
Create the system in the Render phase.
void prepare(void) override
Mark every shadow as not drawn yet. Called once per frame before the entities are visited.
void cleanup(void) override
Remove the shadows of the entities that were not drawn this frame, including those whose elevation dr...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the shadow mesh of one entity and upload it, creating the engine object on the first frame.
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.
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.
bool isInvisible(const utility::graphic::Color32Bit &color)
Whether a color draws nothing.
utility::graphic::Color32Bit opaqueColor(const utility::graphic::Color32Bit &color)
Turn a translucent color into the opaque color it looks like.
What a render system remembers about the engine object it created for one entity.