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
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
void setVertColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:128
void freeOpenGLResources() override
Free opengl buffers.
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
Renderizable generic renderer for objects using the triangles shader.
void freeOpenGLResources() override
Free opengl buffers.
A 3D mesh composed of Triangles and/or Quads.
Definition: CMesh3D.h:35
void enableShowEdges(bool v)
Definition: CMesh3D.h:66
std::vector< shader_id_t > shader_list_t
A list of shader IDs.
Definition: Shader.h:26
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CMesh3D.cpp:186
Context for calls to render()
std::vector< mrpt::math::TPoint3Df > m_normals
Pointer storing the face normals.
Definition: CMesh3D.h:156
mrpt::img::TColorf vert_color
Color of the vertices.
Definition: CMesh3D.h:165
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Definition: CMesh3D.cpp:201
void setEdgeColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:120
void freeOpenGLResources() override
Free opengl buffers.
Definition: CMesh3D.h:46
static constexpr shader_id_t WIREFRAME
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
void setFaceColor(float r, float g, float b, float a=1.f)
Definition: CMesh3D.h:124
Renderizable generic renderer for objects using the points shader.
static constexpr shader_id_t 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
void enableFaceNormals(bool v)
Definition: CMesh3D.h:81
virtual ~CMesh3D() override
std::vector< bool > m_is_quad
Pointer storing whether a face is a quad (1) or a triangle (0)
Definition: CMesh3D.h:146
Renderizable generic renderer for objects using the wireframe shader.
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
void enableShowFaces(bool v)
Definition: CMesh3D.h:71
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
mrpt::img::TColorf edge_color
Color of the edges.
Definition: CMesh3D.h:159
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
const auto bb_min
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
void freeOpenGLResources() override
Free opengl buffers.
virtual shader_list_t requiredShaders() const override
Returns the ID of the OpenGL shader program required to render this class.
Definition: CMesh3D.h:53
This template class provides the basic functionality for a general 2D any-size, resizable container o...
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
std::vector< mrpt::math::TPoint3Df > m_vertices
Pointer storing the coordinates of the vertices.
Definition: CMesh3D.h:153
std::vector< vertex_indices_t > m_face_verts
Pointer storing the vertices that compose each face.
Definition: CMesh3D.h:149
void enableShowVertices(bool v)
Definition: CMesh3D.h:76
mrpt::img::TColorf face_color
Color of the faces.
Definition: CMesh3D.h:162
static constexpr shader_id_t POINTS



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 7ea9b7e81 Mon May 25 11:43:10 2020 +0200 at lun may 25 11:45:15 CEST 2020