clippingShader.cpp 21.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/clippingShader.h"

namespace CGoGN
{

namespace Utils
{

ClippingShader::ClippingShader()
{
35 36
	// Initialize clipping planes variables (planes equations table has zero size by default)
	m_unif_clipPlanesEquations = 0;
37
	
38
	// Initialize color attenuation variables
39 40
	m_colorAttenuationFactor = 0.0;
	m_unif_colorAttenuationFactor = 0;
41

42
	// Initialize display variables
43 44 45 46 47
	m_clipPlanesDisplayColor = Geom::Vec3f (1.0, 0.6, 0.0);
	m_clipPlanesDisplayType = STRAIGHT_GRID;
	m_clipPlanesDisplayXRes = 2;
	m_clipPlanesDisplayYRes = 2;
	m_clipPlanesDisplaySize = 10.0;
48 49 50 51
}

ClippingShader::~ClippingShader()
{
52 53 54 55
	// Destroy remaining clip planes drawers
	size_t i;
	for (i = 0; i < m_clipPlanesDrawers.size(); i++)
		delete m_clipPlanesDrawers[i];
56 57
}

58
void ClippingShader::setClippingPlane(Geom::Vec3f vec1, Geom::Vec3f vec2, Geom::Vec3f origin, int planeIndex)
59
{
60
	// Check if the given index is not out of range
61
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
62 63 64
	{
		CGoGNerr
		<< "ERROR - "
65
		<< "ClippingShader::setClippingPlane"
66 67 68 69 70
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

71 72 73 74 75 76 77 78 79 80 81 82 83 84
	// Copy the given clipping plane parameters
	m_clipPlanes[planeIndex].firstVec = vec1;
	m_clipPlanes[planeIndex].firstVec.normalize();
	m_clipPlanes[planeIndex].secondVec = vec2;
	m_clipPlanes[planeIndex].secondVec.normalize();
	m_clipPlanes[planeIndex].origin = origin;

	// Update the planes equations array
	Geom::Vec3f planeNormal = m_clipPlanes[planeIndex].firstVec ^ m_clipPlanes[planeIndex].secondVec;
	float d = -(planeNormal * m_clipPlanes[planeIndex].origin);
	m_clipPlanesEquations[4*planeIndex + 0] = planeNormal[0];
	m_clipPlanesEquations[4*planeIndex + 1] = planeNormal[1];
	m_clipPlanesEquations[4*planeIndex + 2] = planeNormal[2];
	m_clipPlanesEquations[4*planeIndex + 3] = d;
85

86 87
	// Send again the whole planes equations array to shader
	sendClippingPlanesUniform();
88 89 90

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
91 92
}

93
void ClippingShader::setClippingPlaneFirstVec(Geom::Vec3f vec1, int planeIndex)
94
{
95
	// Check if the given index is not out of range
96
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
97 98 99
	{
		CGoGNerr
		<< "ERROR - "
100
		<< "ClippingShader::setClippingPlane"
101 102
		<< " - Given plane index is out of range"
		<< CGoGNendl;
103
		return;
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

	// Copy the given clipping plane parameter
	m_clipPlanes[planeIndex].firstVec = vec1;
	m_clipPlanes[planeIndex].firstVec.normalize();

	// Update the planes equations array
	Geom::Vec3f planeNormal = m_clipPlanes[planeIndex].firstVec ^ m_clipPlanes[planeIndex].secondVec;
	float d = -(planeNormal * m_clipPlanes[planeIndex].origin);
	m_clipPlanesEquations[4*planeIndex + 0] = planeNormal[0];
	m_clipPlanesEquations[4*planeIndex + 1] = planeNormal[1];
	m_clipPlanesEquations[4*planeIndex + 2] = planeNormal[2];
	m_clipPlanesEquations[4*planeIndex + 3] = d;

	// Send again the whole planes equations array to shader
	sendClippingPlanesUniform();

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

void ClippingShader::setClippingPlaneSecondVec(Geom::Vec3f vec2, int planeIndex)
{
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

	// Copy the given clipping plane parameter
	m_clipPlanes[planeIndex].secondVec = vec2;
	m_clipPlanes[planeIndex].secondVec.normalize();

	// Update the planes equations array
	Geom::Vec3f planeNormal = m_clipPlanes[planeIndex].firstVec ^ m_clipPlanes[planeIndex].secondVec;
	float d = -(planeNormal * m_clipPlanes[planeIndex].origin);
	m_clipPlanesEquations[4*planeIndex + 0] = planeNormal[0];
	m_clipPlanesEquations[4*planeIndex + 1] = planeNormal[1];
	m_clipPlanesEquations[4*planeIndex + 2] = planeNormal[2];
	m_clipPlanesEquations[4*planeIndex + 3] = d;

	// Send again the whole planes equations array to shader
	sendClippingPlanesUniform();

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

void ClippingShader::setClippingPlaneOrigin(Geom::Vec3f origin, int planeIndex)
{
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

	// Copy the given clipping plane parameter
	m_clipPlanes[planeIndex].origin = origin;

	// Update the planes equations array
	Geom::Vec3f planeNormal = m_clipPlanes[planeIndex].firstVec ^ m_clipPlanes[planeIndex].secondVec;
	float d = -(planeNormal * m_clipPlanes[planeIndex].origin);
	m_clipPlanesEquations[4*planeIndex + 0] = planeNormal[0];
	m_clipPlanesEquations[4*planeIndex + 1] = planeNormal[1];
	m_clipPlanesEquations[4*planeIndex + 2] = planeNormal[2];
	m_clipPlanesEquations[4*planeIndex + 3] = d;

	// Send again the whole planes equations array to shader
	sendClippingPlanesUniform();

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

Geom::Vec3f ClippingShader::getClippingPlaneFirstVec(int planeIndex)
{
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
192
	{
193 194 195 196 197 198
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return Geom::Vec3f(0.0, 0.0, 0.0);
199
	}
200 201 202

	// Return the parameter
	return m_clipPlanes[planeIndex].firstVec;
203 204
}

205
Geom::Vec3f ClippingShader::getClippingPlaneSecondVec(int planeIndex)
206
{
207 208 209 210 211 212 213 214 215 216
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return Geom::Vec3f(0.0, 0.0, 0.0);
	}
217

218 219
	// Return the parameter
	return m_clipPlanes[planeIndex].secondVec;
220 221
}

222
Geom::Vec3f ClippingShader::getClippingPlaneOrigin(int planeIndex)
223
{
224 225 226 227 228 229 230 231 232 233 234 235 236 237
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return Geom::Vec3f(0.0, 0.0, 0.0);
	}

	// Return the parameter
	return m_clipPlanes[planeIndex].origin;
}
238 239 240

void ClippingShader::setClippingColorAttenuationFactor(float colorAttenuationFactor)
{
241
	// Copy the given value
242
	m_colorAttenuationFactor = colorAttenuationFactor;
243 244 245

	// Send again the uniform to shader
	sendColorAttenuationFactorUniform();
246 247
}

248
void ClippingShader::setClippingPlanesCount(int planesCount)
249
{
250 251 252 253 254 255
	// Verify that the given clipping planes count is valid
	if (planesCount < 0)
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setPlanesClipping"
256
		<< " - Given clipping planes count is not positive !"
257 258 259 260
		<< CGoGNendl;
		return;
	}

261 262 263
	// Shader name
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

264 265 266 267 268
	// Verify that the shader has been well created
	if (!isCreated())
	{
		CGoGNerr
		<< "ERROR - "
269
		<< "ClippingShader::setPlaneClipping"
270
		<< " - Could not process shader "
271
		<< shaderName
272 273 274 275 276 277 278 279 280 281
		<< " source code : shader has not been created or has failed to compile"
		<< CGoGNendl;
		return;
	}
	
	// Verify that the shader does not use a geometry shader
	if (getGeometryShaderSrc() != NULL)
	{
		CGoGNerr
		<< "ERROR - "
282
		<< "ClippingShader::setPlaneClipping"
283
		<< " - Could not process shader "
284
		<< shaderName
285 286 287 288 289
		<< " source code : unable to add clipping to a shader which uses a geometry shader"
		<< CGoGNendl;
		return;
	}

290 291 292 293 294
	// String for clipping planes count
	std::string planesCountStr;
	std::stringstream ss;
	ss << planesCount;
	planesCountStr = ss.str();
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309
	// Strings that will be inserted into the source code

	std::string VS_head_insertion =
	"\n"
	"VARYING_VERT vec3 clip_NonTransformedPos;\n"
	"\n";

	std::string VS_mainBegin_insertion =
	"\n"
	"	// Pass the non transformed vertex position to the fragment shader for clipping\n"
	"	clip_NonTransformedPos = VertexPosition;\n";

	std::string FS_head_insertion =
	"\n"
310
	"#define CLIP_PLANES_COUNT " + planesCountStr + "\n"
311
	"\n"
312
	"uniform vec4 clip_ClipPlanes[CLIP_PLANES_COUNT];\n"
313 314 315 316 317 318 319
	"uniform float clip_ColorAttenuationFactor;\n"
	"\n"
	"VARYING_FRAG vec3 clip_NonTransformedPos;\n"
	"\n";

	std::string FS_mainBegin_insertion =
	"\n"
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	"	// Distance to the nearest plane, stored for color attenuation\n"
	"	float clip_MinDistanceToPlanes = -1.0;\n"
	"\n"
	"	// Do clipping for each plane\n"
	"	int i;\n"
	"	for (i = 0; i < CLIP_PLANES_COUNT; i++)\n"
	"	{\n"
	"		// Copy the plane to make it modifiable\n"
	"		vec4 clip_CurrClipPlane = clip_ClipPlanes[i];\n"
	"\n"
	"		// If the plane normal is zero, use a default normal vector (0.0, 0.0, 1.0)\n"
	"		float clip_NPlane = length(clip_CurrClipPlane.xyz);\n"
	"		if (clip_NPlane == 0.0)\n"
	"		{\n"
	"			clip_CurrClipPlane.z = 1.0;\n"
	"			clip_NPlane = 1.0;\n"
	"		}\n"
	"\n"
	"		// Signed distance between the point and the plane\n"
339 340
	"		float clip_DistanceToPlane = dot(clip_NonTransformedPos, clip_CurrClipPlane.xyz);\n"
	"		clip_DistanceToPlane += clip_CurrClipPlane.w;\n"
341 342
	"		clip_DistanceToPlane /= clip_NPlane;\n"
	"\n"
343 344 345 346 347 348 349 350 351 352 353 354
	"		// Keep the fragment only if it is 'above' the plane\n"
	"		if (clip_DistanceToPlane < 0.0)\n"
	"			discard;\n"
	"		// Else keep the positive distance to the nearest plane\n"
	"		else\n"
	"		{\n"
	"			if (clip_MinDistanceToPlanes < 0.0)\n"
	"				clip_MinDistanceToPlanes = clip_DistanceToPlane;\n"
	"			else\n"
	"				clip_MinDistanceToPlanes = min(clip_MinDistanceToPlanes, clip_DistanceToPlane);\n"
	"		}\n"
	"	}\n";
355 356 357

	std::string FS_mainEnd_insertion =
	"\n"
358 359 360
	"	// Attenuate the final fragment color depending on its distance to the nearest plane\n"
	"	if (clip_MinDistanceToPlanes > 0.0)\n"
	"		gl_FragColor.rgb /= (1.0 + clip_MinDistanceToPlanes*clip_ColorAttenuationFactor);\n";
361

362 363
	// Previous planes count
	int previousPlanesCount = getClippingPlanesCount();
364

365 366
	// If the previous planes count was zero, the previous shader source codes were the original ones. Store them
	// (the planes count is initially zero when the object is constructed)
367
	if (previousPlanesCount == 0)
368
	{
369 370
		originalVertShaderSrc = getVertexShaderSrc();
		originalFragShaderSrc = getFragmentShaderSrc();
371
	}
372

373
	// If the given plane count is > 0, modify the shader sources
374 375 376
	if (planesCount > 0)
	{

377 378
		// If the previous planes count wasn't zero, there is only one constant to change in the fragment shader
		if (previousPlanesCount != 0)
379
		{
380 381
			// Use a shader mutator
			ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
382

383 384
			// Modify the constant in the fragment shader
			SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", planesCount);
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
			// Reload fragment shader
			reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
		}
		// Else the whole clipping code must be inserted in the original shader sources
		else
		{
			// Use a shader mutator
			ShaderMutator SM(shaderName, originalVertShaderSrc, originalFragShaderSrc);

			// First check if the vertex shader contains the VertexPosition attribute
			if (!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"))
			{
				CGoGNerr
				<< "ERROR - "
				<< "ClippingShader::setPlaneClipping"
				<< " - Could not process shader "
				<< m_nameVS
				<< " source code : no VertexPosition attribute found"
				<< CGoGNendl;
				return;
			}

			// Modify vertex shader source code
			SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_head_insertion);
			SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainBegin_insertion);


			// Modify fragment shader source code
			SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120); // Following code insertions need at least shading language 120 (GLSL arrays)
			SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_head_insertion);
			SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEnd_insertion);
			SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBegin_insertion);

