mapRender.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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25 26
#ifndef _GL2_MAP_RENDER_
#define _GL2_MAP_RENDER_
27

unknown's avatar
unknown committed
28 29 30
#define _USE_MATH_DEFINES
#include <cmath>

31 32 33
#include <GL/glew.h>
#include <vector>
#include <list>
34
#include <set>
35
#include <utility>
36 37 38 39 40 41 42

#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
43 44
#include "Utils/vbo.h"

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
45 46
// forward definition
namespace CGoGN { namespace Utils { class GLSLShader; } }
Sylvain Thery's avatar
Sylvain Thery committed
47

48 49 50 51 52 53 54 55 56
namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
57
namespace GL2
58 59
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
60 61
enum drawingType
{
62 63 64 65 66
	TRIANGLES = 1,
	LINES = 2,
	POINTS = 4,
	EXPLODED = 8,
	FLAT_TRIANGLES = 16,
67 68
	BOUNDARY = 32,
	ERR = 64
69 70
} ;

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
71 72 73
enum bufferIndex
{
	POINT_INDICES = 0,
74
	LINE_INDICES = 1,
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
75
	TRIANGLE_INDICES = 2,
76
	FLAT_BUFFER = 3,
77
	BOUNDARY_INDICES = 4,
78
	SIZE_BUFFER
79 80 81 82 83 84 85 86
} ;

class MapRender
{
protected:
	/**
	 * vbo buffers
	 */
87
	GLuint m_indexBuffers[SIZE_BUFFER] ;
88 89

	/**
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
90
	 * nb indices
91
	 */
92 93 94
	GLuint m_nbIndices[SIZE_BUFFER] ;

	typedef std::pair<GLuint*, unsigned int> buffer_array;
95

96 97
	// forward declaration
	class VertexPoly;
98

99 100
	// comparaison function for multiset
	static bool cmpVP(VertexPoly* lhs, VertexPoly* rhs);
101

102 103
	// multiset typedef for simple writing
	typedef std::multiset< VertexPoly*,bool(*)(VertexPoly*,VertexPoly*)> VPMS;
104 105 106 107 108 109

	class VertexPoly
	{
	public:
		int id;
		float value;
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
110
		float length;
111 112
		VertexPoly* prev;
		VertexPoly* next;
113
		VPMS::iterator ear;
114

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
115
		VertexPoly(int i, float v, float l, VertexPoly* p=NULL): id(i),value(v), length(l), prev(p), next(NULL)
116 117 118 119 120 121 122 123 124 125 126
		{
			if (prev!=NULL)
				prev->next = this;
		}

		static void close(VertexPoly* first, VertexPoly* last)
		{
			last->next = first;
			first->prev = last;
		}

127
		static VertexPoly* erase(VertexPoly* vp)
128
		{
129 130 131 132 133
			VertexPoly* tmp = vp->prev;
			tmp->next = vp->next;
			vp->next->prev = tmp;
			delete vp;
			return tmp;
134 135 136 137
		}

	};

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
public:
	/**
	 * Constructor
	 */
	MapRender() ;

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

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

154 155 156
	buffer_array get_index_buffer() { return std::make_pair(m_indexBuffers, SIZE_BUFFER); }
	buffer_array get_nb_index_buffer() { return std::make_pair(m_nbIndices, SIZE_BUFFER); }

157 158 159 160 161 162 163 164 165
protected:
	/**
	 * 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) ;

166
	template<typename PFP>
167
	inline void addEarTri(typename PFP::MAP& map, Dart d, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position);
168 169

	template<typename PFP>
170
	float computeEarAngle(const typename PFP::VEC3& P1, const typename PFP::VEC3& P2, const typename PFP::VEC3& P3, const typename PFP::VEC3& normalPoly);
171 172

	template<typename PFP>
173
	bool computeEarIntersection(const VertexAttribute<typename PFP::VEC3>& position, VertexPoly* vp, const typename PFP::VEC3& normalPoly);
174 175

	template<typename PFP>
176
	void recompute2Ears(const VertexAttribute<typename PFP::VEC3>& position, VertexPoly* vp, const typename PFP::VEC3& normalPoly, VPMS& ears, bool convex);
177

Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
178 179
	template<typename VEC3>
	bool inTriangle(const VEC3& P, const VEC3& normal, const VEC3& Ta,  const VEC3& Tb, const VEC3& Tc);
180

181
public:
182 183 184 185 186
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
187
	void initTriangles(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position, unsigned int thread = 0) ;
188
	template <typename PFP>
189
	void initTrianglesOptimized(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position, unsigned int thread = 0) ;
190 191 192 193 194 195

	/**
	 * creation of indices table of lines (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
196
	void initLines(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, unsigned int thread = 0) ;
197
	template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
198
	void initLinesOptimized(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, unsigned int thread = 0) ;
199 200 201 202 203 204

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

Pierre Kraemer's avatar
Pierre Kraemer committed
207 208 209 210
	/**
	 * creation of indices table of points
	 * @param tableIndices the table where indices are stored
	 */
211
	template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
212
	void initBoundaries(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, unsigned int thread = 0) ;
213 214 215
	/**
	 * initialization of the VBO indices primitives
	 * computed by a traversal of the map
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
216
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
217 218
	 */
	template <typename PFP>
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
219
	void initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized = true, unsigned int thread = 0) ;
220

221
	template <typename PFP>
222
	void initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, const VertexAttribute<typename PFP::VEC3>* position, bool optimized = true, unsigned int thread = 0) ;
223

224 225 226
	/**
	 * initialization of the VBO indices primitives
	 * using the given table
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
227
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
228 229 230 231 232 233
	 */
	void initPrimitives(int prim, std::vector<GLuint>& tableIndices) ;

	/**
	 * draw the VBO (function to call in the drawing callback)
	 */
Sylvain Thery's avatar
Sylvain Thery committed
234
	void draw(Utils::GLSLShader* sh, int prim) ;
235 236

	unsigned int drawSub(Utils::GLSLShader* sh, int prim, unsigned int nb_elm);
237 238
} ;

Sylvain Thery's avatar
Sylvain Thery committed
239
} // namespace GL2
240 241 242 243 244 245 246

} // namespace Render

} // namespace Algo

} // namespace CGoGN

Sylvain Thery's avatar
Sylvain Thery committed
247
#include "Algo/Render/GL2/mapRender.hpp"
248 249

#endif