Skip to content

Commit c68c6b7

Browse files
[DrawToolGL] FIX crash related to Frame drawing (#5795)
FIX crash related to Frame drawing Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
1 parent a2d8898 commit c68c6b7

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

Sofa/GL/src/sofa/gl/Frame.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
#include <numbers>
3434

3535
template <>
36-
struct std::hash<sofa::type::Vec3f>
36+
struct std::hash<sofa::type::Vec3>
3737
{
38-
std::size_t operator()(const sofa::type::Vec3f& k) const
38+
std::size_t operator()(const sofa::type::Vec3& k) const
3939
{
4040
using std::size_t;
4141
using std::hash;
4242
using std::string;
4343

44-
return ((hash<float>()(k[0])
45-
^ (hash<float>()(k[1]) << 1)) >> 1)
46-
^ (hash<float>()(k[2]) << 1);
44+
return ((hash<SReal>()(k[0])
45+
^ (hash<SReal>()(k[1]) << 1)) >> 1)
46+
^ (hash<SReal>()(k[2]) << 1);
4747
}
4848
};
4949

@@ -81,7 +81,7 @@ namespace sofa::gl
8181

8282
// Triangle structure
8383
struct Triangle {
84-
int v0{}, v1{}, v2{};
84+
unsigned int v0{}, v1{}, v2{};
8585
};
8686

8787
// Cylinder mesh generator
@@ -137,10 +137,10 @@ struct CylinderMesh
137137
// Side triangles
138138
for (int i = 0; i < Segments; ++i)
139139
{
140-
const int bottom_curr = i * 2;
141-
const int top_curr = i * 2 + 1;
142-
const int bottom_next = ((i + 1) % Segments) * 2;
143-
const int top_next = ((i + 1) % Segments) * 2 + 1;
140+
const unsigned int bottom_curr = i * 2;
141+
const unsigned int top_curr = i * 2 + 1;
142+
const unsigned int bottom_next = ((i + 1) % Segments) * 2;
143+
const unsigned int top_next = ((i + 1) % Segments) * 2 + 1;
144144

145145
// Two triangles per side segment
146146
triangles[triangle_idx++] = Triangle{bottom_curr, top_curr, bottom_next};
@@ -153,10 +153,10 @@ struct CylinderMesh
153153

154154
for (int i = 0; i < Segments; ++i)
155155
{
156-
const int bottom_curr = i * 2;
157-
const int top_curr = i * 2 + 1;
158-
const int bottom_next = ((i + 1) % Segments) * 2;
159-
const int top_next = ((i + 1) % Segments) * 2 + 1;
156+
const unsigned int bottom_curr = i * 2;
157+
const unsigned int top_curr = i * 2 + 1;
158+
const unsigned int bottom_next = ((i + 1) % Segments) * 2;
159+
const unsigned int top_next = ((i + 1) % Segments) * 2 + 1;
160160

161161
// Bottom cap (clockwise from below)
162162
triangles[triangle_idx++] = Triangle{bottom_center, bottom_next, bottom_curr};
@@ -232,14 +232,14 @@ struct ConeMesh
232232

233233
// Generate triangles
234234
int triangle_idx = 0;
235-
int tip_idx = 0;
236-
int base_center_idx = vertex_count - 1;
235+
unsigned int tip_idx = 0;
236+
unsigned int base_center_idx = vertex_count - 1;
237237

238238
// Side triangles
239239
for (int i = 0; i < Segments; ++i)
240240
{
241-
const int curr = i + 1;
242-
const int next = (i + 1) % Segments + 1;
241+
const unsigned int curr = i + 1;
242+
const unsigned int next = (i + 1) % Segments + 1;
243243

244244
// Side triangle (tip to base edge)
245245
triangles[triangle_idx++] = Triangle{tip_idx, next, curr};
@@ -366,29 +366,31 @@ void render_coordinate_frame(const CoordinateFrame& frame, const type::Vec3& cen
366366
rotAxis.y(),
367367
rotAxis.z());
368368

369+
glBindVertexArray(0);
369370
glEnableClientState(GL_VERTEX_ARRAY);
370371
glEnableClientState(GL_NORMAL_ARRAY);
371-
372+
constexpr auto gltype = (std::is_same<SReal, double>::value)?GL_DOUBLE:GL_FLOAT;
372373
for (int i = 0; i < 6; ++i)
373374
{
374375
const auto& comp = mesh_components[i];
375376
glColor4d(colors[i][0], colors[i][1], colors[i][2], colors[i][3]);
376-
glVertexPointer(3, GL_DOUBLE, 0, comp.vertices);
377-
glNormalPointer(GL_DOUBLE, 0, comp.normals);
378-
glDrawElements(GL_TRIANGLES, comp.triangle_count * 3, GL_UNSIGNED_INT, comp.triangles);
377+
glVertexPointer(3, gltype, 0, comp.vertices);
378+
glNormalPointer(gltype, 0, comp.triangles);
379379

380-
//glDrawArrays(GL_POINTS, 0, comp.vertex_count);
380+
glDrawElements(GL_TRIANGLES, comp.triangle_count*3, GL_UNSIGNED_INT, comp.triangles);
381381
}
382382

383+
glDisableClientState(GL_NORMAL_ARRAY);
384+
glDisableClientState(GL_VERTEX_ARRAY);
385+
383386
glPopMatrix();
384387
glPopAttrib();
385388
}
386389

387-
std::unordered_map < type::Vec3f, CoordinateFrame > cacheCoordinateFrame;
388-
390+
std::unordered_map < type::Vec3, CoordinateFrame > cacheFrame;
389391
void Frame::draw(const type::Vec3& center, const Quaternion& orient, const type::Vec3& len, const type::RGBAColor& colorX, const type::RGBAColor& colorY, const type::RGBAColor& colorZ )
390392
{
391-
if (cacheCoordinateFrame.find(len) == cacheCoordinateFrame.end())
393+
if (cacheFrame.find(len) == cacheFrame.end())
392394
{
393395
type::Vec3 L = len;
394396

@@ -410,10 +412,10 @@ void Frame::draw(const type::Vec3& center, const Quaternion& orient, const type:
410412
const type::Vec3 lc(Lmax / 5_sreal, Lmax / 5_sreal, Lmax / 5_sreal); // = L / 5;
411413
const type::Vec3 Lc = lc;
412414

413-
cacheCoordinateFrame.emplace(len, CoordinateFrame ({ L[0], Lc[0], l[0], lc[0] }, { L[1], Lc[1], l[1], lc[1] }, { L[2], Lc[2], l[2], lc[2] }));
415+
cacheFrame.emplace(len, CoordinateFrame ({ L[0], Lc[0], l[0], lc[0] }, { L[1], Lc[1], l[1], lc[1] }, { L[2], Lc[2], l[2], lc[2] }));
414416
}
415417

416-
const auto& frame = cacheCoordinateFrame.at(len);
418+
const auto& frame = cacheFrame.at(len);
417419
render_coordinate_frame(frame, center, orient, len, colorX, colorY, colorZ);
418420
}
419421

0 commit comments

Comments
 (0)