GLSLShader.h 7.81 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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 35 36 37 38 39 40 41
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

/***********************************************
*  Thanks to Frederic Larue for this class
***********************************************/

#ifndef __KLIB__GLSHADER__12052005__
#define __KLIB__GLSHADER__12052005__

#include "Utils/os_spec.h"

#include <GL/glew.h>


//#include <GL/glut.h>

#include <map>
#include <stdlib.h>
#include <string>
42
#include <vector>
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47 48 49 50 51 52

namespace CGoGN
{

namespace Utils
{


////////// CLASS DECLARATION //////////

53
class GLSLShader
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55
{

56 57 58 59 60
public:
	/**
	 * enum of supported shader type
	 */
	enum shaderType {VERTEX_SHADER = 1, FRAGMENT_SHADER = 2, GEOMETRY_SHADER = 3 };
Pierre Kraemer's avatar
Pierre Kraemer committed
61 62 63


protected:
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	/**
	 * handle of vertex shader
	 */
	GLhandleARB	m_vertex_shader_object;

	/**
	 * handle of fragment shader
	 */
	GLhandleARB	m_fragment_shader_object;

	/**
	 * handle of geometry shader
	 */
	GLhandleARB	m_geom_shader_object;

	/**
	 * handle of program
	 */
	GLhandleARB m_program_object;


	/**
	 * load vertex shader
	 * @param vertex_shader_source src text shader
	 */
	bool loadVertexShaderSourceString( const char* vertex_shader_source );

	/**
	 * load fragment shader
	 * @param fragment_shader_source src text shader
	 */
	bool loadFragmentShaderSourceString( const char* fragment_shader_source );

	/**
	 * load geometry shader
	 * @param geom_shader_source src text shader
	 */
	bool loadGeometryShaderSourceString( const char* geom_shader_source );
Pierre Kraemer's avatar
Pierre Kraemer committed
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	/**
	 * load vertex shader
	 * @param filename file name
	 */
	bool loadVertexShader( const std::string& filename );

	/**
	 * load fragment shader
	 * @param filename file name
	 */
	bool loadFragmentShader( const std::string& filename );

	/**
	 * load geometry shader
	 * @param filename file name
	 */
	bool  loadGeometryShader( const std::string& filename );

	/**
	 * Load of source file in a char buffer
	 * @param source_file file name
	 */
	char* loadSourceFile( const std::string& source_file );

	/**
	 * create the shader (attach and link shaders into program)
	 */
	bool create(GLint inputGeometryPrimitive=GL_TRIANGLES,GLint outputGeometryPrimitive=GL_TRIANGLES);

132

133 134 135 136 137 138 139

	/**
	 * get log after compiling
	 * @param obj what log do you want ?
	 * @return the log
	 */
	char* getInfoLog( GLhandleARB obj );
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141


142 143
	static std::vector<std::string> m_pathes;

Pierre Kraemer's avatar
Pierre Kraemer committed
144
public:
145 146 147
	/**
	 * constructor
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
148 149 150
	GLSLShader();


151 152 153 154
	/**
	 * destructor
	 */
	virtual ~GLSLShader();
Pierre Kraemer's avatar
Pierre Kraemer committed
155

156 157 158 159 160
	/*
	 * search file in different path
	 */
	static std::string findFile(const std::string filename);

Pierre Kraemer's avatar
Pierre Kraemer committed
161

162 163 164 165
	/**
	 * test support of shader
	 */
	static bool	areShadersSupported();
Pierre Kraemer's avatar
Pierre Kraemer committed
166

167 168 169 170 171 172 173 174 175
	/**
	 * test support of Vertex Buffer Object
	 */
	static bool	areVBOSupported();

	/**
	 * test support of geometry shader
	 */
	static bool	areGeometryShadersSupported();
Pierre Kraemer's avatar
Pierre Kraemer committed
176

177 178 179 180
	/**
	 * test support of gl3
	 */
	static bool	isGL3Supported();
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182


183
	static bool init();
Pierre Kraemer's avatar
Pierre Kraemer committed
184 185


186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	/**
	 * load shaders (compile and link)
	 * @param vs vertex shader source file
	 * @param fs fragment shader source file
	 */
	bool loadShaders(const std::string& vs, const std::string& fs);

	/**
	 * load shaders (compile and link)
	 * @param vs vertex shader source file
	 * @param fs fragment shader source file
	 * @param fs fragment shader source file
	 * @param inputGeometryPrimitive primitives used in geometry shader as input
	 * @param outputGeometryPrimitive primitives generated in geometry shader as output
	 */
	bool loadShaders(const std::string& vs, const std::string& fs, const std::string& gs, GLint inputGeometryPrimitive=GL_TRIANGLES,GLint outputGeometryPrimitive=GL_TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203


204 205 206 207 208
	/**
	 * Link the shader do it just after binding the attributes
	 */
	bool link();

Pierre Kraemer's avatar
Pierre Kraemer committed
209
	inline bool		isCreated();
210

Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213
	bool			isBinded();

	virtual bool	bind();
214

Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217
	virtual void	unbind();


218 219 220
	/**
	 *
	 */
221
	GLuint 	getAttribIndex( char* attribName );
Pierre Kraemer's avatar
Pierre Kraemer committed
222 223

	
224 225 226 227
	/**
	 * get handler of program for external use og gl functions
	 */
	GLuint program_handler() { return m_program_object;}
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
	

230 231 232
	/**
	 * bind vertex attribute with its name in shaders
	 */
Sylvain Thery's avatar
Sylvain Thery committed
233
	void bindAttrib(unsigned int att, const char* name) const;
Pierre Kraemer's avatar
Pierre Kraemer committed
234

235

236 237 238
	/**
	 * check shader validity width official GLSL syntax
	 */
239 240
	bool validateProgram();

241 242 243
	/**
	 * check program link status
	 */
244 245
	bool checkProgram();

246 247 248
	/**
	 * check shader compile status
	 */
249 250
	bool checkShader(int shaderType);

Pierre Kraemer's avatar
Pierre Kraemer committed
251
public:
252

253 254 255 256 257 258 259
	/**
	 * set uniform shader float variable
	 * @warning practical but less efficient that storing id (get with glGetUniformLocation) and use glUniform*fvARB
	 * @param NB template size of variable to set
	 * @param name name in shader
	 * @param pointer on data to copy
	 */
260
	template<unsigned int NB>
261 262
	void setuniformf( const char* name , float* val);

263 264 265 266 267 268 269
	/**
	 * set uniform shader int variable
	 * @warning practical but less efficient that storing id (get with glGetUniformLocation) and use glUniform*ivARB
	 * @param NB template size of variable to set
	 * @param name name in shader
	 * @param pointer on data to copy
	 */
270 271
	template<unsigned int NB>
	void setuniformi( const char* name , int* val);
272 273 274 275 276 277 278

	/**
	 * add search path for file
	 * @param path to add
	 */
	void addPathFileSeach(const std::string& path);

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
};


////////// INLINE FUNCTIONS //////////


inline bool GLSLShader::isCreated()
{
	return ( m_program_object != 0 );
}

template<unsigned int NB>
void GLSLShader::setuniformf( const char* name , float* val)
{
	GLint uni = glGetUniformLocationARB(m_program_object,name);
	if (uni>=0)
295 296 297 298 299 300 301
	{
		switch(NB)
		{
		case 1:
			glUniform1fvARB( uni, 1, val) ;
			break;
		case 2:
302
			glUniform2fvARB( uni, 1, val) ;
303 304
			break;
		case 3:
305
			glUniform3fvARB( uni, 1, val) ;
306 307
			break;
		case 4:
308 309 310 311
			glUniform4fvARB( uni, 1, val) ;
			break;
		case 16:
			glUniformMatrix4fv(uni, 1 , false, val);
312 313 314
			break;
		}
	}
315
}
316

317 318 319 320 321
template<unsigned int NB>
void GLSLShader::setuniformi( const char* name , int* val)
{
	GLint uni = glGetUniformLocationARB(m_program_object,name);
	if (uni>=0)
322 323 324 325 326 327 328
	{
		switch(NB)
		{
		case 1:
			glUniform1ivARB( uni, 1, val) ;
			break;
		case 2:
329
			glUniform2ivARB( uni, 1, val) ;
330 331
			break;
		case 3:
332
			glUniform3ivARB( uni, 1, val) ;
333 334
			break;
		case 4:
335
			glUniform4ivARB( uni, 1, val) ;
336 337 338
			break;
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342
}



343 344


345

346
} //namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
347 348 349 350 351 352
} //namespace CGoGN




#endif /*__KLIB__GLSHADER__12052005__*/