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