clippingShader.h 9.03 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
/*******************************************************************************
* 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"
32
#include "Utils/drawer.h"
33
#include <string>
34 35
#include <sstream>
#include <vector>
36 37 38 39 40 41 42 43 44 45 46

namespace CGoGN
{

namespace Utils
{

class ClippingShader : public GLSLShader
{

public:
47 48 49 50 51 52

	/**
	 * enum used to choose which type of clip plane grid to display
	 */
	enum clipPlaneDisplayGridType { STRAIGHT_GRID, RADIAL_GRID };

53 54 55 56 57
	/**
	 * constructor
	 */
	ClippingShader();

58 59 60 61 62
	/**
	 * destructor
	 */
	~ClippingShader();

63
	/**
64
	 * set all parameters for one clipping plane
65
	 * @warning planeIndex starts at 0
66 67 68 69
	 * @param vec1 first basis vector
	 * @param vec2 second basis vector
	 * @param origin point of the plane that will be used as origin to display it
	 * @param planeIndex index of the plane to modify
70
	 */
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	void setClippingPlane(Geom::Vec3f vec1, Geom::Vec3f vec2, Geom::Vec3f origin, int planeIndex = 0);

	/**
	 * set first vector parameter for one clipping plane
	 * @warning planeIndex starts at 0
	 * @param vec1 first basis vector
	 * @param planeIndex index of the plane to modify
	 */
	void setClippingPlaneFirstVec(Geom::Vec3f vec1, int planeIndex = 0);

	/**
	 * set second vector parameter for one clipping plane
	 * @warning planeIndex starts at 0
	 * @param vec2 second basis vector
	 * @param planeIndex index of the plane to modify
	 */
	void setClippingPlaneSecondVec(Geom::Vec3f vec2, int planeIndex = 0);
88
	
89
	/**
90
	 * set origin parameter for one clipping plane
91
	 * @warning planeIndex starts at 0
92 93
	 * @param origin point of the plane that will be used as origin to display it
	 * @param planeIndex index of the plane to modify
94
	 */
95
	void setClippingPlaneOrigin(Geom::Vec3f origin, int planeIndex = 0);
96 97

	/**
98 99 100
	 * get first parameter for one clipping plane
	 * @warning planeIndex starts at 0
	 * @param planeIndex index of the plane to modify
101
	 */
102
	Geom::Vec3f getClippingPlaneFirstVec(int planeIndex = 0);
103 104

	/**
105 106 107 108 109 110 111 112 113 114
	 * get second vector parameter for one clipping plane
	 * @warning planeIndex starts at 0
	 * @param planeIndex index of the plane to modify
	 */
	Geom::Vec3f getClippingPlaneSecondVec(int planeIndex = 0);

	/**
	 * get origin parameter for one clipping plane
	 * @warning planeIndex starts at 0
	 * @param planeIndex index of the plane to modify
115
	 */
116
	Geom::Vec3f getClippingPlaneOrigin(int planeIndex = 0);
117

118 119
	/**
	 * set the color attenuation factor for clipping
120
	 * @param colorAttenuationFactor color attenuation factor
121 122 123
	 */
	void setClippingColorAttenuationFactor(float colorAttenuationFactor);

124 125 126
	/**
	 * get the color attenuation factor for clipping
	 */
127
	float getClippingColorAttenuationFactor() { return m_colorAttenuationFactor; }
128

129
	/**
130 131 132
	 * set the clipping planes count used for plane clipping
	 * - insert plane clipping instructions into vertex and fragment shader source code
	 * but does not modify the geometry shader source code
133 134
	 * @param planesCount the clipping planes count to use
	 * @warning this function is designed for shaders which *do not* use a geometry shader
135
	 */
136
	void setClippingPlanesCount(int planesCount);
137
	
138 139 140
	/**
	 * get the clipping planes count used for plane clipping
	 */
141
	int getClippingPlanesCount() { return (int)m_clipPlanes.size(); }
142

143
	/**
144
	 * update uniforms (get their locations and send their values again) for clipping
145 146 147
	 */
	void updateClippingUniforms();

148
	/**
149
	 * display all clipping planes
150
	 */
151 152 153
	void displayClippingPlanes();

	/**
154 155 156
	 * set the planes display color
	 * @param color the new color
	 */
157
	void setPlaneDisplayColor(Geom::Vec3f color) { m_clipPlanesDisplayColor = color; updateAllClippingPlanesVBOs(); }
158 159 160 161 162 163 164 165 166 167

	/**
	 * get the planes display color
	 */
	Geom::Vec3f getPlaneDisplayColor() { return m_clipPlanesDisplayColor; }

	/**
	 * set the planes display grids type
	 * @param gridType the new grid type
	 */
