GLSLShader.cpp 31.7 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
24

Sylvain Thery's avatar
Sylvain Thery committed
25
#define CGoGN_UTILS_DLL_EXPORT 1
Pierre Kraemer's avatar
Pierre Kraemer committed
26 27 28 29

#include "Utils/GLSLShader.h"
#include <iostream>
#include <fstream>
30
#include <vector>
Sylvain Thery's avatar
Sylvain Thery committed
31
#include <algorithm>
32
#include "Utils/cgognStream.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
33

Sylvain Thery's avatar
Sylvain Thery committed
34
#include "glm/gtc/matrix_inverse.hpp"
Sylvain Thery's avatar
Sylvain Thery committed
35

Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38 39 40
namespace CGoGN
{

namespace Utils
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
41

42 43


44 45
std::vector<std::string> GLSLShader::m_pathes;

46
std::set< std::pair<void*, GLSLShader*> >* GLSLShader::m_registeredShaders = NULL;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
47

48 49 50
//glm::mat4* GLSLShader::s_current_matrices=NULL;
Utils::GL_Matrices* GLSLShader::s_current_matrices=NULL;

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
51
GLSLShader::GLSLShader() :
52
	m_vao(0),
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
53 54 55
	m_vertex_shader_source(NULL),
	m_fragment_shader_source(NULL),
	m_geom_shader_source(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
56
{
57 58 59 60 61 62 63 64 65
	*m_vertex_shader_object = 0;
	*m_fragment_shader_object = 0;
	*m_geom_shader_object = 0;
	*m_program_object = 0;
	*m_uniMat_Proj = -1;
	*m_uniMat_Model = -1;
	*m_uniMat_ModelProj = -1;
	*m_uniMat_Normal = -1;

66 67
//	if (DEFINES_GL == NULL)
//		setCurrentOGLVersion(MAJOR_OGL_CORE,MINOR_OGL_CORE);
Sylvain Thery's avatar
Sylvain Thery committed
68 69

	m_nbMaxVertices = 16;
70 71 72

	if (m_registeredShaders==NULL)
		m_registeredShaders = new std::set< std::pair<void*, GLSLShader*> >;
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
75
void GLSLShader::registerShader(void* ptr, GLSLShader* shader)
76
{
77
	m_registeredShaders->insert(std::pair<void*,GLSLShader*>(ptr, shader));
78 79
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
80
void GLSLShader::unregisterShader(void* ptr, GLSLShader* shader)
81
{
82
	m_registeredShaders->erase(std::pair<void*,GLSLShader*>(ptr, shader));
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
std::string GLSLShader::defines_gl()
{
#ifdef CGOGN_USE_OGL_CORE_PROFILE
	return std::string("#version 330\n\
#define PRECISION precision highp float\n\
#define ATTRIBUTE in\n\
#define VARYING_VERT out\n\
#define VARYING_FRAG in\n\
#define FRAG_OUT_DEF out vec4 outFragColor\n\
#define FRAG_OUT outFragColor\n\
#define INVARIANT_POS invariant gl_Position\n\
#define TEXTURE2D texture\n");
#else
	return std::string("#version 110\n \
#define PRECISION float pipo_PRECISION\n\
#define ATTRIBUTE attribute\n\
#define VARYING_VERT varying\n\
#define VARYING_FRAG varying\n\
#define FRAG_OUT_DEF float pipo_FRAGDEF\n\
#define FRAG_OUT gl_FragColor\n\
#define INVARIANT_POS float pipo_INVARIANT\n\
#define TEXTURE2D texture2D\n");
#endif
}

110 111
std::string GLSLShader::defines_Geom(const std::string& primitivesIn, const std::string& primitivesOut, int maxVert)
{
112
	if (CURRENT_OGL_VERSION >= 3)
113
	{
114
		std::string str("#version 330\n");
115 116 117 118 119 120 121 122 123 124 125 126
		str.append("precision highp float;\n");
		str.append("layout (");
		str.append(primitivesIn);
		str.append(") in;\n");

		str.append("layout (");
		str.append(primitivesOut);
		str.append(", max_vertices = ");
		std::stringstream ss;
		ss << maxVert;
		str.append(ss.str());
		str.append(") out;\n");
sylvain thery's avatar
sylvain thery committed
127 128
		str.append("#define VARYING_IN in\n");
		str.append("#define VARYING_OUT out\n");
129
		str.append("#define POSITION_IN(X) gl_in[X].gl_Position\n");
130
		str.append("#define NBVERTS_IN gl_in.length()\n");
131 132 133 134
		return str;
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
135 136
		std::string str("#version 110\n");
		str.append("#extension GL_EXT_geometry_shader4 : enable\n");
kvanhoey's avatar
kvanhoey committed
137
		str.append("#define PRECISION float pipo_PRECISION\n");
138 139 140 141
		str.append("#define ATTRIBUTE attribute\n");
		str.append("#define VARYING_IN varying in\n");
		str.append("#define VARYING_OUT varying out\n");
		str.append("#define POSITION_IN(X) gl_PositionIn[X]\n");
142
		str.append("#define NBVERTS_IN gl_VerticesIn\n");
143 144 145 146
		return str;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
147 148
bool GLSLShader::areGeometryShadersSupported()
{
149
	if (!glewGetExtension("GL_ARB_geometry_shader4"))
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
150
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
151 152 153 154 155 156
	return true;
}


bool GLSLShader::areVBOSupported()
{
157
	if (!glewGetExtension("GL_vertex_buffer_object"))
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161
		return false;
	return true;
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
162
char* GLSLShader::loadSourceFile(const std::string& filename)
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165 166 167 168 169 170 171 172
{
	std::ifstream	file;
	int				file_size;
	char*			shader_source;

	/*** opening file ***/
	file.open( filename.c_str() , std::ios::in | std::ios::binary );

	if( !file.good() )
	{
173
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - unable to open the file " << filename << "." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175 176 177 178 179 180 181
		return NULL;
	}

	/*** reading file ***/
	try
	{
		/* get file size */
		file.seekg( 0, std::ios::end );
182
		file_size = int(file.tellg());
Pierre Kraemer's avatar
Pierre Kraemer committed
183 184 185 186 187 188 189 190 191 192 193
		file.seekg( 0, std::ios::beg );

		/* allocate shader source table */
		shader_source = new char [ file_size+1 ];

		/* read source file */
		file.read( shader_source, file_size );
		shader_source[ file_size ] = '\0';
	}
	catch( std::exception& io_exception )
	{
194
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - " << io_exception.what() << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
195 196 197 198 199 200 201 202 203 204 205 206
		file.close();
		return NULL;
	}

	/*** termination ***/
	file.close();
	return shader_source;
}

bool GLSLShader::loadVertexShader(  const std::string& filename )
{
	bool	flag;
207
//	char	*vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
208

209 210
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
211
	m_vertex_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
212

213
	m_vertex_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
214

215
	if( !m_vertex_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
216
	{
217
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
218 219 220
		return false;
	}

221 222
	flag = loadVertexShaderSourceString( m_vertex_shader_source );
//	delete [] vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226

	return flag;
}

227
bool GLSLShader::loadFragmentShader(const std::string& filename )
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229
{
	bool	flag;
230
//	char	*fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
231

232 233
	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;
234
	m_fragment_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
235

236
	m_fragment_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
237

238
	if( !m_fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
239
	{
240
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243
		return false;
	}

244 245
	flag = loadFragmentShaderSourceString( m_fragment_shader_source );
//	delete [] fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249 250 251 252

	return flag;
}

bool GLSLShader::loadGeometryShader(const std::string& filename )
{
	bool	flag;
253
//	char	*geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
254

255 256
	if (m_geom_shader_source)
		delete [] m_geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
257

258
	m_geom_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
259

260
	if( !m_geom_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
261
	{
262
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
263 264 265
		return false;
	}

266 267
	flag = loadGeometryShaderSourceString( m_geom_shader_source );
//	delete [] geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
268 269 270 271

	return flag;
}

Sylvain Thery's avatar
Sylvain Thery committed
272
bool GLSLShader::logError(GLuint handle, const std::string& nameSrc, const char *src)
Pierre Kraemer's avatar
Pierre Kraemer committed
273
{
Sylvain Thery's avatar
Sylvain Thery committed
274
	char *info_log;
275
	info_log = getInfoLogShader( handle );
Sylvain Thery's avatar
Sylvain Thery committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	if (info_log!=NULL)
	{
		CGoGNerr << "============================================================================" << CGoGNendl;
		CGoGNerr << "Error in " << nameSrc << CGoGNendl;
		CGoGNerr << "----------------------------------------------------------------------------" << CGoGNendl;
		char line[256];
		int ln=1;
		std::stringstream ss(src);
		do
		{
			ss.getline(line,256);
			std::cout << ln++ << ": "<< line<< std::endl;
		}while (!ss.eof());
		CGoGNerr << "----------------------------------------------------------------------------" << CGoGNendl;
		CGoGNerr << info_log;
		CGoGNerr << "============================================================================" << CGoGNendl;
		delete [] info_log;
		return false;
	}
	return true;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
297

Sylvain Thery's avatar
Sylvain Thery committed
298 299
bool GLSLShader::loadVertexShaderSourceString( const char *vertex_shader_source )
{
300
	if (*m_vertex_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
301
	{
302 303
		glDeleteShader(*m_vertex_shader_object);
		*m_vertex_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
304
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
305 306

	/*** create shader object ***/
307
	*m_vertex_shader_object = glCreateShader( GL_VERTEX_SHADER );
Pierre Kraemer's avatar
Pierre Kraemer committed
308

309
	if( !*m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
310
	{
311
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
312 313 314 315 316 317
		return false;
	}

	/*** load source file ***/
	if( !vertex_shader_source )
	{
318
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
319

320
		glDeleteShader(*m_vertex_shader_object );
321
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324 325

		return false;
	}

326
	glShaderSource( *m_vertex_shader_object, 1, (const char**)&vertex_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328

	/*** compile shader object ***/
329
	glCompileShader( *m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
330

Sylvain Thery's avatar
Sylvain Thery committed
331
	if (!logError(*m_vertex_shader_object, m_nameVS, vertex_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
332
	{
333
		glDeleteShader( *m_vertex_shader_object );
334
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
335 336 337 338 339 340 341
		return false;
	}

	/*** termination ***/
	return true;
}

342
bool GLSLShader::loadFragmentShaderSourceString( const char *fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
343
{
344
	if (*m_fragment_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
345
	{
346 347
		glDeleteShader(*m_fragment_shader_object);
		*m_fragment_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
348
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
349 350

	/*** create shader object ***/
351
	*m_fragment_shader_object = glCreateShader( GL_FRAGMENT_SHADER );
Pierre Kraemer's avatar
Pierre Kraemer committed
352

353
	if( !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
354
	{
355
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
356 357 358 359
		return false;
	}

	/*** load source file ***/
360
	if( !fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
361
	{
362
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
363

364
		glDeleteShader( *m_fragment_shader_object );
365
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
366 367 368 369

		return false;
	}

370
	glShaderSource( *m_fragment_shader_object, 1, (const char**)&fragment_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
371 372

	/*** compile shader object ***/
373
	glCompileShader( *m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
374

Sylvain Thery's avatar
Sylvain Thery committed
375
	if (!logError(*m_fragment_shader_object, m_nameFS, fragment_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
376
	{
377
		glDeleteShader( *m_fragment_shader_object );
378
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380 381 382 383 384 385 386 387
		return false;
	}

	/*** termination ***/
	return true;
}

bool GLSLShader::loadGeometryShaderSourceString( const char *geom_shader_source )
{
388
	if (*m_geom_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
389
	{
390 391
		glDeleteShader(*m_geom_shader_object);
		*m_geom_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
392
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
393
	/*** create shader object ***/
Sylvain Thery's avatar
Sylvain Thery committed
394
	*m_geom_shader_object = glCreateShader(GL_GEOMETRY_SHADER);
Pierre Kraemer's avatar
Pierre Kraemer committed
395

396
	if( !*m_geom_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
397
	{
398
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
399 400 401 402 403 404
		return false;
	}

	/*** load source file ***/
	if( !geom_shader_source )
	{
405
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
406

407
		glDeleteShader( *m_geom_shader_object );
408
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
409 410 411 412

		return false;
	}

413
	glShaderSource( *m_geom_shader_object, 1, (const char**)&geom_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
414 415

	/*** compile shader object ***/
416
	glCompileShader( *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
417

Sylvain Thery's avatar
Sylvain Thery committed
418
	if (!logError(*m_geom_shader_object, m_nameGS, geom_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
419
	{
420
		glDeleteShader( *m_geom_shader_object );
421
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
422 423 424 425 426 427 428
		return false;
	}

	/*** termination ***/
	return true;
}

429
char* GLSLShader::getInfoLog( GLuint obj )
Pierre Kraemer's avatar
Pierre Kraemer committed
430
{
431 432 433
	char* info_log;
	int info_log_length;
	int length;
Pierre Kraemer's avatar
Pierre Kraemer committed
434

435
	glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &info_log_length);
Sylvain Thery's avatar
Sylvain Thery committed
436 437 438

	if (info_log_length <= 1)
		return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
439 440

	info_log = new char [info_log_length];
441
	glGetProgramInfoLog( obj, info_log_length, &length, info_log );
Pierre Kraemer's avatar
Pierre Kraemer committed
442 443 444 445

	return info_log;
}

446 447
char* GLSLShader::getInfoLogShader( GLuint obj )
{
448 449 450
	char* info_log;
	int	info_log_length;
	int	length;
451 452 453 454 455 456 457 458 459 460 461 462

    glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &info_log_length);

    if (info_log_length <= 1)
        return NULL;

    info_log = new char [info_log_length+1];
    glGetShaderInfoLog( obj, info_log_length, &length, info_log );
    info_log[info_log_length]=0;
    return info_log;
}

Sylvain Thery's avatar
Sylvain Thery committed
463
bool GLSLShader::create(GLint inputGeometryPrimitive,GLint outputGeometryPrimitive, int nb_max_vertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
464
{
465 466
	int	status;
	char* info_log;
Pierre Kraemer's avatar
Pierre Kraemer committed
467

468
	if (nb_max_vertices != -1)
Sylvain Thery's avatar
Sylvain Thery committed
469 470
		m_nbMaxVertices = nb_max_vertices;

471 472
	m_geom_inputPrimitives = inputGeometryPrimitive;
	m_geom_outputPrimitives = outputGeometryPrimitive;
Pierre Kraemer's avatar
Pierre Kraemer committed
473 474

	/*** check if shaders are loaded ***/
475
	if( !*m_vertex_shader_object || !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
476
	{
477
		CGoGNerr << "ERROR - GLSLShader::create() - shaders are not defined." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
478 479 480 481
		return false;
	}

	/*** create program object ***/
482
	m_program_object = glCreateProgram();
Pierre Kraemer's avatar
Pierre Kraemer committed
483

484
	if( !*m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
485
	{
486
		CGoGNerr << "ERROR - GLSLShader::create() - unable to create program object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
487 488 489 490
		return false;
	}

	/*** attach shaders to program object ***/
491 492
	glAttachShader( *m_program_object, *m_vertex_shader_object );
	glAttachShader( *m_program_object, *m_fragment_shader_object );
493
	if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
494
	{
495
		glAttachShader( *m_program_object, *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
496

497 498 499 500 501 502
		if (CURRENT_OGL_VERSION == 2)
		{
			glProgramParameteriEXT(*m_program_object, GL_GEOMETRY_INPUT_TYPE_EXT, inputGeometryPrimitive);
			glProgramParameteriEXT(*m_program_object, GL_GEOMETRY_OUTPUT_TYPE_EXT, outputGeometryPrimitive);
			glProgramParameteriEXT(*m_program_object, GL_GEOMETRY_VERTICES_OUT_EXT, m_nbMaxVertices);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
503 504 505
	}

	/*** link program object ***/
506
	glLinkProgram( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
507

508
	glGetProgramiv( *m_program_object, GL_LINK_STATUS, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
509 510
	if( !status )
	{
511
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
512
		info_log = getInfoLog( *m_program_object );
513
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
514 515
		delete [] info_log;

516 517
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
518
		if (*m_geom_shader_object)
519 520
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
521
		*m_program_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
522 523 524 525

		return false;
	}

526 527 528 529
	*m_uniMat_Proj		= glGetUniformLocation(*m_program_object, "ProjectionMatrix");
	*m_uniMat_Model		= glGetUniformLocation(*m_program_object, "ModelViewMatrix");
	*m_uniMat_ModelProj	= glGetUniformLocation(*m_program_object, "ModelViewProjectionMatrix");
	*m_uniMat_Normal	= glGetUniformLocation(*m_program_object, "NormalMatrix");
530

Pierre Kraemer's avatar
Pierre Kraemer committed
531 532 533
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
534 535 536
bool GLSLShader::changeNbMaxVertices(int nb_max_vertices)
{
	m_nbMaxVertices = nb_max_vertices;
537
	if ((*m_geom_shader_object) && (CURRENT_OGL_VERSION == 2))
Sylvain Thery's avatar
Sylvain Thery committed
538
	{
539
		glProgramParameteriEXT(*m_program_object, GL_GEOMETRY_VERTICES_OUT_EXT, m_nbMaxVertices);
Sylvain Thery's avatar
Sylvain Thery committed
540 541 542 543 544 545
		// need to relink
		return true;
	}
	return false;
}

546 547 548 549 550 551
bool GLSLShader::link()
{
	int		status;
	char	*info_log;

	/*** link program object ***/
552
	glLinkProgram( *m_program_object );
553

554
	glGetProgramiv( *m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
555 556
	if( !status )
	{
557
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
558
		info_log = getInfoLog( *m_program_object );
559
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
560 561
		delete [] info_log;

562 563
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
564
		if (*m_geom_shader_object)
565 566
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
567
		*m_program_object = 0;
568 569 570 571 572 573

		return false;
	}
	return true;
}

574
bool GLSLShader::bind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
575
{
576
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
577
	{
578
		glUseProgram( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
579 580
		return true;
	}
581 582
	else
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
583 584
}

585
void GLSLShader::unbind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
586
{
587
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
588
	{
589
		glUseProgram( 0 );
Pierre Kraemer's avatar
Pierre Kraemer committed
590 591 592 593 594
	}
}

bool GLSLShader::isBinded()
{
595 596 597 598 599 600
	if (*m_program_object == 0)
		return false;

	GLint po;
	glGetIntegerv(GL_CURRENT_PROGRAM,&po);
	return ( *m_program_object == po );
Pierre Kraemer's avatar
Pierre Kraemer committed
601 602 603 604
}

GLSLShader::~GLSLShader()
{
605
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
606 607 608
	{
		unbind();

609
		if( *m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
610
		{
611 612
			glDetachShader( *m_program_object, *m_vertex_shader_object );
			glDeleteShader( *m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
613
		}
614
		if( *m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
615
		{
616 617
			glDetachShader( *m_program_object, *m_fragment_shader_object );
			glDeleteShader( *m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
618
		}
619
		if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
620
		{
621 622
			glDetachShader( *m_program_object, *m_geom_shader_object );
			glDeleteShader( *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
623 624
		}

625
		glDeleteShader( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
626
	}
627

628 629 630 631 632 633 634
	if (m_vertex_shader_source != NULL)
		delete[] m_vertex_shader_source;
	if (m_fragment_shader_source != NULL)
		delete[] m_fragment_shader_source;
	if (m_geom_shader_source != NULL)
		delete[] m_geom_shader_source;

635
//	m_registeredShaders->erase(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
636 637 638 639
}

std::string GLSLShader::findFile(const std::string filename)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
640
	// cherche d'abord dans le repertoire courant
Pierre Kraemer's avatar
Pierre Kraemer committed
641 642 643 644 645 646 647 648 649
	std::ifstream file;
	file.open(filename.c_str(),std::ios::in );
	if (!file.fail())
	{
		file.close();
		return filename;
	}
	file.close();

650 651 652 653 654 655 656 657 658 659 660 661 662 663
	for (std::vector<std::string>::const_iterator ipath = m_pathes.begin(); ipath != m_pathes.end(); ++ipath)
	{
		std::string st(*ipath);
		st.append(filename);

		std::ifstream file2;
		file2.open(st.c_str(),std::ios::in);
		if (!file2.fail())
		{
			file2.close();
			return st;
		}
	}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
664
	// LA MACRO SHADERPATH contient le chemin du repertoire qui contient les fichiers textes
Pierre Kraemer's avatar
Pierre Kraemer committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	std::string st(SHADERPATH);
	st.append(filename);

	std::ifstream file2; // on ne peut pas réutiliser file ????
	file2.open(st.c_str(),std::ios::in);
	if (!file2.fail())
	{
		file2.close();
		return st;
	}

	return filename;
}

bool GLSLShader::init()
{
681
#ifndef CGOGN_GLEW_MX
Pierre Kraemer's avatar
Pierre Kraemer committed
682 683 684
	GLenum error = glewInit();

	if (error != GLEW_OK)
685
		CGoGNerr << "Error: " << glewGetErrorString(error) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
686
	else
687
		CGoGNout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
688 689

	if (!areVBOSupported())
690
		CGoGNerr << "VBO not supported !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
691

692 693 694
	if  ((CURRENT_OGL_VERSION == 2) && (!glewIsSupported("GL_VERSION_2_0")))
	{
		CGoGNerr << " GL 2.0 not supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
695 696
		return false;
	}
697 698 699 700 701 702 703

	if ((CURRENT_OGL_VERSION == 3) && (!glewIsSupported("GL_VERSION_3_3")))
	{
		CGoGNerr << " GL 3.3 not supported" << CGoGNendl;
		return false;
	}

704 705 706
#endif
	return true;

Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709 710
}

bool GLSLShader::loadShaders(const std::string& vs, const std::string& ps)
{
Sylvain Thery's avatar
Sylvain Thery committed
711 712
	m_nameVS = vs;
	m_nameFS = ps;
713

Pierre Kraemer's avatar
Pierre Kraemer committed
714 715 716 717
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;
	
	std::string pss = findFile(ps);
718
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
719 720

	if(!create()) {
721
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
722 723
		return false;
	}
724
	CGoGNout << "Shaders loaded (" << vs << "," << ps << ")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
725 726 727
	return true; 
}

Sylvain Thery's avatar
Sylvain Thery committed
728
bool GLSLShader::loadShaders(const std::string& vs, const std::string& ps, const std::string& gs, GLint inputGeometryPrimitive,GLint outputGeometryPrimitive, int nb_max_vertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
729
{
Sylvain Thery's avatar
Sylvain Thery committed
730 731 732 733
	m_nameVS = vs;
	m_nameFS = ps;
	m_nameGS = gs;

Pierre Kraemer's avatar
Pierre Kraemer committed
734 735 736 737
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;

	std::string pss = findFile(ps);
738
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
739 740 741 742 743 744

	std::string gss = findFile(gs);
	bool geomShaderLoaded = loadGeometryShader(gss);

	if (!geomShaderLoaded)
	{
745
		CGoGNerr << "Error while loading geometry shader" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
746 747
	}

Sylvain Thery's avatar
Sylvain Thery committed
748
	if(!create(inputGeometryPrimitive,outputGeometryPrimitive,nb_max_vertices))
Pierre Kraemer's avatar
Pierre Kraemer committed
749
	{
750
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
751 752 753
		return false;
	}

754
	CGoGNout << "Shaders loaded (" << vs << "," << ps << "," << gs <<")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
755 756 757
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
758 759
bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs)
{
760 761
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
762
	m_vertex_shader_source = NULL;
763

764
	size_t sz = strlen(vs);
765
	m_vertex_shader_source = new char[sz+1];
766 767 768
	memcpy(m_vertex_shader_source, vs,sz+1);
	

769 770 771 772 773 774

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

	sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
775
	memcpy(m_fragment_shader_source, fs,sz+1);
776

777 778
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
779

780 781
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
782 783 784

	if(!create())
	{
785
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
786 787 788 789 790
		return false;
	}
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
791
bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs, const char* gs, GLint inputGeometryPrimitive,GLint outputGeometryPrimitive, int nb_max_vertices)
Sylvain Thery's avatar
Sylvain Thery committed
792
{
793 794
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
795
	m_vertex_shader_source = NULL;
796

797
	size_t sz = strlen(vs);
798
	m_vertex_shader_source = new char[sz+1];
799
	memcpy(m_vertex_shader_source,vs,sz+1);
800 801 802 803 804 805

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

	sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
806
	memcpy(m_fragment_shader_source,fs,sz+1);
807 808 809 810 811 812

	if (m_geom_shader_source)
		delete [] m_geom_shader_source;

	sz = strlen(gs);
	m_geom_shader_source = new char[sz+1];
813
	memcpy(m_geom_shader_source,gs,sz+1);
814

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
815 816
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
817

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
818 819
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
820

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
821 822
	if(!loadGeometryShaderSourceString(gs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
823

824
	if(!create(inputGeometryPrimitive, outputGeometryPrimitive, nb_max_vertices))
Sylvain Thery's avatar
Sylvain Thery committed
825
	{
826
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
827 828 829 830 831 832
		return false;
	}

	return true;
}

833 834 835 836
bool GLSLShader::reloadVertexShaderFromMemory(const char* vs)
{
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
837
	m_vertex_shader_source = NULL;
838

839
	size_t sz = strlen(vs);
840
	m_vertex_shader_source = new char[sz+1];
841
	memcpy(m_vertex_shader_source,vs,sz+1);
842 843 844 845 846 847 848 849 850

	return true;
}

bool GLSLShader::reloadFragmentShaderFromMemory(const char* fs)
{
	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

851
	unsigned int sz = uint32(strlen(fs));
852
	m_fragment_shader_source = new char[sz+1];
853
	memcpy(m_fragment_shader_source,fs,sz+1);
854 855 856 857 858 859 860 861 862

	return true;
}

bool GLSLShader::reloadGeometryShaderFromMemory(const char* gs)
{
	if (m_geom_shader_source)
		delete [] m_geom_shader_source;

863
	unsigned int sz = uint32(strlen(gs));
864
	m_geom_shader_source = new char[sz+1];
865
	memcpy(m_geom_shader_source,gs,sz+1);
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880

	return true;
}

bool GLSLShader::recompile()
{
	if (m_vertex_shader_source)
		if(!loadVertexShaderSourceString(m_vertex_shader_source)) return false;
	if (m_fragment_shader_source)
		if(!loadFragmentShaderSourceString(m_fragment_shader_source)) return false;
	if (m_geom_shader_source)
		if(!loadGeometryShaderSourceString(m_geom_shader_source)) return false;

	if(!create(m_geom_inputPrimitives,m_geom_outputPrimitives))
	{
881
		CGoGNerr << "Unable to create the shaders !" << CGoGNendl;
882 883 884
		return false;
	}

885 886 887
	*m_uniMat_Proj		= glGetUniformLocation(*m_program_object,"ProjectionMatrix");
	*m_uniMat_Model		= glGetUniformLocation(*m_program_object,"ModelViewMatrix");
	*m_uniMat_ModelProj	= glGetUniformLocation(*m_program_object,"ModelViewProjectionMatrix");
Sylvain Thery's avatar
Sylvain Thery committed
888
	*m_uniMat_Normal	= glGetUniformLocation(*m_program_object,"NormalMatrix");
889 890 891

	restoreUniformsAttribs();

892 893
	updateClippingUniforms();

894 895 896
	return true;
}

897 898
bool GLSLShader::validateProgram()
{
899
	if(!*m_program_object)
900 901
		return false;

902
	glValidateProgram(*m_program_object);
903
	GLint Result = GL_FALSE;
904
	glGetProgramiv(*m_program_object, GL_VALIDATE_STATUS, &Result);
905 906 907

	if(Result == GL_FALSE)
	{
908
		CGoGNout << "Validate program:" << CGoGNendl;
909
		int InfoLogLength;
910
		glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
911
		std::vector<char> Buffer(InfoLogLength);
912
		glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
913
		CGoGNout <<  &(Buffer[0]) << CGoGNendl;
914 915 916 917 918 919 920 921 922
		return false;
	}

	return true;
}

bool GLSLShader::checkProgram()
{
	GLint Result = GL_FALSE;
923
	glGetProgramiv(*m_program_object, GL_LINK_STATUS, &Result);
924 925

	int InfoLogLength;
926
	glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
927
	std::vector<char> Buffer(std::max(InfoLogLength, int(1)));
928
	glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
929
	CGoGNout << &Buffer[0] << CGoGNendl;
930 931 932 933 934 935 936 937

	return Result == GL_TRUE;
}

bool GLSLShader::checkShader(int shaderType)
{
	GLint Result = GL_FALSE;
	int InfoLogLength;
938
	GLuint id;
939 940 941 942

	switch(shaderType)
	{
	case VERTEX_SHADER:
943
		id = *m_vertex_shader_object;
944 945
		break;
	case FRAGMENT_SHADER:
946
		id = *m_fragment_shader_object;
947 948
		break;
	case GEOMETRY_SHADER:
949
		id = *m_geom_shader_object;
950 951
		break;
	default:
952
		CGoGNerr << "Error unkown shader type" << CGoGNendl;
953 954 955 956 957 958 959 960
		return false;
		break;
	}

	glGetShaderiv(id, GL_COMPILE_STATUS, &Result);
	glGetShaderiv(id, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> Buffer(InfoLogLength);
	glGetShaderInfoLog(id, InfoLogLength, NULL, &Buffer[0]);
961
	CGoGNout << &Buffer[0] << CGoGNendl;
962 963 964 965

	return Result == GL_TRUE;
}

Sylvain Thery's avatar
Sylvain Thery committed
966
void GLSLShader::bindAttrib(unsigned int att, const char* name) const
967
{
968
	glBindAttribLocation(*m_program_object, att, name);
969 970
}

971 972 973 974 975
void GLSLShader::addPathFileSeach(const std::string& path)
{
	m_pathes.push_back(path);
}

976
unsigned int GLSLShader::bindVA_VBO(const std::string& name, VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
977
{
978
	GLint idVA = glGetAttribLocation(*(this->m_program_object), name.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
979 980 981
	//valid ?
	if (idVA < 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
982
		CGoGNerr << "GLSLShader: Attribute " << name << " does not exist in shader" << CGoGNendl;
983
		return idVA;
Sylvain Thery's avatar
Sylvain Thery committed
984
	}
985

Sylvain Thery's avatar
Sylvain Thery committed
986 987 988 989 990
	// search if name already exist
	for (std::vector<VAStr>::iterator it = m_va_vbo_binding.begin(); it != m_va_vbo_binding.end(); ++it)
	{
		if (it->va_id == idVA)
		{
991
			it->vbo_ptr = vbo;