clippingShader.cpp 26.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
	// Initialize uniforms ids
36
	m_unif_clipPlanesEquations = 0;
37
	m_unif_colorAttenuationFactor = 0;
38
	
39
	// Initialize default color attenuation variables
40
	m_colorAttenuationFactor = 1.0;
41

42
	// Initialize default 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

	// Initialize default special variables
	m_useAdvancedPlaneClipping = false;
51 52 53 54
}

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

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

74 75 76 77 78 79 80
	// 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;

81 82
	// Update the plane arrays
	updateClippingPlanesArrays(planeIndex);
83

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

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
89 90
}

91
void ClippingShader::setClipPlaneFirstVec(Geom::Vec3f vec1, int planeIndex)
92
{
93
	// Check if the given index is not out of range
94
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
95 96 97
	{
		CGoGNerr
		<< "ERROR - "
98
		<< "ClippingShader::setClippingPlane"
99 100
		<< " - Given plane index is out of range"
		<< CGoGNendl;
101
		return;
102
	}
103 104 105 106 107

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

108 109
	// Update the plane arrays
	updateClippingPlanesArrays(planeIndex);
110 111 112 113 114 115 116 117

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

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

118
void ClippingShader::setClipPlaneSecondVec(Geom::Vec3f vec2, int planeIndex)
119 120
{
	// Check if the given index is not out of range
121
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
122 123 124 125 126 127 128 129 130 131 132 133 134
	{
		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();

135 136
	// Update the plane arrays
	updateClippingPlanesArrays(planeIndex);
137 138 139 140 141 142 143 144

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

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

145
void ClippingShader::setClipPlaneOrigin(Geom::Vec3f origin, int planeIndex)
146 147
{
	// Check if the given index is not out of range
148
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
149 150 151 152 153 154 155 156 157 158 159 160
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

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

161 162
	// Update the plane arrays
	updateClippingPlanesArrays(planeIndex);
163 164 165 166 167 168 169 170

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

	// Update plane VBO
	updateClippingPlaneVBO(planeIndex);
}

171
Geom::Vec3f ClippingShader::getClipPlaneFirstVec(int planeIndex)
172 173
{
	// Check if the given index is not out of range
174
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
175
	{
176 177 178 179 180 181
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setClippingPlane"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return Geom::Vec3f(0.0, 0.0, 0.0);
182
	}
183 184 185

	// Return the parameter
	return m_clipPlanes[planeIndex].firstVec;
186 187
}

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

201 202
	// Return the parameter
	return m_clipPlanes[planeIndex].secondVec;
203 204
}

205
Geom::Vec3f ClippingShader::getClipPlaneOrigin(int planeIndex)
206
{
207
	// Check if the given index is not out of range
208
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
209 210 211 212 213 214 215 216 217 218 219 220
	{
		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;
}
221

222
void ClippingShader::setClipColorAttenuationFactor(float colorAttenuationFactor)
223
{
224
	// Copy the given value
225
	m_colorAttenuationFactor = colorAttenuationFactor;
226 227 228

	// Send again the uniform to shader
	sendColorAttenuationFactorUniform();
229 230
}

231
void ClippingShader::setClipPlanesCount(int planesCount)
232
{
233 234 235 236 237 238
	// Verify that the given clipping planes count is valid
	if (planesCount < 0)
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::setPlanesClipping"
239
		<< " - Given clipping planes count is not positive !"
240 241 242 243
		<< CGoGNendl;
		return;
	}

244 245 246
	// Shader name
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

247 248 249 250 251
	// Verify that the shader has been well created
	if (!isCreated())
	{
		CGoGNerr
		<< "ERROR - "
252
		<< "ClippingShader::setPlaneClipping"
253
		<< " - Could not process shader "
254
		<< shaderName
255 256 257 258 259 260 261 262 263 264
		<< " 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 - "
265
		<< "ClippingShader::setPlaneClipping"
266
		<< " - Could not process shader "
267
		<< shaderName
268 269 270 271 272
		<< " source code : unable to add clipping to a shader which uses a geometry shader"
		<< CGoGNendl;
		return;
	}

273 274 275 276 277
	// String for clipping planes count
	std::string planesCountStr;
	std::stringstream ss;
	ss << planesCount;
	planesCountStr = ss.str();
278

279 280 281 282 283 284 285 286 287 288 289 290
	// 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";

291 292
	std::string FS_head_insertion;
	std::string FS_mainBegin_insertion;
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 338 339 340 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 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	if (!m_useAdvancedPlaneClipping) // the shader is slightly different for advanced plane clipping
	{
		FS_head_insertion =
		"\n"
		"#define CLIP_PLANES_COUNT " + planesCountStr + "\n"
		"\n"
		"uniform vec4 clip_ClipPlanes[CLIP_PLANES_COUNT];\n"
		"uniform float clip_ColorAttenuationFactor;\n"
		"\n"
		"VARYING_FRAG vec3 clip_NonTransformedPos;\n"
		"\n";

		FS_mainBegin_insertion =
		"\n"
		"	// 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"
		"		float clip_DistanceToPlane = dot(clip_NonTransformedPos, clip_CurrClipPlane.xyz);\n"
		"		clip_DistanceToPlane += clip_CurrClipPlane.w;\n"
		"		clip_DistanceToPlane /= clip_NPlane;\n"
		"\n"
		"		// Keep the fragment only if it is 'above' the plane\n"
		"		if (clip_DistanceToPlane < 0.0)\n"
		"			discard;\n"
		"		else\n"
		"		{\n"
		"			// Keep the distance to the nearest plane\n"
		"			if (clip_MinDistanceToPlanes < 0.0)\n"
		"				clip_MinDistanceToPlanes = clip_DistanceToPlane;\n"
		"			else\n"
		"				clip_MinDistanceToPlanes = min(clip_MinDistanceToPlanes, clip_DistanceToPlane);\n"
		"		}\n"
		"	}\n";
	}
	else
	{
		FS_head_insertion =
		"\n"
		"#define CLIP_PLANES_COUNT " + planesCountStr + "\n"
		"\n"
		"uniform vec4 clip_ClipPlanes[CLIP_PLANES_COUNT];\n"
		"uniform vec3 clip_ClipPlanesFirstVec[CLIP_PLANES_COUNT];\n"
		"uniform vec3 clip_ClipPlanesSecondVec[CLIP_PLANES_COUNT];\n"
		"uniform vec3 clip_ClipPlanesOrigin[CLIP_PLANES_COUNT];\n"
		"\n"
		"uniform float clip_ClipPlanesSize;\n"
		"\n"
		"uniform float clip_ColorAttenuationFactor;\n"
		"\n"
		"VARYING_FRAG vec3 clip_NonTransformedPos;\n"
		"\n";

		FS_mainBegin_insertion =
		"\n"
		"	// 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"
		"		float clip_DistanceToPlane = dot(clip_NonTransformedPos, clip_CurrClipPlane.xyz);\n"
		"		clip_DistanceToPlane += clip_CurrClipPlane.w;\n"
		"		clip_DistanceToPlane /= clip_NPlane;\n"
		"\n"
		"		// Keep the fragment only if it is 'above' or 'outside' the plane\n"
		"		if (clip_DistanceToPlane < 0.0)\n"
		"		{\n"
		"			// Get the orthogonal projection of the point on the plane\n"
		"			vec3 clip_PosPlaneProj = clip_NonTransformedPos;\n"
		"			clip_PosPlaneProj -= (clip_DistanceToPlane*clip_NPlane) * clip_CurrClipPlane.xyz;\n"
		"\n"
		"			// Get the distance of the point to origin on each plane vector\n"
		"			vec3 clip_ToPlaneOrigin = clip_PosPlaneProj - clip_ClipPlanesOrigin[i];\n"
		"			float clip_PosVec1Val = abs(dot(clip_ToPlaneOrigin, vec3(clip_ClipPlanesFirstVec[i].x, clip_ClipPlanesFirstVec[i].y, clip_ClipPlanesFirstVec[i].z)));\n"
		"			clip_PosVec1Val /= length(clip_ClipPlanesFirstVec[i]);\n"
		"			float clip_PosVec2Val = abs(dot(clip_ToPlaneOrigin, vec3(clip_ClipPlanesSecondVec[i].x, clip_ClipPlanesSecondVec[i].y, clip_ClipPlanesSecondVec[i].z)));\n"
		"			clip_PosVec2Val /= length(clip_ClipPlanesSecondVec[i]);\n"
		"\n"
		"			// Check if the projected point is 'inside' the plane\n"
		"			if ( ( clip_PosVec1Val < (length(clip_ClipPlanesFirstVec[i]) * 0.5 * clip_ClipPlanesSize))\n"
		"			&& ( clip_PosVec2Val < (length(clip_ClipPlanesSecondVec[i]) * 0.5 * clip_ClipPlanesSize)) )\n"
		"				discard;\n"
		"			else\n"
		"			{\n"
		"				// Keep the distance to the nearest plane\n"
		"				if (clip_MinDistanceToPlanes < 0.0)\n"
		"					clip_MinDistanceToPlanes = -clip_DistanceToPlane;\n"
		"				else\n"
		"					clip_MinDistanceToPlanes = min(clip_MinDistanceToPlanes, -clip_DistanceToPlane);\n"
		"			}\n"
		"		}\n"
		"		else\n"
		"		{\n"
		"			// Keep the distance to the nearest plane\n"
		"			if (clip_MinDistanceToPlanes < 0.0)\n"
		"				clip_MinDistanceToPlanes = clip_DistanceToPlane;\n"
		"			else\n"
		"				clip_MinDistanceToPlanes = min(clip_MinDistanceToPlanes, clip_DistanceToPlane);\n"
		"		}\n"
		"	}\n";
	}
424 425 426

	std::string FS_mainEnd_insertion =
	"\n"
427 428 429
	"	// 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";
430

431
	// Previous planes count
432
	int previousPlanesCount = getClipPlanesCount();
433

434 435
	// 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)
436
	if (previousPlanesCount == 0)
437
	{
438 439
		originalVertShaderSrc = getVertexShaderSrc();
		originalFragShaderSrc = getFragmentShaderSrc();
440
	}
441

442
	// If the given plane count is > 0, modify the shader sources
443 444 445
	if (planesCount > 0)
	{

446 447
		// If the previous planes count wasn't zero, there is only one constant to change in the fragment shader
		if (previousPlanesCount != 0)
448
		{
449 450
			// Use a shader mutator
			ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
451

452 453
			// Modify the constant in the fragment shader
			SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", planesCount);
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
			// 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());
		}
492

493 494 495 496 497 498 499 500
	}
	// 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());
	}
501

502
	// Resize the planes arrays to the right size
503
	m_clipPlanes.resize((size_t)planesCount);
504
	m_clipPlanesEquations.resize(4*(size_t)planesCount, 0.0);
505 506 507
	m_clipPlanesFirstVectors.resize(4*(size_t)planesCount, 0.0);
	m_clipPlanesSecondVectors.resize(4*(size_t)planesCount, 0.0);
	m_clipPlanesOrigins.resize(4*(size_t)planesCount, 0.0);
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	// 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++)
533
			setClipPlane(defaultFirstVec, defaultSecondVec, defaultOrigin, i);
534 535
	}

536
	// Recompile shaders (automatically calls updateClippingUniforms)
537 538 539 540 541
	recompile();
}

