clippingShader.cpp 39.1 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
/*******************************************************************************
* 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
{

33

34
35
/***********************************************
 *
36
 * 		Constructor / Destructor
37
38
39
 *
 ***********************************************/

40

41
42
43
44
45
46
47
ClippingShader::ClippingShader():
		// Initialize clipping shapes variables
		m_unif_clipPlanesEquations (0),
		m_unif_clipSpheresCentersAndRadiuses (0),

		// Initialize default global clipping variables
		m_hasClippingCodeBeenInserted (false),
48
		m_clipColorAttenuationFactor (1.0f),
49
		m_unif_clipColorAttenuationFactor (0),
50
		m_colorAttenuationMode (COLOR_ATTENUATION_MODE_LINEAR),
51
		m_clipMode (CLIPPING_MODE_AND)
52
{
53

54
55
}

56

57
58
59
60
61
/***********************************************
 *
 * 		Plane Clipping
 *
 ***********************************************/
62

63

64
unsigned int ClippingShader::addClipPlane()
65
{
66
67
68
	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::addClipPlane"))
		return -1;
69

70
71
	// Shader name string
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
72

73
74
	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
75

76
77
	// Previous planes count
	int previousPlanesCount = getClipPlanesCount();
78

79
80
81
82
83
84
	// Modify the clip planes count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_PLANES_COUNT", previousPlanesCount + 1))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", previousPlanesCount + 1)),
			"ClippingShader::addClipPlane"))
		return -1;
85

86
87
88
	// Reload both shaders
	reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
	reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
89

90
91
	// Get new plane id
	unsigned int newPlaneId = getFreeClipPlaneId();
92

93
94
	// Resize planes arrays to the right size
	m_clipPlanes.resize((size_t)(previousPlanesCount + 1));
95
96
	if (newPlaneId >= m_clipPlanesIds.size())
		m_clipPlanesIds.resize((size_t)(newPlaneId + 1));
97
	m_clipPlanesEquations.resize(4*(size_t)(previousPlanesCount + 1), 0.0f);
98

99
100
101
	// Set new plane id
	m_clipPlanesIds[newPlaneId].used = true;
	m_clipPlanesIds[newPlaneId].index = previousPlanesCount;
102

103
	// Set default parameters values for the new plane
104
105
	Geom::Vec3f defaultNormal (0.0f, 0.0f, 1.0f);
	Geom::Vec3f defaultOrigin (0.0f, 0.0f, 0.0f);
106
	setClipPlaneParamsAll(newPlaneId, defaultNormal, defaultOrigin);
107

108
109
	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
110

111
112
113
114
115
116
117
118
	return newPlaneId;
}

void ClippingShader::deleteClipPlane(unsigned int id)
{
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::deleteClipPlane"))
		return;
119
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::deleteClipPlane"))
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
		return;

	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteClipPlane"))
		return;

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

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Previous planes count
	int previousPlanesCount = getClipPlanesCount();

	// Modify the clip planes count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_PLANES_COUNT", previousPlanesCount - 1))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", previousPlanesCount - 1)),
			"ClippingShader::deleteClipPlane"))
		return;

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

	// Rearrange planes arrays
	m_clipPlanes.erase(m_clipPlanes.begin() + m_clipPlanesIds[id].index);
148
	for (size_t i = 0; i < m_clipPlanesIds.size(); i++)
149
150
151
	{
		if (m_clipPlanesIds[i].index > m_clipPlanesIds[id].index)
			m_clipPlanesIds[i].index -= 1;
152
	}
153
154
155
156
157
158
159
	m_clipPlanesIds[id].used = false;
	m_clipPlanesEquations.erase(
			m_clipPlanesEquations.begin() + 4*m_clipPlanesIds[id].index,
			m_clipPlanesEquations.begin() + 4*m_clipPlanesIds[id].index + 4);

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
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
192
193
void ClippingShader::deleteAllClipPlanes()
{
	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteAllClipPlanes"))
		return;

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

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Modify the clip planes count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_PLANES_COUNT", 0))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_PLANES_COUNT", 0)),
			"ClippingShader::deleteAllClipPlanes"))
		return;

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

	// Rearrange planes arrays
	m_clipPlanes.resize(0);
	m_clipPlanesIds.resize(0);
	m_clipPlanesEquations.resize(0);

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
}

194
195
196
197
198
int ClippingShader::getClipPlanesCount()
{
	return (int)m_clipPlanes.size();
}

199
void ClippingShader::setClipPlaneParamsAll(unsigned int id, Geom::Vec3f normal, Geom::Vec3f origin)
200
{
201
202
203
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::setClipPlaneParamsAll"))
		return;
204
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::setClipPlaneParamsAll"))
205
206
207
208
		return;

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
209

210
	// Normalize
211
212
	Geom::Vec3f normalNormalized = normal;
	normalNormalized.normalize();
213

214
215
216
217
	// Check if it is worth updating values !
	if ((normalNormalized == m_clipPlanes[planeIndex].normal)
			&& (origin == m_clipPlanes[planeIndex].origin))
		return;
218

219
220
221
	// Copy the given clipping plane parameters
	m_clipPlanes[planeIndex].normal = normalNormalized;
	m_clipPlanes[planeIndex].origin = origin;
222

223
224
225
226
227
	// Update the plane arrays
	updateClipPlaneUniformsArray(id);

	// Send again the whole planes equations array to shader
	sendClipPlanesEquationsUniform();
228
229
}

