topoRender.hpp 10.8 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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           *
Pierre Kraemer's avatar
Pierre Kraemer committed
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/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Geometry/vector_gen.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Topology/generic/dartmarker.h"

namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
38
namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41
{

template<typename PFP>
42
void TopoRenderMapD::updateData(typename PFP::MAP& map, const typename PFP::TVEC3& positions, float ke, float kf, const FunctorSelect& good)
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47 48 49 50 51
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	std::vector<Dart> vecDarts;
	vecDarts.reserve(map.getNbDarts()/4); // not too big but not too many reallocation

	if (m_attIndex.map() != &map)
	{
52
		m_attIndex  = map.template addAttribute<unsigned int>(DART, "dart_index");
Pierre Kraemer's avatar
Pierre Kraemer committed
53 54 55 56 57 58 59 60 61 62 63 64
	}

	for(Dart d = map.begin(); d!= map.end(); map.next(d))
	{
		if (good(d))
		{
			vecDarts.push_back(d);
		}
	}
	m_nbDarts = vecDarts.size();

	// debut phi1
65
	AutoAttributeHandler<VEC3> fv1(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
66
	// fin phi1
67
	AutoAttributeHandler<VEC3> fv11(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
68
	// phi2
69
	AutoAttributeHandler<VEC3> fv2(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
70

71 72 73
	m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

76 77 78
	m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
80
	unsigned int posDBI = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83 84 85 86 87 88 89 90 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

	DartMarker mf(map);
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		if (!mf.isMarked(d))
		{
			std::vector<VEC3> vecPos;
			vecPos.reserve(16);

			// store the face & center
			VEC3 center(0.0f,0.0f,0.0f);
			Dart dd = d;
			do
			{
				const VEC3& P = positions[d];
				vecPos.push_back(P);
				m_attIndex[d] = posDBI;
				posDBI+=2;
				center += P;
				d = map.phi1(d);
			} while (d != dd);
			center /= REAL(vecPos.size());

			//shrink the face
			unsigned int nb = vecPos.size();
			float k = 1.0f - kf;
			for (unsigned int i = 0; i < nb; ++i)
			{
				vecPos[i] = center*k + vecPos[i]*kf;
			}
			vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop

			k = 1.0f - ke;
			for (unsigned int i = 0; i < nb; ++i)
			{
				VEC3 P = vecPos[i]*ke + vecPos[i+1]*k;
				VEC3 Q = vecPos[i+1]*ke + vecPos[i]*k;

				*positionDartBuf++ = P;
				*positionDartBuf++ = Q;
				*colorDartBuf++ = VEC3(1.,1.,1.);
				*colorDartBuf++ = VEC3(1.,1.,1.);

				VEC3 f = P*0.5f + Q*0.5f;
				fv2[d] = f;
				f = P*0.1f + Q*0.9f;
				fv1[d] = f;
				f = P*0.9f + Q*0.1f;
				fv11[d] = f;
				d = map.phi1(d);
			}
133
			mf.markOrbit(FACE, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136
		}
	}

137 138
	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
139

140 141
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
142

143 144 145
	m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
146

147 148 149
	m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

	VEC3* positionF1 = reinterpret_cast<VEC3*>(PositionBuffer1);
	VEC3* positionF2 = reinterpret_cast<VEC3*>(PositionBuffer2);

	m_nbRel2 =0;

	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;

		Dart e = map.phi2(d);
		if (e.index > d.index)
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}

		e = map.phi1(d);
		*positionF1++ = fv1[d];
		*positionF1++ = fv11[e];
	}

173 174 175 176 177
	m_vbo1->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);

	m_vbo2->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
178 179 180

}

181

