28 bool enableTransparency,
bool antiAliasing,
bool enableShowEdges,
29 bool enableShowFaces,
bool enableShowVertices)
30 : m_enableTransparency(enableTransparency),
31 m_antiAliasing(antiAliasing),
32 m_showEdges(enableShowEdges),
33 m_showFaces(enableShowFaces),
34 m_showVertices(enableShowVertices),
35 m_computeNormals(true),
62 unsigned int num_verts,
unsigned int num_faces,
int* verts_per_face,
63 int* face_verts,
float* vert_coords)
65 m_num_verts = num_verts;
66 m_num_faces = num_faces;
69 m_is_quad =
new bool[num_faces];
70 for (
unsigned int i = 0; i < num_faces; i++)
72 if (verts_per_face[i] == 3)
74 else if (verts_per_face[i] == 4)
79 "\n Incorrect mesh format. It can only be composed of " 80 "triangles and/or quads.");
86 m_face_verts =
new f_verts[num_faces];
87 unsigned int count = 0;
88 for (
unsigned int f = 0; f < num_faces; f++)
90 m_face_verts[f][0] = face_verts[
count++];
91 m_face_verts[f][1] = face_verts[
count++];
92 m_face_verts[f][2] = face_verts[
count++];
94 m_face_verts[f][3] = face_verts[
count++];
96 m_face_verts[f][3] = -1;
100 m_vert_coords =
new coord3D[num_verts];
101 for (
unsigned int i = 0; i < num_verts; i++)
103 m_vert_coords[i][0] = vert_coords[3 * i];
104 m_vert_coords[i][1] = vert_coords[3 * i + 1];
105 m_vert_coords[i][2] = vert_coords[3 * i + 2];
109 if (m_computeNormals)
111 m_normals =
new coord3D[num_faces];
113 for (
unsigned int f = 0; f < num_faces; f++)
115 const unsigned int v1 = m_face_verts[f][0];
116 const unsigned int v2 = m_face_verts[f][1];
117 const unsigned int v3 = m_face_verts[f][2];
118 const unsigned int v4 = m_face_verts[f][3];
122 const float vec1[3] = {
123 m_vert_coords[
v3][0] - m_vert_coords[
v1][0],
124 m_vert_coords[
v3][1] - m_vert_coords[
v1][1],
125 m_vert_coords[
v3][2] - m_vert_coords[
v1][2]};
126 const float vec2[3] = {
127 m_vert_coords[v4][0] - m_vert_coords[
v2][0],
128 m_vert_coords[v4][1] - m_vert_coords[
v2][1],
129 m_vert_coords[v4][2] - m_vert_coords[
v2][2]};
130 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
131 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
132 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
136 const float vec1[3] = {
137 m_vert_coords[
v2][0] - m_vert_coords[
v1][0],
138 m_vert_coords[
v2][1] - m_vert_coords[
v1][1],
139 m_vert_coords[
v2][2] - m_vert_coords[
v1][2]};
140 const float vec2[3] = {
141 m_vert_coords[
v3][0] - m_vert_coords[
v1][0],
142 m_vert_coords[
v3][1] - m_vert_coords[
v1][1],
143 m_vert_coords[
v3][2] - m_vert_coords[
v1][2]};
144 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
145 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
146 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
155 unsigned int num_verts,
unsigned int num_faces,
156 const Array<bool, 1, Dynamic>& is_quad,
157 const Array<int, 4, Dynamic>& face_verts,
158 const Array<float, 3, Dynamic>& vert_coords)
160 m_num_verts = num_verts;
161 m_num_faces = num_faces;
164 m_is_quad =
new bool[num_faces];
165 for (
unsigned int i = 0; i < num_faces; i++) m_is_quad[i] = is_quad(i);
168 m_face_verts =
new f_verts[num_faces];
169 for (
unsigned int f = 0; f < num_faces; f++)
171 m_face_verts[f][0] = face_verts(0, f);
172 m_face_verts[f][1] = face_verts(1, f);
173 m_face_verts[f][2] = face_verts(2, f);
175 m_face_verts[f][3] = face_verts(3, f);
177 m_face_verts[f][3] = -1;
181 m_vert_coords =
new coord3D[num_verts];
182 for (
unsigned int i = 0; i < num_verts; i++)
184 m_vert_coords[i][0] = vert_coords(0, i);
185 m_vert_coords[i][1] = vert_coords(1, i);
186 m_vert_coords[i][2] = vert_coords(2, i);
190 m_normals =
new coord3D[num_faces];
191 if (m_computeNormals)
192 for (
unsigned int f = 0; f < num_faces; f++)
194 const unsigned int v1 = m_face_verts[f][0];
195 const unsigned int v2 = m_face_verts[f][1];
196 const unsigned int v3 = m_face_verts[f][2];
197 const unsigned int v4 = m_face_verts[f][3];
201 const float vec1[3] = {
202 m_vert_coords[
v3][0] - m_vert_coords[
v1][0],
203 m_vert_coords[
v3][1] - m_vert_coords[
v1][1],
204 m_vert_coords[
v3][2] - m_vert_coords[
v1][2]};
205 const float vec2[3] = {
206 m_vert_coords[v4][0] - m_vert_coords[
v2][0],
207 m_vert_coords[v4][1] - m_vert_coords[
v2][1],
208 m_vert_coords[v4][2] - m_vert_coords[
v2][2]};
209 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
210 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
211 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
215 const float vec1[3] = {
216 m_vert_coords[
v2][0] - m_vert_coords[
v1][0],
217 m_vert_coords[
v2][1] - m_vert_coords[
v1][1],
218 m_vert_coords[
v2][2] - m_vert_coords[
v1][2]};
219 const float vec2[3] = {
220 m_vert_coords[
v3][0] - m_vert_coords[
v1][0],
221 m_vert_coords[
v3][1] - m_vert_coords[
v1][1],
222 m_vert_coords[
v3][2] - m_vert_coords[
v1][2]};
223 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
224 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
225 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
237 #if MRPT_HAS_OPENGL_GLUT 239 if (m_enableTransparency || m_antiAliasing)
255 if (m_num_verts == 0)
return;
267 glColor4f(face_color[0], face_color[1], face_color[2], face_color[3]);
269 for (
unsigned int f = 0; f < m_num_faces; f++)
272 if (m_computeNormals)
273 glNormal3f(m_normals[f][0], m_normals[f][1], m_normals[f][2]);
279 for (
int i = 0; i < 4; i++)
281 const unsigned int vert_ind = m_face_verts[f][i];
283 m_vert_coords[vert_ind][0], m_vert_coords[vert_ind][1],
284 m_vert_coords[vert_ind][2]);
292 for (
int i = 0; i < 3; i++)
294 const unsigned int vert_ind = m_face_verts[f][i];
296 m_vert_coords[vert_ind][0], m_vert_coords[vert_ind][1],
297 m_vert_coords[vert_ind][2]);
307 glColor4f(edge_color[0], edge_color[1], edge_color[2], edge_color[3]);
312 for (
unsigned int f = 0; f < m_num_faces; f++)
314 const unsigned char num_vert = 3 + m_is_quad[f];
315 for (
int i = 0; i < num_vert - 1; i++)
317 const unsigned int v_0 = m_face_verts[f][i];
318 const unsigned int v_1 = m_face_verts[f][i + 1];
321 m_vert_coords[v_0][0], m_vert_coords[v_0][1],
322 m_vert_coords[v_0][2]);
324 m_vert_coords[v_1][0], m_vert_coords[v_1][1],
325 m_vert_coords[v_1][2]);
330 const int v_0 = m_face_verts[f][num_vert - 1];
331 const int v_1 = m_face_verts[f][0];
334 m_vert_coords[v_0][0], m_vert_coords[v_0][1],
335 m_vert_coords[v_0][2]);
337 m_vert_coords[v_1][0], m_vert_coords[v_1][1],
338 m_vert_coords[v_1][2]);
348 glColor4f(vert_color[0], vert_color[1], vert_color[2], vert_color[3]);
353 for (
unsigned int v = 0;
v < m_num_verts;
v++)
355 m_vert_coords[
v][0], m_vert_coords[
v][1], m_vert_coords[
v][2]);
407 if (m_num_verts == 0)
409 "\n The mesh is empty and has no size. The returned information " 413 for (
unsigned int i = 0; i < m_num_verts; i++)
416 if (m_vert_coords[i][0] > bb_max.
x) bb_max.
x = m_vert_coords[i][0];
417 if (m_vert_coords[i][1] > bb_max.
y) bb_max.
y = m_vert_coords[i][1];
418 if (m_vert_coords[i][2] > bb_max.
z) bb_max.
z = m_vert_coords[i][2];
421 if (m_vert_coords[i][0] < bb_min.
x) bb_min.
x = m_vert_coords[i][0];
422 if (m_vert_coords[i][1] < bb_min.
y) bb_min.
y = m_vert_coords[i][1];
423 if (m_vert_coords[i][2] < bb_min.
z) bb_min.
z = m_vert_coords[i][2];
428 m_pose.composePoint(bb_min, bb_min);
429 m_pose.composePoint(bb_max, bb_max);
GLuint GLuint GLsizei count
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
GLAPI void GLAPIENTRY glEnable(GLenum cap)
#define THROW_EXCEPTION(msg)
GLAPI void GLAPIENTRY glPointSize(GLfloat size)
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
void render_dl() const override
Render.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
GLAPI void GLAPIENTRY glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)
EIGEN_STRONG_INLINE void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
#define GL_ONE_MINUS_SRC_ALPHA
#define GL_COLOR_MATERIAL
A 3D mesh composed of Triangles and/or Quads.
GLAPI void GLAPIENTRY glShadeModel(GLenum mode)
GLAPI void GLAPIENTRY glLineWidth(GLfloat width)
GLAPI void GLAPIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
A renderizable object suitable for rendering with OpenGL's display lists.
void setVertColor(float r, float g, float b, float a=1.f)
This base provides a set of functions for maths stuff.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
GLfloat GLfloat GLfloat GLfloat v3
double x
X,Y,Z coordinates.
GLAPI void GLAPIENTRY glBegin(GLenum mode)
void loadMesh(unsigned int num_verts, unsigned int num_faces, int *verts_per_face, int *face_verts, float *vert_coords)
Load a 3D mesh.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
GLAPI void GLAPIENTRY glVertex3f(GLfloat x, GLfloat y, GLfloat z)
void setFaceColor(float r, float g, float b, float a=1.f)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
GLdouble GLdouble GLdouble r
virtual ~CMesh3D()
Private, virtual destructor: only can be deleted from smart pointers.
The namespace for 3D scene representation and rendering.
GLAPI void GLAPIENTRY glEnd(void)
GLfloat GLfloat GLfloat v2
GLAPI void GLAPIENTRY glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
void setEdgeColor(float r, float g, float b, float a=1.f)
GLAPI void GLAPIENTRY glDisable(GLenum cap)
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
GLubyte GLubyte GLubyte a