230
void ClippingShader::setClipPlaneParamsNormal(unsigned int id, Geom::Vec3f normal)
231
{
232
233
234
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::setClipPlaneParamsFirstVec"))
		return;
235
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::setClipPlaneParamsFirstVec"))
236
237
238
239
		return;

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
240

241
	// Normalize
242
243
	Geom::Vec3f normalNormalized = normal;
	normalNormalized.normalize();
244

245
246
247
	// Check if it is worth updating values !
	if (normalNormalized == m_clipPlanes[planeIndex].normal)
		return;
248

249
250
	// Copy the given clipping plane parameter
	m_clipPlanes[planeIndex].normal = normalNormalized;
251

252
253
254
255
256
	// Update the plane arrays
	updateClipPlaneUniformsArray(id);

	// Send again the whole planes equations array to shader
	sendClipPlanesEquationsUniform();
257
258
}

259
void ClippingShader::setClipPlaneParamsOrigin(unsigned int id, Geom::Vec3f origin)
260
{
261
262
263
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::setClipPlaneParamsOrigin"))
		return;
264
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::setClipPlaneParamsOrigin"))
265
266
267
268
		return;

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
269

270
271
272
	// Check if it is worth updating values !
	if (origin == m_clipPlanes[planeIndex].origin)
		return;
273

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

277
278
279
280
281
	// Update the plane arrays
	updateClipPlaneUniformsArray(id);

	// Send again the whole planes equations array to shader
	sendClipPlanesEquationsUniform();
282
283
}

284
Geom::Vec3f ClippingShader::getClipPlaneParamsNormal(unsigned int id)
285
{
286
287
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::getClipPlaneParamsFirstVec"))
288
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
289
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::getClipPlaneParamsFirstVec"))
290
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
291
292
293

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
294
295

	// Return the parameter
296
	return m_clipPlanes[planeIndex].normal;
297
298
}

299
Geom::Vec3f ClippingShader::getClipPlaneParamsOrigin(unsigned int id)
300
{
301
302
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::getClipPlaneParamsOrigin"))
303
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
304
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::getClipPlaneParamsOrigin"))
305
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
306
307
308

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
309
310
311
312
313

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

314
unsigned int ClippingShader::getFreeClipPlaneId()
315
{
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
	unsigned int freeId = 0;

	// Free id = either out of current ids vector size or no more used
	while (freeId < m_clipPlanesIds.size())
	{
		if (!m_clipPlanesIds[freeId].used)
			return freeId;
		else
			freeId++;
	}

	return freeId;
}

void ClippingShader::updateClipPlaneUniformsArray(unsigned int id)
{
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipPlanesIds.size()), "ClippingShader::updateClipPlaneUniformsArray"))
		return;
335
	if (errorRaiseWrongId(!m_clipPlanesIds[id].used, "ClippingShader::updateClipPlaneUniformsArray"))
336
337
338
339
		return;

	// Get the corresponding plane index
	int planeIndex = m_clipPlanesIds[id].index;
340

341
	// Update the planes equations array
342
343
344
345
	float d = -(m_clipPlanes[planeIndex].normal * m_clipPlanes[planeIndex].origin);
	m_clipPlanesEquations[4*planeIndex + 0] = m_clipPlanes[planeIndex].normal[0];
	m_clipPlanesEquations[4*planeIndex + 1] = m_clipPlanes[planeIndex].normal[1];
	m_clipPlanesEquations[4*planeIndex + 2] = m_clipPlanes[planeIndex].normal[2];
346
347
348
349
	m_clipPlanesEquations[4*planeIndex + 3] = d;

}

350

351
352
353
354
355
356
357
/***********************************************
 *
 * 		Sphere Clipping
 *
 ***********************************************/


358
unsigned int ClippingShader::addClipSphere()
359
{
360
361
362
	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::addClipSphere"))
		return -1;
363

364
365
	// Shader name string
	std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;
366

367
368
	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());
369

370
371
	// Previous spheres count
	int previousSpheresCount = getClipSpheresCount();
372

373
374
375
376
377
378
	// Modify the clip spheres count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_SPHERES_COUNT", previousSpheresCount + 1))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_SPHERES_COUNT", previousSpheresCount + 1)),
			"ClippingShader::addClipSphere"))
		return -1;
379

380
381
382
	// Reload both shaders
	reloadVertexShaderFromMemory(SM.getModifiedVertexShaderSrc().c_str());
	reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());
383

384
385
	// Get new sphere id
	unsigned int newSphereId = getFreeClipSphereId();
386

387
388
	// Resize spheres arrays to the right size
	m_clipSpheres.resize((size_t)(previousSpheresCount + 1));
389
390
	if (newSphereId >= m_clipSpheresIds.size())
		m_clipSpheresIds.resize((size_t)(newSphereId + 1));
