topo3Render.cpp 12.2 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
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           *
Sylvain Thery's avatar
Sylvain Thery 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.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Render/GL2/topo3Render.h"
#include "Topology/generic/dart.h"
27 28 29
#include "Utils/Shaders/shaderSimpleColor.h"
#include "Utils/Shaders/shaderColorPerVertex.h"

Sylvain Thery's avatar
Sylvain Thery committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <string.h>

namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace GL2
{

44
Topo3Render::Topo3Render():
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48 49 50 51
	m_nbDarts(0),
	m_nbRel1(0),
	m_nbRel2(0),
	m_nbRel3(0),
	m_topo_dart_width(2.0f),
	m_topo_relation_width(3.0f),
	m_color_save(NULL),
52 53
	m_dartsColor(1.0f,1.0f,1.0f),
	m_bufferDartPosition(NULL)
Sylvain Thery's avatar
Sylvain Thery committed
54
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	m_vbo0 = new Utils::VBO();
	m_vbo1 = new Utils::VBO();
	m_vbo2 = new Utils::VBO();
	m_vbo3 = new Utils::VBO();
	m_vbo4 = new Utils::VBO();

	m_vbo0->setDataSize(3);
	m_vbo1->setDataSize(3);
	m_vbo2->setDataSize(3);
	m_vbo3->setDataSize(3);
	m_vbo4->setDataSize(3);

	m_shader1 = new Utils::ShaderSimpleColor();
	m_shader2 = new Utils::ShaderColorPerVertex();

	// binding VBO - VA
	m_vaId = m_shader1->setAttributePosition(m_vbo1);

	m_shader2->setAttributePosition(m_vbo0);
	m_shader2->setAttributeColor(m_vbo4);

	// registering for auto matrices update
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
77 78
	Utils::GLSLShader::registerShader(NULL, m_shader1);
	Utils::GLSLShader::registerShader(NULL, m_shader2);
Sylvain Thery's avatar
Sylvain Thery committed
79 80
}

81
Topo3Render::~Topo3Render()
Sylvain Thery's avatar
Sylvain Thery committed
82
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
83 84
	Utils::GLSLShader::unregisterShader(NULL, m_shader2);
	Utils::GLSLShader::unregisterShader(NULL, m_shader1);
85 86 87 88 89 90 91 92 93

	delete m_shader2;
	delete m_shader1;
	delete m_vbo4;
	delete m_vbo3;
	delete m_vbo2;
	delete m_vbo1;
	delete m_vbo0;

Sylvain Thery's avatar
Sylvain Thery committed
94 95 96 97 98 99 100
	if (m_attIndex.map() != NULL)
		static_cast<AttribMap*>(m_attIndex.map())->removeAttribute(m_attIndex);

	if (m_color_save!=NULL)
	{
		delete[] m_color_save;
	}
101 102 103

	if (m_bufferDartPosition!=NULL)
		delete[] m_bufferDartPosition;
Sylvain Thery's avatar
Sylvain Thery committed
104 105
}

106
void Topo3Render::setDartWidth(float dw)
Sylvain Thery's avatar
Sylvain Thery committed
107 108 109 110
{
	m_topo_dart_width = dw;
}

111
void Topo3Render::setRelationWidth(float pw)
Sylvain Thery's avatar
Sylvain Thery committed
112 113 114 115
{
	m_topo_relation_width = pw;
}

116
void Topo3Render::setDartColor(Dart d, float r, float g, float b)
Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120
{
	float RGB[6];
	RGB[0]=r; RGB[1]=g; RGB[2]=b;
	RGB[3]=r; RGB[4]=g; RGB[5]=b;
121
	m_vbo4->bind();
Sylvain Thery's avatar
Sylvain Thery committed
122 123 124
	glBufferSubData(GL_ARRAY_BUFFER, m_attIndex[d]*3*sizeof(float), 6*sizeof(float),RGB);
}

125
void Topo3Render::setAllDartsColor(float r, float g, float b)
Sylvain Thery's avatar
Sylvain Thery committed
126
{
127
	m_vbo4->bind();
Sylvain Thery's avatar
Sylvain Thery committed
128 129 130 131 132 133 134 135
	GLvoid* ColorDartsBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);
	float* colorDartBuf = reinterpret_cast<float*>(ColorDartsBuffer);
	for (unsigned int i=0; i < 2*m_nbDarts; ++i)
	{
		*colorDartBuf++ = r;
		*colorDartBuf++ = g;
		*colorDartBuf++ = b;
	}