			// Reload both shaders
			reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
			reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
		}
423

424 425 426 427 428 429 430 431
	}
	// Else no clipping is wanted anymore, so get back the original shader sources
	else
	{
		// Reload both original shaders
		reloadVertexShaderFromMemory(originalVertShaderSrc.c_str());
		reloadFragmentShaderFromMemory(originalFragShaderSrc.c_str());
	}
432

433 434 435 436
	// Resize the planes array to the right size
	m_clipPlanes.resize((size_t)planesCount);

	// Resize the planes equations array to the right size
437 438
	m_clipPlanesEquations.resize(4*(size_t)planesCount, 0.0);

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	// Resize the planes drawers array to the right size, and create/destroy objects
	if (planesCount > previousPlanesCount)
	{
		m_clipPlanesDrawers.resize((size_t)planesCount, NULL);
		int i;
		for (i = previousPlanesCount; i < planesCount; i++)
			m_clipPlanesDrawers[i] = new Drawer;
	}
	else
	{
		int i;
		for (i = planesCount; i < previousPlanesCount; i++)
			delete m_clipPlanesDrawers[i];
	}

	// Set default parameters values for new planes
	if (planesCount > previousPlanesCount)
	{
		Geom::Vec3f defaultFirstVec (1.0, 0.0, 0.0);
		Geom::Vec3f defaultSecondVec (0.0, 1.0, 0.0);
		Geom::Vec3f defaultOrigin (0.0, 0.0, 0.0);

		int i;
		for (i = previousPlanesCount; i < planesCount; i++)
			setClippingPlane(defaultFirstVec, defaultSecondVec, defaultOrigin, i);
	}

