mapRender.h 8.12 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
#include <cmath>

30 31
#include <vector>
#include <list>
32
#include <set>
33
#include <utility>
34

Thery Sylvain's avatar
Thery Sylvain committed
35
#include "Utils/gl_def.h"
36 37 38 39 40 41
#include "Topology/generic/dart.h"
#include "Topology/generic/functor.h"
#include "Topology/generic/attributeHandler.h"
#include "Container/convert.h"
#include "Geometry/vector_gen.h"

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

Sylvain Thery's avatar
Sylvain Thery committed
45 46 47 48 49 50 51 52
#ifdef WIN32
#ifndef CGoGN_ALGO_API
#if defined CGoGN_ALGO_DLL_EXPORT
#define CGoGN_ALGO_API __declspec(dllexport)
#else
#define CGoGN_ALGO_API __declspec(dllimport)
#endif
#endif
Thery Sylvain's avatar
Thery Sylvain committed
53 54
#else
#define CGoGN_ALGO_API
Sylvain Thery's avatar
Sylvain Thery committed
55 56
#endif

57 58 59 60 61 62 63 64 65
namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
66
namespace GL2
67 68
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
69 70
enum drawingType
{
71 72 73 74 75 76
	POINTS = 0,
	LINES = 1,
	TRIANGLES = 2,
	FLAT_TRIANGLES = 3,
	BOUNDARY = 4,
	EXPLODED = 5,
77
	SIZE_BUFFER
78 79
} ;

Sylvain Thery's avatar
Sylvain Thery committed
80
class CGoGN_ALGO_API MapRender
81 82 83 84 85
{
protected:
	/**
	 * vbo buffers
	 */
86
	GLuint m_indexBuffers[SIZE_BUFFER] ;
87
	bool m_indexBufferUpToDate[SIZE_BUFFER];
88

89
	GLuint m_currentSize[SIZE_BUFFER] ;
90 91

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

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

98 99
	// forward declaration
	class VertexPoly;
100

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

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

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

117
		VertexPoly(int i, float v, float l, VertexPoly* p = NULL) : id(i), value(v), length(l), prev(p), next(NULL)
118
		{
119
			if (prev != NULL)
120 121 122 123 124 125 126 127 128
				prev->next = this;
		}

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

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

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

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

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

155 156 157
	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); }

158 159 160 161 162 163 164
protected:
	/**
	 * addition of indices table of one triangle
	 * @param d a dart of the triangle
	 * @param tableIndices the indices table
	 */
	template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
165
	void addTri(typename PFP::MAP& map, Face f, std::vector<GLuint>& tableIndices) ;
166

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

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

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

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

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

182
public:
183 184 185 186 187
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
188
	void initTriangles(typename PFP::MAP& map, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>* position) ;
189
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
190
	void initTrianglesOptimized(typename PFP::MAP& map, std::vector<GLuint>& tableIndices, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>* position) ;
191 192 193 194 195 196

	/**
	 * creation of indices table of lines (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
197
	void initLines(typename PFP::MAP& map, std::vector<GLuint>& tableIndices) ;
198
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
199
	void initLinesOptimized(typename PFP::MAP& map, std::vector<GLuint>& tableIndices) ;
200 201 202 203 204 205

	/**
	 * creation of indices table of points
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
206
	void initPoints(typename PFP::MAP& map, std::vector<GLuint>& tableIndices) ;
207

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

222
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
223
	void initPrimitives(typename PFP::MAP& map, int prim, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>* position, bool optimized = true) ;
224

225 226 227
	/**
	 * add primitives to the VBO of indices
	 */
228
	template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
229
	void addPrimitives(typename PFP::MAP& map, int prim, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>* position, bool optimized = true);
230

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

238
	/**
239
	 * return if the given primitive connectivity VBO is up to date
240 241 242 243 244
	 * @param prim primitive to draw: POINT_INDICES, LINE_INDICES, TRIANGLE_INDICES
	 */
	bool isPrimitiveUpToDate(int prim) { return m_indexBufferUpToDate[prim]; }

	/**
245
	 * set the given primitive connectivity VBO dirty
246 247 248 249
	 * @param prim primitive to draw: POINT_INDICES, LINE_INDICES, TRIANGLE_INDICES
	 */
	void setPrimitiveDirty(int prim) { m_indexBufferUpToDate[prim] = false; }

250 251 252
	/**
	 * draw the VBO (function to call in the drawing callback)
	 */
Sylvain Thery's avatar
Sylvain Thery committed
253
	void draw(Utils::GLSLShader* sh, int prim) ;
254 255

	unsigned int drawSub(Utils::GLSLShader* sh, int prim, unsigned int nb_elm);
256 257
} ;

Sylvain Thery's avatar
Sylvain Thery committed
258
} // namespace GL2
259 260 261 262 263 264 265

} // namespace Render

} // namespace Algo

} // namespace CGoGN

Sylvain Thery's avatar
Sylvain Thery committed
266
#include "Algo/Render/GL2/mapRender.hpp"
267 268

#endif