136
	glUnmapBufferARB(GL_ARRAY_BUFFER);
Sylvain Thery's avatar
Sylvain Thery committed
137 138 139

}

140 141 142 143 144
void Topo3Render::setInitialDartsColor(float r, float g, float b)
{
	m_dartsColor = Geom::Vec3f(r,g,b);
}

145
void Topo3Render::drawDarts()
Sylvain Thery's avatar
Sylvain Thery committed
146
{
147 148 149
	if (m_nbDarts==0)
		return;

150
	m_shader2->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
151

152
	glLineWidth(m_topo_dart_width);
Sylvain Thery's avatar
Sylvain Thery committed
153 154
	glDrawArrays(GL_LINES, 0, m_nbDarts*2);

155 156
	// change the stride to take 1/2 vertices
	m_shader2->enableVertexAttribs(6*sizeof(GL_FLOAT));
Sylvain Thery's avatar
Sylvain Thery committed
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
	glPointSize(2.0f*m_topo_dart_width);
 	glDrawArrays(GL_POINTS, 0, m_nbDarts);

 	m_shader2->disableVertexAttribs();

//
////	glColor3f(1.0f,1.0f,1.0f);
//	glLineWidth(m_topo_dart_width);
//	glPointSize(2.0f*m_topo_dart_width);
//
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[4]);
//	glColorPointer(3, GL_FLOAT, 0, 0);
//	glEnableClientState(GL_COLOR_ARRAY);
//
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[0]);
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
//
// 	glVertexPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), 0);
//
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[4]);
// 	glColorPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), 0);
// 	glDrawArrays(GL_POINTS, 0, m_nbDarts)
// 	;
//	glDisableClientState(GL_COLOR_ARRAY);
//	glDisableClientState(GL_VERTEX_ARRAY);
Sylvain Thery's avatar
Sylvain Thery committed
185 186 187 188 189

}



190
void Topo3Render::drawRelation1()
Sylvain Thery's avatar
Sylvain Thery committed
191
{
192 193 194
	if (m_nbDarts==0)
		return;

Sylvain Thery's avatar
Sylvain Thery committed
195 196
	glLineWidth(m_topo_relation_width);

197 198 199 200
	m_shader1->changeVA_VBO(m_vaId, m_vbo1);
	m_shader1->setColor(Geom::Vec4f(0.0f,1.0f,1.0f,0.0f));
	m_shader1->enableVertexAttribs();

201
	glDrawArrays(GL_LINES, 0, m_nbRel1*2);
202 203 204 205 206 207 208 209 210 211 212

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(0.0f,1.0f,1.0f);
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[1]);
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
//	glDisableClientState(GL_VERTEX_ARRAY);
Sylvain Thery's avatar
Sylvain Thery committed
213 214 215
}


216
void Topo3Render::drawRelation2()
Sylvain Thery's avatar
Sylvain Thery committed
217
{
218 219 220
	if (m_nbRel2==0)
		return;

221 222 223
	m_shader1->changeVA_VBO(m_vaId, m_vbo2);
	m_shader1->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
	m_shader1->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
224 225

	glDrawArrays(GL_QUADS, 0, m_nbRel2*4);
226 227 228 229 230 231 232 233 234 235 236

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(1.0f,0.0f,0.0f);
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[2]);
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_QUADS, 0, m_nbRel2*4);
//	glDisableClientState(GL_VERTEX_ARRAY);
Sylvain Thery's avatar
Sylvain Thery committed
237 238 239
}


240
void Topo3Render::drawRelation3(Geom::Vec4f c)
Sylvain Thery's avatar
Sylvain Thery committed
241
{
242 243 244
	if (m_nbRel3==0)
		return;

245
	m_shader1->changeVA_VBO(m_vaId, m_vbo3);
246
	m_shader1->setColor(c);
247
	m_shader1->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
248 249

	glDrawArrays(GL_QUADS, 0, m_nbRel3*4);
250 251 252 253 254 255 256 257 258 259 260

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(1.0f,1.0f,0.0f);
//	glBindBufferARB(GL_ARRAY_BUFFER, m_VBOBuffers[3]);
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_QUADS, 0, m_nbRel3*4);
//	glDisableClientState(GL_VERTEX_ARRAY);
Sylvain Thery's avatar
Sylvain Thery committed
261 262 263
}