466
	// Recompile shaders (automatically calls updateClippingUniforms)
467 468 469 470 471
	recompile();
}

void ClippingShader::updateClippingUniforms()
{
472 473 474 475
	// These uniforms only exist if the clipping planes count is > 0
	if (getClippingPlanesCount() <= 0)
		return;

476 477 478
	// Shader name
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

479
	// Get uniforms locations
480 481
	m_unif_clipPlanesEquations = glGetUniformLocation(program_handler(), "clip_ClipPlanes");
	if (m_unif_clipPlanesEquations == -1)
482 483 484
	{
		CGoGNerr
		<< "ERROR - "
485
		<< "ClippingShader::updateClippingUniforms"
486
		<< " - uniform 'clip_ClipPlane' not found in shader "
487
		<< shaderName
488 489 490 491 492 493 494
		<< CGoGNendl;
	}
	m_unif_colorAttenuationFactor = glGetUniformLocation(program_handler(), "clip_ColorAttenuationFactor");
	if (m_unif_colorAttenuationFactor == -1)
	{
		CGoGNerr
		<< "ERROR - "
495
		<< "ClippingShader::updateClippingUniforms"
496
		<< " - uniform 'clip_ColorAttenuationFactor' not found in shader "
497
		<< shaderName
498 499 500
		<< CGoGNendl;
	}
	
501 502 503
	// Send again uniforms values
	sendClippingPlanesUniform();
	sendColorAttenuationFactorUniform();
504 505
}

