GLSLShader.cpp 26.6 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-2011, 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.u-strasbg.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

Pierre Kraemer's avatar
Pierre Kraemer committed
25
26
27
28
29
#define EXPORTING 1

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

Sylvain Thery's avatar
Sylvain Thery committed
33
34
#include "glm/gtx/inverse_transpose.hpp"

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

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

unsigned int GLSLShader::CURRENT_OGL_VERSION = 2;
Sylvain Thery's avatar
Sylvain Thery committed
42
43
44

std::string GLSLShader::DEFINES_GL2=\
"#version 110\n"
45
"#define PRECISON float pipo_PRECISION\n"
Sylvain Thery's avatar
Sylvain Thery committed
46
47
48
"#define ATTRIBUTE attribute\n"
"#define VARYING_VERT varying\n"
"#define VARYING_FRAG varying\n"
49
50
"#define FRAG_OUT_DEF float pipo_FRAGDEF\n"
"#define INVARIANT_POS float pipo_INVARIANT\n";
Sylvain Thery's avatar
Sylvain Thery committed
51

Pierre Kraemer's avatar
Pierre Kraemer committed
52

Sylvain Thery's avatar
Sylvain Thery committed
53
std::string GLSLShader::DEFINES_GL3=\
54
"#version 150\n"
55
"#define PRECISON precision highp float\n"
Sylvain Thery's avatar
Sylvain Thery committed
56
57
58
59
"#define ATTRIBUTE in\n"
"#define VARYING_VERT smooth out\n"
"#define VARYING_FRAG smooth in\n"
"#define FRAG_OUT_DEF out vec4 gl_FragColor\n"
60
"#define INVARIANT_POS invariant gl_Position\n";
Sylvain Thery's avatar
Sylvain Thery committed
61

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
62
63

std::string* GLSLShader::DEFINES_GL = NULL;
64
65
66

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

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
std::set< std::pair<void*, GLSLShader*> > GLSLShader::m_registeredShaders;


GLSLShader::GLSLShader() :
	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),
	m_vertex_shader_source(NULL),
	m_fragment_shader_source(NULL),
	m_geom_shader_source(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
82
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
83
	if (DEFINES_GL == NULL)
Sylvain Thery's avatar
Sylvain Thery committed
84
		DEFINES_GL = &DEFINES_GL3;
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
87
void GLSLShader::registerShader(void* ptr, GLSLShader* shader)
88
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
89
	m_registeredShaders.insert(std::pair<void*,GLSLShader*>(ptr, shader));
90
91
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
92
void GLSLShader::unregisterShader(void* ptr, GLSLShader* shader)
93
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
94
	m_registeredShaders.erase(std::pair<void*,GLSLShader*>(ptr, shader));
95
96
97
98
}

std::string GLSLShader::defines_Geom(const std::string& primitivesIn, const std::string& primitivesOut, int maxVert)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
99
	if (CURRENT_OGL_VERSION == 3)
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	{
		std::string str("#version 150\n");
		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");
		str.append("#define VARYING_IN in\n");
		str.append("#define VARYING_OUT smooth out\n");
		str.append("#define POSITION_IN(X) gl_in[X].gl_Position\n");
117
		str.append("#define NBVERTS_IN gl_in.length()\n");
118
119
120
121
122
123
124
125
126
127
		return str;
	}
	else
	{
		std::string str("#extension GL_EXT_geometry_shader4 : enable\n");
		str.append("#define PRECISION\n");
		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");
128
		str.append("#define NBVERTS_IN gl_VerticesIn\n");
129
130
131
132
		return str;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
133
134
bool GLSLShader::areGeometryShadersSupported()
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
135
136
	if (!glewGetExtension("GL_EXT_geometry_shader4"))
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	return true;
}

bool GLSLShader::areShadersSupported()
{
	if ( ! glewGetExtension("GL_ARB_vertex_shader")) return false;
	if ( ! glewGetExtension("GL_ARB_fragment_shader")) return false;
	if ( ! glewGetExtension("GL_ARB_shader_objects")) return false;
	if ( ! glewGetExtension("GL_ARB_shading_language_100")) return false;

	return true;
}

