23#include "guillaume/systems/rectangle_render.hpp"
25#include "guillaume/components/ring.hpp"
26#include "guillaume/mesh_renderable.hpp"
35 float RectangleRender::extractAverageRadius(
36 const components::Borders &borders)
const
38 return (borders.getTopLeftRadius() + borders.getTopRightRadius()
39 + borders.getBottomRightRadius()
40 + borders.getBottomLeftRadius())
44 std::vector<utility::math::Vector2F>
45 RectangleRender::buildAxisAlignedRectVertices(
46 const float halfWidth,
const float halfHeight)
const
49 utility::math::Vector2F({ -halfWidth, -halfHeight }),
50 utility::math::Vector2F({ halfWidth, -halfHeight }),
51 utility::math::Vector2F({ halfWidth, halfHeight }),
52 utility::math::Vector2F({ -halfWidth, halfHeight }),
56 void RectangleRender::appendRoundedCornerArc(
57 std::vector<utility::math::Vector2F> &localVertices,
58 const utility::math::Vector2F &arcCenter,
const float startAngle,
59 const float endAngle,
const float radius,
const int arcSegments)
const
61 for (
int i = 0; i <= arcSegments; ++i) {
63 static_cast<float>(i) /
static_cast<float>(arcSegments);
64 const float angle = startAngle + (endAngle - startAngle) * t;
65 localVertices.push_back(
67 + utility::math::Vector2F(
68 { std::cos(angle) * radius, std::sin(angle) * radius }));
72 std::vector<utility::math::Vector2F>
73 RectangleRender::buildLocalRoundedRectVertices(
74 const float halfWidth,
const float halfHeight,
const float radius,
75 int arcSegments,
const float epsilon)
const
77 const float pi = std::acos(-1.0f);
79 if (arcSegments <= 0) {
83 const float cornerRadius =
84 std::max(0.0f, (std::min)({ radius, halfWidth, halfHeight }));
86 if (cornerRadius <= epsilon) {
87 return buildAxisAlignedRectVertices(halfWidth, halfHeight);
90 std::vector<utility::math::Vector2F> localVertices;
91 localVertices.reserve(
static_cast<std::size_t
>(4 * (arcSegments + 2)));
93 const utility::math::Vector2F topLeftCenter(
94 { -halfWidth + cornerRadius, -halfHeight + cornerRadius });
95 const utility::math::Vector2F topRightCenter(
96 { halfWidth - cornerRadius, -halfHeight + cornerRadius });
97 const utility::math::Vector2F bottomRightCenter(
98 { halfWidth - cornerRadius, halfHeight - cornerRadius });
99 const utility::math::Vector2F bottomLeftCenter(
100 { -halfWidth + cornerRadius, halfHeight - cornerRadius });
102 appendRoundedCornerArc(localVertices, topRightCenter, -pi / 2.0f, 0.0f,
103 cornerRadius, arcSegments);
104 appendRoundedCornerArc(localVertices, bottomRightCenter, 0.0f,
105 pi / 2.0f, cornerRadius, arcSegments);
106 appendRoundedCornerArc(localVertices, bottomLeftCenter, pi / 2.0f, pi,
107 cornerRadius, arcSegments);
108 appendRoundedCornerArc(localVertices, topLeftCenter, pi,
109 3.0f * pi / 2.0f, cornerRadius, arcSegments);
111 return localVertices;
114 std::vector<utility::graphic::PositionF>
115 RectangleRender::buildRoundedRectVertices(
116 const utility::graphic::PositionF ¢er,
117 const utility::graphic::OrientationF &orientation,
118 const utility::math::Vector2F &scale,
119 const utility::graphic::SizeF &size,
float radius,
int arcSegments,
122 const float halfWidth = (
size.getWidth() / 2.0f) * std::abs(scale[0]);
123 const float halfHeight = (
size.getHeight() / 2.0f) * std::abs(scale[1]);
124 const auto localVertices = buildLocalRoundedRectVertices(
125 halfWidth, halfHeight, radius, arcSegments, epsilon);
126 return shape::transformToWorldVertices(localVertices, center,
156 getLogger().debug() <<
"Updating RectangleRender system for entity "
174 const auto &transformComponent =
176 const auto &boundComponent =
178 const auto &colorComponent =
180 const auto &bordersComponent =
183 const auto &pose = transformComponent.getPose();
184 const auto &color = colorComponent.getColor();
187 <<
"Rendering rectangle with pose: " << pose
188 <<
", size: " << boundComponent.getWidth() <<
"x"
189 << boundComponent.getHeight() <<
", color: " << color;
191 const utility::graphic::PositionF center(
192 pose.getPosition().x + boundComponent.getWidth() / 2.0f,
193 pose.getPosition().y - boundComponent.getHeight() / 2.0f,
194 pose.getPosition().z);
196 const auto roundedVertices = buildRoundedRectVertices(
197 center, pose.getOrientation(),
198 utility::math::Vector2F({ 1.0f, 1.0f }), boundComponent,
199 extractAverageRadius(bordersComponent), 16, 0.001f);
201 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
202 std::vector<uint32_t> {});
205 std::vector<utility::graphic::Mesh> meshes;
206 if (!mesh.getVertices().empty()) {
207 meshes.push_back(mesh);
210 if (bordersComponent.hasStroke()) {
211 const utility::graphic::PositionF strokeCenter(
212 -pose.getOrientation().getForward() *
StrokeLift);
213 utility::graphic::Mesh stroke(
214 std::vector<utility::graphic::VertexF> {},
215 std::vector<uint32_t> {});
218 utility::graphic::PositionF(center[0] + strokeCenter[0],
219 center[1] + strokeCenter[1],
220 center[2] + strokeCenter[2]),
221 pose.getOrientation(), boundComponent.getWidth() / 2.0f,
222 boundComponent.getHeight() / 2.0f,
223 extractAverageRadius(bordersComponent),
224 bordersComponent.getWidth(), 16, bordersComponent.getColor());
225 if (!stroke.getVertices().empty()) {
226 meshes.push_back(stroke);
232 if (meshes.empty()) {
237 std::make_shared<MeshRenderable>(meshes,
"mesh_material");
240 if (handle ==
nullptr) {
241 size_t objectId = _engine.createObject(renderable);
245 if (!_engine.updateObject(renderable, handle->
objectId)) {
246 handle->
objectId = _engine.createObject(renderable);
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
SystemFiller(Phase phase)
ecs::ComponentRegistry & getComponentRegistry(void)
Get the component registry of the scene being processed.
bool hasComponent(const ecs::Entity::Identifier &entityIdentifier) const
Tell whether an entity has a component, in the bound registry.
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...
void prepare(void) override
Mark every engine object as not drawn yet. Called once per frame before the entities are visited.
~RectangleRender(void)
Forget the engine objects without removing them from the engine.
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the fill and border meshes of one entity and upload them, creating the engine object on the f...
void cleanup(void) override
Remove the engine objects of the entities that were not drawn this frame. Called once per frame after...
RectangleRender(utility::Engine &engine)
Create the system in the Render phase.
static constexpr float StrokeLift
Distance the border is moved towards the viewer, along the forward axis of the entity,...
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.
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 buildRoundedStrokeMesh(utility::graphic::Mesh &mesh, const utility::graphic::PositionF ¢er, const utility::graphic::OrientationF &orientation, float halfWidth, float halfHeight, float radius, float width, int arcSegments, const utility::graphic::Color32Bit &color)
Build the border of a rounded rectangle as a band of constant width.
void buildTriangleFanVertices(utility::graphic::Mesh &mesh, const utility::graphic::PositionF ¢er, 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.
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.