vbo_MapRender.h 8.86 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef _VBO_MAP_RENDER_
#define _VBO_MAP_RENDER_

28
#include <GL/glew.h>
Pierre Kraemer's avatar
Pierre Kraemer committed
29
30
31
#include <vector>
#include <list>

32
#include "Topology/generic/dart.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33
#include "Topology/generic/functor.h"
34
#include "Topology/generic/attributeHandler.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
35
#include "Container/convert.h"
36
#include "Geometry/vector_gen.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
37

38
39
#include "Utils/GLSLShader.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace VBO
{

enum drawingType {
	TRIANGLES = 1,
	LINES = 2,
	POINTS = 4,
	EXPLODED = 8,
	FLAT_TRIANGLES = 16,
	ERR = 32
} ;

enum bufferType {
	POSITIONS = 1,
	NORMALS = 2,
	COLORS = 4,
	TEXCOORDS = 8,
	ENDVECTORS = 16
} ;

enum bufferIndex {
	TRIANGLE_INDICES = 0,
	LINE_INDICES = 1,
	POINT_INDICES = 2,
Sylvain Thery's avatar
Sylvain Thery committed
73
74
75
	FLAT_BUFFER = 3,
	POSITIONS_BUFFER = 4,
	NORMALS_BUFFER = 5,
76
77
	COLORS_BUFFER = 6,
	FIRST_ATTRIBUTE_BUFFER = 7
Pierre Kraemer's avatar
Pierre Kraemer committed
78
79
} ;

80
const unsigned int NB_BUFFERS = 20 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Warning using attributes forbid using the following buildin attributes
// gl_SecondaryColor
// gl_FogCoord
// gl_MultiTexCoord0
// gl_MultiTexCoord1
// gl_MultiTexCoord2
// gl_MultiTexCoord3
// gl_MultiTexCoord4
// gl_MultiTexCoord5
// gl_MultiTexCoord6
// gl_MultiTexCoord7
const unsigned int FIRST_VERTEX_ATTRIB = 4 ;

const unsigned int NB_ATTRIBUTES = 12 ;
96

Pierre Kraemer's avatar
Pierre Kraemer committed
97
98
99
100
101
102
103
104
105
106
107
class MapRender_VBO
{

protected:
	/**
	 * vbo buffers
	 */
	GLuint m_VBOBuffers[NB_BUFFERS] ;
	bool m_allocatedBuffers[NB_BUFFERS] ;
	bool m_usedBuffers[NB_BUFFERS] ;

108
109
110
111
112
113
114
115
116
	bool m_allocatedAttributes[NB_ATTRIBUTES] ;
	bool m_usedAttributes[NB_ATTRIBUTES] ;
	unsigned int m_AttributesDataSize[NB_ATTRIBUTES];

	unsigned int m_nbVertexAttrib;

	std::map<std::string,GLuint> m_attributebyName;


Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
	/**
	 * number of indices of triangles
	 */
	GLuint m_nbIndicesTri ;

	/**
	 * number of indices of lines
	 */
	GLuint m_nbIndicesLines ;

	/**
	 * number of indices of points
	 */
	GLuint m_nbIndicesPoints ;


	GLuint m_nbFlatElts;

public:
	/**
	 * Constructor
	 */
139
	MapRender_VBO() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140

Sylvain Thery's avatar
Sylvain Thery committed
141
142
143
144
145
	/**
	 * Constructor that share vertices attributes vbo (position/normals/colors...)
	 */
	MapRender_VBO(const MapRender_VBO& mrvbo);

Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	/**
	 * Destructor
	 */
	~MapRender_VBO() ;

	/**
	 * update the data
	 * @param uptype that have to be updated: POSITIONS, NORMALS, COLORS, TEXCOORDS, ???
	 * @param attribId attribute where data is stored
	 * @param conv Callback of attribute conversion (NULL if direct copy, default value)
	 */
	template <typename ATTR_HANDLER>
	void updateData(int upType, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;

	void enableBuffers(int buffersMask) ;
	void disableBuffers(int buffersMask) ;

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

	/**
	 * update the data for vertex attributes
	 * @param vertex_attrib vertex attrib id
	 * @param attrib attribute where data is stored
	 * @param conv Callback of attribute conversion (NULL if direct copy, default value)
	 */
	template <typename ATTR_HANDLER>
	void updateVAData(unsigned int vertex_attrib, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;

	/**
	 * update the data for vertex attributes
	 * @param va_name vertex attrib name (in shader)
	 * @param attrib attribute where data is stored
	 * @param conv Callback of attribute conversion (NULL if direct copy, default value)
	 */
	template <typename ATTR_HANDLER>
	void updateVAData(const std::string& name, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;


	/**
	 * enable a vertex attribute for rendering (updateDate automatically enable attrib)
	 */
	void enableVertexAttrib(const std::string& name);

	/**
	 * disable a vertex attribute for rendering
	 */
	void disableVertexAttrib(const std::string& name);


	/**
	 * associate a name to a vertex attribute
	 * @param name the name in shader
	 * @param sh the shader
	 * @return the id to use with update (if not using name)
	 */
	unsigned int useVertexAttributeName(const std::string& name, const Utils::GLSLShader& sh);

Pierre Kraemer's avatar
Pierre Kraemer committed
202
protected:
203
204
205
206
207
208
209
210
211
212
213
214
215

	unsigned int vbo_index_attribute( unsigned int att) { return att + FIRST_ATTRIBUTE_BUFFER - FIRST_VERTEX_ATTRIB;}

	/**
	 * enable a vertex attribute for rendering (updateDate automatically enable attrib)
	 */
	void enableVertexAttrib(unsigned int index);

	/**
	 * disable a vertex attribute for rendering
	 */
	void disableVertexAttrib(unsigned int index);

Pierre Kraemer's avatar
Pierre Kraemer committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	/**
	* fill buffer directly from attribute
	*/
	template <typename ATTR_HANDLER>
	void fillBufferDirect(unsigned int indexVBO, const ATTR_HANDLER& attrib) ;

	/**
	* fill buffer with conversion from attribute
	*/
	template <typename ATTR_HANDLER>
	void fillBufferConvert(unsigned int indexVBO, const ATTR_HANDLER& attrib, ConvertAttrib* conv) ;

	/**
	 * addition of indices table of one triangle
	 * @param d a dart of the triangle
	 * @param tableIndices the indices table
	 */
233
234
	template <typename PFP>
	void addTri(typename PFP::MAP& map, Dart d, std::vector<GLuint>& tableIndices) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
235
236
237
238
239
240

public:
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
241
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
242
	void initTriangles(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
243
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
244
	void initTrianglesOptimized(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
245
246
247
248
249

	/**
	 * creation of indices table of lines (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
250
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
251
	void initLines(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
252
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
253
	void initLinesOptimized(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
254
255
256
257
258

	/**
	 * creation of indices table of points
	 * @param tableIndices the table where indices are stored
	 */
259
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
260
	void initPoints(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
261
262
263
264

	/**
	 * creation of VBO for flat faces rendering
	 */
265
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
266
	void initFlatTriangles(typename PFP::MAP& map, const FunctorSelect& good, unsigned int thread=0);
Pierre Kraemer's avatar
Pierre Kraemer committed
267
268
269
270
271
272

	/**
	 * initialization of the VBO indices primitives
	 * computed by a traversal of the map
	 * @param prim primitive to draw: VBO_TRIANGLES, VBO_LINES
	 */
273
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
274
	void initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized = true, unsigned int thread=0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	/**
	 * initialization of the VBO indices primitives
	 * using the given table
	 * @param prim primitive to draw: VBO_TRIANGLES, VBO_LINES
	 */
	void initPrimitives(int prim, std::vector<GLuint>& tableIndices) ;

protected:
	/**
	 * Drawing triangles function
	 */
	void drawTriangles(bool bindColors = true) ;

	/**
	 * Drawing lines function
	 */
	void drawLines(bool bindColors = true) ;

	/**
	 * Drawing points function
	 */
	void drawPoints(bool bindColors = true) ;

	/**
	 * Drawing flat faces function
	 */
	void drawFlat();

public:
	/**
	 * draw the VBO (function to call in the drawing callback)
	 */
	void draw(int prim, bool bindColors = true) ;
} ;

} // namespace VBO

} // namespace Render

} // namespace Algo

} // namespace CGoGN

319
#include "Algo/Render/Deprecated/vbo_MapRender.hpp"
Pierre Kraemer's avatar
Pierre Kraemer committed
320
321

#endif