bool GLSLShader::areVBOSupported()
{
	if (!glewGetExtension("GL_ARB_vertex_buffer_object"))
		return false;
	return true;
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
157
char* GLSLShader::loadSourceFile(const std::string& filename)
Pierre Kraemer's avatar
Pierre Kraemer committed
158
159
160
161
162
163
164
165
166
167
{
	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() )
	{
168
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - unable to open the file " << filename << "." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		return NULL;
	}

	/*** reading file ***/
	try
	{
		/* get file size */
		file.seekg( 0, std::ios::end );
		file_size = file.tellg();
		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 )
	{
189
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - " << io_exception.what() << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
190
191
192
193
194
195
196
197
198
199
200
201
		file.close();
		return NULL;
	}

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

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

204
205
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
206

207
	m_vertex_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
208

209
	if( !m_vertex_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
210
	{
211
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
212
213
214
215
		return false;
	}


216
217
	flag = loadVertexShaderSourceString( m_vertex_shader_source );
//	delete [] vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
218
219
220
221

	return flag;
}

222
bool GLSLShader::loadFragmentShader(const std::string& filename )
Pierre Kraemer's avatar
Pierre Kraemer committed
223
224
{
	bool	flag;
225
//	char	*fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
226

227
228
	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
229

230
	m_fragment_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
231

232
	if( !m_fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
233
	{
234
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
235
236
237
		return false;
	}

238
239
	flag = loadFragmentShaderSourceString( m_fragment_shader_source );
//	delete [] fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
240
241
242
243
244
245
246
247


	return flag;
}

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

250
251
	if (m_geom_shader_source)
		delete [] m_geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
252

253
	m_geom_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
254

255
	if( !m_geom_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
256
	{
257
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
258
259
260
		return false;
	}

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

	return flag;
}

bool GLSLShader::loadVertexShaderSourceString( const char *vertex_shader_source )
{
	int		status;
	char	*info_log;

Sylvain Thery's avatar
Sylvain Thery committed
272
273
274
275
276
	if (m_vertex_shader_object==0)
	{
		glDeleteShader(m_vertex_shader_object);
		m_vertex_shader_object=0;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
277
278

	/*** create shader object ***/
Sylvain Thery's avatar
Sylvain Thery committed
279
	m_vertex_shader_object = glCreateShader( GL_VERTEX_SHADER );
Pierre Kraemer's avatar
Pierre Kraemer committed
280

281
	if( !m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
282
	{
283
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
284
285
286
287
288
289
		return false;
	}

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

292
293
		glDeleteObjectARB( m_vertex_shader_object );
		m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
294
295
296
297

		return false;
	}

298
	glShaderSourceARB( m_vertex_shader_object, 1, (const char**)&vertex_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
299
300

	/*** compile shader object ***/
301
	glCompileShaderARB( m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
302

303
	glGetObjectParameterivARB( m_vertex_shader_object, GL_OBJECT_COMPILE_STATUS_ARB, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
304
305
	if( !status )
	{
306
		CGoGNerr << "ERROR - GLshader::loadVertexShader() - error occured while compiling shader " << m_nameVS<< CGoGNendl;
307
		info_log = getInfoLog( m_vertex_shader_object );
308
				CGoGNerr << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
309
310
		delete [] info_log;

311
312
		glDeleteObjectARB( m_vertex_shader_object );
		m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
313
314
315
316
317
318
319
320

		return false;
	}

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

321
bool GLSLShader::loadFragmentShaderSourceString( const char *fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
322
323
324
325
{
	int		status;
	char	*info_log;

Sylvain Thery's avatar
Sylvain Thery committed
326
327
328
329
330
	if (m_fragment_shader_object==0)
	{
		glDeleteShader(m_fragment_shader_object);
		m_fragment_shader_object=0;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
331
332

	/*** create shader object ***/
Sylvain Thery's avatar
Sylvain Thery committed
333
	m_fragment_shader_object = glCreateShader( GL_FRAGMENT_SHADER );
Pierre Kraemer's avatar
Pierre Kraemer committed
334

335
	if( !m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
336
	{
337
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
338
339
340
341
		return false;
	}

	/*** load source file ***/
342
	if( !fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
343
	{
344
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
345

346
347
		glDeleteObjectARB( m_fragment_shader_object );
		m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
348
349
350
351

		return false;
	}

352
	glShaderSourceARB( m_fragment_shader_object, 1, (const char**)&fragment_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
353
354

	/*** compile shader object ***/
355
	glCompileShaderARB( m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
356

357
	glGetObjectParameterivARB( m_fragment_shader_object, GL_OBJECT_COMPILE_STATUS_ARB, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
358
359
	if( !status )
	{
360
		CGoGNerr << "ERROR - GLshader::loadFragmentShader() - error occured while compiling shader " <<  m_nameFS << CGoGNendl;
361
		info_log = getInfoLog( m_fragment_shader_object );
362
		CGoGNerr << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
363
364
		delete [] info_log;

365
366
		glDeleteObjectARB( m_fragment_shader_object );
		m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
367
368
369
370
371
372
373
374
375
376
377
378
379

		return false;
	}

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

bool GLSLShader::loadGeometryShaderSourceString( const char *geom_shader_source )
{
	int		status;
	char	*info_log;

Sylvain Thery's avatar
Sylvain Thery committed
380
381
382
383
384
	if (m_geom_shader_object==0)
	{
		glDeleteShader(m_geom_shader_object);
		m_geom_shader_object=0;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
385
	/*** create shader object ***/
386
	m_geom_shader_object = glCreateShader(GL_GEOMETRY_SHADER_EXT);
Pierre Kraemer's avatar
Pierre Kraemer committed
387

388
	if( !m_geom_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
389
	{
390
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
391
392
393
394
395
396
		return false;
	}

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

399
400
		glDeleteObjectARB( m_geom_shader_object );
		m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
401
402
403
404

		return false;
	}

405
	glShaderSourceARB( m_geom_shader_object, 1, (const char**)&geom_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
406
407

	/*** compile shader object ***/
408
	glCompileShaderARB( m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
409

410
	glGetObjectParameterivARB( m_geom_shader_object, GL_OBJECT_COMPILE_STATUS_ARB, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
411
412
	if( !status )
	{
413
		CGoGNerr << "ERROR - GLshader::loadGeometryShader() - error occured while compiling shader "<< m_nameGS << CGoGNendl;
414
		info_log = getInfoLog( m_geom_shader_object );
415
		CGoGNerr << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
416
417
		delete [] info_log;

418
419
		glDeleteObjectARB( m_geom_shader_object );
		m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

		return false;
	}

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

char* GLSLShader::getInfoLog( GLhandleARB obj )
{
	char	*info_log;
	int		info_log_length;
	int		length;

	glGetObjectParameterivARB( obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_log_length );

	info_log = new char [info_log_length];
	glGetInfoLogARB( obj, info_log_length, &length, info_log );

	return info_log;
}

bool GLSLShader::create(GLint inputGeometryPrimitive,GLint outputGeometryPrimitive)
{
	int		status;
	char	*info_log;

447
448
	m_geom_inputPrimitives = inputGeometryPrimitive;
	m_geom_outputPrimitives = outputGeometryPrimitive;
Pierre Kraemer's avatar
Pierre Kraemer committed
449
450

	/*** check if shaders are loaded ***/
451
	if( !m_vertex_shader_object || !m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
452
	{
453
		CGoGNerr << "ERROR - GLSLShader::create() - shaders are not defined." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
454
455
456
457
		return false;
	}

	/*** create program object ***/
458
	m_program_object = glCreateProgramObjectARB();
Pierre Kraemer's avatar
Pierre Kraemer committed
459

460
	if( !m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
461
	{
462
		CGoGNerr << "ERROR - GLSLShader::create() - unable to create program object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
463
464
465
466
		return false;
	}

	/*** attach shaders to program object ***/
467
468
469
	glAttachObjectARB( m_program_object, m_vertex_shader_object );
	glAttachObjectARB( m_program_object, m_fragment_shader_object );
	if (m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
470
	{
471
		glAttachObjectARB( m_program_object, m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
472

473
474
		glProgramParameteriEXT(m_program_object,GL_GEOMETRY_INPUT_TYPE_EXT,inputGeometryPrimitive);
		glProgramParameteriEXT(m_program_object,GL_GEOMETRY_OUTPUT_TYPE_EXT,outputGeometryPrimitive);
Pierre Kraemer's avatar
Pierre Kraemer committed
475
476
		int temp;
		glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT,&temp);
477
		glProgramParameteriEXT(m_program_object,GL_GEOMETRY_VERTICES_OUT_EXT,temp);
Pierre Kraemer's avatar
Pierre Kraemer committed
478
479
480
	}

	/*** link program object ***/
481
	glLinkProgramARB( m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
482

483
	glGetObjectParameterivARB( m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
484
485
	if( !status )
	{
486
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
487
		info_log = getInfoLog( m_program_object );
488
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
489
490
		delete [] info_log;

491
492
493
494
495
496
		glDetachObjectARB( m_program_object, m_vertex_shader_object );
		glDetachObjectARB( m_program_object, m_fragment_shader_object );
		if (m_geom_shader_object)
			glDetachObjectARB( m_program_object, m_geom_shader_object );
		glDeleteObjectARB( m_program_object );
		m_program_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
497
498
499
500

		return false;
	}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
501
502
503
504
	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");
505

Pierre Kraemer's avatar
Pierre Kraemer committed
506
507
508
	return true;
}

509
510
511
512
513
514
515
516
517
518
519
bool GLSLShader::link()
{
	int		status;
	char	*info_log;

	/*** link program object ***/
	glLinkProgramARB( m_program_object );

	glGetObjectParameterivARB( m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
	if( !status )
	{
520
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
521
		info_log = getInfoLog( m_program_object );
522
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
		delete [] info_log;

		glDetachObjectARB( m_program_object, m_vertex_shader_object );
		glDetachObjectARB( m_program_object, m_fragment_shader_object );
		if (m_geom_shader_object)
			glDetachObjectARB( m_program_object, m_geom_shader_object );
		glDeleteObjectARB( m_program_object );
		m_program_object = 0;

		return false;
	}

	return true;
}

538
bool GLSLShader::bind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
539
{
540
	if( m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
541
	{
542
		glUseProgramObjectARB( m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
543
544
		return true;
	}
545
546
	else
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
547
548
}

549
void GLSLShader::unbind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
550
{
551
	if( m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
552
553
554
555
556
557
558
	{
		glUseProgramObjectARB( 0 );
	}
}

bool GLSLShader::isBinded()
{
559
	return ( m_program_object && m_program_object == glGetHandleARB(GL_PROGRAM_OBJECT_ARB) );
Pierre Kraemer's avatar
Pierre Kraemer committed
560
561
562
563
}

GLSLShader::~GLSLShader()
{
564
	if( m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
565
566
567
	{
		unbind();

568
		if( m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
569
		{
570
571
			glDetachObjectARB( m_program_object, m_vertex_shader_object );
			glDeleteObjectARB( m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
572
		}
573
		if( m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
574
		{
575
576
			glDetachObjectARB( m_program_object, m_fragment_shader_object );
			glDeleteObjectARB( m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
577
		}
578
		if (m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
579
		{
580
581
			glDetachObjectARB( m_program_object, m_geom_shader_object );
			glDeleteObjectARB( m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
582
583
		}

584
		glDeleteObjectARB( m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
585
	}
586

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
587
//	m_registeredShaders.erase(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
588
589
590
591
}

std::string GLSLShader::findFile(const std::string filename)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
592
	// cherche d'abord dans le repertoire courant
Pierre Kraemer's avatar
Pierre Kraemer committed
593
594
595
596
597
598
599
600
601
	std::ifstream file;
	file.open(filename.c_str(),std::ios::in );
	if (!file.fail())
	{
		file.close();
		return filename;
	}
	file.close();

602
603
604
605
606
607
608
609
610
611
612
613
614
615
	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
616
	// LA MACRO SHADERPATH contient le chemin du repertoire qui contient les fichiers textes
Pierre Kraemer's avatar
Pierre Kraemer committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
	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()
{
	GLenum error = glewInit();

	if (error != GLEW_OK)
636
		CGoGNerr << "Error: " << glewGetErrorString(error) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
637
	else
638
		CGoGNout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
639
640

	if (!areVBOSupported())
641
		CGoGNout << "VBO not supported !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
642
643

	if(!areShadersSupported()) {
644
		CGoGNout << "Shaders not supported !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
645
646
647
648
649
650
651
		return false;
	}
	return true; 
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
655
656
657
658
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;
	
	std::string pss = findFile(ps);
659
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
660
661

	if(!create()) {
662
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
663
664
		return false;
	}
665
	CGoGNout << "Shaders loaded (" << vs << "," << ps << ")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
666
667
668
669
670
	return true; 
}

bool GLSLShader::loadShaders(const std::string& vs, const std::string& ps, const std::string& gs, GLint inputGeometryPrimitive,GLint outputGeometryPrimitive)
{
Sylvain Thery's avatar
Sylvain Thery committed
671
672
673
674
	m_nameVS = vs;
	m_nameFS = ps;
	m_nameGS = gs;

Pierre Kraemer's avatar
Pierre Kraemer committed
675
676
677
678
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;

	std::string pss = findFile(ps);
679
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
680
681
682
683
684
685

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

	if (!geomShaderLoaded)
	{
686
		CGoGNerr << "Error while loading geometry shader" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
687
688
689
690
	}

	if(!create(inputGeometryPrimitive,outputGeometryPrimitive))
	{
691
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
692
693
694
		return false;
	}

695
	CGoGNout << "Shaders loaded (" << vs << "," << ps << "," << gs <<")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
696
697
698
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
699
700
bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs)
{
701
702
703
704
705
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;

	unsigned int sz = strlen(vs);
	m_vertex_shader_source = new char[sz+1];
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
706
	strcpy(m_vertex_shader_source, vs);
707
708
709
710
711
712

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

	sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
713
	strcpy(m_fragment_shader_source, fs);
714

Sylvain Thery's avatar
Sylvain Thery committed
715
716
717
718
719
720
	if(!loadVertexShaderSourceString(vs)) return false;

	if(!loadFragmentShaderSourceString(fs)) return false;

	if(!create())
	{
721
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
722
723
724
725
726
727
728
		return false;
	}
	return true;
}

bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs, const char* gs, GLint inputGeometryPrimitive,GLint outputGeometryPrimitive)
{
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;

	unsigned int sz = strlen(vs);
	m_vertex_shader_source = new char[sz+1];
	strcpy(m_vertex_shader_source,vs);

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

	sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
	strcpy(m_fragment_shader_source,fs);

	if (m_geom_shader_source)
		delete [] m_geom_shader_source;

	sz = strlen(gs);
	m_geom_shader_source = new char[sz+1];
	strcpy(m_geom_shader_source,gs);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
750
751
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
752

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
753
754
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
755

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
756
757
	if(!loadGeometryShaderSourceString(gs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
758
759
760

	if(!create(inputGeometryPrimitive,outputGeometryPrimitive))
	{
761
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
762
763
764
765
766
767
		return false;
	}

	return true;
}

768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
bool GLSLShader::reloadVertexShaderFromMemory(const char* vs)
{
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;

	unsigned int sz = strlen(vs);
	m_vertex_shader_source = new char[sz+1];
	strcpy(m_vertex_shader_source,vs);

	return true;
}

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

	unsigned int sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
	strcpy(m_fragment_shader_source,fs);

	return true;
}

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

	unsigned int sz = strlen(gs);
	m_geom_shader_source = new char[sz+1];
	strcpy(m_geom_shader_source,gs);

	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))
	{
815
		CGoGNerr << "Unable to create the shaders !" << CGoGNendl;
816
817
818
819
820
821
822
823
824
825
826
827
828
		return false;
	}

	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");

	restoreUniformsAttribs();

	return true;
}

829
830
831
832
833
834
835
836
837
838
839
bool GLSLShader::validateProgram()
{
	if(!m_program_object)
		return false;

	glValidateProgram(m_program_object);
	GLint Result = GL_FALSE;
	glGetProgramiv(m_program_object, GL_VALIDATE_STATUS, &Result);

	if(Result == GL_FALSE)
	{
840
		CGoGNout << "Validate program:" << CGoGNendl;
841
842
843
844
		int InfoLogLength;
		glGetProgramiv(m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
		std::vector<char> Buffer(InfoLogLength);
		glGetProgramInfoLog(m_program_object, InfoLogLength, NULL, &Buffer[0]);
845
		CGoGNout <<  &(Buffer[0]) << CGoGNendl;
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
		return false;
	}

	return true;
}

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

	int InfoLogLength;
	glGetProgramiv(m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
	std::vector<char> Buffer(std::max(InfoLogLength, int(1)));
	glGetProgramInfoLog(m_program_object, InfoLogLength, NULL, &Buffer[0]);
861
	CGoGNout << &Buffer[0] << CGoGNendl;
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883

	return Result == GL_TRUE;
}

bool GLSLShader::checkShader(int shaderType)
{
	GLint Result = GL_FALSE;
	int InfoLogLength;
	GLhandleARB id;

	switch(shaderType)
	{
	case VERTEX_SHADER:
		id = m_vertex_shader_object;
		break;
	case FRAGMENT_SHADER:
		id = m_fragment_shader_object;
		break;
	case GEOMETRY_SHADER:
		id = m_geom_shader_object;
		break;
	default:
884
		CGoGNerr << "Error unkown shader type"<< CGoGNendl;
885
886
887
888
889
890
891
892
		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]);
893
	CGoGNout << &Buffer[0] << CGoGNendl;
894
895
896
897

	return Result == GL_TRUE;
}

Sylvain Thery's avatar
Sylvain Thery committed
898
void GLSLShader::bindAttrib(unsigned int att, const char* name) const
899
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
900
	glBindAttribLocation(m_program_object, att, name);
901
902
}

903
904
905
906
907
void GLSLShader::addPathFileSeach(const std::string& path)
{
	m_pathes.push_back(path);
}

Sylvain Thery's avatar
Sylvain Thery committed
908
909
910
void GLSLShader::unbindVBO(VBO* ptr)
{
	unsigned int nb = m_va_vbo_binding.size();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
911
	for (unsigned int i = 0; i < nb; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
912
913
914
915
916
917
918
919
920
921
922
	{
		if (m_va_vbo_binding[i].vbo_ptr == ptr)
		{
			if (i != (nb-1))
				m_va_vbo_binding[i] = m_va_vbo_binding[nb-1];
			m_va_vbo_binding.pop_back();
			return;
		}
	}
}

923
unsigned int GLSLShader::bindVA_VBO(const std::string& name, VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
924
{
925
	vbo->ref(this);
Sylvain Thery's avatar
Sylvain Thery committed
926
927
928
929
930

	GLint idVA = glGetAttribLocation(this->m_program_object, name.c_str());
	//valid ?
	if (idVA < 0)
	{
931
		CGoGNerr << "GLSLShader: Attribute "<<name<< " does not exist in shader"<< CGoGNendl;
932
		return idVA;
Sylvain Thery's avatar
Sylvain Thery committed
933
934
935
936
937
938
	}
	// 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)
		{
939
940
			it->vbo_ptr = vbo;
			return (it - m_va_vbo_binding.begin());
Sylvain Thery's avatar
Sylvain Thery committed
941
942
943
944
945
		}
	}
	// new one:
	VAStr temp;
	temp.va_id = idVA;
946
	temp.vbo_ptr = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
947
	m_va_vbo_binding.push_back(temp);
948
949
950
951
952
953
	return (m_va_vbo_binding.size() -1);
}

void GLSLShader::changeVA_VBO(unsigned int id, VBO* vbo)
{
	m_va_vbo_binding[id].vbo_ptr = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
954
955
956
957
958
959
960
961
}

void GLSLShader::unbindVA(const std::string& name)
{
	GLint idVA = glGetAttribLocation(this->m_program_object, name.c_str());
	//valid ?
	if (idVA < 0)
	{
962
		CGoGNerr << "GLSLShader: Attribute "<<name<< " does not exist in shader, not unbinded"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
		return;
	}
	// search if name already exist
	unsigned int nb = m_va_vbo_binding.size();
	for (unsigned int i =0; i<nb; ++i)
	{
		if (m_va_vbo_binding[i].va_id == idVA)
		{
			if (i != (nb-1))
				m_va_vbo_binding[i] = m_va_vbo_binding[nb-1];
			m_va_vbo_binding.pop_back();
			return;
		}
	}
977
	CGoGNerr << "GLSLShader: Attribute "<<name<< " not binded"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
978
979
980
981
}

void GLSLShader::setCurrentOGLVersion(unsigned int version)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
982
	CURRENT_OGL_VERSION = version;
Sylvain Thery's avatar
Sylvain Thery committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
	switch(version)
	{
	case 2:
		DEFINES_GL = &DEFINES_GL2;
		break;
	case 3:
		DEFINES_GL = &DEFINES_GL3;
		break;
	}
}

/**
 * update projection, modelview, ... matrices
 */
void GLSLShader::updateMatrices(const glm::mat4& projection, const glm::mat4& modelview)
{
	this->bind();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1000
	glm::mat4 PMV = projection * modelview;
1001
	glm::mat4 normalMatrix = glm::gtx::inverse_transpose::inverseTranspose(modelview);
Sylvain Thery's avatar
Sylvain Thery committed
1002

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1003
	if (m_uniMat_Proj >= 0)
1004
		glUniformMatrix4fv(m_uniMat_Proj,		1 , false, &projection[0][0]);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1005
	if (m_uniMat_Model >= 0)
1006
		glUniformMatrix4fv(m_uniMat_Model,		1 , false, &modelview[0][0]);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1007
	if (m_uniMat_ModelProj >= 0)
1008
		glUniformMatrix4fv(m_uniMat_ModelProj,	1 , false, &PMV[0][0]);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1009
	if (m_uniMat_Normal >= 0)
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
		glUniformMatrix4fv(m_uniMat_Normal, 	1 , false, &normalMatrix[0][0]);
}

void GLSLShader::enableVertexAttribs(unsigned int stride) const
{
	this->bind();
	for (std::vector<Utils::GLSLShader::VAStr>::const_iterator it= m_va_vbo_binding.begin(); it != m_va_vbo_binding.end(); ++it)
	{
		glBindBuffer(GL_ARRAY_BUFFER, it->vbo_ptr->id());
		glEnableVertexAttribArray(it->va_id);
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1020
		glVertexAttribPointer(it->va_id, it->vbo_ptr->dataSize(), GL_FLOAT, false, stride, 0);
1021
1022
	}
}
Sylvain Thery's avatar
Sylvain Thery committed
1023

1024
1025
1026
1027
void GLSLShader::disableVertexAttribs() const
{
	for (std::vector<Utils::GLSLShader::VAStr>::const_iterator it= m_va_vbo_binding.begin(); it != m_va_vbo_binding.end(); ++it)
		glEnableVertexAttribArray(it->va_id);
Sylvain Thery's avatar
Sylvain Thery committed
1028
}
Pierre Kraemer's avatar
Pierre Kraemer committed
1029
1030

} // namespace Utils
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
1031

Pierre Kraemer's avatar
Pierre Kraemer committed
1032
} // namespace CGoGN