Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

clippingShader.h 11.4 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                                        *
*                                                                              *
*******************************************************************************/

#ifndef _CGoGN_CLIPPINGSHADER_H_
#define _CGoGN_CLIPPINGSHADER_H_

#include "Utils/GLSLShader.h"
#include "Geometry/vector_gen.h"
#include "Utils/cgognStream.h"
#include "Utils/shaderMutator.h"
#include <string>
33
34
#include <sstream>
#include <vector>
35
#include <cmath>
36
37
38
39
40
41
42
43
44
45

namespace CGoGN
{

namespace Utils
{

class ClippingShader : public GLSLShader
{

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

	/***********************************************
	 *
	 * 		Constructor / Destructor
	 *
	 ***********************************************/

public :

	/// constructor
	ClippingShader();


	/***********************************************
	 *
	 * 		Plane Clipping
	 *
	 ***********************************************/
64

65
66
public:

67
68
69
	/*
	 * adds a clip plane
	 * @return clip plane id (positive value on success, else -1)
70
	 * @warning insertClippingCode must be called first
71
	 */
72
73
74
75
76
77
78
	int addClipPlane();

	/*
	 * deletes a clip plane
	 * @param id clip plane id
	 */
	void deleteClipPlane(unsigned int id);
79

80
	/// gets the clip planes count
81
	int getClipPlanesCount();
82

83
	/**
84
	 * sets all parameters for one clip plane
85
	 * @param id clip plane id
86
	 * @param normal normal
87
	 * @param origin origin
88
	 */
89
	void setClipPlaneParamsAll(unsigned int id, Geom::Vec3f normal, Geom::Vec3f origin);
90
91

	/**
92
	 * sets first vector for one clip plane
93
	 * @param id clip plane id
94
	 * @param normal normal
95
	 */
96
	void setClipPlaneParamsNormal(unsigned int id, Geom::Vec3f normal);
97
	
98
	/**
99
	 * sets origin for one clip plane
100
	 * @param id clip plane id
101
	 * @param origin origin
102
	 */
103
	void setClipPlaneParamsOrigin(unsigned int id, Geom::Vec3f origin);
104
105

	/**
106
	 * gets normal vector for one clip plane
107
	 * @param id clip plane id
108
	 */
109
	Geom::Vec3f getClipPlaneParamsNormal(unsigned int id);
110
111

	/**
112
	 * gets origin for one clip plane
113
	 * @param id clip plane id
114
	 */
115
	Geom::Vec3f getClipPlaneParamsOrigin(unsigned int id);
116
117
118

private:

119
120
121
	/// gets a non used clip plane id
	unsigned int getFreeClipPlaneId();

122
	/**
123
	 * updates clip plane equation uniforms array
124
	 * @param id clip plane id
125
	 */
126
	void updateClipPlaneUniformsArray(unsigned int id);
127
128
129
130

	/// clip planes structure
	struct clipPlane
	{
131
		Geom::Vec3f normal, origin;
132
133
134
135
136
	};

	/// clip planes array
	std::vector<clipPlane> m_clipPlanes;

137
138
139
140
141
142
143
144
145
146
	/// clip planes ids structure
	struct clipPlaneId
	{
		bool used;
		int index; // Corresponding index in the clip planes array
	};

	/// clip planes ids array
	std::vector<clipPlaneId> m_clipPlanesIds;

147
	/// clip planes equations uniforms array
148
149
	std::vector<float> m_clipPlanesEquations;

150
	/// clip planes equations uniforms array id
151
152
153
	GLint m_unif_clipPlanesEquations;


154
155
156
157
158
159
160
161
	/***********************************************
	 *
	 * 		Sphere Clipping
	 *
	 ***********************************************/

public:

162
163
164
	/*
	 * adds a clip sphere
	 * @return clip sphere id (positive value on success, else -1)
165
166
	 * @warning insertClippingCode must be called first
	 */
167
168
169
170
171
172
173
	int addClipSphere();

