libalmath  2.5.7.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mesh.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015 Aldebaran. All rights reserved.
3  *
4  */
5 
6 #ifndef LIB_ALMATH_SCENEGRAPH_MESH_H
7 #define LIB_ALMATH_SCENEGRAPH_MESH_H
8 
9 #include <boost/optional.hpp>
10 #include <string>
11 #include <vector>
12 
13 namespace AL {
14 
15 // Mesh describe a mesh using a set of polygons using in buffers similar to
16 // Collada "polylist".
17 //
18 // The mesh is described using up to 6 arrays
19 // * a buffer of vertices positions coordinates: positions()
20 // * a buffer of vertices normals coordinates: normals()
21 // * optionally a buffer of vertices texture coordinates: texCoords()
22 // * a buffer of vertices which defines a vertex as a 2/3-tuple of indexes
23 // referring to the positions, normals and (optionally) texCoords
24 // buffers: vertices()
25 // * a buffer of polygons vertices counts (number of vertices per polygon),
26 // defining how the vertices are groupes per polygon: polygonVerticesCounts()
27 //
28 // The face winding is counter-clockwise.
29 // For instance, for the triangle defined as
30 //
31 // Mesh m;
32 // m.normal(0, 0, 1);
33 // m.begin(Mesh::TRIANGLES);
34 // m.vertex(0, 0.6, 0); // vertex 0
35 // m.vertex(-0.5, 0, 2); // vertex 1
36 // m.vertex(0, -0.6, 2, // vertex 2
37 // m.end();
38 //
39 // The 3 vertices are in the order 0->1->2, and the faces are:
40 //
41 // interior exterior
42 // (invisible) (visible)
43 // side side
44 // 0 0
45 // |\ /|
46 // | 1 1 |
47 // |/ \|
48 // 2 2
49 //
50 //
51 // For instance, let see how the following quadrilateron could be described
52 //
53 // 0 ^ y
54 // /|\ |
55 // 1 | 3 +--> x
56 // \|/
57 // 2
58 //
59 // vertice 0 is at position (0, 0.6, 2)
60 // vertice 1 is at position (-0.5, 0, 2)
61 // vertice 2 is at position (0, -0.6, 2)
62 // vertice 3 is at position (0.5, 0, 2)
63 // All three vertices have the same normal (0, 0, 1)
64 //
65 // m.positions():
66 // { 0, 0.6, 2,
67 // -0.5, 0, 2,
68 // 0, -0.6, 2,
69 // 0.5, 0, 2}
70 // m.normals():
71 // {0, 0, 1}
72 //
73 // If described as one quadrilateron:
74 // m.vertices():
75 // {0, 0, 1, 0, 2, 0, 3, 0}
76 // m.polygonVerticesCounts():
77 // {4}
78 //
79 // If described as two triangles:
80 // m.vertices():
81 // {0, 0, 1, 0, 2, 0, 0, 0, 2, 0, 3, 0}
82 // m.polygonVerticesCounts():
83 // {3, 3}
84 //
85 //
86 // The data sctructures were loosely adapted from:
87 // http://www.ics.com/blog/qt-and-opengl-part-1-loading-3d-model-open-asset-import-library-assimp
88 class Mesh {
89  public:
90  Mesh(bool withTexCoords = false);
91  bool withTexCoords() const;
92 
93  // number of indexes per vertex in the vertices() buffer
94  // 2 or 3 depending on withTexCoords()
95  size_t verticesStride() const;
96 
97  // offset for the vertex position index within a vertex indices 2/3-tuple
98  static const size_t positionOffset = 0;
99  // offset for the vertex normal index within a vertex indices 2/3-tuple
100  static const size_t normalOffset = 1;
101  // offset for the vertex texCoord index within a vertex indices 3-tuple
102  static const size_t texCoordOffset = 2;
103 
104  const std::vector<float> &positions() const;
105  size_t positionsNb() const;
106  float const *positionPtrAt(size_t index) const;
107 
108  const std::vector<float> &normals() const;
109  size_t normalsNb() const;
110  float const *normalPtrAt(size_t index) const;
111 
112  const std::vector<float> &texCoords() const;
113  size_t texCoordsNb() const;
114  float const *texCoordPtrAt(size_t index) const;
115 
116  const std::vector<size_t> &vertices() const;
117  size_t verticesNb() const;
118  size_t const *vertexPtrAt(size_t index) const;
119 
120  const std::vector<size_t> &polygonVerticesCounts() const;
121  size_t polygonsNb() const;
122  size_t polygonVerticesCountAt(size_t index) const;
123 
124  // the following members are useful to build a mesh
125 
127  static const size_t NO_INDEX;
128  // Add a vertex normal and declare it as the current one. Return its index.
129  // Modeled after OpenGl's glNormal.
130  size_t normal(float x, float y, float z);
131  // Declare the normal at index as the current one.
132  // Throw if the index is out range.
133  // Modeled after OpenGl's glNormal3v.
134  void normal(size_t index);
135 
136  // Add a texture coordinate and declare it as the current one. Return its
137  // index.
138  // Modeled after OpenGl's glTexCoord.
139  size_t texCoord(float u, float v);
140  // Declare the texture coordinate at index as the current one.
141  // Throw if the index is out range.
142  // Modeled after OpenGl's glTexCoord3v.
143  void texCoord(size_t index);
144 
145  // Add a vertex position. Return its index.
146  size_t position(float x, float y, float z);
147 
148  // Begin a new polygon (if mode is POLYGON) or a new set of triangles or
149  // quads (if mode is TRIANGES or QUADS, respectively).
150  // Throw if called between begin()/end() calls.
151  void begin(Mode mode);
152 
153  // create a new vertex using positionIndex and the current normal and
154  // texture coordinate indexes.
155  // Must be called between begin()/end() calls pair. Throw otherwise.
156  // Throw if positionIndex is out range.
157  void vertex(size_t positionIndex);
158  // create a new vertex using the provided position coordinates and the
159  // texture coordinate indexes.
160  // Equivalent to m.vertex(m.position(x,y,x))
161  // Modeled after glVertex
162  void vertex(float x, float y, float z);
163 
164  // End the current polygon or set of polygons.
165  // Must be called after a begin() call. Throw otherwise.
166  // Throw if there is an unfinished polygon.
167  void end();
168 
169  private:
170  // data which describes the mesh
171  bool _withTexCoords;
172  std::vector<float> _positions;
173  std::vector<float> _normals;
174  std::vector<float> _texCoords;
175  std::vector<size_t> _vertices;
176  std::vector<size_t> _vcounts;
177 
178  // data which is used when building the mesh
179  boost::optional<Mode> _currentMode;
180  // vertice count of the polygon currently being constructed
181  size_t _currentVcount;
182  size_t _currentNormal;
183  size_t _currentTexCoord;
184 };
185 
186 } // ends namespace AL
187 
188 #endif
float const * positionPtrAt(size_t index) const
const std::vector< float > & normals() const
size_t normal(float x, float y, float z)
size_t const * vertexPtrAt(size_t index) const
size_t texCoord(float u, float v)
size_t verticesNb() const
void vertex(size_t positionIndex)
static const size_t positionOffset
Definition: mesh.h:98
size_t texCoordsNb() const
void begin(Mode mode)
size_t normalsNb() const
static const size_t normalOffset
Definition: mesh.h:100
float const * normalPtrAt(size_t index) const
const std::vector< size_t > & vertices() const
Mode
Definition: mesh.h:126
float const * texCoordPtrAt(size_t index) const
void end()
const std::vector< float > & positions() const
size_t polygonsNb() const
const std::vector< float > & texCoords() const
static const size_t texCoordOffset
Definition: mesh.h:102
size_t position(float x, float y, float z)
size_t positionsNb() const
Definition: mesh.h:88
Mesh(bool withTexCoords=false)
const std::vector< size_t > & polygonVerticesCounts() const
static const size_t NO_INDEX
Definition: mesh.h:127
size_t verticesStride() const
size_t polygonVerticesCountAt(size_t index) const
bool withTexCoords() const