168
	void setPlaneDisplayType(clipPlaneDisplayGridType gridType) { m_clipPlanesDisplayType = gridType; updateAllClippingPlanesVBOs(); }
169 170 171 172 173 174 175 176 177 178

	/**
	 * get the planes display grids type
	 */
	clipPlaneDisplayGridType getPlaneDisplayType() { return m_clipPlanesDisplayType; }

	/**
	 * set the planes display grid x resolution
	 * @param res the new resolution
	 */
179
	void setPlaneDisplayXRes(size_t res) { m_clipPlanesDisplayXRes = res; updateAllClippingPlanesVBOs(); }
180 181 182 183 184 185 186 187 188 189

	/**
	 * get the planes display grid x resolution
	 */
	size_t getPlaneDisplayXRes() { return m_clipPlanesDisplayXRes; }

	/**
	 * set the planes display grid y resolution
	 * @param res the new resolution
	 */
190
	void setPlaneDisplayYRes(size_t res) { m_clipPlanesDisplayYRes = res; updateAllClippingPlanesVBOs(); }
191 192 193 194 195 196 197 198

	/**
	 * get the planes display grid y resolution
	 */
	size_t getPlaneDisplayYRes() { return m_clipPlanesDisplayYRes; }

	/**
	 * set the planes display size
199 200
	 * @param size the new size
	 */
201
	void setPlaneDisplaySize(float size) { m_clipPlanesDisplaySize = size; updateAllClippingPlanesVBOs(); }
202 203

	/**
204
	 * get the planes display size
205
	 */
206
	float getPlaneDisplaySize() { return m_clipPlanesDisplaySize; }
207

208 209 210

private:

211 212 213 214 215 216 217 218
	/**
	 * clipping planes structure
	 */
	struct clipPlane
	{
		Geom::Vec3f firstVec, secondVec, origin;
	};

219
	/**
220 221 222 223 224 225 226 227 228
	 * sends the clipping planes uniform to shader
	 */
	void sendClippingPlanesUniform();

	/**
	 * sends the color attenuation factor uniform to shader
	 */
	void sendColorAttenuationFactorUniform();

229
	/**
230
	 * update VBO for one plane
231 232 233 234
	 * @param planeIndex index of the plane
	 */
	void updateClippingPlaneVBO(int planeIndex);

235 236 237 238 239
	/**
	 * update VBOs for all planes
	 */
	void updateAllClippingPlanesVBOs();

240 241
	/**
	 * original vertex shader source code (without clipping)
242 243 244 245
	 */
	std::string originalVertShaderSrc;

	/**
246
	 * original fragment shader source code (without clipping)
247 248 249
	 */
	std::string originalFragShaderSrc;

250
	/**
251
	 * clip planes array
252
	 */
253 254
	std::vector<clipPlane> m_clipPlanes;

255
	/**
256
	 * clip planes equations array (size = 4*(planes count))
257
	 * - ** only used for sending planes data to shader **
258
	 */
259
	std::vector<float> m_clipPlanesEquations;
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	/**
	 * clip planes first vectors array (size = 3*(planes count))
	 * - ** only used for sending planes data to shader **
	 */
	std::vector<float> m_clipPlanesFirstVectors;

	/**
	 * clip planes second vectors array (size = 3*(planes count))
	 * - ** only used for sending planes data to shader **
	 */
	std::vector<float> m_clipPlanesSecondVectors;

	/**
	 * clip planes origins array (size = 3*(planes count))
	 * - ** only used for sending planes data to shader **
	 */
	std::vector<float> m_clipPlanesOrigins;

279
	/**
280
	 * clip planes equations vector uniform id
281
	 */
282
	GLint m_unif_clipPlanesEquations;
283 284 285 286 287 288 289 290 291 292 293
	
	/**
	 * color attenuation factor
	 */
	float m_colorAttenuationFactor;
	
	/**
	 * color attenuation factor uniform id
	 */
	GLint m_unif_colorAttenuationFactor;

294
	/**
295 296 297 298 299
	 * clip planes drawers array
	 */
	std::vector<Drawer*> m_clipPlanesDrawers;

	/**
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	 * clip planes display color
	 */
	Geom::Vec3f m_clipPlanesDisplayColor;

	/**
	 * clip planes display grid type
	 */
	clipPlaneDisplayGridType m_clipPlanesDisplayType;

	/**
	 * clip planes x grid display resolution
	 */
	size_t m_clipPlanesDisplayXRes;

	/**
	 * clip planes y grid display resolution
	 */
	size_t m_clipPlanesDisplayYRes;

	/**
	 * clip planes display size
321
	 */
322
	float m_clipPlanesDisplaySize;
323

324 325 326 327 328 329 330 331
};


} // namespace Utils

} // namespace CGoGN

#endif