23#include "guillaume/systems/elevation_render.hpp"
33#include "guillaume/mesh_renderable.hpp"
39 utility::graphic::PositionF rotateByOrientation(
40 const utility::graphic::PositionF &position,
41 const utility::graphic::OrientationF &orientation)
43 const auto normalized = orientation.normalized();
44 const float qx = normalized.x;
45 const float qy = normalized.y;
46 const float qz = normalized.z;
47 const float qw = normalized.w;
49 const float positionX = position[0];
50 const float positionY = position[1];
51 const float positionZ = position[2];
53 const float crossX = (qy * positionZ) - (qz * positionY);
54 const float crossY = (qz * positionX) - (qx * positionZ);
55 const float crossZ = (qx * positionY) - (qy * positionX);
57 const float tX = 2.0f * crossX;
58 const float tY = 2.0f * crossY;
59 const float tZ = 2.0f * crossZ;
61 const float crossTX = (qy * tZ) - (qz * tY);
62 const float crossTY = (qz * tX) - (qx * tZ);
63 const float crossTZ = (qx * tY) - (qy * tX);
65 return utility::graphic::PositionF(positionX + (qw * tX) + crossTX,
66 positionY + (qw * tY) + crossTY,
67 positionZ + (qw * tZ) + crossTZ);
70 std::vector<utility::math::Vector2F>
71 buildRoundedRectOutline(
float halfWidth,
float halfHeight,
72 float radius,
int arcSegments)
74 const float pi = std::acos(-1.0f);
76 if (arcSegments <= 0) {
80 const float cornerRadius =
81 std::max(0.0f, (std::min)({ radius, halfWidth, halfHeight }));
83 std::vector<utility::math::Vector2F> localVertices;
85 if (cornerRadius <= 0.0f) {
87 utility::math::Vector2F({ -halfWidth, -halfHeight }),
88 utility::math::Vector2F({ halfWidth, -halfHeight }),
89 utility::math::Vector2F({ halfWidth, halfHeight }),
90 utility::math::Vector2F({ -halfWidth, halfHeight }),
95 auto appendArc = [&localVertices, arcSegments](
96 const utility::math::Vector2F ¢er,
97 float startAngle,
float endAngle,
99 for (
int i = 0; i <= arcSegments; ++i) {
101 static_cast<float>(i) /
static_cast<float>(arcSegments);
103 startAngle + (endAngle - startAngle) * t;
104 localVertices.push_back(
106 + utility::math::Vector2F(
107 { std::cos(angle) * arcRadius,
108 std::sin(angle) * arcRadius }));
112 appendArc(utility::math::Vector2F({ halfWidth - cornerRadius,
113 -halfHeight + cornerRadius }),
114 -pi / 2.0f, 0.0f, cornerRadius);
115 appendArc(utility::math::Vector2F({ halfWidth - cornerRadius,
116 halfHeight - cornerRadius }),
117 0.0f, pi / 2.0f, cornerRadius);
118 appendArc(utility::math::Vector2F({ -halfWidth + cornerRadius,
119 halfHeight - cornerRadius }),
120 pi / 2.0f, pi, cornerRadius);
121 appendArc(utility::math::Vector2F({ -halfWidth + cornerRadius,
122 -halfHeight + cornerRadius }),
123 pi, 3.0f * pi / 2.0f, cornerRadius);
125 return localVertices;
128 void appendLayer(utility::graphic::Mesh &mesh,
129 const utility::graphic::PositionF ¢er,
130 const utility::graphic::OrientationF &orientation,
131 float halfWidth,
float halfHeight,
float radius,
132 const utility::graphic::Color32Bit &color)
138 buildRoundedRectOutline(halfWidth, halfHeight, radius, 8);
140 const uint32_t base =
141 static_cast<uint32_t
>(mesh.getVertices().size());
143 utility::graphic::VertexF centerVertex;
144 centerVertex.setPosition(center);
146 mesh.addVertex(centerVertex);
148 for (
const auto &localVertex: outline) {
149 const utility::graphic::PositionF localPosition(
150 localVertex[0], localVertex[1], 0.0f);
152 rotateByOrientation(localPosition, orientation);
154 utility::graphic::VertexF vertex;
155 vertex.setPosition(utility::graphic::PositionF(
156 center[0] + rotated[0], center[1] + rotated[1],
157 center[2] + rotated[2]));
159 mesh.addVertex(vertex);
162 for (std::size_t i = 1; i < outline.size(); ++i) {
164 mesh.addIndex(base +
static_cast<uint32_t
>(i));
165 mesh.addIndex(base +
static_cast<uint32_t
>(i + 1));
168 mesh.addIndex(base +
static_cast<uint32_t
>(outline.size()));
169 mesh.addIndex(base + 1u);
202 const auto &elevation =
207 if (!elevation.hasShadow()) {
211 const utility::graphic::PositionF center(
212 pose.getPosition().x + bound.getWidth() / 2.0f,
213 pose.getPosition().y - bound.getHeight() / 2.0f,
214 pose.getPosition().z);
216 const float halfWidth = bound.getWidth() / 2.0f;
217 const float halfHeight = bound.getHeight() / 2.0f;
219 const int layers = std::max(1, elevation.getLayers());
220 const auto &orientation = pose.getOrientation();
221 const auto &color = elevation.getColor();
223 utility::graphic::Mesh mesh(std::vector<utility::graphic::VertexF> {},
224 std::vector<uint32_t> {});
226 for (
int i = 0; i < layers; ++i) {
228 static_cast<float>(i + 1) /
static_cast<float>(layers);
229 const float expansion =
230 elevation.getSpread() * elevation.getLevel() * t;
232 static_cast<float>(color.getAlpha()) * (1.0f - t);
234 appendLayer(mesh, center, orientation, halfWidth + expansion,
235 halfHeight + expansion,
236 (std::min)(halfWidth, halfHeight) + expansion,
237 color.withAlpha(
static_cast<std::uint8_t
>(alpha)));
242 if (mesh.getVertices().empty()) {
246 auto renderable = std::make_shared<MeshRenderable>(
247 std::vector<utility::graphic::Mesh> { mesh },
"mesh_material");
250 if (handle ==
nullptr) {
252 RenderHandle { _engine.createObject(renderable), renderable,
255 if (!_engine.updateObject(renderable, handle->objectId)) {
256 handle->objectId = _engine.createObject(renderable);
258 handle->renderable = 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)
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...
ElevationRender(utility::Engine &engine)
Create the system in the Render phase.
void prepare(void) override
Mark every shadow as not drawn yet. Called once per frame before the entities are visited.
void cleanup(void) override
Remove the shadows of the entities that were not drawn this frame, including those whose elevation dr...
void update(const ecs::Entity::Identifier &entityIdentifier) override
Rebuild the shadow mesh of one entity 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.
Rules. Every class here runs each frame on the entities that have the components it declares.
Mesh builders shared by the shape render systems.
bool isInvisible(const utility::graphic::Color32Bit &color)
Whether a color draws nothing.
utility::graphic::Color32Bit opaqueColor(const utility::graphic::Color32Bit &color)
Turn a translucent color into the opaque color it looks like.
What a render system remembers about the engine object it created for one entity.