guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
line_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/line_render.hpp"
24
25#include <memory>
26#include <vector>
27
28#include "guillaume/mesh_renderable.hpp"
30
31namespace guillaume::systems
32{
33 LineRender::LineRender(utility::Engine &engine)
34 : ecs::SystemFiller<components::Transform, components::Bound,
35 components::Color, components::Line>(
36 ecs::Phase::Render)
37 , _engine(engine)
38 {
39 }
40
42 {
44 }
45
47 {
48 removeUnused(_engine);
49 }
50
51 void LineRender::update(const ecs::Entity::Identifier &entityIdentifier)
52 {
53 if (!requireComponent<components::Bound>(entityIdentifier)
54 || !requireComponent<components::Transform>(entityIdentifier)
55 || !requireComponent<components::Color>(entityIdentifier)
56 || !requireComponent<components::Line>(entityIdentifier)) {
57 return;
58 }
59
60 const auto &pose =
61 getComponent<components::Transform>(entityIdentifier).getPose();
62 const auto &bound = getComponent<components::Bound>(entityIdentifier);
63 const auto &line = getComponent<components::Line>(entityIdentifier);
64 const auto &color = line.hasColor()
65 ? line.getColor()
66 : getComponent<components::Color>(entityIdentifier).getColor();
67
68 float centerY = bound.getHeight() / 2.0f;
69 if (line.getPlacement() == components::Line::Placement::Top) {
70 centerY = line.getThickness() / 2.0f;
71 } else if (line.getPlacement() == components::Line::Placement::Bottom) {
72 centerY = bound.getHeight() - line.getThickness() / 2.0f;
73 }
74
75 // A line shares its entity's plane, and the entity may also be
76 // filled (a checkbox draws its indeterminate dash over its box):
77 // lift it half a layer so it draws over the fill.
78 const utility::graphic::PositionF lift(
79 -pose.getOrientation().getForward() * StrokeLift);
80
81 const utility::graphic::PositionF center(
82 pose.getPosition().x + bound.getWidth() / 2.0f + lift[0],
83 pose.getPosition().y - centerY + lift[1],
84 pose.getPosition().z + lift[2]);
85
86 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
87 std::vector<uint32_t> {});
88
89 shape::buildLineMesh(mesh, center, pose.getOrientation(),
90 bound.getWidth() / 2.0f,
91 line.getThickness() / 2.0f, line.isDashed(),
92 line.getDashLength(), line.getGapLength(), color);
93
94 // A transparent shape adds no geometry: submit nothing, so the
95 // object is removed at the end of the frame.
96 if (mesh.getVertices().empty()) {
97 return;
98 }
99
100 auto renderable = std::make_shared<MeshRenderable>(
101 std::vector<utility::graphic::Mesh> { mesh }, "mesh_material");
102
103 RenderHandle *handle = find(entityIdentifier);
104 if (handle == nullptr) {
105 insert(entityIdentifier,
106 RenderHandle { _engine.createObject(renderable), renderable,
107 true });
108 } else {
109 if (!_engine.updateObject(renderable, handle->objectId)) {
110 handle->objectId = _engine.createObject(renderable);
111 }
112 handle->renderable = renderable;
113 handle->used = true;
114 }
115 }
116
117} // namespace guillaume::systems
@ Top
Inside the bound, against its top edge.
Definition line.hpp:69
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...
LineRender(utility::Engine &engine)
Create the system in the Render phase.
static constexpr float StrokeLift
Distance a stroke placed on the top or bottom edge is moved towards the viewer, along the forward axi...
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...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the mesh of one stroke 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.
void buildLineMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF &center, const utility::graphic::OrientationF &orientation, float halfLength, float halfThickness, bool dashed, float dashLength, float gapLength, const utility::graphic::Color32Bit &color)
Build a straight stroke along X, solid or dashed.
What a render system remembers about the engine object it created for one entity.