Pierre Kraemer's avatar
Pierre Kraemer committed
182
template<typename PFP>
183
void TopoRenderGMap::updateData(typename PFP::MAP& map, const typename PFP::TVEC3& positions, float ke, float kf, const FunctorSelect& good)
Pierre Kraemer's avatar
Pierre Kraemer committed
184
{
185
	/*
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188 189 190 191 192 193
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	std::vector<Dart> vecDarts;
	vecDarts.reserve(map.getNbDarts()/4); // not too big but not too many reallocation

	if (m_attIndex.map() != &map)
	{
194
		m_attIndex  = map.template addAttribute<unsigned int>(DART, "dart_index");
Pierre Kraemer's avatar
Pierre Kraemer committed
195 196 197 198 199 200 201 202 203 204 205 206
	}

	for(Dart d = map.begin(); d!= map.end(); map.next(d))
	{
		if (good(d))
		{
			vecDarts.push_back(d);
		}
	}
	m_nbDarts = vecDarts.size();

	// beta1
207
	AutoAttributeHandler<VEC3> fv1(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
208
	// beta2
209
	AutoAttributeHandler<VEC3> fv2(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
210
	// sommets du brin
211
//	AutoAttributeHandler<VEC3> vert(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
212
	// 2ieme sommet du brin
213
//	AutoAttributeHandler<VEC3> vert2(map, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 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

	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[3]);
	glBufferDataARB(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* ColorDartsBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[0]);
	glBufferDataARB(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionDartsBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);
	unsigned int posDBI=0;

	DartMarker mf(map);
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		if (!mf.isMarked(d))
		{
			std::vector<VEC3> vecPos;
			vecPos.reserve(16);

			// store the face & center
			VEC3 center(0.0f,0.0f,0.0f);
			Dart dd = d;
			do
			{
				const VEC3& P = positions[d];
				vecPos.push_back(P);
				center += P;
				m_attIndex[d] = posDBI;
				posDBI+=2;
				m_attIndex[map.beta0(d)] = posDBI;
				posDBI+=2;
				d = map.phi1(d);
			} while (d != dd);
			center /= REAL(vecPos.size());

			//shrink the face
			unsigned int nb = vecPos.size();
			float k = 1.0f - kf;
			for (unsigned int i = 0; i < nb; ++i)
			{
				vecPos[i] = center*k + vecPos[i]*kf;
			}
			vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop

			k = 1.0f - ke;
			for (unsigned int i = 0; i < nb; ++i)
			{
				VEC3 P = vecPos[i]*ke + vecPos[i+1]*k;
				VEC3 Q = vecPos[i+1]*ke + vecPos[i]*k;
				VEC3 PP = REAL(0.52)*P + REAL(0.48)*Q;
				VEC3 QQ = REAL(0.52)*Q + REAL(0.48)*P;

				*positionDartBuf++ = P;
				*positionDartBuf++ = PP;
				*colorDartBuf++ = VEC3(1.,1.,1.);
				*colorDartBuf++ = VEC3(1.,1.,1.);

				*positionDartBuf++ = Q;
				*positionDartBuf++ = QQ;
				*colorDartBuf++ = VEC3(1.,1.,1.);
				*colorDartBuf++ = VEC3(1.,1.,1.);

				Dart d0 = map.beta0(d);

				VEC3 f = Q*REAL(0.5) + QQ*REAL(0.5);
				fv2[d0] = f;
				f = QQ*REAL(0.2) + Q*REAL(0.8);
				fv1[d0] = f;

				f = P*REAL(0.5) + PP*REAL(0.5);
				fv2[d] = f;
				f = PP*REAL(0.2) + P*REAL(0.8);
				fv1[d] = f;

				d = map.phi1(d);
			}
292
			mf.markOrbit(FACE, d);
Pierre Kraemer's avatar
Pierre Kraemer committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
		}
	}

	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[0]);
	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER);

	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[3]);
	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER);

	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[1]);
	glBufferDataARB(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer1 = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[2]);
	glBufferDataARB(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer2 = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	VEC3* positionF1 = reinterpret_cast<VEC3*>(PositionBuffer1);
	VEC3* positionF2 = reinterpret_cast<VEC3*>(PositionBuffer2);

	m_nbRel2=0;

	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;

		Dart e = map.beta2(d);
		if (e.index > d.index)
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}


		e = map.beta1(d);
		*positionF1++ = fv1[d];
		*positionF1++ = fv1[e];
	}

	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[1]);
	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER);

	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, m_VBOBuffers[2]);
	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER);
338
*/
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340
}

Sylvain Thery's avatar
Sylvain Thery committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

template<typename PFP>
void TopoRender::setDartsIdColor(typename PFP::MAP& map, const FunctorSelect& good)
{

	m_vbo3->bind();
	float* colorBuffer =  reinterpret_cast<float*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb=0;

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if (nb < m_nbDarts)
		{
			if (good(d))
			{
				float r,g,b;
				dartToCol(d, r,g,b);
				float* local = colorBuffer+3*m_attIndex[d]; // get the right position in VBO
				*local++ = r;
				*local++ = g;
				*local++ = b;
				*local++ = r;
				*local++ = g;
				*local++ = b;

				nb++;
			}
		}
		else
		{
371
			CGoGNerr << "Error buffer too small for color picking (change the good parameter ?)" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
			d = map.end();
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
Dart TopoRender::picking(typename PFP::MAP& map, const FunctorSelect& good, int x, int y)
{
	pushColors();
	setDartsIdColor<PFP>(map,good);
	Dart d = pickColor(x,y);
	popColors();
	return d;

}


Sylvain Thery's avatar
Sylvain Thery committed
390
}//end namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
391 392 393 394 395 396

}//end namespace Algo

}//end namespace Render

}//end namespace CGoGN