void ClippingShader::updateClippingUniforms()
{
542
	// These uniforms only exist if the clipping planes count is > 0
543
	if (getClipPlanesCount() <= 0)
544 545
		return;

546 547 548
	// Shader name
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

549
	// Get uniforms locations
550

551 552
	m_unif_clipPlanesEquations = glGetUniformLocation(program_handler(), "clip_ClipPlanes");
	if (m_unif_clipPlanesEquations == -1)
553 554 555
	{
		CGoGNerr
		<< "ERROR - "
556
		<< "ClippingShader::updateClippingUniforms"
557
		<< " - uniform 'clip_ClipPlane' not found in shader "
558
		<< shaderName
559 560
		<< CGoGNendl;
	}
561

562 563 564 565 566
	m_unif_colorAttenuationFactor = glGetUniformLocation(program_handler(), "clip_ColorAttenuationFactor");
	if (m_unif_colorAttenuationFactor == -1)
	{
		CGoGNerr
		<< "ERROR - "
567
		<< "ClippingShader::updateClippingUniforms"
568
		<< " - uniform 'clip_ColorAttenuationFactor' not found in shader "
569
		<< shaderName
570 571 572
		<< CGoGNendl;
	}
	
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
	// For advanced plane clipping
	if (m_useAdvancedPlaneClipping)
	{
		m_unif_clipPlanesFirstVectors = glGetUniformLocation(program_handler(), "clip_ClipPlanesFirstVec");
		if (m_unif_clipPlanesFirstVectors == -1)
		{
			CGoGNerr
			<< "ERROR - "
			<< "ClippingShader::updateClippingUniforms"
			<< " - uniform 'clip_ClipPlanesFirstVec' not found in shader "
			<< shaderName
			<< CGoGNendl;
		}
		m_unif_clipPlanesSecondVectors = glGetUniformLocation(program_handler(), "clip_ClipPlanesSecondVec");
		if (m_unif_clipPlanesSecondVectors == -1)
		{
			CGoGNerr
			<< "ERROR - "
			<< "ClippingShader::updateClippingUniforms"
			<< " - uniform 'clip_ClipPlanesSecondVec' not found in shader "
			<< shaderName
			<< CGoGNendl;
		}
		m_unif_clipPlanesOrigins = glGetUniformLocation(program_handler(), "clip_ClipPlanesOrigin");
		if (m_unif_clipPlanesOrigins == -1)
		{
			CGoGNerr
			<< "ERROR - "
			<< "ClippingShader::updateClippingUniforms"
			<< " - uniform 'clip_ClipPlanesOrigin' not found in shader "
			<< shaderName
			<< CGoGNendl;
		}
		m_unif_clipPlanesDisplaySize = glGetUniformLocation(program_handler(), "clip_ClipPlanesSize");
		if (m_unif_clipPlanesDisplaySize == -1)
		{
			CGoGNerr
			<< "ERROR - "
			<< "ClippingShader::updateClippingUniforms"
			<< " - uniform 'clip_ClipPlanesSize' not found in shader "
			<< shaderName
			<< CGoGNendl;
		}
	}

618 619 620
	// Send again uniforms values
	sendClippingPlanesUniform();
	sendColorAttenuationFactorUniform();
621 622
}

