GLIP-Lib
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Geometry.hpp
Go to the documentation of this file.
1 /* ************************************************************************************************************* */
2 /* */
3 /* GLIP-LIB */
4 /* OpenGL Image Processing LIBrary */
5 /* */
6 /* Author : R. Kerviche */
7 /* LICENSE : MIT License */
8 /* Website : glip-lib.net */
9 /* */
10 /* File : Geometry.hpp */
11 /* Original Date : June 29th 2013 */
12 /* */
13 /* Description : Geometry objects and memory management */
14 /* */
15 /* ************************************************************************************************************* */
16 
24 #ifndef __GLIPLIB_GEOMETRY__
25 #define __GLIPLIB_GEOMETRY__
26 
27  // Includes
28  #include <map>
29  #include "devDebugTools.hpp"
30  #include "Core/LibTools.hpp"
31  #include "Core/OglInclude.hpp"
32  #include "Core/HdlVBO.hpp"
33 
34  namespace Glip
35  {
36  namespace CoreGL
37  {
38  // Prototypes
39  class HdlVBO;
40  }
41 
42  using namespace CoreGL;
43 
44  namespace CorePipeline
45  {
50  class GLIP_API GeometryModel
51  {
52  public :
55  {
67  Unknown
68  };
69 
70  private :
71  std::vector<GLfloat> vertices,
72  normals,
73  texCoords;
74  std::vector<GLuint> elements;
75 
76  protected :
77  // Tools :
78  GeometryModel(GeometryType _type, int _dim, GLenum _primitiveGL, bool _hasNormals, bool _hasTexCoords);
79 
80  void reserveVertices(size_t nVertices);
81  void increaseVerticesReservation(size_t nNewVertices);
82  void reserveElements(size_t nVertices);
83  void increaseElementsReservation(size_t nNewVertices);
84  void addVertices2DInterleaved(const size_t N, const GLfloat* interleavedXY, const GLfloat* interleavedNormalsXY=NULL, const GLfloat* interleavedUV=NULL);
85  void addVertices2D(const size_t N, const GLfloat* x, const GLfloat* y, const GLfloat* nx=NULL, const GLfloat* ny=NULL, const GLfloat* u=NULL, const GLfloat* v=NULL);
86  GLuint addVertex2D(const GLfloat& x, const GLfloat& y, const GLfloat& nx=0.0f, const GLfloat& ny=0.0f, const GLfloat& u=0.0f, const GLfloat& v=0.0f);
87  void addVertices3DInterleaved(const size_t N, const GLfloat* interleavedXYZ, const GLfloat* interleavedNormalsXYZ=NULL, const GLfloat* interleavedUV=NULL);
88  void addVertices3D(const size_t N, const GLfloat* x, const GLfloat* y, const GLfloat* z, const GLfloat* nx=NULL, const GLfloat* ny=NULL, const GLfloat* nz=NULL, const GLfloat* u=NULL, const GLfloat* v=NULL);
89  GLuint addVertex3D(const GLfloat& x, const GLfloat& y, const GLfloat& z, const GLfloat& nx=0.0f, const GLfloat& ny=0.0f, const GLfloat& nz=0.0f, const GLfloat& u=0.0f, const GLfloat& v=0.0f);
90  void addElementsInterleaved(const size_t N, GLuint* interleavedIndices);
91  void addElements(const size_t N, GLuint* a, GLuint* b=NULL, GLuint* c=NULL, GLuint* d=NULL);
92  GLuint addElement(GLuint a);
93  GLuint addElement(GLuint a, GLuint b);
94  GLuint addElement(GLuint a, GLuint b, GLuint c);
95  GLuint addElement(GLuint a, GLuint b, GLuint c, GLuint d);
96  GLuint addElement(const std::vector<GLuint>& indices);
97  GLfloat& x(GLuint i);
98  GLfloat& y(GLuint i);
99  GLfloat& z(GLuint i);
100  GLfloat& nx(GLuint i);
101  GLfloat& ny(GLuint i);
102  GLfloat& nz(GLuint i);
103  GLfloat& u(GLuint i);
104  GLfloat& v(GLuint i);
105  GLuint& a(GLuint i);
106  GLuint& b(GLuint i);
107  GLuint& c(GLuint i);
108  GLuint& d(GLuint i);
109  void generateNormals(void);
110 
111  public :
115  const bool hasNormals,
117  hasTexCoords;
119  const int dim,
123  elementStride;
125  const GLenum primitiveGL;
126 
127  GeometryModel(const GeometryModel& mdl);
128  virtual ~GeometryModel(void);
129 
130  unsigned int getNumVertices(void) const;
131  unsigned int getNumElements(void) const;
132  const GLenum& getGLPrimitive(void) const;
133  const GLfloat& x(GLuint i) const;
134  const GLfloat& y(GLuint i) const;
135  const GLfloat& z(GLuint i) const;
136  const GLfloat& nx(GLuint i) const;
137  const GLfloat& ny(GLuint i) const;
138  const GLfloat& nz(GLuint i) const;
139  const GLfloat& u(GLuint i) const;
140  const GLfloat& v(GLuint i) const;
141  const GLuint& a(GLuint i) const;
142  const GLuint& b(GLuint i) const;
143  const GLuint& c(GLuint i) const;
144  const GLuint& d(GLuint i) const;
145  bool testIndices(void) const;
146  bool operator==(const GeometryModel& mdl) const;
147 
148  HdlVBO* getVBO(GLenum freq) const;
149 
150  static int getNumVerticesInPrimitive(const GLenum& _primitiveGL);
151  static int getPrimitiveStride(const GLenum& _primitiveGL);
152  };
153 
158  class GLIP_API GeometryInstance
159  {
160  private :
161  // Static memory bank :
162  static int nextIndex;
163  static std::map<int, HdlVBO*> vbos;
164  static std::map<int, GeometryModel*> models;
165  static std::map<int, int> counters;
166 
167  // Current :
168  int id;
169 
170  public :
171  GeometryInstance(const GeometryModel& mdl, GLenum freq);
172  GeometryInstance(const GeometryInstance& instance);
173  ~GeometryInstance(void);
174 
175  const GeometryModel& model(void) const;
176  const HdlVBO& vbo(void) const;
177  void draw(void);
178  };
179 
180  // Geometries :
181  namespace GeometryPrimitives
182  {
187  class GLIP_API StandardQuad : public GeometryModel
188  {
189  public :
190  StandardQuad(void);
191  StandardQuad(const StandardQuad& mdl);
192  };
193 
198  class GLIP_API ReversedQuad : public GeometryModel
199  {
200  public :
201  ReversedQuad(void);
202  ReversedQuad(const ReversedQuad& mdl);
203  };
204 
209  class GLIP_API PointsGrid2D : public GeometryModel
210  {
211  public :
212  const int width,
213  height;
214  const bool normalized;
215 
216  PointsGrid2D(int w, int h, bool _normalized=false);
217  PointsGrid2D(const PointsGrid2D& mdl);
218  };
219 
224  class GLIP_API PointsGrid3D : public GeometryModel
225  {
226  public :
227  const int width,
228  height,
229  depth;
230  const bool normalized;
231 
232  PointsGrid3D(int w, int h, int d, bool _normalized=false);
233  PointsGrid3D(const PointsGrid3D& mdl);
234  };
235 
240  class GLIP_API CustomModel : public GeometryModel
241  {
242  public :
243  CustomModel(int _dim, GLenum _primitiveGL, bool _hasNormals, bool _hasTexCoords);
244 
245  void reserveVertices(size_t nVertices);
246  void increaseVerticesReservation(size_t nNewVertices);
247  void reserveElements(size_t nVertices);
248  void increaseElementsReservation(size_t nNewVertices);
249  void newVertices2DInterleaved(const size_t N, const GLfloat* interleavedXY, const GLfloat* interleavedNormalsXY=NULL, const GLfloat* interleavedUV=NULL);
250  void newVertices2D(const size_t N, const GLfloat* x, const GLfloat* y, const GLfloat* nx=NULL, const GLfloat* ny=NULL, const GLfloat* u=NULL, const GLfloat* v=NULL);
251  GLuint newVertex2D(const GLfloat& x, const GLfloat& y, const GLfloat& nx=0.0f, const GLfloat& ny=0.0f, const GLfloat& u=0.0f, const GLfloat& v=0.0f);
252  void newVertices3DInterleaved(const size_t N, const GLfloat* interleavedXYZ, const GLfloat* interleavedNormalsXYZ=NULL, const GLfloat* interleavedUV=NULL);
253  void newVertices3D(const size_t N, const GLfloat* x, const GLfloat* y, const GLfloat* z, const GLfloat* nx=NULL, const GLfloat* ny=NULL, const GLfloat* nz=NULL, const GLfloat* u=NULL, const GLfloat* v=NULL);
254  GLuint newVertex3D(const GLfloat& x, const GLfloat& y, const GLfloat& z, const GLfloat& nx=0.0f, const GLfloat& ny=0.0f, const GLfloat& nz=0.0f, const GLfloat& u=0.0f, const GLfloat& v=0.0f);
255  void newElementsInterleaved(const size_t N, GLuint* interleavedIndices);
256  void newElements(const size_t N, GLuint* a, GLuint* b=NULL, GLuint* c=NULL, GLuint* d=NULL);
257  GLuint newElement(GLuint a);
258  GLuint newElement(GLuint a, GLuint b);
259  GLuint newElement(GLuint a, GLuint b, GLuint c);
260  GLuint newElement(GLuint a, GLuint b, GLuint c, GLuint d);
261  GLuint newElement(const std::vector<GLuint>& indices);
262  void generateNormals(void);
263  };
264  }
265  }
266  }
267 
268 #endif
269 
Specific defines for the API structure.
Instance of the GeometryModel. Stored on GPU (VBO).
Definition: Geometry.hpp:158
GeometryType
Description of the type of geometry (automatic).
Definition: Geometry.hpp:54
Object handle for OpenGL Vertex Buffer Objects.
Definition: HdlVBO.hpp:41
3D grid of dots.
Definition: Geometry.hpp:65
Definition: Component.hpp:32
Reversed quad, covering the area between (-1,-1) and (1,1).
Definition: Geometry.hpp:61
Geometry : the standard quad, covering the area between (-1,-1) and (1,1).
Definition: Geometry.hpp:187
Geometry : a 3D grid of points.
Definition: Geometry.hpp:224
Geometry : a 2D grid of points.
Definition: Geometry.hpp:209
const GLenum primitiveGL
ID of the primitive for GL.
Definition: Geometry.hpp:125
2D grid of dots.
Definition: Geometry.hpp:63
Geometry : build a model.
Definition: Geometry.hpp:240
const bool hasTexCoords
Dimension of the geometry (either 2 or 3).
Definition: Geometry.hpp:115
OpenGL Pixel Buffer Object Handle.
Custom model.
Definition: Geometry.hpp:57
Development and debugging tools.
const GeometryType type
Geometry Type.
Definition: Geometry.hpp:113
Geometry stored on the host.
Definition: Geometry.hpp:50
Standard quad, covering the area between (-1,-1) and (1,1).
Definition: Geometry.hpp:59
OpenGL includes and tools.
const int numVerticesPerElement
Number of vertices per elements of the geometry.
Definition: Geometry.hpp:119
Geometry : the reversed quad, covering the area between (-1,-1) and (1,1).
Definition: Geometry.hpp:198