drawer.cpp 12.7 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
#include "GL/glew.h"
Sylvain Thery's avatar
Sylvain Thery committed
26 27

#define CGoGN_UTILS_DLL_EXPORT 1
28
#include "Utils/drawer.h"
29

Thery Sylvain's avatar
Thery Sylvain committed
30
#include "Utils/vbo_base.h"
Sylvain Thery's avatar
Sylvain Thery committed
31
#include "Utils/svg.h"
32 33 34

namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
35

36 37 38
namespace Utils
{

Sylvain Thery's avatar
Sylvain Thery committed
39
	Drawer::Drawer(int lineMode) :
40
	m_currentWidth(1.0f),
Sylvain Thery's avatar
Sylvain Thery committed
41 42
	m_currentSize(1.0f),
	m_shader(NULL),
Sylvain Thery's avatar
Sylvain Thery committed
43 44 45
	m_shaderCL(NULL),
	m_shader3DCL(NULL),
	m_lineMode(lineMode)
46 47 48 49 50 51 52 53 54 55 56
{
	m_vboPos = new Utils::VBO();
	m_vboPos->setDataSize(3);

	m_vboCol = new Utils::VBO();
	m_vboCol->setDataSize(3);

	m_shader = new Utils::ShaderColorPerVertex();
	m_shader->setAttributePosition(m_vboPos);
	m_shader->setAttributeColor(m_vboCol);

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
57
	Utils::GLSLShader::registerShader(NULL, m_shader);
58

59 60
	if (GLSLShader::CURRENT_OGL_VERSION >=3)
	{
Sylvain Thery's avatar
Sylvain Thery committed
61 62 63 64 65 66 67 68 69 70 71 72
		m_shaderCL = new Utils::ShaderBoldColorLines();
		m_shaderCL->setAttributePosition(m_vboPos);
		m_shaderCL->setAttributeColor(m_vboCol);
		m_shaderCL->setNoClippingPlane();
		Utils::GLSLShader::registerShader(NULL, m_shaderCL);

		m_shader3DCL = new Utils::ShaderBold3DColorLines();
		m_shader3DCL->setAttributePosition(m_vboPos);
		m_shader3DCL->setAttributeColor(m_vboCol);
		m_shader3DCL->setNoClippingPlane();
		Utils::GLSLShader::registerShader(NULL, m_shader3DCL);

73 74
	}

75 76 77 78 79 80 81 82
	m_dataPos.reserve(128);
	m_dataCol.reserve(128);
	m_begins.reserve(16);
//	m_modes.reserve(16);
}

Drawer::~Drawer()
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
83
	Utils::GLSLShader::unregisterShader(NULL, m_shader);
84 85
	delete m_vboPos;
	delete m_vboCol;
86
	delete m_shader;
Sylvain Thery's avatar
Sylvain Thery committed
87 88 89 90
	if (m_shaderCL)
		delete m_shaderCL;
	if (m_shader3DCL)
		delete m_shader3DCL;
91 92 93 94 95 96 97 98 99 100 101
}

//Utils::ShaderColorPerVertex* Drawer::getShader()
//{
//	return m_shader;
//}

std::vector<Utils::GLSLShader*> Drawer::getShaders()
{
	std::vector<Utils::GLSLShader*> shaders;
	shaders.push_back(m_shader);
Sylvain Thery's avatar
Sylvain Thery committed
102 103 104 105 106
	if (m_shaderCL)
		shaders.push_back(m_shaderCL);
	if (m_shader3DCL)
		shaders.push_back(m_shader3DCL);

107
	return shaders;
108 109
}

110
void Drawer::updateMatrices(const glm::mat4& projection, const glm::mat4& modelview)
111
{
112
	m_shader->updateMatrices(projection,modelview);
Sylvain Thery's avatar
Sylvain Thery committed
113 114 115 116
	if (m_shaderCL)
		m_shaderCL->updateMatrices(projection,modelview);
	if (m_shader3DCL)
		m_shader3DCL->updateMatrices(projection, modelview);
117 118
}

119

120 121
void Drawer::lineWidth(float lw)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
122
	m_currentWidth = lw;
123 124 125 126
}

void Drawer::pointSize(float ps)
{
127
	m_currentSize = ps;
128 129
}

