guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
ellipse_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/ellipse_render.hpp"
24#include "guillaume/mesh_renderable.hpp"
26
27#include <algorithm>
28#include <cmath>
29#include <memory>
30
31namespace guillaume::systems
32{
33 std::vector<utility::math::Vector2F>
34 EllipseRender::buildLocalEllipseVertices(const float halfWidth,
35 const float halfHeight,
36 int segments) const
37 {
38 const float pi = std::acos(-1.0f);
39
40 if (segments <= 0) {
41 segments = 1;
42 }
43
44 std::vector<utility::math::Vector2F> localVertices;
45 localVertices.reserve(static_cast<std::size_t>(segments + 1));
46
47 // Parametric ellipse: x = a * cos(t), y = b * sin(t)
48 for (int i = 0; i <= segments; ++i) {
49 const float t =
50 (static_cast<float>(i) / static_cast<float>(segments)) * 2.0f
51 * pi;
52 const float x = halfWidth * std::cos(t);
53 const float y = halfHeight * std::sin(t);
54 localVertices.push_back(utility::math::Vector2F({ x, y }));
55 }
56
57 return localVertices;
58 }
59
60 std::vector<utility::graphic::PositionF>
61 EllipseRender::buildEllipseVertices(
62 const utility::graphic::PositionF &center,
63 const utility::graphic::OrientationF &orientation,
64 const utility::math::Vector2F &scale,
65 const utility::graphic::SizeF &size, int segments)
66 {
67 const float halfWidth = (size.getWidth() / 2.0f) * std::abs(scale[0]);
68 const float halfHeight = (size.getHeight() / 2.0f) * std::abs(scale[1]);
69 const auto localVertices =
70 buildLocalEllipseVertices(halfWidth, halfHeight, segments);
71 return shape::transformToWorldVertices(localVertices, center,
72 orientation);
73 }
74
75 EllipseRender::EllipseRender(utility::Engine &engine)
76 : ecs::SystemFiller<components::Transform, components::Bound,
77 components::Color, components::Ellipse>(
78 ecs::Phase::Render)
79 , _engine(engine)
80 {
81 }
82
84 {
85 clear();
86 }
87
89 {
91 }
92
94 {
95 removeUnused(_engine);
96 }
97
98 void EllipseRender::update(const ecs::Entity::Identifier &entityIdentifier)
99 {
100 getLogger().debug()
101 << "Updating EllipseRender system for entity " << entityIdentifier;
102
103 if (!requireComponent<components::Bound>(entityIdentifier)
104 || !requireComponent<components::Transform>(entityIdentifier)
105 || !requireComponent<components::Color>(entityIdentifier)
106 || !requireComponent<components::Ellipse>(entityIdentifier)) {
107 return;
108 }
109
110 const auto &transformComponent =
111 getComponent<components::Transform>(entityIdentifier);
112 const auto &boundComponent =
113 getComponent<components::Bound>(entityIdentifier);
114 const auto &colorComponent =
115 getComponent<components::Color>(entityIdentifier);
116 const auto &ellipseComponent =
117 getComponent<components::Ellipse>(entityIdentifier);
118
119 const auto pose = transformComponent.getPose();
120 const auto color = colorComponent.getColor();
121 const auto segments = ellipseComponent.getSegments();
122
123 getLogger().debug()
124 << "Rendering ellipse with pose: " << pose
125 << ", size: " << boundComponent.getWidth() << "x"
126 << boundComponent.getHeight() << ", segments: " << segments
127 << ", color: " << color;
128
129 const utility::graphic::PositionF center(
130 pose.getPosition().x + boundComponent.getWidth() / 2.0f,
131 pose.getPosition().y - boundComponent.getHeight() / 2.0f,
132 pose.getPosition().z);
133
134 const auto ellipseVertices = buildEllipseVertices(
135 center, pose.getOrientation(),
136 utility::math::Vector2F({ 1.0f, 1.0f }), boundComponent, segments);
137
138 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
139 std::vector<uint32_t> {});
140 shape::buildTriangleFanVertices(mesh, center, ellipseVertices, color);
141
142 // A transparent shape adds no geometry: submit nothing, so the
143 // object is removed at the end of the frame.
144 if (mesh.getVertices().empty()) {
145 return;
146 }
147
148 auto renderable = std::make_shared<MeshRenderable>(
149 std::vector<utility::graphic::Mesh> { mesh }, "mesh_material");
150
151 RenderHandle *handle = find(entityIdentifier);
152 if (handle == nullptr) {
153 size_t objectId = _engine.createObject(renderable);
154 insert(entityIdentifier,
155 RenderHandle { objectId, renderable, true });
156 } else {
157 if (!_engine.updateObject(renderable, handle->objectId)) {
158 handle->objectId = _engine.createObject(renderable);
159 }
160 handle->renderable = renderable;
161 handle->used = true;
162 }
163 }
164
165} // 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...
~EllipseRender(void)
Forget the engine objects without removing them from the engine.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the mesh of one ellipse and upload it, creating the engine object on the first frame.
void prepare(void) override
Mark every engine object as not drawn yet. Called once per frame before the entities are visited.
void cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame. Called once per frame after...
EllipseRender(utility::Engine &engine)
Create the system in the Render phase.
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 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.