	/*
	 * deletes a clip sphere
	 * @param id clip sphere id
	 */
	void deleteClipSphere(unsigned int id);
174

175
	/// gets the clip spheres count
176
177
178
	int getClipSpheresCount();

	/**
179
	 * sets all parameters for one clip sphere
180
	 * @param id clip sphere id
181
182
183
	 * @param center center
	 * @param radius radius
	 */
184
	void setClipSphereParamsAll(unsigned int id, Geom::Vec3f center, float radius);
185
186

	/**
187
	 * sets center for one clip sphere
188
	 * @param id clip sphere id
189
190
	 * @param center center
	 */
191
	void setClipSphereParamsCenter(unsigned int id, Geom::Vec3f center);
192
193

	/**
194
	 * sets radius for one clip sphere
195
	 * @param id clip sphere id
196
197
	 * @param radius radius
	 */
198
	void setClipSphereParamsRadius(unsigned int id, float radius);
199
200

	/**
201
	 * gets center for one clip sphere
202
	 * @param id clip sphere id
203
	 */
204
	Geom::Vec3f getClipSphereParamsCenter(unsigned int id);
205
206

	/**
207
	 * gets radius for one clip sphere
208
	 * @param id clip sphere id
209
	 */
210
	float getClipSphereParamsRadius(unsigned int id);
211
212
213

private:

214
215
216
	/// gets a non used clip sphere id
	unsigned int getFreeClipSphereId();

217
	/**
218
	 * updates clip sphere center and radius uniforms array
219
	 * @param id clip sphere id
220
	 */
221
	void updateClipSphereUniformsArray(unsigned int id);
222
223
224
225
226
227
228
229
230
231
232

	/// clip spheres structure
	struct clipSphere
	{
		Geom::Vec3f center;
		float radius;
	};

	/// clip spheres array
	std::vector<clipSphere> m_clipSpheres;

233
234
235
236
237
238
239
240
241
242
	/// clip spheres ids structure
	struct clipSphereId
	{
		bool used;
		int index; // Corresponding index in the clip spheres array
	};

	/// clip spheres ids array
	std::vector<clipSphereId> m_clipSpheresIds;

243
	/// clip spheres centers and radiuses uniforms array
244
245
	std::vector<float> m_clipSpheresCentersAndRadiuses;

246
	/// clip spheres centers and radiuses uniforms array id
247
248
249
	GLint m_unif_clipSpheresCentersAndRadiuses;


250
251
252
253
254
255
	/***********************************************
	 *
	 * 		Global Clipping Stuff
	 *
	 ***********************************************/

256
257
public:

258
	/// enum used to choose clipping mode
259
260
	enum clippingMode { CLIPPING_MODE_AND, CLIPPING_MODE_OR };

261
	/**
262
	 * inserts clipping instructions into shader source code
263
	 * @warning this function is designed for shaders which *do not* use a geometry shader
264
	 * @return true if shader was processed successfully
265
266
267
	 */
	bool insertClippingCode();

268
	/**
269
	 * sets the color attenuation factor
270
271
272
273
	 * @param colorAttenuationFactor color attenuation factor
	 */
	void setClipColorAttenuationFactor(float colorAttenuationFactor);

274
	/// gets the color attenuation factor
275
	float getClipColorAttenuationFactor();
276

277
	/*
278
	 * sets the clipping mode
279
280
281
282
	 * @param clipMode clipping mode
	 */
	void setClipMode(clippingMode clipMode);

283
	/// gets the clipping mode
284
285
	clippingMode getClipMode();

286
287
private:

288
	/// used to control clipping code has been inserted before adding clipping objects
289
290
	bool m_hasClippingCodeBeenInserted;

291
292
293
294
295
296
	/// color attenuation factor
	float m_clipColorAttenuationFactor;