130
int Drawer::begin(GLenum mode)
131
{
132
	int res = int(m_begins.size());
133

134
	if (mode == GL_POINTS)
135
		m_begins.push_back(PrimParam(uint32(m_dataPos.size()), mode, m_currentSize));
136
	else
137
		m_begins.push_back(PrimParam(uint32(m_dataPos.size()), mode, m_currentWidth));
138

139
	return res;
140 141 142 143
}

void Drawer::end()
{
144
	m_begins.back().nb = uint32(m_dataPos.size() - m_begins.back().begin);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

void Drawer::color(const Geom::Vec3f& col)
{
	if (m_dataPos.size() == m_dataCol.size())
		m_dataCol.push_back(col);
	else
		m_dataCol.back() = col;
}

void Drawer::color3f(float r, float g, float b)
{
	color(Geom::Vec3f(r,g,b));
}

Sylvain Thery's avatar
Sylvain Thery committed
160
unsigned int Drawer::vertex(const Geom::Vec3f& v)
161 162 163 164
{
	if (m_dataPos.size() == m_dataCol.size())
	{
		if (m_dataCol.empty())
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
165
			m_dataCol.push_back(Geom::Vec3f(1.0f, 1.0f, 1.0f));
166 167 168 169
		else
			m_dataCol.push_back( m_dataCol.back());
	}
	m_dataPos.push_back(v);
170
	return  uint32(m_dataPos.size()-1);
171 172
}

Sylvain Thery's avatar
Sylvain Thery committed
173
unsigned int Drawer::vertex3f(float r, float g, float b)
174
{
Sylvain Thery's avatar
Sylvain Thery committed
175
	return vertex(Geom::Vec3f(r,g,b));
176 177 178 179 180 181 182 183 184 185 186 187
}

void Drawer::newList(GLenum comp)
{
	m_compile = comp;
	m_dataPos.clear();
	m_dataCol.clear();
	m_begins.clear();
}

void Drawer::endList()
{
188
	unsigned int nbElts = uint32(m_dataPos.size());
CGoGN GIT Supervisor's avatar
CGoGN GIT Supervisor committed
189 190 191
	
	if (nbElts == 0)
		return;
192 193

	m_vboPos->bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
194
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataPos[0]), GL_STREAM_DRAW);
195 196

	m_vboCol->bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
197
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataCol[0]), GL_STREAM_DRAW);
198 199 200 201 202 203 204 205 206 207 208

	// free memory
	std::vector<Geom::Vec3f> tempo;
	tempo.swap(m_dataPos);
	std::vector<Geom::Vec3f> tempo2;
	tempo2.swap(m_dataCol);

	if (m_compile != GL_COMPILE)
		callList();
}

Sylvain Thery's avatar
Sylvain Thery committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222

void Drawer::updatePositions(unsigned int first, unsigned int nb, const Geom::Vec3f* P)
{
	m_vboPos->bind();
	glBufferSubData(GL_ARRAY_BUFFER, first * sizeof(Geom::Vec3f), nb * sizeof(Geom::Vec3f), P);
}

void Drawer::updatePositions(unsigned int first, unsigned int nb, const float* P)
{
	m_vboPos->bind();
	glBufferSubData(GL_ARRAY_BUFFER, first * 3 * sizeof(float), nb * 3 * sizeof(float), P);
}


