gl3mapRender.h 7.62 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef _GL3_MAP_RENDER_
#define _GL3_MAP_RENDER_

#include <GL/glew.h>
#include <vector>
#include <list>

#include "Topology/generic/dart.h"
#include "Topology/generic/functor.h"
#include "Topology/generic/attributeHandler.h"
#include "Container/convert.h"
#include "Geometry/vector_gen.h"

Sylvain Thery's avatar
Sylvain Thery committed
38 39
#include "Utils/GLSLShader.h"

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 73 74 75 76 77 78
namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace GL3
{

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

enum bufferIndex {
	TRIANGLE_INDICES = 0,
	LINE_INDICES = 1,
	POINT_INDICES = 2,
	FLAT_BUFFER = 3,
	FIRST_ATTRIBUTE_BUFFER = 4,
} ;

const unsigned int NB_BUFFERS = 16 ;

class MapRender
{

protected:
	/**
	 * vbo buffers
	 */
	GLuint m_VBOBuffers[NB_BUFFERS] ;
Sylvain Thery's avatar
bug  
Sylvain Thery committed
79 80 81 82

	/**
	 *
	 */
83
	bool m_allocatedAttributes[NB_BUFFERS] ;
Sylvain Thery's avatar
bug  
Sylvain Thery committed
84 85 86 87

	/**
	 *
	 */
88 89
	bool m_usedAttributes[NB_BUFFERS] ;

Sylvain Thery's avatar
bug  
Sylvain Thery committed
90 91 92
	/**
	 *
	 */
93 94
	unsigned int m_AttributesDataSize[NB_BUFFERS];

Sylvain Thery's avatar
bug  
Sylvain Thery committed
95 96 97
	/**
	 *
	 */
Sylvain Thery's avatar
Sylvain Thery committed
98 99 100 101 102 103 104
	std::map<std::string,GLuint> m_attributebyName;

	/**
	 * number of vertex attributes
	 */
	GLuint m_nbVertexAttrib ;

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	/**
	 * number of indices of triangles
	 */
	GLuint m_nbIndicesTri ;

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

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

	/**
	 * number of elts for flat vbo
	 */
	GLuint m_nbFlatElts;

public:
	/**
	 * Constructor
	 */
	MapRender() ;

	/**
	 * Constructor that share vertices attributes vbo (position/normals/colors...)
	 */
	MapRender(const MapRender& mrvbo);

	/**
	 * Destructor
	 */
	~MapRender() ;

Sylvain Thery's avatar
Sylvain Thery committed
141
public:
142 143
	/**
	 * update the data
Sylvain Thery's avatar
Sylvain Thery committed
144 145
	 * @param vertex_attrib vertex attrib id
	 * @param attrib attribute where data is stored
146 147 148 149 150
	 * @param conv Callback of attribute conversion (NULL if direct copy, default value)
	 */
	template <typename ATTR_HANDLER>
	void updateData(unsigned int vertex_attrib, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;

Sylvain Thery's avatar
Sylvain Thery committed
151 152 153 154 155 156 157 158 159
	/**
	 * update the data
	 * @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 updateData(const std::string& name, const ATTR_HANDLER& attrib, ConvertAttrib* conv = NULL) ;

160 161 162
	/**
	 * enable a vertex attribute for rendering (updateDate automatically enable attrib)
	 */
Sylvain Thery's avatar
Sylvain Thery committed
163
	void enableVertexAttrib(const std::string& name);
164 165 166 167

	/**
	 * disable a vertex attribute for rendering
	 */
Sylvain Thery's avatar
Sylvain Thery committed
168
	void disableVertexAttrib(const std::string& name);
169

Sylvain Thery's avatar
Sylvain Thery committed
170 171 172 173 174 175 176 177
	/**
	 * 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);

178
protected:
Sylvain Thery's avatar
Sylvain Thery committed
179 180 181 182 183 184 185 186 187 188
	/**
	 * 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);

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	/**
	* 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
	 */
	template <typename PFP>
	void addTri(typename PFP::MAP& map, Dart d, std::vector<GLuint>& tableIndices) ;

public:
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
215
	void initTriangles(typename PFP::MAP& map,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
216
	template <typename PFP>
217
	void initTrianglesOptimized(typename PFP::MAP& map,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
218 219 220 221 222 223

	/**
	 * creation of indices table of lines (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
224
	void initLines(typename PFP::MAP& map, std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
225
	template <typename PFP>
226
	void initLinesOptimized(typename PFP::MAP& map, std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
227 228 229 230 231 232

	/**
	 * creation of indices table of points
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
233
	void initPoints(typename PFP::MAP& map,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
234 235 236 237 238

	/**
	 * creation of VBO for flat faces rendering
	 */
	template <typename PFP>
239
	void initFlatTriangles(typename PFP::MAP& map, unsigned int vertex_attrib_position , unsigned int thread=0);
240 241 242 243 244 245 246

	/**
	 * initialization of the VBO indices primitives
	 * computed by a traversal of the map
	 * @param prim primitive to draw: VBO_TRIANGLES, VBO_LINES
	 */
	template <typename PFP>
247
	void initPrimitives(typename PFP::MAP& map, int prim, bool optimized = true, unsigned int thread=0) ;
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	/**
	 * 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

#include "Algo/Render/gl3mapRender.hpp"

#endif