506 507 508 509 510 511 512
void ClippingShader::displayClippingPlanes()
{
	size_t i;
	for (i = 0; i < m_clipPlanesDrawers.size(); i++)
		m_clipPlanesDrawers[i]->callList();
}

513 514 515
void ClippingShader::sendClippingPlanesUniform()
{
	bind();
516
	glUniform4fv(m_unif_clipPlanesEquations, getClippingPlanesCount(), &m_clipPlanesEquations.front());
517 518 519 520 521 522 523 524
}

void ClippingShader::sendColorAttenuationFactorUniform()
{
	bind();
	glUniform1f(m_unif_colorAttenuationFactor, m_colorAttenuationFactor);
}

525
void ClippingShader::updateClippingPlaneVBO(int planeIndex)
526
{
527 528 529 530 531 532 533 534 535 536 537 538 539
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClippingPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::updateClippingPlaneVBO"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

	// Compute four point of the plane at equal distance from plane origin
	Geom::Vec3f p1 = m_clipPlanes[planeIndex].origin
540 541
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
542
	Geom::Vec3f p2 = m_clipPlanes[planeIndex].origin
543 544
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
545
	Geom::Vec3f p3 = m_clipPlanes[planeIndex].origin
546 547
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
548
	Geom::Vec3f p4 = m_clipPlanes[planeIndex].origin
549 550
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
551

552
	// Build again the VBO with the new points
553
	m_clipPlanesDrawers[planeIndex]->newList(GL_COMPILE);
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

	// Only display the grid if both x and y resolutions are not zero
	if ( (m_clipPlanesDisplayXRes != 0) && (m_clipPlanesDisplayYRes != 0) )
	{
		size_t i;
		float t;
		Geom::Vec3f p1p2Interp;
		Geom::Vec3f p4p3Interp;
		Geom::Vec3f p3p4Interp; // Used for non straight grid construction
		Geom::Vec3f p2p3Interp;
		Geom::Vec3f p1p4Interp;
		Geom::Vec3f p4p1Interp; // Used for non straight grid construction

		// X lines
		for (i = 0; i <= m_clipPlanesDisplayXRes; i++)
		{
			// Compute the linear interpolation parameter from the current value of 'i'
			t = (float)i / (float)m_clipPlanesDisplayXRes;

			// Straight grid construction
			if (m_clipPlanesDisplayType == STRAIGHT_GRID)
			{
				// Compute linear interpolations between points
				p1p2Interp = p1*t + p2*(1.0 - t);
				p4p3Interp = p4*t + p3*(1.0 - t);

				// Draw lines between the resulting points
				m_clipPlanesDrawers[planeIndex]->begin(GL_LINES);
				m_clipPlanesDrawers[planeIndex]->color3f(
						m_clipPlanesDisplayColor[0],
						m_clipPlanesDisplayColor[1],
						m_clipPlanesDisplayColor[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p1p2Interp[0],
						p1p2Interp[1],
						p1p2Interp[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p4p3Interp[0],
						p4p3Interp[1],
						p4p3Interp[2]);
				m_clipPlanesDrawers[planeIndex]->end();
			}
			// Radial grid construction
			else if (m_clipPlanesDisplayType == RADIAL_GRID)
			{
				// Compute linear interpolations between points
				p1p2Interp = p1*t + p2*(1.0 - t);
				p3p4Interp = p3*t + p4*(1.0 - t);

				// Draw lines between the resulting points
				m_clipPlanesDrawers[planeIndex]->begin(GL_LINES);
				m_clipPlanesDrawers[planeIndex]->color3f(
						m_clipPlanesDisplayColor[0],
						m_clipPlanesDisplayColor[1],
						m_clipPlanesDisplayColor[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p1p2Interp[0],
						p1p2Interp[1],
						p1p2Interp[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p3p4Interp[0],
						p3p4Interp[1],
						p3p4Interp[2]);
				m_clipPlanesDrawers[planeIndex]->end();
			}
		}

		// Y lines
		for (i = 0; i <= m_clipPlanesDisplayYRes; i++)
		{
			// Compute the linear interpolation parameter from the current value of 'i'
			t = (float)i / (float)m_clipPlanesDisplayYRes;

			// Straight grid construction
			if (m_clipPlanesDisplayType == STRAIGHT_GRID)
			{
				// Compute linear interpolations between points
				p2p3Interp = p2*t + p3*(1.0 - t);
				p1p4Interp = p1*t + p4*(1.0 - t);

				// Draw lines between the resulting points
				m_clipPlanesDrawers[planeIndex]->begin(GL_LINES);
				m_clipPlanesDrawers[planeIndex]->color3f(
						m_clipPlanesDisplayColor[0],
						m_clipPlanesDisplayColor[1],
						m_clipPlanesDisplayColor[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p2p3Interp[0],
						p2p3Interp[1],
						p2p3Interp[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p1p4Interp[0],
						p1p4Interp[1],
						p1p4Interp[2]);
				m_clipPlanesDrawers[planeIndex]->end();
			}
			// Radial grid construction
			else if (m_clipPlanesDisplayType == RADIAL_GRID)
			{
				// Compute linear interpolations between points
				p2p3Interp = p2*t + p3*(1.0 - t);
				p4p1Interp = p4*t + p1*(1.0 - t);

				// Draw lines between the resulting points
				m_clipPlanesDrawers[planeIndex]->begin(GL_LINES);
				m_clipPlanesDrawers[planeIndex]->color3f(
						m_clipPlanesDisplayColor[0],
						m_clipPlanesDisplayColor[1],
						m_clipPlanesDisplayColor[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p2p3Interp[0],
						p2p3Interp[1],
						p2p3Interp[2]);
				m_clipPlanesDrawers[planeIndex]->vertex3f(
						p4p1Interp[0],
						p4p1Interp[1],
						p4p1Interp[2]);
				m_clipPlanesDrawers[planeIndex]->end();
			}
		}

	}

677
	m_clipPlanesDrawers[planeIndex]->endList();
678 679
}

680 681 682
} // namespace Utils

} // namespace CGoGN