391
	m_clipSpheresCentersAndRadiuses.resize(4*(size_t)(previousSpheresCount + 1), 0.0f);
392

393
394
395
	// Set new sphere id
	m_clipSpheresIds[newSphereId].used = true;
	m_clipSpheresIds[newSphereId].index = previousSpheresCount;
396

397
	// Set default parameters values for the new sphere
398
399
	Geom::Vec3f defaultCenter (0.0f, 0.0f, 0.0f);
	float defaultRadius = 10.0f;
400
	setClipSphereParamsAll(newSphereId, defaultCenter, defaultRadius);
401

402
403
	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
404

405
406
407
408
409
410
411
412
	return newSphereId;
}

void ClippingShader::deleteClipSphere(unsigned int id)
{
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::deleteClipSphere"))
		return;
413
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::deleteClipSphere"))
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
		return;

	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteClipSphere"))
		return;

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

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Previous spheres count
	int previousSpheresCount = getClipSpheresCount();

	// Modify the clip spheres count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_SPHERES_COUNT", previousSpheresCount - 1))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_SPHERES_COUNT", previousSpheresCount - 1)),
			"ClippingShader::deleteClipSphere"))
		return;

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

	// Rearrange spheres arrays
	m_clipSpheres.erase(m_clipSpheres.begin() + m_clipSpheresIds[id].index);
442
	for (size_t i = 0; i < m_clipSpheresIds.size(); i++)
443
444
445
	{
		if (m_clipSpheresIds[i].index > m_clipSpheresIds[id].index)
			m_clipSpheresIds[i].index -= 1;
446
	}
