mapRender.h 7.99 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
#include <vector>
#include <list>
33
#include <set>
34
#include <utility>
35

Thery Sylvain's avatar
Thery Sylvain committed
36
#include "Utils/gl_def.h"
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

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

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

namespace Algo
{

namespace Render
{

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

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
59 60
enum drawingType
{
61 62 63 64 65 66
	POINTS = 0,
	LINES = 1,
	TRIANGLES = 2,
	FLAT_TRIANGLES = 3,
	BOUNDARY = 4,
	EXPLODED = 5,
67
	SIZE_BUFFER
68 69 70 71 72 73 74 75
} ;

class MapRender
{
protected:
	/**
	 * vbo buffers
	 */
76
	GLuint m_indexBuffers[SIZE_BUFFER] ;
77
	bool m_indexBufferUpToDate[SIZE_BUFFER];
78 79

	/**
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
80
	 * nb indices
81
	 */
82 83 84
	GLuint m_nbIndices[SIZE_BUFFER] ;

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

86 87
	// forward declaration
	class VertexPoly;
88

89 90
	// comparaison function for multiset
	static bool cmpVP(VertexPoly* lhs, VertexPoly* rhs);
91

92
	// multiset typedef for simple writing
93
	typedef std::multiset<VertexPoly*, bool(*)(VertexPoly*,VertexPoly*)> VPMS;
94 95 96 97 98 99

	class VertexPoly
	{
	public:
		int id;
		float value;
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
100
		float length;
101 102
		VertexPoly* prev;
		VertexPoly* next;
103
		VPMS::iterator ear;
104

105
		VertexPoly(int i, float v, float l, VertexPoly* p = NULL) : id(i), value(v), length(l), prev(p), next(NULL)
106
		{
107
			if (prev != NULL)
108 109 110 111 112 113 114 115 116
				prev->next = this;
		}

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

117
		static VertexPoly* erase(VertexPoly* vp)
118
		{
119 120 121 122 123
			VertexPoly* tmp = vp->prev;
			tmp->next = vp->next;
			vp->next->prev = tmp;
			delete vp;
			return tmp;
124 125 126
		}
	};

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
public:
	/**
	 * Constructor
	 */
	MapRender() ;

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

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

143 144 145
	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); }

146 147 148 149 150 151 152 153 154
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) ;

155
	template<typename PFP>
156
	inline void addEarTri(typename PFP::MAP& map, Dart d, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position);
157 158

	template<typename PFP>
159
	float computeEarAngle(const typename PFP::VEC3& P1, const typename PFP::VEC3& P2, const typename PFP::VEC3& P3, const typename PFP::VEC3& normalPoly);
160 161

	template<typename PFP>
162
	bool computeEarIntersection(const VertexAttribute<typename PFP::VEC3>& position, VertexPoly* vp, const typename PFP::VEC3& normalPoly);
163 164

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

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

170
public:
171 172 173 174 175
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
176
	void initTriangles(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position, unsigned int thread = 0) ;
177
	template <typename PFP>
178
	void initTrianglesOptimized(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3>* position, unsigned int thread = 0) ;
179 180 181 182 183 184

	/**
	 * 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
185
	void initLines(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, unsigned int thread = 0) ;
186
	template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
187
	void initLinesOptimized(typename PFP::MAP& map, const FunctorSelect& good, std::vector<GLuint>& tableIndices, unsigned int thread = 0) ;
188 189 190 191 192 193

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

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

203 204 205
	/**
	 * initialization of the VBO indices primitives
	 * computed by a traversal of the map
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
206
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
207 208
	 */
	template <typename PFP>
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
209
	void initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized = true, unsigned int thread = 0) ;
210

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

214 215 216
	/**
	 * initialization of the VBO indices primitives
	 * using the given table
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
217
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
218 219 220
	 */
	void initPrimitives(int prim, std::vector<GLuint>& tableIndices) ;

221 222 223 224 225 226 227 228 229 230 231 232 233 234
	/**
	 * initialization of the VBO indices primitives
	 * using the given table
	 * @param prim primitive to draw: POINT_INDICES, LINE_INDICES, TRIANGLE_INDICES
	 */
	bool isPrimitiveUpToDate(int prim) { return m_indexBufferUpToDate[prim]; }

	/**
	 * initialization of the VBO indices primitives
	 * using the given table
	 * @param prim primitive to draw: POINT_INDICES, LINE_INDICES, TRIANGLE_INDICES
	 */
	void setPrimitiveDirty(int prim) { m_indexBufferUpToDate[prim] = false; }

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

	unsigned int drawSub(Utils::GLSLShader* sh, int prim, unsigned int nb_elm);
241 242
} ;

Sylvain Thery's avatar
Sylvain Thery committed
243
} // namespace GL2
244 245 246 247 248 249 250

} // namespace Render

} // namespace Algo

} // namespace CGoGN

Sylvain Thery's avatar
Sylvain Thery committed
251
#include "Algo/Render/GL2/mapRender.hpp"
252 253

#endif