mapRender.h 8.2 KB
Newer Older
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, 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.u-strasbg.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 28 29 30

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

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

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

44 45 46 47 48 49 50 51 52
namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
53
namespace GL2
54 55
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
56 57
enum drawingType
{
58 59 60 61 62 63 64 65
	TRIANGLES = 1,
	LINES = 2,
	POINTS = 4,
	EXPLODED = 8,
	FLAT_TRIANGLES = 16,
	ERR = 32
} ;

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
66 67 68
enum bufferIndex
{
	POINT_INDICES = 0,
69
	LINE_INDICES = 1,
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
70
	TRIANGLE_INDICES = 2,
71 72
	FLAT_BUFFER = 3,
	SIZE_BUFFER
73 74 75 76 77 78 79 80 81 82
} ;


class MapRender
{

protected:
	/**
	 * vbo buffers
	 */
83
	GLuint m_indexBuffers[SIZE_BUFFER] ;
84 85

	/**
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
86
	 * nb indices
87
	 */
88 89 90
	GLuint m_nbIndices[SIZE_BUFFER] ;

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

92 93 94 95 96 97 98 99 100 101 102 103 104 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 202 203 204 205 206 207 208 209 210 211 212 213 214



	class VertexPoly
	{
	public:
		int id;
		float value;
		VertexPoly* prev;
		VertexPoly* next;
		VertexPoly* store_link;

		VertexPoly(int i, float v, VertexPoly* p=NULL): id(i),value(v), prev(p), next(NULL),store_link(NULL)
		{
			if (prev!=NULL)
			{
				prev->next = this;
				prev->store_link = this;
			}
		}

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

		VertexPoly* unlink()
		{
			this->prev->next = this->next;
			this->next->prev = this->prev;
			this->value = 20.0f;

			return this->prev;
		}

		static void erasePoly(VertexPoly* vp)
		{
			VertexPoly* curr = vp;
			do
			{
				VertexPoly* tmp = curr;
				curr = curr->store_link;
				delete tmp;
			}while (curr != vp);
		}
	};


//	struct VertexPoly
//	{
//		int id;
//		float value;
//		unsigned int prev;
//		unsigned int next;
//		VertexPoly(int i, float v): id(i),value(v) {}
//	};
//
//	class Polygon
//	{
//	protected:
//		std::vector<VertexPoly> m_vertices;
//	public:
//		Polygon()
//		{
//			m_vertices.reserve(256);
//		}
//
//		inline void addVertex(int id, float val)
//		{
//			m_vertices.push_back(VertexPoly(id,val));
//		}
//
//		inline void link_all()
//		{
//			unsigned int sz = m_vertices.size()-1;
//			for (unsigned int i = 1; i<sz; ++i)
//			{
//				m_vertices[i].next = i+1;
//				m_vertices[i].prev = i-1;
//			}
//			m_vertices[0].next = 1;
//			m_vertices[0].prev = m_vertices.size();
//			m_vertices[sz].next = 0;
//			m_vertices[sz].prev = sz-1;
//		}
//
//		inline void unlink(unsigned int v)
//		{
//			m_vertices[ m_vertices[v].prev].next = m_vertices[v].next;
//			m_vertices[ m_vertices[v].next].prev = m_vertices[v].prev;
//		}
//
//		inline const VertexPoly& prev(unsigned int v) const
//		{
//			return  m_vertices[m_vertices[v].prev];
//		}
//
//		inline const VertexPoly& next(unsigned int v) const
//		{
//			return  m_vertices[m_vertices[v].next];
//		}
//
//		inline unsigned int prevId(unsigned int v) const
//		{
//			return  m_vertices[v].prev;
//		}
//
//		inline unsigned int nextId(unsigned int v) const
//		{
//			return  m_vertices[v].next;
//		}
//
//		inline VertexPoly&  operator()(unsigned int v)
//		{
//			return  m_vertices[v];
//		}
//	};




215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
public:
	/**
	 * Constructor
	 */
	MapRender() ;

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

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

231 232 233
	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); }

234
protected:
235

236 237 238 239 240 241 242 243
	/**
	 * 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) ;

244 245 246 247 248 249 250 251 252 253 254 255 256
	template<typename PFP>
	inline void addEarTri(typename PFP::MAP& map, Dart d, std::vector<GLuint>& tableIndices);

	template<typename PFP>
	float computeEarAngle(AttributeHandler<typename PFP::VEC3>& position, const typename PFP::VEC3& normalPoly, unsigned int i, unsigned int j, unsigned int k);

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

	template<typename PFP>
	void recompute2Ears( AttributeHandler<typename PFP::VEC3>& position, VertexPoly* vp, const typename PFP::VEC3& normalPoly, std::multimap<float, VertexPoly*>& ears, bool convex);


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
public:
	/**
	 * creation of indices table of triangles (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
	void initTriangles(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
	template <typename PFP>
	void initTrianglesOptimized(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;

	/**
	 * creation of indices table of lines (optimized order)
	 * @param tableIndices the table where indices are stored
	 */
	template <typename PFP>
	void initLines(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;
	template <typename PFP>
	void initLinesOptimized(typename PFP::MAP& map, const FunctorSelect& good,std::vector<GLuint>& tableIndices, unsigned int thread=0) ;

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

	/**
	 * initialization of the VBO indices primitives
	 * computed by a traversal of the map
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
286
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
287 288
	 */
	template <typename PFP>
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
289
	void initPrimitives(typename PFP::MAP& map, const FunctorSelect& good, int prim, bool optimized = true, unsigned int thread = 0) ;
290 291 292 293

	/**
	 * initialization of the VBO indices primitives
	 * using the given table
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
294
	 * @param prim primitive to draw: POINTS, LINES, TRIANGLES
295 296 297 298 299 300
	 */
	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
301
	void draw(Utils::GLSLShader* sh, int prim) ;
302 303

	unsigned int drawSub(Utils::GLSLShader* sh, int prim, unsigned int nb_elm);
304 305
} ;

Sylvain Thery's avatar
Sylvain Thery committed
306
} // namespace GL2
307 308 309 310 311 312 313

} // namespace Render

} // namespace Algo

} // namespace CGoGN

Sylvain Thery's avatar
Sylvain Thery committed
314
#include "Algo/Render/GL2/mapRender.hpp"
315 316

#endif