623
void ClippingShader::displayClipPlanes()
624 625 626 627 628 629
{
	size_t i;
	for (i = 0; i < m_clipPlanesDrawers.size(); i++)
		m_clipPlanesDrawers[i]->callList();
}

630 631 632
void ClippingShader::sendClippingPlanesUniform()
{
	bind();
633 634 635 636 637 638 639 640 641 642 643

	glUniform4fv(m_unif_clipPlanesEquations, getClipPlanesCount(), &m_clipPlanesEquations.front());

	// For advanced plane clipping
	if (m_useAdvancedPlaneClipping)
	{
		glUniform3fv(m_unif_clipPlanesFirstVectors, getClipPlanesCount(), &m_clipPlanesFirstVectors.front());
		glUniform3fv(m_unif_clipPlanesSecondVectors, getClipPlanesCount(), &m_clipPlanesSecondVectors.front());
		glUniform3fv(m_unif_clipPlanesOrigins, getClipPlanesCount(), &m_clipPlanesOrigins.front());
		glUniform1f(m_unif_clipPlanesDisplaySize, m_clipPlanesDisplaySize);
	}
644 645 646 647 648 649 650 651
}

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

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 677 678 679 680 681 682 683
void ClippingShader::updateClippingPlanesArrays(int planeIndex)
{
	// Check if the given index is not out of range
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
	{
		CGoGNerr
		<< "ERROR - "
		<< "ClippingShader::updateClippingPlaneVBO"
		<< " - Given plane index is out of range"
		<< CGoGNendl;
		return;
	}

	// Update the plane first vector, second vector and origin arrays
	int i;
	for (i = 0; i < 3; i++)
	{
		m_clipPlanesFirstVectors[4*planeIndex + i] = m_clipPlanes[planeIndex].firstVec[i];
		m_clipPlanesSecondVectors[4*planeIndex + i] = m_clipPlanes[planeIndex].secondVec[i];
		m_clipPlanesOrigins[4*planeIndex + i] = m_clipPlanes[planeIndex].origin[i];
	}

	// Update the plane equation 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;

}

