guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
mesh_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 <vector>
24
25#include <utility/graphic/model.hpp>
26
27#include "guillaume/systems/mesh_render.hpp"
28
30#include "guillaume/mesh_renderable.hpp"
31
32namespace guillaume::systems
33{
35 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
36 utility::Engine &engine)
37 : ecs::SystemFiller<components::Transform, components::Bound,
38 components::Mesh>(ecs::Phase::Render)
39 , _ressourceProvider(ressourceProvider)
40 , _engine(engine)
41 {
42 }
43
45 {
46 clear();
47 }
48
50 {
52 }
53
55 {
56 removeUnused(_engine);
57 }
58
59 void MeshRender::update(const ecs::Entity::Identifier &entityIdentifier)
60 {
61 getLogger().debug()
62 << "Updating MeshRender system for entity " << entityIdentifier;
63 if (!requireComponent<components::Transform>(entityIdentifier)
64 || !requireComponent<components::Bound>(entityIdentifier)
65 || !requireComponent<components::Mesh>(entityIdentifier)) {
66 return;
67 }
68
69 const auto &transformComponent =
71 const auto &modelComponent =
72 getComponent<components::Mesh>(entityIdentifier);
73
74 const std::string &meshPath = modelComponent.getMeshPath();
75 const std::string &texturePath = modelComponent.getTexturePath();
76 const auto &pose = transformComponent.getPose();
77
78 getLogger().debug() << "Rendering model '" << meshPath
79 << "' for entity " << entityIdentifier;
80
81 auto model = texturePath.empty()
82 ? _ressourceProvider->loadModel(meshPath, pose)
83 : _ressourceProvider->loadModel(meshPath, pose, texturePath);
84
85 if (!model) {
86 getLogger().warning() << "Failed to load model '" << meshPath
87 << "' for entity " << entityIdentifier;
88 return;
89 }
90
91 // The model's vertices are in its own local units, around its own
92 // origin, and MeshRenderable draws vertices as given: move them into
93 // the entity's box. The model is scaled to its fit size (see
94 // components::Mesh::setFitSize), centred in the Bound, and rotated
95 // with the entity, like the other renderers do.
96 utility::graphic::PositionF localMin(0.0f, 0.0f, 0.0f);
97 utility::graphic::PositionF localMax(0.0f, 0.0f, 0.0f);
98 bool first = true;
99 for (const auto &mesh: model->getMeshes()) {
100 for (const auto &vertex: mesh->getVertices()) {
101 const auto position = vertex.getPosition();
102 for (std::size_t axis = 0; axis < 3; ++axis) {
103 if (first || position[axis] < localMin[axis]) {
104 localMin[axis] = position[axis];
105 }
106 if (first || position[axis] > localMax[axis]) {
107 localMax[axis] = position[axis];
108 }
109 }
110 first = false;
111 }
112 }
113 const float scale = modelComponent.scaleFor(localMax[0] - localMin[0],
114 localMax[1] - localMin[1]);
115 const auto &bound = getComponent<components::Bound>(entityIdentifier);
116 // The model has depth: move its centre toward the viewer by half its
117 // depth, so it sits in front of the surface it is laid out on
118 // instead of half inside it.
119 const float halfDepth = (localMax[2] - localMin[2]) * scale / 2.0f;
120 const auto towardViewer =
121 -pose.getOrientation().getForward() * (halfDepth + 1.0f);
122 const utility::graphic::PositionF center(
123 pose.getPosition().x + bound.getWidth() / 2.0f + towardViewer[0],
124 pose.getPosition().y - bound.getHeight() / 2.0f + towardViewer[1],
125 pose.getPosition().z + towardViewer[2]);
126
127 std::vector<utility::graphic::Mesh> meshes;
128 meshes.reserve(model->getMeshes().size());
129 for (const auto &mesh: model->getMeshes()) {
130 std::vector<utility::graphic::VertexF> vertices;
131 vertices.reserve(mesh->getVertices().size());
132 for (auto vertex: mesh->getVertices()) {
133 const auto position = vertex.getPosition();
134 const utility::graphic::PositionF local(
135 (position[0] - (localMin[0] + localMax[0]) / 2.0f) * scale,
136 (position[1] - (localMin[1] + localMax[1]) / 2.0f) * scale,
137 (position[2] - (localMin[2] + localMax[2]) / 2.0f) * scale);
138 const auto rotated = shape::rotatePositionByQuaternion(
139 local, pose.getOrientation());
140 vertex.setPosition(utility::graphic::PositionF(
141 center[0] + rotated[0], center[1] + rotated[1],
142 center[2] + rotated[2]));
143 vertices.push_back(vertex);
144 }
145 meshes.emplace_back(vertices, mesh->getIndices());
146 }
147
148 auto renderable = std::make_shared<MeshRenderable>(
149 std::move(meshes),
150 texturePath.empty() ? "default_material" : "image_" + texturePath);
151
152 RenderHandle *handle = find(entityIdentifier);
153 if (handle == nullptr) {
154 size_t objectId = _engine.createObject(renderable);
155 insert(entityIdentifier,
156 RenderHandle { objectId, renderable, true });
157 } else {
158 if (!_engine.updateObject(renderable, handle->objectId)) {
159 handle->objectId = _engine.createObject(renderable);
160 }
161 handle->renderable = renderable;
162 handle->used = true;
163 }
164 }
165
166} // 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...
MeshRender(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Render phase.
void cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame, including those whose model...
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Load the model of one entity at its current pose and upload it, creating the engine object on the fir...
void prepare(void) override
Mark every model as not drawn yet. Called once per frame before the entities are visited.
~MeshRender(void)
Forget the engine objects without removing them from the engine.
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.
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::PositionF rotatePositionByQuaternion(const utility::graphic::PositionF &position, const utility::graphic::OrientationF &orientation)
Rotate a 3D point around the origin by a quaternion.
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.