Unravel Engine C++ Reference
Loading...
Searching...
No Matches
gizmos.cpp
Go to the documentation of this file.
1#include "gizmos.h"
2
4#include <bx/math.h>
6#include <algorithm>
7#include <vector>
8#include <array>
9namespace unravel
10{
11
12auto to_bx(const glm::vec3& data) -> bx::Vec3
13{
14 return {data.x, data.y, data.z};
15}
16
18{
19 bx::Sphere sphere;
20 sphere.center = to_bx(sh.center);
21 sphere.radius = sh.radius;
22 dde.draw(sphere);
23}
24
25// void draw(DebugDrawEncoder& dde, const physics_plane_shape& sh)
26// {
27// auto center = sh.normal * sh.constant;
28// dde.drawQuad(to_bx(-sh.normal), to_bx(center), 20);
29// }
30
32{
33 math::vec3 axis{0, 1, 0};
34 dde.drawCylinder(to_bx(sh.center + axis * -sh.length * 0.5f),
35 to_bx(sh.center + axis * sh.length * 0.5f),
36 sh.radius);
37}
38
40{
41 // auto axis = edyn::coordinate_axis_vector(sh.axis);
42 math::vec3 axis{0, 1, 0};
43 dde.drawCapsule(to_bx(sh.center + axis * -sh.length * 0.5f), to_bx(sh.center + axis * sh.length * 0.5f), sh.radius);
44}
45
47{
48 auto aabb = bx::Aabb{to_bx(sh.center - sh.extends * 0.5f), to_bx(sh.center + sh.extends * 0.5f)};
49 dde.draw(aabb);
50}
51
53{
54 if(!sh.mesh_asset || !sh.mesh_asset.is_ready())
55 {
56 return;
57 }
58
59 const auto& mesh_ref = sh.mesh_asset.get();
60
61 // Get vertex and index data from mesh
62 auto* vertex_data = mesh_ref->get_system_vb();
63 auto* index_data = mesh_ref->get_system_ib();
64 auto vertex_count = mesh_ref->get_vertex_count();
65 auto face_count = mesh_ref->get_face_count();
66 const auto& vertex_format = mesh_ref->get_vertex_format();
67
68 if(!vertex_data || !index_data || vertex_count == 0 || face_count == 0)
69 {
70 return;
71 }
72
73 // Find position attribute offset in vertex format
74 auto position_offset = vertex_format.getOffset(bgfx::Attrib::Position);
75
76 if(position_offset == UINT16_MAX)
77 {
78 return; // No position data
79 }
80
81 // Extract vertex positions (node-local space) into DdVertex format. Indices are global,
82 // so a single shared vertex array is reused by every submesh geometry.
83 std::vector<DdVertex> vertices;
84 vertices.reserve(vertex_count);
85
86 for(uint32_t i = 0; i < vertex_count; ++i)
87 {
88 std::array<float, 4> pos;
89 gfx::vertex_unpack(pos.data(), gfx::attribute::Position, vertex_format, vertex_data, i);
90
91 DdVertex v;
92 v.x = pos[0];
93 v.y = pos[1];
94 v.z = pos[2];
95 vertices.push_back(v);
96 }
97
98 // Set color based on collision type. Green for convex, blue for concave.
99 dde.setColor(sh.collision_type == mesh_collision_type::convex ? 0xff00ff00 : 0xff0000ff);
100
101 // Enable wireframe mode for better visibility
102 dde.setWireframe(true);
103
104 // Build one geometry per submesh and draw it with its own node transform. The vertex
105 // buffer stores positions in node-local space; the renderer applies each submesh's
106 // accumulated armature node transform (relative to the model root) on top, so we mirror
107 // that here to keep the debug wireframe aligned with the rendered mesh and collision shape.
108 const auto& submeshes = mesh_ref->get_submeshes();
109 const auto node_transforms = mesh_ref->get_submesh_node_transforms();
110
111 for(size_t s = 0; s < submeshes.size(); ++s)
112 {
113 const auto* submesh = submeshes[s];
114 if(!submesh || submesh->face_start < 0 || submesh->face_count == 0)
115 {
116 continue;
117 }
118
119 const uint32_t face_begin = static_cast<uint32_t>(submesh->face_start);
120 if(face_begin >= face_count)
121 {
122 continue;
123 }
124 const uint32_t face_render_count = std::min(submesh->face_count, face_count - face_begin);
125 const uint32_t submesh_index_count = face_render_count * 3;
126 const uint32_t* submesh_indices = index_data + static_cast<size_t>(face_begin) * 3;
127
128 GeometryHandle geom_handle = ddCreateGeometry(static_cast<uint32_t>(vertices.size()),
129 vertices.data(),
130 submesh_index_count,
131 submesh_indices,
132 true); // Use 32-bit indices
133 if(!isValid(geom_handle))
134 {
135 continue;
136 }
137
138 // Apply the shape center on top of the submesh node transform.
139 math::transform local_transform;
140 if(s < node_transforms.size())
141 {
142 local_transform = node_transforms[s];
143 }
144 local_transform.set_position(local_transform.get_position() + sh.center);
145 dde.pushTransform((const float*)local_transform);
146 dde.draw(geom_handle);
147 dde.popTransform();
148 ddDestroy(geom_handle);
149 }
150}
151
153{
154 hpp::visit(
155 [&](auto&& s)
156 {
157 draw(dde, s);
158 },
159 sh.shape);
160}
161
162void draw(DebugDrawEncoder& dde, const std::vector<physics_compound_shape>& shapes)
163{
164 for(auto& shape : shapes)
165 {
166 draw(dde, shape);
167 }
168}
169
170
171} // namespace unravel
General purpose transformation class designed to maintain each component of the transformation separa...
Definition transform.hpp:27
auto get_position() const noexcept -> const vec3_t &
Get the position component.
void set_position(const vec3_t &position) noexcept
Set the position component.
void vertex_unpack(float _output[4], attribute _attr, const vertex_layout &_decl, const void *_data, uint32_t _index)
Definition graphics.cpp:364
@ sphere
Sphere type reflection probe.
void draw(DebugDrawEncoder &dde, const physics_sphere_shape &sh)
Definition gizmos.cpp:17
@ convex
Convex mesh collision (can be dynamic, faster)
auto to_bx(const glm::vec3 &data) -> bx::Vec3
Definition gizmos.cpp:12
float x
Definition debugdraw.h:27
void draw(const bx::Aabb &_aabb)
void drawCylinder(const bx::Vec3 &_from, const bx::Vec3 &_to, float _radius)
void setColor(uint32_t _abgr)
void setWireframe(bool _wireframe)
void pushTransform(const void *_mtx)
void drawCapsule(const bx::Vec3 &_from, const bx::Vec3 &_to, float _radius)
Represents a box shape for physics calculations.
math::vec3 extends
Extents of the box.
math::vec3 center
Center of the box.
Represents a capsule shape for physics calculations.
float length
Length of the capsule.
math::vec3 center
Center of the capsule.
float radius
Radius of the capsule.
Represents a compound shape that can contain multiple types of shapes.
shape_t shape
The shape contained in the compound shape.
Represents a cylinder shape for physics calculations.
float length
Length of the cylinder.
math::vec3 center
Center of the cylinder.
float radius
Radius of the cylinder.
Represents a mesh shape for physics calculations.
math::vec3 center
Center offset of the mesh.
mesh_collision_type collision_type
Type of collision shape.
asset_handle< mesh > mesh_asset
Reference to the mesh asset.
Represents a sphere shape for physics calculations.
float radius
Radius of the sphere.
math::vec3 center
Center of the sphere.
GeometryHandle ddCreateGeometry(uint32_t _numVertices, const DdVertex *_vertices, uint32_t _numIndices, const void *_indices, bool _index32)
void ddDestroy(SpriteHandle _handle)
bool isValid(SpriteHandle _handle)
Definition debugdraw.h:34