guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
image_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 <memory>
24
25#include "guillaume/systems/image_render.hpp"
26#include "guillaume/mesh_renderable.hpp"
27
28namespace guillaume::systems
29{
31 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
32 utility::Engine &engine)
33 : ecs::SystemFiller<components::Transform, components::Bound,
34 components::Image>(ecs::Phase::Render)
35 , _ressourceProvider(ressourceProvider)
36 , _engine(engine)
37 {
38 }
39
41 {
42 clear();
43 }
44
46 {
48 }
49
51 {
52 removeUnused(_engine);
53 }
54
55 void ImageRender::update(const ecs::Entity::Identifier &entityIdentifier)
56 {
57 getLogger().debug()
58 << "Updating ImageRender system for entity " << entityIdentifier;
59
60 if (!requireComponent<components::Transform>(entityIdentifier)
61 || !requireComponent<components::Bound>(entityIdentifier)
62 || !requireComponent<components::Image>(entityIdentifier)) {
63 return;
64 }
65
66 const auto &transformComponent =
68 auto &imageComponent =
69 getComponent<components::Image>(entityIdentifier);
70
71 std::string texturePath = imageComponent.getTexturePath();
72
73 getLogger().debug() << "Rendering image '" << texturePath
74 << "' for entity " << entityIdentifier;
75
76 bool usingFallback = false;
77 if (!_ressourceProvider->loadImageMaterial(texturePath)) {
78 getLogger().warning()
79 << "Failed to load image material for texture: " << texturePath;
80 const std::string &fallback = imageComponent.getFallbackPath();
81 if (fallback.empty()
82 || !_ressourceProvider->loadImageMaterial(fallback)) {
83 imageComponent.setLoadState(
85 return;
86 }
87 texturePath = fallback;
88 usingFallback = true;
89 }
90 imageComponent.setLoadState(components::Image::LoadState::Loaded,
91 usingFallback);
92
93 // An image built without a size takes its texture's size, in dp, so
94 // it is not an invisible 0 x 0 quad. With only one dimension set, the
95 // other one follows the texture's aspect ratio.
96 auto &bound = getComponent<components::Bound>(entityIdentifier);
97 if (bound.getWidth() <= 0.0f || bound.getHeight() <= 0.0f) {
98 const auto texture = _ressourceProvider->loadTexture(texturePath);
99 if (texture != nullptr && texture->width() > 0
100 && texture->height() > 0) {
101 const float textureWidth = static_cast<float>(texture->width());
102 const float textureHeight =
103 static_cast<float>(texture->height());
104 if (bound.getWidth() > 0.0f) {
105 bound.setHeight(bound.getWidth() * textureHeight
106 / textureWidth);
107 } else if (bound.getHeight() > 0.0f) {
108 bound.setWidth(bound.getHeight() * textureWidth
109 / textureHeight);
110 } else {
111 bound.setWidth(textureWidth).setHeight(textureHeight);
112 }
113 }
114 }
115
116 const auto &boundComponent = bound;
117 const float halfWidth = boundComponent.getWidth() / 2.0f;
118 const float halfHeight = boundComponent.getHeight() / 2.0f;
119
120 const utility::graphic::PositionF center(
121 transformComponent.getPose().getPosition().x + halfWidth,
122 transformComponent.getPose().getPosition().y - halfHeight,
123 transformComponent.getPose().getPosition().z);
124
125 const utility::graphic::Color32Bit white(255, 255, 255, 255);
126
127 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
128 std::vector<uint32_t> {});
129
130 const auto addVertex = [&](float x, float y, float u, float v) {
131 utility::graphic::VertexF vertex;
132 vertex.setPosition(utility::graphic::PositionF(
133 center[0] + x, center[1] + y, center[2]));
134 vertex.setColor(white);
135 vertex.setTextureCoordinates(utility::math::Vector2F({ u, v }));
136 mesh.addVertex(vertex);
137 };
138
139 addVertex(-halfWidth, -halfHeight, 0.0f, 0.0f);
140 addVertex(halfWidth, -halfHeight, 1.0f, 0.0f);
141 addVertex(halfWidth, halfHeight, 1.0f, 1.0f);
142 addVertex(-halfWidth, halfHeight, 0.0f, 1.0f);
143
144 mesh.addIndex(0);
145 mesh.addIndex(1);
146 mesh.addIndex(2);
147 mesh.addIndex(0);
148 mesh.addIndex(2);
149 mesh.addIndex(3);
150
151 auto renderable = std::make_shared<MeshRenderable>(
152 std::vector<utility::graphic::Mesh> { mesh },
153 "image_" + texturePath);
154
155 RenderHandle *handle = find(entityIdentifier);
156 if (handle == nullptr) {
157 size_t objectId = _engine.createObject(renderable);
158 insert(entityIdentifier,
159 RenderHandle { objectId, renderable, true });
160 } else {
161 if (!_engine.updateObject(renderable, handle->objectId)) {
162 handle->objectId = _engine.createObject(renderable);
163 }
164 handle->renderable = renderable;
165 handle->used = true;
166 }
167 }
168
169} // namespace guillaume::systems
@ Loaded
The texture (or the fallback) is drawn.
Definition image.hpp:64
@ Failed
Neither loaded; nothing is drawn.
Definition image.hpp:65
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...
ImageRender(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Render phase.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Load the texture of one image, record the outcome in its components::Image and upload a textured quad...
void prepare(void) override
Mark every picture 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, including those whose textu...
~ImageRender(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.
What a render system remembers about the engine object it created for one entity.