684
void ClippingShader::updateClippingPlaneVBO(int planeIndex)
685
{
686
	// Check if the given index is not out of range
687
	if ((planeIndex < 0) || (planeIndex > (getClipPlanesCount() - 1)))
688 689 690 691 692 693 694 695 696 697 698
	{
		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
699 700
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
701
	Geom::Vec3f p2 = m_clipPlanes[planeIndex].origin
702 703
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
704
	Geom::Vec3f p3 = m_clipPlanes[planeIndex].origin
705 706
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
707
	Geom::Vec3f p4 = m_clipPlanes[planeIndex].origin
708 709
			- (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].firstVec
			+ (0.5f * m_clipPlanesDisplaySize) * m_clipPlanes[planeIndex].secondVec;
710

711

712
	// Build again the VBO with the new points
713

714
	m_clipPlanesDrawers[planeIndex]->newList(GL_COMPILE);
715 716 717 718 719 720 721 722

	// 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;
723
		Geom::Vec3f p3p4Interp; // Used for radial grid construction
724 725
		Geom::Vec3f p2p3Interp;
		Geom::Vec3f p1p4Interp;
726
		Geom::Vec3f p4p1Interp; // Used for radial grid construction
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

		// 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();
			}
		}

	}

838
	m_clipPlanesDrawers[planeIndex]->endList();
839 840
}

841 842 843
void ClippingShader::updateAllClippingPlanesVBOs()
{
	int i;
844
	for (i = 0; i < getClipPlanesCount(); i++)
845 846 847
		updateClippingPlaneVBO(i);
}

848 849 850
} // namespace Utils

} // namespace CGoGN