	/// color attenuation factor uniform id
	GLint m_unif_clipColorAttenuationFactor;

297
298
299
	/// clipping mode
	clippingMode m_clipMode;

300
301
302
303
304
305
306

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

307
308
public:

309
	/// updates uniforms (get their locations and send their values again)
310
311
	void updateClippingUniforms();

312
313
private:

314
	/// sends the clip planes equations uniforms array to shader
315
	void sendClipPlanesEquationsUniform();
316

317
	/// sends the clip spheres centers and radiuses uniforms array to shader
318
319
	void sendClipSpheresCentersAndRadiusesUniform();

320
	/// sends the color attenuation factor uniform to shader
321
	void sendClipColorAttenuationFactorUniform();
322

323
324
325
326
327
328
329
330
331

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

private:

332
333
334
335
336
337
338
339
	/**
	 * Outputs a "shader mutator failure" error if the condition is satisfied
	 * @param condition condition to satisfy
	 * @param location name of the function where the error raising was done
	 * @return true if the error has been raised
	 */
	bool errorRaiseShaderMutatorFailure(bool condition, const std::string& location);

340
	/**
341
	 * Outputs a "shader source is empty" error if the condition is satisfied
342
	 * @param condition condition to satisfy
343
	 * @param location name of the function where the error raising was done
344
	 * @param shaderType type of the shader
345
	 * @return true if the error has been raised
346
	 */
347
	bool errorRaiseShaderSourceIsEmpty(bool condition, const std::string& location, ShaderMutator::shaderSrcType shaderType);
348
349

	/**
350
	 * Outputs a "shader uses a geometry shader" error if the condition is satisfied
351
	 * @param condition condition to satisfy
352
	 * @param location name of the function where the error raising was done
353
	 * @return true if the error has been raised
354
355
356
357
	 */
	bool errorRaiseShaderUsesGeometryShader(bool condition, const std::string& location);

	/**
358
	 * Outputs a ".. not found in shader" error if the condition is satisfied
359
	 * @param condition condition to satisfy
360
	 * @param location name of the function where the error raising was done
361
362
	 * @param shaderType type of the shader
	 * @param varName name of the variable that may not be found
363
	 * @return true if the error has been raised
364
365
366
367
	 */
	bool errorRaiseVariableNotFoundInShader(bool condition, const std::string& location, ShaderMutator::shaderSrcType shaderType, const std::string& varName);

	/**
368
	 * Outputs a "wrong ID" error if the condition is satisfied
369
	 * @param condition condition to satisfy
370
	 * @param location name of the function where the error raising was done
371
	 * @return true if the error has been raised
372
	 */
373
	bool errorRaiseWrongId(bool condition, const std::string& location);
374
375

	/**
376
	 * Outputs a "uniform .. not found in shader" error if the condition is satisfied
377
	 * @param condition condition to satisfy
378
	 * @param location name of the function where the error raising was done
379
	 * @param uniformName name of the uniform that may not be found
380
	 * @return true if the error has been raised
381
382
383
	 */
	bool errorRaiseUniformNotFoundInShader(bool condition, const std::string& location, const std::string& uniformName);

384
	/**
385
	 * Outputs a "clipping code was already inserted" error if the condition is satisfied
386
	 * @param condition condition to satisfy
387
	 * @param location name of the function where the error raising was done
388
	 * @return true if the error has been raised
389
390
391
392
	 */
	bool errorRaiseClippingCodeAlreadyInserted(bool condition, const std::string& location);

	/**
393
	 * Outputs a "clipping code has not been inserted yet" error if the condition is satisfied
394
	 * @param condition condition to satisfy
395
	 * @param location name of the function where the error raising was done
396
	 * @return true if the error has been raised
397
398
399
	 */
	bool errorRaiseClippingCodeNotInserted(bool condition, const std::string& location);

400
401
402
403
404
405
406
407
};


} // namespace Utils

} // namespace CGoGN

#endif