264
void Topo3Render::drawTopo()
Sylvain Thery's avatar
Sylvain Thery committed
265 266 267 268
{
	drawDarts();
	drawRelation1();
	drawRelation2();
269
	drawRelation3(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
270 271
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

void Topo3Render::overdrawDart(Dart d, float width, float r, float g, float b)
{
	unsigned int indexDart =  m_attIndex[d];

	m_shader1->changeVA_VBO(m_vaId, m_vbo0);
	m_shader1->setColor(Geom::Vec4f(r,g,b,0.0f));
	m_shader1->enableVertexAttribs();

	glLineWidth(width);
	glDrawArrays(GL_LINES, indexDart, 2);

	glPointSize(2.0f*width);
 	glDrawArrays(GL_POINTS, indexDart, 1);

 	m_shader2->disableVertexAttribs();


}


void Topo3Render::pushColors()
Sylvain Thery's avatar
Sylvain Thery committed
294 295
{
	m_color_save = new float[6*m_nbDarts];
296
	m_vbo4->bind();
Sylvain Thery's avatar
Sylvain Thery committed
297 298 299 300 301 302 303
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	memcpy(m_color_save, colorBuffer, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);
}


304
void Topo3Render::popColors()
Sylvain Thery's avatar
Sylvain Thery committed
305
{
306
	m_vbo4->bind();
Sylvain Thery's avatar
Sylvain Thery committed
307 308 309 310 311 312 313 314 315 316
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

	memcpy(colorBuffer, m_color_save, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);

	delete[] m_color_save;
	m_color_save=0;
}


317
Dart Topo3Render::colToDart(float* color)
Sylvain Thery's avatar
Sylvain Thery committed
318 319 320 321 322 323 324
{
	unsigned int r = (unsigned int)(color[0]*255.0f);
	unsigned int g = (unsigned int)(color[1]*255.0f);
	unsigned int b = (unsigned int)(color[2]*255.0f);

	unsigned int id = r + 255*g +255*255*b;

325 326 327
	if (id == 0)
		return NIL;
	return Dart(id-1);
Sylvain Thery's avatar
Sylvain Thery committed
328 329 330
}


331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
void Topo3Render::dartToCol(Dart d, float& r, float& g, float& b)
{
	// here use dart.index beacause it is what we want (and not map.dartIndex(d) !!)
	unsigned int lab = d.index + 1; // add one to avoid picking the black of screen

	r = float(lab%255) / 255.0f; lab = lab/255;
	g = float(lab%255) / 255.0f; lab = lab/255;
	b = float(lab%255) / 255.0f; lab = lab/255;
	if (lab!=0)
		CGoGNerr << "Error picking color, too many darts"<< CGoGNendl;
}




Sylvain Thery's avatar
Sylvain Thery committed
346
Dart Topo3Render::pickColor(unsigned int x, unsigned int y)
Sylvain Thery's avatar
Sylvain Thery committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
	//more easy picking for
	unsigned int dw = m_topo_dart_width;
	m_topo_dart_width+=2;

	// save clear color and set to zero
	float cc[4];
	glGetFloatv(GL_COLOR_CLEAR_VALUE,cc);

	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glDisable(GL_LIGHTING);
	// draw in back buffer (not shown on screen)
Sylvain Thery's avatar
Sylvain Thery committed
361

Sylvain Thery's avatar
Sylvain Thery committed
362 363 364 365 366 367 368 369 370 371 372
	drawDarts();

	// restore dart with
	m_topo_dart_width = dw;

	// read the pixel under the mouse in back buffer
	glReadBuffer(GL_BACK);
	float color[3];
	glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,color);

	glClearColor(cc[0], cc[1], cc[2], cc[3]);
373 374 375
	
	
	std::cout << color[0] << ", "<<color[1] << ", "<<color[2] <<std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
376 377 378
	return colToDart(color);
}

379 380 381 382 383 384 385 386 387 388
void Topo3Render::svgout2D(const std::string& filename, const glm::mat4& model, const glm::mat4& proj)
{
	Utils::SVG::SVGOut svg(filename,model,proj);
	toSVG(svg);
}

