GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
DebugVisualization.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_PHYSICS_DEBUG_VISUZLIATION_HPP
5#define GREM_PHYSICS_DEBUG_VISUZLIATION_HPP
6
8
9#ifdef GREM_PHYSICS_USE_DEBUG_VISUALIZATION
19#include <GREM/core/math.hpp>
31
32#include <new> // std::launder
33#include <stdexcept> // std::length_error
34#else
36#endif
37
38namespace grem::physics {
39
45template <size_t N>
47#ifdef GREM_PHYSICS_USE_DEBUG_VISUALIZATION
49public:
53 void clear() noexcept {
54 worldDrawCommands.clear();
55 uiDrawCommands.clear();
56 stringData.clear();
57 }
58
69 void drawWorldCube(const mat4& transformation, Color color) requires(N == 3) {
70 worldDrawCommands.emplace_back(DrawWorldCubeCommand{.transformation = transformation, .color = color});
71 }
72
83 void drawWorldCubeWireframe(const mat4& transformation, Color color) requires(N == 3) {
84 worldDrawCommands.emplace_back(DrawWorldCubeWireframeCommand{.transformation = transformation, .color = color});
85 }
86
97 void drawWorldAABBWireframe(const Box<N>& aabb, Color color) {
98 worldDrawCommands.emplace_back(DrawWorldAABBWireframeCommand{.aabb = aabb, .color = color});
99 }
100
111 void drawWorldAABBWireframe(const grem::Box<N, Scalar>& aabb, Color color) {
112 drawWorldAABBWireframe(aabb * Box<N>::UNIT, color);
113 }
114
126 void drawWorldPoint(Position<N> point, Color color, Coefficient radiusCoefficient = Scalar{1}) {
127 worldDrawCommands.emplace_back(DrawWorldPointCommand{.point = point, .color = color, .radiusCoefficient = radiusCoefficient});
128 }
129
143 void drawWorldLineSegment(Position<N> pointA, Position<N> pointB, Color color, Coefficient thicknessCoefficient = Scalar{1}) {
144 worldDrawCommands.emplace_back(DrawWorldLineSegmentCommand{.pointA = pointA, .pointB = pointB, .color = color, .thicknessCoefficient = thicknessCoefficient});
145 }
146
162 void drawWorldVector(Position<N> point, Scale<N> directionScale, Color color, Coefficient lengthCoefficient = Scalar{1}, Coefficient thicknessCoefficient = Scalar{1}) {
163 drawWorldLineSegment(point, point + directionScale * Scalar{0.25} * METERS * lengthCoefficient, color, thicknessCoefficient);
164 }
165
178 void drawUIText(vec2 position, uint32_t characterSize, Color color, UTF8StringView string) {
179 if (string.size() > Limits<uint32_t>::MAX || stringData.size() > Limits<uint32_t>::MAX - string.size()) {
180 throw std::length_error{"Maximum internal string buffer size exceeded."};
181 }
182 const uint32_t stringDataOffset = static_cast<uint32_t>(stringData.size());
183 const uint32_t stringDataSize = static_cast<uint32_t>(string.size());
184 stringData.append(string);
185 uiDrawCommands.emplace_back(
186 DrawUITextCommand{.position = position, .characterSize = characterSize, .color = color, .stringDataOffset = stringDataOffset, .stringDataSize = stringDataSize});
187 }
188
201 void drawUIText(vec2 position, uint32_t characterSize, Color color, StringView string) {
202 drawUIText(position, characterSize, color, UTF8StringView{std::launder(reinterpret_cast<const char8_t*>(string.data())), string.size()});
203 }
204
217 void putWorldVisualizationInstances(graphics::Renderer2D& renderer2D, graphics::Instances2D& instances, Length2D lengthUnit = Length2D{Scalar{1} * METERS}) const
218 requires(N == 2) {
219 (void)renderer2D;
220 const Scale2D lengthScale = Scalar{1} * METERS / lengthUnit;
221 for (const WorldDrawCommand& worldDrawCommand : worldDrawCommands) {
222 GREM_MATCH(worldDrawCommand) {
223 GREM_CASE(const DrawWorldCubeCommand& command) break;
224 GREM_CASE(const DrawWorldCubeWireframeCommand& command) break;
225 GREM_CASE(const DrawWorldAABBWireframeCommand& command) {
226 const vec2 aabbMin = lengthScale * command.aabb.min.in(Position2D::UNIT);
227 const vec2 aabbMax = lengthScale * command.aabb.max.in(Position2D::UNIT);
228 const vec2 aabbSize = aabbMax - aabbMin;
229 instances.putRectangleInstance(graphics::RectangleInstance2D{.position{aabbMin.x, aabbMin.y}, .size{aabbSize.x, 1.0f}, .color = command.color});
230 instances.putRectangleInstance(graphics::RectangleInstance2D{.position{aabbMin.x, aabbMax.y - 1.0f}, .size{aabbSize.x, 1.0f}, .color = command.color});
231 instances.putRectangleInstance(graphics::RectangleInstance2D{.position{aabbMin.x, aabbMin.y + 1.0f}, .size{1.0f, aabbSize.y - 2.0f}, .color = command.color});
232 instances.putRectangleInstance(
233 graphics::RectangleInstance2D{.position{aabbMax.x - 1.0f, aabbMin.y + 1.0f}, .size{1.0f, aabbSize.y - 2.0f}, .color = command.color});
234 break;
235 }
236 GREM_CASE(const DrawWorldPointCommand& command) {
237 instances.putRectangleInstance(graphics::RectangleInstance2D{
238 .position = lengthScale * command.point.in(Position2D::UNIT),
239 .size{static_cast<float>(Scalar{4} * command.radiusCoefficient)},
240 .origin{0.5f, 0.5f},
241 .color = command.color,
242 });
243 break;
244 }
245 GREM_CASE(const DrawWorldLineSegmentCommand& command) {
246 const Length2D difference = command.pointB - command.pointA;
247 if (difference != 0) {
248 instances.putRectangleInstance(graphics::RectangleInstance2D{
249 .position = lengthScale * command.pointA.in(Position2D::UNIT),
250 .angle = (Scalar{-90} * DEGREES).as(RADIANS) + getAngle(difference),
251 .size{static_cast<float>(Scalar{2} * command.thicknessCoefficient), static_cast<float>(length(lengthScale * difference.in(Distance::UNIT)))},
252 .origin{0.5f, 0.0f},
253 .color = command.color,
254 });
255 }
256 break;
257 }
258 }
259 }
260 }
261
274 void putWorldVisualizationInstances(graphics::Renderer3D& renderer3D, graphics::Instances3D& instances, Length3D lengthUnit = Length3D{Scalar{1} * METERS}) const
275 requires(N == 3) {
276 const mat4 lengthScale{mat3{scale(Scalar{1} * METERS / lengthUnit)}};
277 for (const WorldDrawCommand& worldDrawCommand : worldDrawCommands) {
278 GREM_MATCH(worldDrawCommand) {
279 GREM_CASE(const DrawWorldCubeCommand& command) {
280 instances.putShadedModelInstance(renderer3D.getHDRUnlitModel3DShaderPipelineSet(), renderer3D.getCubeModel3D(), lengthScale * command.transformation,
281 graphics::ModelInstance3D{.color = command.color});
282 break;
283 }
284 GREM_CASE(const DrawWorldCubeWireframeCommand& command) {
285 instances.putShadedModelInstance(renderer3D.getHDRWireframeModel3DShaderPipelineSet(), renderer3D.getCubeModel3D(), lengthScale * command.transformation,
286 graphics::ModelInstance3D{.color = command.color});
287 break;
288 }
289 GREM_CASE(const DrawWorldAABBWireframeCommand& command) {
290 const Position3D aabbCenter = midpoint(command.aabb.min, command.aabb.max);
291 const Length3D aabbHalfExtents = (command.aabb.max - command.aabb.min) * Scalar{0.5};
292 const mat4 transformation = translateScale(aabbCenter, aabbHalfExtents.in(Length3D::UNIT));
293 instances.putShadedModelInstance(renderer3D.getHDRWireframeModel3DShaderPipelineSet(), renderer3D.getCubeModel3D(), lengthScale * transformation,
294 graphics::ModelInstance3D{.color = command.color});
295 break;
296 }
297 GREM_CASE(const DrawWorldPointCommand& command) {
298 const mat4 transformation = translateScale(command.point, Scale3D{static_cast<Scalar>(0.02) * command.radiusCoefficient});
299 instances.putShadedModelInstance(renderer3D.getHDRUnlitModel3DShaderPipelineSet(), renderer3D.getCubeModel3D(), lengthScale * transformation,
300 graphics::ModelInstance3D{.color = command.color});
301 break;
302 }
303 GREM_CASE(const DrawWorldLineSegmentCommand& command) {
304 const Length3D difference = command.pointB - command.pointA;
305 if (difference != 0) {
306 const Position3D midpoint = command.pointA + difference * Scalar{0.5};
307 const mat4 transformation = translateRotateScale(midpoint, Orientation3D::rotation(Z_AXIS_3D, normalize(difference.in(Length3D::UNIT))),
308 Scale3D{static_cast<Scalar>(0.01) * command.thicknessCoefficient, static_cast<Scalar>(0.01) * command.thicknessCoefficient,
309 Scalar{0.5} * length(difference).in(Distance::UNIT)});
310 instances.putShadedModelInstance(renderer3D.getHDRUnlitModel3DShaderPipelineSet(), renderer3D.getCubeModel3D(), lengthScale * transformation,
311 graphics::ModelInstance3D{.color = command.color});
312 }
313 break;
314 }
315 }
316 }
317 }
318
331 void putUIVisualizationInstances(graphics::Renderer2D& renderer2D, graphics::Instances2D& instances, graphics::Font2D& font) const {
332 (void)renderer2D;
333 for (const UIDrawCommand& uiDrawCommand : uiDrawCommands) {
334 GREM_MATCH(uiDrawCommand) {
335 GREM_CASE(const DrawUITextCommand& command) {
336 instances.putTextStringInstance(font, UTF8StringView{stringData}.substr(command.stringDataOffset, command.stringDataSize),
337 graphics::TextStringInstance2D{.characterSize = command.characterSize, .position = command.position, .color = command.color});
338 break;
339 }
340 }
341 }
342 }
343
344private:
345 struct DrawWorldCubeCommand {
346 mat4 transformation;
347 Color color;
348 };
349
350 struct DrawWorldCubeWireframeCommand {
351 mat4 transformation;
352 Color color;
353 };
354
355 struct DrawWorldAABBWireframeCommand {
356 Box<N> aabb;
357 Color color;
358 };
359
360 struct DrawWorldPointCommand {
361 Position<N> point;
362 Color color;
363 Coefficient radiusCoefficient;
364 };
365
366 struct DrawWorldLineSegmentCommand {
367 Position<N> pointA;
368 Position<N> pointB;
369 Color color;
370 Coefficient thicknessCoefficient;
371 };
372
373 struct DrawUITextCommand {
374 vec2 position;
375 uint32_t characterSize;
376 Color color;
377 uint32_t stringDataOffset;
378 uint32_t stringDataSize;
379 };
380
381 using WorldDrawCommand = Variant< //
382 DrawWorldCubeCommand, //
383 DrawWorldCubeWireframeCommand, //
384 DrawWorldAABBWireframeCommand, //
385 DrawWorldPointCommand, //
386 DrawWorldLineSegmentCommand>;
387 using UIDrawCommand = Variant< //
388 DrawUITextCommand>;
389
390 ArrayList<WorldDrawCommand> worldDrawCommands{};
391 ArrayList<UIDrawCommand> uiDrawCommands{};
392 UTF8String stringData{};
393}
394#endif
395;
396
397using DebugVisualization2D = DebugVisualization<2>;
398using DebugVisualization3D = DebugVisualization<3>;
399
400} // namespace grem::physics
401
402#endif
#define GREM_MATCH(...)
Definition Variant.hpp:1997
#define GREM_CASE(...)
Definition Variant.hpp:1998
Floating-point linear RGBA color in the Rec.
Definition Color.hpp:60
Bundle of draw commands for drawing debugging information about a Simulation.
Definition DebugVisualization.hpp:46
@ N
'n'
Definition keyboard.hpp:84
json::VariantBase< std::allocator > Variant
JSON value type using the default allocator.
Definition json.hpp:1094
Definition Broadphase.hpp:24
GREM_ALWAYS_INLINE constexpr auto GREM_VECTORCALL length(A a)
Definition quantities.hpp:3302
constexpr Direction3D Z_AXIS_3D
The Z axis in 3-dimensional space.
Definition quantities.hpp:4914
GREM_ALWAYS_INLINE constexpr auto GREM_VECTORCALL midpoint(A a, B b)
Definition quantities.hpp:3497
float Scalar
Underlying scalar type of physics quantity components.
Definition quantities.hpp:1480
DebugVisualization< 2 > DebugVisualization2D
Bundle of draw commands for drawing debugging information about a 2-dimensional Simulation.
Definition DebugVisualization.hpp:48
GREM_ALWAYS_INLINE LocalTransformation< N > GREM_VECTORCALL translateRotateScale(Length< N > localOffset, Orientation< N > localOrientation, Scale< N > localScale)
Definition quantities.hpp:5063
Scale< 3 > Scale3D
Dimensionless scale in 3-dimensional space. Unitless.
Definition quantities.hpp:3987
GREM_ALWAYS_INLINE Basis< N > scale(Quantity< N, UnitT > s)
Definition quantities.hpp:4708
Scale1D Coefficient
Unitless scalar quantity.
Definition quantities.hpp:3988
GREM_ALWAYS_INLINE constexpr auto GREM_VECTORCALL normalize(A a)
Definition quantities.hpp:4187
Length< 3 > Length3D
Relative distance/length offset in 3-dimensional space. Unit: Meters.
Definition quantities.hpp:4927
GREM_ALWAYS_INLINE constexpr auto GREM_VECTORCALL getAngle(A a)
Definition quantities.hpp:3323
GREM_ALWAYS_INLINE LocalTransformation< N > GREM_VECTORCALL translateScale(Length< N > localOffset, Scale< N > localScale)
Definition quantities.hpp:5089
DebugVisualization< 3 > DebugVisualization3D
Bundle of draw commands for drawing debugging information about a 3-dimensional Simulation.
Definition DebugVisualization.hpp:49
Position< 3 > Position3D
Absolute position in 3-dimensional space. Unit: Meters.
Definition quantities.hpp:4952
Quantity< N, Absolute< Meters > > Position
Absolute position in world space.
Definition quantities.hpp:4949
Quantity< N, Unitless > Scale
Dimensionless scale.
Definition quantities.hpp:3984
Scale< 2 > Scale2D
Dimensionless scale in 2-dimensional space. Unitless.
Definition quantities.hpp:3986
Length< 2 > Length2D
Relative distance/length offset in 2-dimensional space. Unit: Meters.
Definition quantities.hpp:4926
vec< 2, float > vec2
Definition math.hpp:438
mat< 3, 3, float > mat3
Definition math.hpp:805
StringBase< char8_t, std::char_traits< char8_t >, std::allocator< char8_t > > UTF8String
Definition String.hpp:18
mat< 4, 4, float > mat4
Definition math.hpp:806
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
StringViewBase< char8_t, std::char_traits< char8_t > > UTF8StringView
Definition StringView.hpp:17
static constexpr T MAX
Definition fundamentals.hpp:29
static constexpr Unit UNIT
Unit.
Definition Shape.hpp:817
static GREM_ALWAYS_INLINE Orientation rotation(Scale3D from, Scale3D to)
Definition quantities.hpp:4522