447
448
449
450
451
452
453
	m_clipSpheresIds[id].used = false;
	m_clipSpheresCentersAndRadiuses.erase(
			m_clipSpheresCentersAndRadiuses.begin() + 4*m_clipSpheresIds[id].index,
			m_clipSpheresCentersAndRadiuses.begin() + 4*m_clipSpheresIds[id].index + 4);

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
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
void ClippingShader::deleteAllClipSpheres()
{
	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::deleteAllClipSpheres"))
		return;

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

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Modify the clip spheres count constant in both shader
	if (errorRaiseShaderMutatorFailure(
			   (!SM.changeIntConstantValue(ShaderMutator::VERTEX_SHADER, "CLIP_SPHERES_COUNT", 0))
			|| (!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIP_SPHERES_COUNT", 0)),
			"ClippingShader::deleteAllClipSpheres"))
		return;

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

	// Rearrange spheres arrays
	m_clipSpheres.resize(0);
	m_clipSpheresIds.resize(0);
	m_clipSpheresCentersAndRadiuses.resize(0);

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
}

488
489
490
491
492
int ClippingShader::getClipSpheresCount()
{
	return (int)m_clipSpheres.size();
}

493
void ClippingShader::setClipSphereParamsAll(unsigned int id, Geom::Vec3f center, float radius)
494
{
495
496
497
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::setClipSphereParamsAll"))
		return;
498
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::setClipSphereParamsAll"))
499
500
501
502
		return;

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
503

504
505
506
507
	// Check if it is worth updating values !
	if ((center == m_clipSpheres[sphereIndex].center)
			&& (radius == m_clipSpheres[sphereIndex].radius))
		return;
508

509
510
511
	// Copy the given clipping sphere parameters
	m_clipSpheres[sphereIndex].center = center;
	m_clipSpheres[sphereIndex].radius = radius;
512

513
514
515
516
517
	// Update the sphere array
	updateClipSphereUniformsArray(id);

	// Send again the whole spheres centers and radiuses array to shader
	sendClipSpheresCentersAndRadiusesUniform();
518
519
}

520
void ClippingShader::setClipSphereParamsCenter(unsigned int id, Geom::Vec3f center)
521
{
522
523
524
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::setClipSphereParamsCenter"))
		return;
525
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::setClipSphereParamsCenter"))
526
527
528
529
		return;

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
530

531
532
533
	// Check if it is worth updating values !
	if (center == m_clipSpheres[sphereIndex].center)
		return;
534

535
536
	// Copy the given clipping sphere parameter
	m_clipSpheres[sphereIndex].center = center;
537

538
539
540
541
542
	// Update the sphere array
	updateClipSphereUniformsArray(id);

	// Send again the whole spheres centers and radiuses array to shader
	sendClipSpheresCentersAndRadiusesUniform();
543
544
}

545
void ClippingShader::setClipSphereParamsRadius(unsigned int id, float radius)
546
{
547
548
549
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::setClipSphereParamsRadius"))
		return;
550
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::setClipSphereParamsRadius"))
551
552
553
554
		return;

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
555

556
557
558
	// Check if it is worth updating values !
	if (radius == m_clipSpheres[sphereIndex].radius)
		return;
559

560
561
	// Copy the given clipping sphere parameter
	m_clipSpheres[sphereIndex].radius = radius;
562

563
564
565
566
567
	// Update the sphere array
	updateClipSphereUniformsArray(id);

	// Send again the whole spheres centers and radiuses array to shader
	sendClipSpheresCentersAndRadiusesUniform();
568
569
}

570
Geom::Vec3f ClippingShader::getClipSphereParamsCenter(unsigned int id)
571
{
572
573
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::getClipSphereParamsCenter"))
574
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
575
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::getClipSphereParamsCenter"))
576
		return Geom::Vec3f(0.0f, 0.0f, 0.0f);
577
578
579

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
580
581
582
583
584

	// Return the parameter
	return m_clipSpheres[sphereIndex].center;
}

585
float ClippingShader::getClipSphereParamsRadius(unsigned int id)
586
{
587
588
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::getClipSphereParamsRadius"))
589
		return 0.0f;
590
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::getClipSphereParamsRadius"))
591
		return 0.0f;
592
593
594

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
595
596
597
598
599

	// Return the parameter
	return m_clipSpheres[sphereIndex].radius;
}

600
unsigned int ClippingShader::getFreeClipSphereId()
601
{
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
	unsigned int freeId = 0;

	// Free id = either out of current ids vector size or no more used
	while (freeId < m_clipSpheresIds.size())
	{
		if (!m_clipSpheresIds[freeId].used)
			return freeId;
		else
			freeId++;
	}

	return freeId;
}

void ClippingShader::updateClipSphereUniformsArray(unsigned int id)
{
	// Check if the given id is valid
	if (errorRaiseWrongId(id > (m_clipSpheresIds.size()), "ClippingShader::updateClipSphereUniformsArray"))
		return;
621
	if (errorRaiseWrongId(!m_clipSpheresIds[id].used, "ClippingShader::updateClipSphereUniformsArray"))
622
623
624
625
		return;

	// Get the corresponding sphere index
	int sphereIndex = m_clipSpheresIds[id].index;
626
627
628
629
630
631
632
633
634
635

	// Update the spheres centers and radiuses array
	m_clipSpheresCentersAndRadiuses[4*sphereIndex + 0] = m_clipSpheres[sphereIndex].center[0];
	m_clipSpheresCentersAndRadiuses[4*sphereIndex + 1] = m_clipSpheres[sphereIndex].center[1];
	m_clipSpheresCentersAndRadiuses[4*sphereIndex + 2] = m_clipSpheres[sphereIndex].center[2];
	m_clipSpheresCentersAndRadiuses[4*sphereIndex + 3] = m_clipSpheres[sphereIndex].radius;

}


636
637
638
639
640
641
642
/***********************************************
 *
 * 		Global Clipping Stuff
 *
 ***********************************************/


643
644
bool ClippingShader::insertClippingCode()
{
645
	// Check if the code has not already been inserted
646
647
648
649
650
651
652
	if (errorRaiseClippingCodeAlreadyInserted(m_hasClippingCodeBeenInserted, "ClippingShader::insertClippingCode"))
		return false;

	// Check if the vertex and fragment sources are not empty
	if (errorRaiseShaderSourceIsEmpty((getVertexShaderSrc() == NULL), "ClippingShader::insertClippingCode", ShaderMutator::VERTEX_SHADER))
		return false;
	if (errorRaiseShaderSourceIsEmpty((getFragmentShaderSrc() == NULL), "ClippingShader::insertClippingCode", ShaderMutator::FRAGMENT_SHADER))
653
		return false;
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
684
685
686
687
688
689
690
691
692
693

	// Check if the shader does not use a geometry shader
	if (errorRaiseShaderUsesGeometryShader((getGeometryShaderSrc() != NULL), "ClippingShader::insertClippingCode"))
		return false;


	// Strings to insert in shader sources

	std::string VS_headInsertion =
	"\n"
	"#define CLIP_PLANES_COUNT 0\n"
	"#define CLIP_SPHERES_COUNT 0\n"
	"\n"
	"#define PLANE_CLIPPING_ENABLED (CLIP_PLANES_COUNT > 0)\n"
	"#define SPHERE_CLIPPING_ENABLED (CLIP_SPHERES_COUNT > 0)\n"
	"\n"
	"#define CLIPPING_ENABLED (PLANE_CLIPPING_ENABLED || SPHERE_CLIPPING_ENABLED)\n"
	"\n"
	"#if CLIPPING_ENABLED\n"
	"	VARYING_VERT vec3 clip_nonTransformedPos;\n"
	"#endif\n"
	"\n";

	std::string VS_mainEndInsertion =
	"\n"
	"	#if CLIPPING_ENABLED\n"
	"		// Pass the non transformed vertex position to the fragment shader for clipping\n"
	"		clip_nonTransformedPos = VertexPosition;\n"
	"	#endif\n";

	std::string FS_headInsertion =
	"\n"
	"#define CLIP_PLANES_COUNT 0\n"
	"#define CLIP_SPHERES_COUNT 0\n"
	"\n"
	"#define PLANE_CLIPPING_ENABLED (CLIP_PLANES_COUNT > 0)\n"
	"#define SPHERE_CLIPPING_ENABLED (CLIP_SPHERES_COUNT > 0)\n"
	"\n"
	"#define CLIPPING_ENABLED (PLANE_CLIPPING_ENABLED || SPHERE_CLIPPING_ENABLED)\n"
	"\n"
694
695
696
697
698
699
700
	"// In following clipping modes, pixels may be deleted :\n"
	"//  - OR : only after being matched with every object\n"
	"//  - AND : on the fly as soon as one object does not match\n"
	"#define CLIPPING_MODE_AND 0\n"
	"#define CLIPPING_MODE_OR 1\n"
	"#define CLIPPING_MODE 0\n"
	"\n"
701
702
703
704
	"#define CLIPPING_COLOR_ATTENUATION_MODE_LINEAR 0\n"
	"#define CLIPPING_COLOR_ATTENUATION_MODE_QUADRATIC 1\n"
	"#define CLIPPING_COLOR_ATTENUATION_MODE 0\n"
	"\n"
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
	"#if CLIPPING_ENABLED\n"
	"\n"
	"	#if PLANE_CLIPPING_ENABLED\n"
	"		uniform vec4 clip_clipPlanesEquations[CLIP_PLANES_COUNT];\n"
	"	#endif\n"
	"\n"
	"	#if SPHERE_CLIPPING_ENABLED\n"
	"		uniform vec4 clip_clipSpheresCentersAndRadiuses[CLIP_SPHERES_COUNT];\n"
	"	#endif\n"
	"\n"
	"	uniform float clip_clipColorAttenuationFactor;\n"
	"\n"
	"	VARYING_FRAG vec3 clip_nonTransformedPos;\n"
	"\n"
	"#endif\n"
	"\n"
	"#if CLIPPING_ENABLED\n"
722
	"\n"
723
724
725
726
727
	"	float clip_doClippingAndGetClippingDistance()\n"
	"	{\n"
	"		// Distance to the nearest clipping object\n"
	"		float minDistanceToClipping = -1.0;\n"
	"\n"
728
729
730
731
732
733
	"		// OR clipping mode needs a boolean to know if the pixel must be clipped or not\n"
	"		// By default set to true : one clipping object matched sets it to false\n"
	"		#if (CLIPPING_MODE == CLIPPING_MODE_OR)\n"
	"			bool discardPixel = true;\n"
	"		#endif\n"
	"\n"
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
	"		#if PLANE_CLIPPING_ENABLED\n"
	"\n"
	"			// Do clipping for each plane\n"
	"			for (int i = 0; i < CLIP_PLANES_COUNT; i++)\n"
	"			{\n"
	"				// Get the current plane equation\n"
	"				vec4 currClipPlane = clip_clipPlanesEquations[i];\n"
	"\n"
	"				// If the plane normal is zero, use a default normal vector (0.0, 0.0, 1.0)\n"
	"				float clipPlaneNormalLength = length(currClipPlane.xyz);\n"
	"				if (clipPlaneNormalLength == 0.0)\n"
	"				{\n"
	"					currClipPlane.z = 1.0;\n"
	"					clipPlaneNormalLength = 1.0;\n"
	"				}\n"
	"\n"
	"				// Signed distance between the point and the plane\n"
	"				float distanceToPlane = dot(clip_nonTransformedPos, currClipPlane.xyz);\n"
	"				distanceToPlane += currClipPlane.w;\n"
	"				distanceToPlane /= clipPlaneNormalLength;\n"
	"\n"
755
756
757
	"				// AND clipping mode discards at first unmatched clipping object\n"
	"				#if (CLIPPING_MODE == CLIPPING_MODE_AND)\n"
	"					if (distanceToPlane > 0.0)\n"
758
	"						discard;\n"
759
760
761
762
763
	"				#endif\n"
	"\n"
	"				// In OR clipping mode, one match = no pixel clipping\n"
	"				#if (CLIPPING_MODE == CLIPPING_MODE_OR)\n"
	"					if (distanceToPlane < 0.0)\n"
764
	"						discardPixel = false;\n"
765
	"				#endif\n"
766
	"\n"
767
768
769
770
771
	"				// Keep the distance to the nearest plane\n"
	"				if (minDistanceToClipping < 0.0)\n"
	"					minDistanceToClipping = abs(distanceToPlane);\n"
	"				else\n"
	"					minDistanceToClipping = min(minDistanceToClipping, abs(distanceToPlane));\n"
772
773
774
775
776
	"			}\n"
	"\n"
	"		#endif\n"
	"\n"
	"		#if SPHERE_CLIPPING_ENABLED\n"
777
	"\n"
778
779
780
781
782
783
784
785
786
	"			// Do clipping for each sphere\n"
	"			for (int i = 0; i < CLIP_SPHERES_COUNT; i++)\n"
	"			{\n"
	"				// Get the current sphere center and radius\n"
	"				vec3 currClipSphereCenter = clip_clipSpheresCentersAndRadiuses[i].xyz;\n"
	"				float currClipSphereRadius = clip_clipSpheresCentersAndRadiuses[i].w;\n"
	"\n"
	"				// Signed distance between the point and the sphere\n"
	"				float distanceToSphere = length(clip_nonTransformedPos - currClipSphereCenter);\n"
787
788
789
790
	"				distanceToSphere -= abs(currClipSphereRadius);\n"
	"\n"
	"				// If the sphere radius is negative, this inverses the clipping effect\n"
	"				distanceToSphere *= sign(currClipSphereRadius);\n"
791
	"\n"
792
793
794
	"				// AND clipping mode discards at first unmatched clipping object\n"
	"				#if (CLIPPING_MODE == CLIPPING_MODE_AND)\n"
	"					if (distanceToSphere > 0.0)\n"
795
	"						discard;\n"
796
797
798
799
800
	"				#endif\n"
	"\n"
	"				// In OR clipping mode, one match = no pixel clipping\n"
	"				#if (CLIPPING_MODE == CLIPPING_MODE_OR)\n"
	"					if (distanceToSphere < 0.0)\n"
801
	"						discardPixel = false;\n"
802
	"				#endif\n"
803
	"\n"
804
805
806
807
808
	"				// Keep the distance to the nearest sphere\n"
	"				if (minDistanceToClipping < 0.0)\n"
	"					minDistanceToClipping = abs(distanceToSphere);\n"
	"				else\n"
	"					minDistanceToClipping = min(minDistanceToClipping, abs(distanceToSphere));\n"
809
810
811
812
	"			}\n"
	"\n"
	"		#endif\n"
	"\n"
813
814
815
816
817
818
	"		// In OR clipping mode, the pixel may be deleted only after being matched with every object\n"
	"		#if (CLIPPING_MODE == CLIPPING_MODE_OR)\n"
	"			if (discardPixel)\n"
	"				discard;\n"
	"		#endif\n"
	"\n"
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
	"		return minDistanceToClipping;\n"
	"	}\n"
	"\n"
	"#endif\n"
	"\n";

	std::string FS_mainBeginInsertion =
	"\n"
	"	#if CLIPPING_ENABLED\n"
	"		// Apply clipping and get the clipping distance\n"
	"		float clip_minDistanceToClipping = clip_doClippingAndGetClippingDistance();\n"
	"	#endif\n";

	std::string FS_mainEndInsertion =
	"\n"
	"	#if CLIPPING_ENABLED\n"
835
836
837
838
839
840
841
	"		// Attenuate the final fragment color depending on its distance to clipping objects\n"
	"		float clip_colorAttenuation = clip_minDistanceToClipping * clip_clipColorAttenuationFactor;\n"
	"		#if (CLIPPING_COLOR_ATTENUATION_MODE == CLIPPING_COLOR_ATTENUATION_MODE_QUADRATIC)\n"
	"			clip_colorAttenuation *= clip_colorAttenuation;\n"
	"		#endif\n"
	"		gl_FragColor.rgb /= (1.0 + clip_colorAttenuation);\n"
	"	#endif;\n";
842
843
844
845
846
847
848
849
850
851
852
853

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

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// First check if the vertex shader contains the VertexPosition attribute
	if (errorRaiseVariableNotFoundInShader(!SM.containsVariableDeclaration(ShaderMutator::VERTEX_SHADER, "VertexPosition"), "ClippingShader::insertClippingCode", ShaderMutator::VERTEX_SHADER, "VertexPosition"))
		return false;

	// Modify vertex shader source code
854
855
856
857
858
	if (errorRaiseShaderMutatorFailure(
			   (!SM.insertCodeBeforeMainFunction(ShaderMutator::VERTEX_SHADER, VS_headInsertion))
			|| (!SM.insertCodeAtMainFunctionBeginning(ShaderMutator::VERTEX_SHADER, VS_mainEndInsertion)),
			"ClippingShader::insertClippingCode"))
		return false;
859
860

	// Modify fragment shader source code
861
862
863
864
865
866
867
	if (errorRaiseShaderMutatorFailure(
			   (!SM.setMinShadingLanguageVersion(ShaderMutator::FRAGMENT_SHADER, 120)) // Following code insertions need at least shading language 120 (GLSL arrays)
			|| (!SM.insertCodeBeforeMainFunction(ShaderMutator::FRAGMENT_SHADER, FS_headInsertion))
			|| (!SM.insertCodeAtMainFunctionBeginning(ShaderMutator::FRAGMENT_SHADER, FS_mainBeginInsertion))
			|| (!SM.insertCodeAtMainFunctionEnd(ShaderMutator::FRAGMENT_SHADER, FS_mainEndInsertion)),
			"ClippingShader::insertClippingCode"))
		return false;
868
869
870
871
872
873
874
875
876
877
878
879
880

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

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();

	m_hasClippingCodeBeenInserted = true;

	return true;
}

881
void ClippingShader::setClipColorAttenuationFactorAbsolute(float colorAttenuationFactor)
882
{
883
884
885
	// Check if it is worth updating values !
	if (colorAttenuationFactor == m_clipColorAttenuationFactor)
		return;
886

887
888
889
890
891
	// Copy the given value
	m_clipColorAttenuationFactor = colorAttenuationFactor;

	// Send again the uniform to shader
	sendClipColorAttenuationFactorUniform();
892
893
}

894
895
896
897
void ClippingShader::setClipColorAttenuationFactorRelative(float size, float factor)
{
	// Compute the relative color attenuation factor
	float colAttFact;
898
	if (size != 0.0f)
899
900
901
902
903
904
905
906
		colAttFact = factor / size;
	else
		colAttFact = factor;

	// Set the resulting factor in absolute
	setClipColorAttenuationFactorAbsolute(colAttFact);
}

907
908
909
910
911
float ClippingShader::getClipColorAttenuationFactor()
{
	return m_clipColorAttenuationFactor;
}

912
913
void ClippingShader::setClipColorAttenuationMode(colorAttenuationMode colAttMode)
{
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
	// Check if it is worth updating values !
	if (colAttMode == m_colorAttenuationMode)
		return;

	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::setClipColorAttenuationMode"))
		return;

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

	// Copy the given value
	m_colorAttenuationMode = colAttMode;

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Change color attenuation mode constant
	int newConstantValue;
	switch (colAttMode)
934
	{
935
936
937
938
939
940
941
942
943
944
945
		case COLOR_ATTENUATION_MODE_LINEAR :
			newConstantValue = 0;
			break;

		case COLOR_ATTENUATION_MODE_QUADRATIC :
			newConstantValue = 1;
			break;

		default :
			newConstantValue = 0;
			break;
946
	}
947
948
949
950
951
952
953
954
955
956
	if (errorRaiseShaderMutatorFailure(
			(!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIPPING_COLOR_ATTENUATION_MODE", newConstantValue)),
			"ClippingShader::setClipColorAttenuationMode"))
		return;

	// Reload modified shader
	reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
957
958
959
960
961
962
963
}

ClippingShader::colorAttenuationMode ClippingShader::getClipColorAttenuationMode()
{
	return m_colorAttenuationMode;
}

964
965
void ClippingShader::setClipMode(clippingMode clipMode)
{
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
	// Check if it is worth updating values !
	if (clipMode == m_clipMode)
		return;

	// Check if the clipping code has been inserted into shader
	if (errorRaiseClippingCodeNotInserted(!m_hasClippingCodeBeenInserted, "ClippingShader::setClipMode"))
		return;

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

	// Copy the given value
	m_clipMode = clipMode;

	// Use a shader mutator
	ShaderMutator SM(shaderName, getVertexShaderSrc(), getFragmentShaderSrc());

	// Change clipping mode constant
	int newConstantValue;
	switch (clipMode)
986
	{
987
988
989
990
991
992
993
994
995
996
997
		case CLIPPING_MODE_AND :
			newConstantValue = 0;
			break;

		case CLIPPING_MODE_OR :
			newConstantValue = 1;
			break;

		default :
			newConstantValue = 0;
			break;
998
	}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
	if (errorRaiseShaderMutatorFailure(
			(!SM.changeIntConstantValue(ShaderMutator::FRAGMENT_SHADER, "CLIPPING_MODE", newConstantValue)),
			"ClippingShader::setClipMode"))
		return;

	// Reload modified shader
	reloadFragmentShaderFromMemory(SM.getModifiedFragmentShaderSrc().c_str());

	// Recompile shaders (automatically calls updateClippingUniforms)
	recompile();
1009
1010
1011
1012
1013
1014
1015
}

ClippingShader::clippingMode ClippingShader::getClipMode()
{
	return m_clipMode;
}

1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

/***********************************************
 *
 * 		Clipping Uniforms Handling
 *
 ***********************************************/


void ClippingShader::updateClippingUniforms()
{
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
	// Plane clipping uniforms
	if (getClipPlanesCount() > 0)
	{
		// Get uniform location
		m_unif_clipPlanesEquations = glGetUniformLocation(program_handler(), "clip_clipPlanesEquations");
		errorRaiseUniformNotFoundInShader((m_unif_clipPlanesEquations == -1), "ClippingShader::updateClippingUniforms", "clip_clipPlanesEquations");

		// Send again uniform value
		sendClipPlanesEquationsUniform();
	}
1036

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
	// Sphere clipping uniforms
	if (getClipSpheresCount() > 0)
	{
		// Get uniform location
		m_unif_clipSpheresCentersAndRadiuses = glGetUniformLocation(program_handler(), "clip_clipSpheresCentersAndRadiuses");
		errorRaiseUniformNotFoundInShader((m_unif_clipSpheresCentersAndRadiuses == -1), "ClippingShader::updateClippingUniforms", "clip_clipSpheresCentersAndRadiuses");

		// Send again uniform value
		sendClipSpheresCentersAndRadiusesUniform();
	}

	// Global clipping uniforms
	if ((getClipPlanesCount() > 0) || (getClipSpheresCount() > 0))
	{
		// Get uniform location
		m_unif_clipColorAttenuationFactor = glGetUniformLocation(program_handler(), "clip_clipColorAttenuationFactor");
		errorRaiseUniformNotFoundInShader((m_unif_clipColorAttenuationFactor == -1), "ClippingShader::updateClippingUniforms", "clip_clipColorAttenuationFactor");
1054

1055
1056
1057
		// Send again uniform value
		sendClipColorAttenuationFactorUniform();
	}
1058
1059
1060
1061
1062
1063
1064
1065
}

void ClippingShader::sendClipPlanesEquationsUniform()
{
	bind();
	glUniform4fv(m_unif_clipPlanesEquations, getClipPlanesCount(), &m_clipPlanesEquations.front());
}

1066
1067
1068
1069
1070
1071
void ClippingShader::sendClipSpheresCentersAndRadiusesUniform()
{
	bind();
	glUniform4fv(m_unif_clipSpheresCentersAndRadiuses, getClipSpheresCount(), &m_clipSpheresCentersAndRadiuses.front());
}

1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
void ClippingShader::sendClipColorAttenuationFactorUniform()
{
	bind();
	glUniform1f(m_unif_clipColorAttenuationFactor, m_clipColorAttenuationFactor);
}


/***********************************************
 *
 * 		Error Raising
 *
 ***********************************************/


1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
bool ClippingShader::errorRaiseShaderMutatorFailure(bool condition, const std::string& location)
{
	if (condition)
	{
		CGoGNerr
		<< "ERROR - "
		<< location
		<< " - Shader Mutator failure"
		<< CGoGNendl;
	}

	return condition;
}

1100
bool ClippingShader::errorRaiseShaderSourceIsEmpty(bool condition, const std::string& location, ShaderMutator::shaderSrcType shaderType)
1101
1102
1103
{
	if (condition)
	{
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
		std::string shaderName;
		switch (shaderType)
		{
			case ShaderMutator::VERTEX_SHADER :
				shaderName = m_nameVS;
				break;

			case ShaderMutator::FRAGMENT_SHADER :
				shaderName = m_nameFS;
				break;

			case ShaderMutator::GEOMETRY_SHADER :
				shaderName = m_nameGS;
				break;

			default :
				shaderName = m_nameVS;
				break;
		}
1123
1124
1125
1126
1127

		CGoGNerr
		<< "ERROR - "
		<< location
		<< " - Could not process shader "
1128
		<< shaderName
1129
		<< " source code : shader source is empty"
1130
1131
1132
		<< CGoGNendl;
	}

1133
1134
1135
1136
1137
1138
	return condition;
}

bool ClippingShader::errorRaiseShaderUsesGeometryShader(bool condition, const std::string& location)
{
	if (condition)
1139
	{
1140
1141
		std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

1142
1143
		CGoGNerr
		<< "ERROR - "
1144
1145
		<< location
		<< " - Could not process shader "
1146
		<< shaderName
1147
		<< " source code : unable to add clipping to a shader which uses a geometry shader"
1148
1149
1150
		<< CGoGNendl;
	}

1151
	return condition;
1152
1153
}

1154
bool ClippingShader::errorRaiseVariableNotFoundInShader(bool condition, const std::string& location, ShaderMutator::shaderSrcType shaderType, const std::string& varName)
1155
{
1156
1157
1158
	if (condition)
	{
		std::string shaderName;
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
		switch (shaderType)
		{
			case ShaderMutator::VERTEX_SHADER :
				shaderName = m_nameVS;
				break;

			case ShaderMutator::FRAGMENT_SHADER :
				shaderName = m_nameFS;
				break;

			case ShaderMutator::GEOMETRY_SHADER :
				shaderName = m_nameGS;
				break;

			default :
				shaderName = m_nameVS;
				break;
		}
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189

		CGoGNerr
		<< "ERROR - "
		<< location
		<< " - Could not process shader "
		<< shaderName
		<< " source code : "
		<< varName
		<< " not found"
		<< CGoGNendl;
	}

	return condition;
1190
}
1191
bool ClippingShader::errorRaiseWrongId(bool condition, const std::string& location)
1192
1193
1194
1195
1196
1197
{
	if (condition)
	{
		CGoGNerr
		<< "ERROR - "
		<< location
1198
		<< " - Given ID is not valid"
1199
1200
		<< CGoGNendl;
	}
1201

1202
1203
1204
1205
	return condition;
}

bool ClippingShader::errorRaiseUniformNotFoundInShader(bool condition, const std::string& location, const std::string& uniformName)
1206
{
1207
1208
1209
1210
1211
1212
1213
	if (condition)
	{
		std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

		CGoGNerr
		<< "ERROR - "
		<< location
1214
		<< " - Uniform "
1215
1216
1217
1218
1219
1220
1221
		<< uniformName
		<< " not found in shader "
		<< shaderName
		<< CGoGNendl;
	}

	return condition;
1222
1223
}

1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
bool ClippingShader::errorRaiseClippingCodeAlreadyInserted(bool condition, const std::string& location)
{
	if (condition)
	{
		std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

		CGoGNerr
		<< "ERROR - "
		<< location
		<< " - Clipping code has already been inserted into shader "
		<< shaderName
		<< CGoGNendl;
	}

	return condition;
}

bool ClippingShader::errorRaiseClippingCodeNotInserted(bool condition, const std::string& location)
{
	if (condition)
	{
		std::string shaderName = m_nameVS + "/" + m_nameFS + "/" + m_nameGS;

		CGoGNerr
		<< "ERROR - "
		<< location
		<< " - Clipping code must be inserted into shader "
		<< shaderName
		<< " before adding clipping objects"
		<< CGoGNendl;
	}

	return condition;
}

1259
1260
1261
} // namespace Utils

} // namespace CGoGN