223
void Drawer::callList(float opacity)
224 225 226 227
{
	if (m_begins.empty())
		return;

228 229 230 231 232 233 234 235 236 237 238 239 240
	if (GLSLShader::CURRENT_OGL_VERSION >=3)
	{
		m_shader->setOpacity(opacity);
		m_shader->enableVertexAttribs();
		for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
		{
			if (pp->mode == GL_POINTS)
			{
				glPointSize(pp->width);
				glDrawArrays(GL_POINTS, pp->begin, pp->nb);
			}
		}
		m_shader->disableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
241

Sylvain Thery's avatar
Sylvain Thery committed
242
		switch (m_lineMode)
243
		{
Sylvain Thery's avatar
Sylvain Thery committed
244 245 246 247
		case 0:
			m_shader->setOpacity(opacity);
			m_shader->enableVertexAttribs();
			for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
248
			{
Sylvain Thery's avatar
Sylvain Thery committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
				if (pp->mode != GL_POINTS)
				{
					glDrawArrays(pp->mode, pp->begin, pp->nb);
				}
			}
			m_shader->disableVertexAttribs();
			break;
		case 1:
			m_shaderCL->setOpacity(opacity);
			m_shaderCL->enableVertexAttribs();
			for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
			{
				if (pp->mode != GL_POINTS)
				{
					m_shaderCL->setLineWidth(pp->width);
					m_shaderCL->bind();
					glDrawArrays(pp->mode, pp->begin, pp->nb);
				}
267
			}
Sylvain Thery's avatar
Sylvain Thery committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
			m_shaderCL->disableVertexAttribs();
			break;
		case 2:
			m_shader3DCL->setOpacity(opacity);
			m_shader3DCL->enableVertexAttribs();
			for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
			{
				if (pp->mode != GL_POINTS)
				{
					m_shader3DCL->setLineWidth(pp->width);
					m_shader3DCL->bind();
					glDrawArrays(pp->mode, pp->begin, pp->nb);
				}
			}
			m_shader3DCL->disableVertexAttribs();
			break;
		default:
			break;
286
		}
Sylvain Thery's avatar
Sylvain Thery committed
287

288 289
	}
	else
290
	{
291 292 293 294 295 296 297 298 299 300 301
		m_shader->setOpacity(opacity);
		m_shader->enableVertexAttribs();
		for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
		{
			if (pp->mode == GL_POINTS)
				glPointSize(pp->width);
			if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
				glLineWidth(pp->width);
			glDrawArrays(pp->mode, pp->begin, pp->nb);
		}
		m_shader->disableVertexAttribs();
302 303 304
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
305

306 307 308 309 310 311
void Drawer::callSubList(int index, float opacity)
{
	if (index >= int(m_begins.size()))
		return;
	PrimParam* pp = & (m_begins[index]);

312 313 314
	if (GLSLShader::CURRENT_OGL_VERSION >=3)
	{
		if (pp->mode == GL_POINTS)
315
		{
316 317 318
			m_shader->setOpacity(opacity);
			m_shader->enableVertexAttribs();
			glPointSize(pp->width);
319
			glDrawArrays(pp->mode, pp->begin, pp->nb);
320
			m_shader->disableVertexAttribs();
321
		}
322
		else
323
		{
Sylvain Thery's avatar
Sylvain Thery committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
			switch (m_lineMode)
			{
			case 0:
				m_shader->setOpacity(opacity);
				m_shader->enableVertexAttribs();
				glDrawArrays(pp->mode, pp->begin, pp->nb);
				m_shader->disableVertexAttribs();
				break;
			case 1:
				m_shaderCL->setOpacity(opacity);
				m_shaderCL->setLineWidth(pp->width);
				m_shaderCL->enableVertexAttribs();
				glDrawArrays(pp->mode, pp->begin, pp->nb);
				m_shaderCL->disableVertexAttribs();
				break;
			case 2:
				m_shader3DCL->setOpacity(opacity);
				m_shader3DCL->setLineWidth(pp->width);
				m_shader3DCL->enableVertexAttribs();
				glDrawArrays(pp->mode, pp->begin, pp->nb);
				m_shader3DCL->disableVertexAttribs();
				break;
			default:
				break;
			}
349
		}
350 351 352 353 354 355 356 357 358 359 360 361
	}
	else
	{
		m_shader->setOpacity(opacity);
		m_shader->enableVertexAttribs();
		if (pp->mode == GL_POINTS)
			glPointSize(pp->width);
		if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
			glLineWidth(pp->width);
		glDrawArrays(pp->mode, pp->begin, pp->nb);
		m_shader->disableVertexAttribs();
	}
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
//void Drawer::callSubLists(int first, int nb, float opacity)
//{
//	m_shader->setOpacity(opacity);
//	m_shader->enableVertexAttribs();

//	int last = first+nb;
//	for (int i = first; i< last; ++i)
//		if (i < int(m_begins.size()))
//		{
//			PrimParam* pp = & (m_begins[i]);

//			if (pp->mode == GL_POINTS)
//				glPointSize(pp->width);
//			if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
//				glLineWidth(pp->width);
//			glDrawArrays(pp->mode, pp->begin, pp->nb);
//		}

//	m_shader->disableVertexAttribs();
//}

//void Drawer::callSubLists(std::vector<int> indices, float opacity)
//{
//	m_shader->setOpacity(opacity);

//	m_shader->enableVertexAttribs();

//	for (std::vector<int>::iterator it = indices.begin(); it != indices.end(); ++it)
//		if (*it < int(m_begins.size()))
//		{
//			PrimParam* pp = & (m_begins[*it]);

//			if (pp->mode == GL_POINTS)
//				glPointSize(pp->width);
//			if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP) || (pp->mode == GL_LINE_STRIP))
//				glLineWidth(pp->width);
//			glDrawArrays(pp->mode, pp->begin, pp->nb);
//		}

//	m_shader->disableVertexAttribs();
//}

Sylvain Thery's avatar
Sylvain Thery committed
406

Sylvain Thery's avatar
Sylvain Thery committed
407 408 409 410 411
void Drawer::toSVG(Utils::SVG::SVGOut& svg)
{
	const Geom::Vec3f* ptrP = reinterpret_cast<Geom::Vec3f*>(m_vboPos->lockPtr());
	const Geom::Vec3f* ptrC = reinterpret_cast<Geom::Vec3f*>(m_vboCol->lockPtr());

412 413 414 415
	Utils::SVG::SvgGroup* svg1 = new Utils::SVG::SvgGroup("points", svg.m_model, svg.m_proj);
	Utils::SVG::SvgGroup* svg2 = new Utils::SVG::SvgGroup("lines", svg.m_model, svg.m_proj);
	Utils::SVG::SvgGroup* svg3 = new Utils::SVG::SvgGroup("faces", svg.m_model, svg.m_proj);

Sylvain Thery's avatar
Sylvain Thery committed
416 417
	for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
	{
418
		svg1->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
419 420 421
		if (pp->mode == GL_POINTS)
		{
			unsigned int end = pp->begin + pp->nb;
422
			svg1->beginPoints();
Sylvain Thery's avatar
Sylvain Thery committed
423
			for (unsigned int i=pp->begin; i<end; ++i)
424 425
				svg1->addPoint(ptrP[i], ptrC[i]);
			svg1->endPoints();
Sylvain Thery's avatar
Sylvain Thery committed
426 427
		}

428
		svg2->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
429 430 431
		if (pp->mode == GL_LINES)
		{
			unsigned int end = pp->begin + pp->nb;
432
			svg2->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
433
			for (unsigned int i=pp->begin; i<end; i+=2)
434 435
				svg2->addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg2->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
436 437
		}

438
		svg3->setWidth(pp->width);
Sylvain Thery's avatar
Sylvain Thery committed
439 440 441
		if ((pp->mode == GL_LINE_LOOP) || (pp->mode == GL_POLYGON))
		{
			unsigned int end = pp->begin + pp->nb-1;
442
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
443
			for (unsigned int i=pp->begin; i<=end; ++i)
444 445 446
				svg3->addLine(ptrP[i], ptrP[i+1], ptrC[i]);
			svg3->addLine(ptrP[end], ptrP[pp->begin], ptrC[end]);
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
447 448 449 450
		}
		if (pp->mode == GL_TRIANGLES)
		{
			unsigned int end = pp->begin + pp->nb;
451
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
452 453
			for (unsigned int i=pp->begin; i<end; i+=3)
			{
454 455 456
				svg3->addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg3->addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg3->addLine(ptrP[i+2], ptrP[i],   ptrC[i+2]);
Sylvain Thery's avatar
Sylvain Thery committed
457
			}
458
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
459 460 461 462
		}
		if (pp->mode == GL_QUADS)
		{
			unsigned int end = pp->begin + pp->nb;
463
			svg3->beginLines();
Sylvain Thery's avatar
Sylvain Thery committed
464 465
			for (unsigned int i=pp->begin; i<end; i+=4)
			{
466 467 468 469
				svg3->addLine(ptrP[i],   ptrP[i+1], ptrC[i]);
				svg3->addLine(ptrP[i+1], ptrP[i+2], ptrC[i+1]);
				svg3->addLine(ptrP[i+2], ptrP[i+3], ptrC[i+2]);
				svg3->addLine(ptrP[i+3], ptrP[i],   ptrC[i+3]);
Sylvain Thery's avatar
Sylvain Thery committed
470
			}
471
			svg3->endLines();
Sylvain Thery's avatar
Sylvain Thery committed
472 473 474
		}
	}

475 476 477 478
	svg.addGroup(svg1);
	svg.addGroup(svg2);
	svg.addGroup(svg3);

Sylvain Thery's avatar
Sylvain Thery committed
479 480 481 482
	m_vboPos->releasePtr();
	m_vboCol->releasePtr();
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
483
} // namespace Utils
484

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
485
} // namespace CGoGN