MRPT  2.0.4
CMesh3D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/math/CMatrixFixed.h>
13 #include <mrpt/math/TPoint3D.h>
17 #include <array>
18 
19 namespace mrpt::opengl
20 {
21 /** A 3D mesh composed of Triangles and/or Quads.
22  * A typical usage example would be a 3D model of an object.
23  * \sa opengl::COpenGLScene,opengl::CMesh,opengl::CAssimpModel
24  *
25  * <div align="center">
26  * <table border="0" cellspan="4" cellspacing="4" style="border-width: 1px;
27  * border-style: solid;">
28  * <tr> <td> mrpt::opengl::CMesh3D </td> <td> \image html preview_CMesh3D.png
29  * </td> </tr>
30  * </table>
31  * </div>
32  *
33  * \ingroup mrpt_opengl_grp
34  */
38 {
40 
41  public:
42  /** @name Renderizable shader API virtual methods
43  * @{ */
44  void render(const RenderContext& rc) const override;
45  void renderUpdateBuffers() const override;
46  void freeOpenGLResources() override
47  {
51  }
52 
53  virtual shader_list_t requiredShaders() const override
54  {
57  }
58  void onUpdateBuffers_Wireframe() override;
59  void onUpdateBuffers_Triangles() override;
60  void onUpdateBuffers_Points() override;
61  /** @} */
62 
63  CMesh3D() = default;
64  virtual ~CMesh3D() override;
65 
66  void enableShowEdges(bool v)
67  {
68  m_showEdges = v;
70  }
71  void enableShowFaces(bool v)
72  {
73  m_showFaces = v;
75  }
76  void enableShowVertices(bool v)
77  {
78  m_showVertices = v;
80  }
81  void enableFaceNormals(bool v)
82  {
83  m_computeNormals = v;
85  }
86 
87  /** Load a 3D mesh. The arguments indicate:
88  - num_verts: Number of vertices of the mesh
89  - num_faces: Number of faces of the mesh
90  - verts_per_face: An array (pointer) with the number of vertices of each
91  face. The elements must be set either to 3 (triangle) or 4 (quad).
92  - face_verts: An array (pointer) with the vertices of each face. The
93  vertices of each face must be consecutive in this array.
94  - vert_coords: An array (pointer) with the coordinates of each vertex.
95  The xyz coordinates of each vertex must be consecutive in this array.
96  */
97  void loadMesh(
98  unsigned int num_verts, unsigned int num_faces, int* verts_per_face,
99  int* face_verts, float* vert_coords);
100 
101  /** Load a 3D mesh. The arguments indicate:
102  - num_verts: Number of vertices of the mesh
103  - num_faces: Number of faces of the mesh
104  - is_quad: A binary array saying whether the face is a quad (1)
105  or a triangle (0)
106  - face_verts: An array with the vertices of each face. For every
107  column (face), each row contains the num of a vertex. The fourth does not
108  need
109  to be filled if the face is a triangle.
110  - vert_coords: An array with the coordinates of each vertex. For
111  every column (vertex), each row contains the xyz coordinates of the
112  vertex.
113  */
114  void loadMesh(
115  unsigned int num_verts, unsigned int num_faces,
116  const mrpt::math::CMatrixDynamic<bool>& is_quad,
117  const mrpt::math::CMatrixDynamic<int>& face_verts,
118  const mrpt::math::CMatrixDynamic<float>& vert_coords);
119 
120  void setEdgeColor(float r, float g, float b, float a = 1.f)
121  {
122  edge_color = mrpt::img::TColorf(r, g, b, a);
123  }
124  void setFaceColor(float r, float g, float b, float a = 1.f)
125  {
126  face_color = mrpt::img::TColorf(r, g, b, a);
127  }
128  void setVertColor(float r, float g, float b, float a = 1.f)
129  {
130  vert_color = mrpt::img::TColorf(r, g, b, a);
131  }
132 
133  void getBoundingBox(
135  mrpt::math::TPoint3D& bb_max) const override;
136 
137  protected:
139 
140  bool m_showEdges = true;
141  bool m_showFaces = true;
142  bool m_showVertices = false;
143  bool m_computeNormals = true;
144 
145  /** Pointer storing whether a face is a quad (1) or a triangle (0) */
146  std::vector<bool> m_is_quad;
147  /** Pointer storing the vertices that compose each face. Size: 4 x num_faces
148  * (4 for the possible max number - quad) */
149  std::vector<vertex_indices_t> m_face_verts;
150 
151  /** Pointer storing the coordinates of the vertices. Size: 3 x num_vertices
152  */
153  std::vector<mrpt::math::TPoint3Df> m_vertices;
154 
155  /** Pointer storing the face normals. Size: 3 x num_faces */
156  std::vector<mrpt::math::TPoint3Df> m_normals;
157 
158  /** Color of the edges */
159  mrpt::img::TColorf edge_color = {.9f, .9f, .9f, 1.0f};
160 
161  /** Color of the faces */
162  mrpt::img::TColorf face_color = {.7f, .7f, .8f, 1.0f};
163 
164  /** Color of the vertices */
165  mrpt::img::TColorf vert_color = {.3f, .3f, .3f, 1.0f};
166 };
167 
168 } // namespace mrpt::opengl
mrpt::opengl::CMesh3D::enableFaceNormals
void enableFaceNormals(bool v)
Definition: CMesh3D.h:81
mrpt::opengl::CMesh3D::loadMesh
void loadMesh(unsigned int num_verts, unsigned int num_faces, int *verts_per_face, int *face_verts, float *vert_coords)
Load a 3D mesh.
Definition: CMesh3D.cpp:29
mrpt::opengl::CRenderizable::notifyChange
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
Definition: CRenderizable.h:315
mrpt::opengl::CMesh3D::m_is_quad
std::vector< bool > m_is_quad
Pointer storing whether a face is a quad (1) or a triangle (0)
Definition: CMesh3D.h:146
CMatrixFixed.h
mrpt::math::TPoint3D_< double >
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:152
mrpt::opengl::CMesh3D::onUpdateBuffers_Points
void onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CMesh3D.cpp:261
mrpt::opengl::CMesh3D::~CMesh3D
virtual ~CMesh3D() override
mrpt::opengl::CRenderizableShaderWireFrame
Renderizable generic renderer for objects using the wireframe shader.
Definition: CRenderizableShaderWireFrame.h:24
mrpt::opengl::CMesh3D::getBoundingBox
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...
Definition: CMesh3D.cpp:296
mrpt::opengl::CMesh3D::requiredShaders
virtual shader_list_t requiredShaders() const override
Returns the ID of the OpenGL shader program required to render this class.
Definition: CMesh3D.h:53
mrpt::opengl::CMesh3D::onUpdateBuffers_Triangles
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CMesh3D.cpp:238
mrpt::opengl::CMesh3D::m_showVertices
bool m_showVertices
Definition: CMesh3D.h:142
mrpt::opengl::CMesh3D::enableShowVertices
void enableShowVertices(bool v)
Definition: CMesh3D.h:76
mrpt::opengl::CRenderizableShaderTriangles::freeOpenGLResources
void freeOpenGLResources() override
Free opengl buffers.
Definition: CRenderizableShaderTriangles.h:45
mrpt::opengl::CMesh3D::edge_color
mrpt::img::TColorf edge_color
Color of the edges.
Definition: CMesh3D.h:159
mrpt::opengl::CRenderizableShaderWireFrame::freeOpenGLResources
void freeOpenGLResources() override
Free opengl buffers.
Definition: CRenderizableShaderWireFrame.h:57
mrpt::opengl::CMesh3D::enableShowFaces
void enableShowFaces(bool v)
Definition: CMesh3D.h:71
CRenderizableShaderTriangles.h
mrpt::opengl::CMesh3D::face_color
mrpt::img::TColorf face_color
Color of the faces.
Definition: CMesh3D.h:162
bb_max
const auto bb_max
Definition: CPose3DPDFGrid_unittest.cpp:25
mrpt::opengl::CMesh3D::onUpdateBuffers_Wireframe
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CMesh3D.cpp:208
mrpt::opengl::DefaultShaderID::WIREFRAME
static constexpr shader_id_t WIREFRAME
Definition: DefaultShaders.h:25
bb_min
const auto bb_min
Definition: CPose3DPDFGrid_unittest.cpp:23
mrpt::opengl::DefaultShaderID::TRIANGLES
static constexpr shader_id_t TRIANGLES
Definition: DefaultShaders.h:27
mrpt::math::CMatrixFixed
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
mrpt::opengl::CMesh3D::setVertColor
void setVertColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:128
mrpt::opengl::CMesh3D
A 3D mesh composed of Triangles and/or Quads.
Definition: CMesh3D.h:35
mrpt::opengl::CMesh3D::m_face_verts
std::vector< vertex_indices_t > m_face_verts
Pointer storing the vertices that compose each face.
Definition: CMesh3D.h:149
CRenderizableShaderWireFrame.h
mrpt::img::TColorf
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
mrpt::opengl::CMesh3D::m_vertices
std::vector< mrpt::math::TPoint3Df > m_vertices
Pointer storing the coordinates of the vertices.
Definition: CMesh3D.h:153
mrpt::opengl::CRenderizable::RenderContext
Context for calls to render()
Definition: CRenderizable.h:266
mrpt::opengl::CMesh3D::CMesh3D
CMesh3D()=default
mrpt::opengl::CRenderizableShaderPoints::freeOpenGLResources
void freeOpenGLResources() override
Free opengl buffers.
Definition: CRenderizableShaderPoints.h:86
mrpt::opengl::CMesh3D::m_normals
std::vector< mrpt::math::TPoint3Df > m_normals
Pointer storing the face normals.
Definition: CMesh3D.h:156
TPoint3D.h
mrpt::opengl::CMesh3D::vert_color
mrpt::img::TColorf vert_color
Color of the vertices.
Definition: CMesh3D.h:165
CRenderizableShaderPoints.h
mrpt::opengl::CMesh3D::m_computeNormals
bool m_computeNormals
Definition: CMesh3D.h:143
mrpt::opengl::CMesh3D::m_showEdges
bool m_showEdges
Definition: CMesh3D.h:140
mrpt::opengl::CRenderizableShaderPoints
Renderizable generic renderer for objects using the points shader.
Definition: CRenderizableShaderPoints.h:38
mrpt::opengl::shader_list_t
std::vector< shader_id_t > shader_list_t
A list of shader IDs.
Definition: Shader.h:26
mrpt::opengl::CRenderizableShaderTriangles
Renderizable generic renderer for objects using the triangles shader.
Definition: CRenderizableShaderTriangles.h:25
mrpt::opengl::CMesh3D::m_showFaces
bool m_showFaces
Definition: CMesh3D.h:141
mrpt::opengl::CMesh3D::renderUpdateBuffers
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers,...
Definition: CMesh3D.cpp:201
mrpt::opengl::CMesh3D::setEdgeColor
void setEdgeColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:120
mrpt::opengl::CMesh3D::freeOpenGLResources
void freeOpenGLResources() override
Free opengl buffers.
Definition: CMesh3D.h:46
mrpt::opengl::CMesh3D::render
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CMesh3D.cpp:186
mrpt::opengl::CMesh3D::enableShowEdges
void enableShowEdges(bool v)
Definition: CMesh3D.h:66
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
mrpt::opengl::DefaultShaderID::POINTS
static constexpr shader_id_t POINTS
Definition: DefaultShaders.h:24
mrpt::math::CMatrixDynamic
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Definition: CMatrixDynamic.h:39
mrpt::opengl::CMesh3D::setFaceColor
void setFaceColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:124



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Fri Jul 17 08:43:33 UTC 2020