void Topo3Render::toSVG(Utils::SVG::SVGOut& svg)
{

	// PHI3 / beta3
389
	Utils::SVG::SvgGroup* svg1 = new Utils::SVG::SvgGroup("phi3", svg.m_model, svg.m_proj);
390
	const Geom::Vec3f* ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo3->lockPtr());
391 392
	svg1->setWidth(m_topo_relation_width);
	svg1->beginLines();
393 394 395 396
	for (unsigned int i=0; i<m_nbRel3; ++i)
	{
		Geom::Vec3f P = (ptr[4*i]+ ptr[4*i+3])/2.0f;
		Geom::Vec3f Q = (ptr[4*i+1]+ ptr[4*i+2])/2.0f;
397
		svg1->addLine(P, Q,Geom::Vec3f(0.8f,0.8f,0.0f));
398
	}
399
	svg1->endLines();
400 401
	m_vbo3->releasePtr();

402
	svg.addGroup(svg1);
403 404

	// PHI2 / beta2
405
	Utils::SVG::SvgGroup* svg2 = new Utils::SVG::SvgGroup("phi2", svg.m_model, svg.m_proj);
406
	ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo2->lockPtr());
407 408
	svg2->setWidth(m_topo_relation_width);
	svg2->beginLines();
409 410 411 412
	for (unsigned int i=0; i<m_nbRel2; ++i)
	{
		Geom::Vec3f P = (ptr[4*i]+ ptr[4*i+3])/2.0f;
		Geom::Vec3f Q = (ptr[4*i+1]+ ptr[4*i+2])/2.0f;
413
		svg2->addLine(P, Q,Geom::Vec3f(0.8f,0.0f,0.0f));
414
	}
415
	svg2->endLines();
416 417
	m_vbo2->releasePtr();

418 419
	svg.addGroup(svg2);

420
	//PHI1 /beta1
421
	Utils::SVG::SvgGroup* svg3 = new Utils::SVG::SvgGroup("phi1", svg.m_model, svg.m_proj);
422
	ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo1->lockPtr());
423 424
	svg3->setWidth(m_topo_relation_width);
	svg3->beginLines();
425
	for (unsigned int i=0; i<m_nbRel1; ++i)
426 427
		svg3->addLine(ptr[2*i], ptr[2*i+1],Geom::Vec3f(0.0f,0.7f,0.7f));
	svg3->endLines();
428 429
	m_vbo1->releasePtr();

430
	svg.addGroup(svg3);
431 432 433 434

	const Geom::Vec3f* colorsPtr = reinterpret_cast<const Geom::Vec3f*>(m_vbo4->lockPtr());
	ptr= reinterpret_cast<Geom::Vec3f*>(m_vbo0->lockPtr());

435 436
	Utils::SVG::SvgGroup* svg4 = new Utils::SVG::SvgGroup("darts", svg.m_model, svg.m_proj);
	svg4->setWidth(m_topo_dart_width);
437

438
	svg4->beginLines();
439 440 441 442 443
	for (unsigned int i=0; i<m_nbDarts; ++i)
	{
		Geom::Vec3f col = colorsPtr[2*i];
		if (col.norm2()>2.9f)
			col = Geom::Vec3f(1.0f,1.0f,1.0f) - col;
444
		svg4->addLine(ptr[2*i], ptr[2*i+1], col);
445
	}
446
	svg4->endLines();
447

448 449 450 451 452
	svg.addGroup(svg4);

	Utils::SVG::SvgGroup* svg5 = new Utils::SVG::SvgGroup("dartEmb", svg.m_model, svg.m_proj);
	svg5->setWidth(m_topo_dart_width);
	svg5->beginPoints();
453 454 455 456 457
	for (unsigned int i=0; i<m_nbDarts; ++i)
	{
		Geom::Vec3f col = colorsPtr[2*i];
		if (col.norm2()>2.9f)
			col = Geom::Vec3f(1.0f,1.0f,1.0f) - col;
458
		svg5->addPoint(ptr[2*i], col);
459
	}
460 461 462
	svg5->endPoints();

	svg.addGroup(svg5);
463 464 465 466

	m_vbo0->releasePtr();
	m_vbo4->releasePtr();
}
Sylvain Thery's avatar
Sylvain Thery committed
467 468 469 470 471 472 473 474 475


}//end namespace GL2

}//end namespace Render

}//end namespace Algo

}//end namespace CGoGN