GLSLShader.cpp 31.9 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

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>
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
#ifdef CGOGN_USE_OGL_CORE_PROFILE
43
unsigned int GLSLShader::CURRENT_OGL_VERSION = 3;
Sylvain Thery's avatar
Sylvain Thery committed
44
45
unsigned int GLSLShader::MAJOR_OGL_CORE = 3;
unsigned int GLSLShader::MINOR_OGL_CORE = 3;
46
#else
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
47
unsigned int GLSLShader::CURRENT_OGL_VERSION = 2;
Sylvain Thery's avatar
Sylvain Thery committed
48
49
unsigned int GLSLShader::MAJOR_OGL_CORE = 2;
unsigned int GLSLShader::MINOR_OGL_CORE = 1;
50
51
52
#endif


Sylvain Thery's avatar
Sylvain Thery committed
53
54
55

std::string GLSLShader::DEFINES_GL2=\
"#version 110\n"
56
"#define PRECISION float pipo_PRECISION\n"
Sylvain Thery's avatar
Sylvain Thery committed
57
58
59
"#define ATTRIBUTE attribute\n"
"#define VARYING_VERT varying\n"
"#define VARYING_FRAG varying\n"
60
"#define FRAG_OUT_DEF float pipo_FRAGDEF\n"
61
"#define FRAG_OUT gl_FragColor\n"
62
"#define INVARIANT_POS float pipo_INVARIANT\n";
Sylvain Thery's avatar
Sylvain Thery committed
63

Pierre Kraemer's avatar
Pierre Kraemer committed
64

Sylvain Thery's avatar
Sylvain Thery committed
65
std::string GLSLShader::DEFINES_GL3=\
66
"#version 150\n"
67
"#define PRECISION precision highp float\n"
Sylvain Thery's avatar
Sylvain Thery committed
68
"#define ATTRIBUTE in\n"
sylvain thery's avatar
sylvain thery committed
69
70
"#define VARYING_VERT out\n"
"#define VARYING_FRAG in\n"
71
72
"#define FRAG_OUT_DEF out vec4 outFragColor\n"
"#define FRAG_OUT outFragColor\n"
73
"#define INVARIANT_POS invariant gl_Position\n";
Sylvain Thery's avatar
Sylvain Thery committed
74

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
75
76

std::string* GLSLShader::DEFINES_GL = NULL;
77
78
79

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

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

82
83
84
//glm::mat4* GLSLShader::s_current_matrices=NULL;
Utils::GL_Matrices* GLSLShader::s_current_matrices=NULL;

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
85
GLSLShader::GLSLShader() :
86
	m_vao(0),
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
87
88
89
	m_vertex_shader_source(NULL),
	m_fragment_shader_source(NULL),
	m_geom_shader_source(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
90
{
91
92
93
94
95
96
97
98
99
	*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;

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
100
	if (DEFINES_GL == NULL)
101
		DEFINES_GL = &DEFINES_GL2;
Sylvain Thery's avatar
Sylvain Thery committed
102
103

	m_nbMaxVertices = 16;
104
105
106

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

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
109
void GLSLShader::registerShader(void* ptr, GLSLShader* shader)
110
{
111
	m_registeredShaders->insert(std::pair<void*,GLSLShader*>(ptr, shader));
112
113
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
114
void GLSLShader::unregisterShader(void* ptr, GLSLShader* shader)
115
{
116
	m_registeredShaders->erase(std::pair<void*,GLSLShader*>(ptr, shader));
117
118
119
120
}

std::string GLSLShader::defines_Geom(const std::string& primitivesIn, const std::string& primitivesOut, int maxVert)
{
121
	if (CURRENT_OGL_VERSION >= 3)
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	{
		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");
sylvain thery's avatar
sylvain thery committed
136
137
		str.append("#define VARYING_IN in\n");
		str.append("#define VARYING_OUT out\n");
138
		str.append("#define POSITION_IN(X) gl_in[X].gl_Position\n");
139
		str.append("#define NBVERTS_IN gl_in.length()\n");
140
141
142
143
		return str;
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
144
145
		std::string str("#version 110\n");
		str.append("#extension GL_EXT_geometry_shader4 : enable\n");
kvanhoey's avatar
kvanhoey committed
146
		str.append("#define PRECISION float pipo_PRECISION\n");
147
148
149
150
		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");
151
		str.append("#define NBVERTS_IN gl_VerticesIn\n");
152
153
154
155
		return str;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
bool GLSLShader::areGeometryShadersSupported()
{
158
	if (!glewGetExtension("GL_ARB_geometry_shader4"))
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
159
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
160
161
162
163
164
165
	return true;
}


bool GLSLShader::areVBOSupported()
{
166
	if (!glewGetExtension("GL_vertex_buffer_object"))
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
		return false;
	return true;
}

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
171
char* GLSLShader::loadSourceFile(const std::string& filename)
Pierre Kraemer's avatar
Pierre Kraemer committed
172
173
174
175
176
177
178
179
180
181
{
	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() )
	{
182
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - unable to open the file " << filename << "." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
183
184
185
186
187
188
189
190
		return NULL;
	}

	/*** reading file ***/
	try
	{
		/* get file size */
		file.seekg( 0, std::ios::end );
191
		file_size = int(file.tellg());
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
194
195
196
197
198
199
200
201
202
		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 )
	{
203
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - " << io_exception.what() << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
204
205
206
207
208
209
210
211
212
213
214
215
		file.close();
		return NULL;
	}

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

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

218
219
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
220
	m_vertex_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
221

222
	m_vertex_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
223

224
	if( !m_vertex_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
225
	{
226
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
227
228
229
		return false;
	}

230
231
	flag = loadVertexShaderSourceString( m_vertex_shader_source );
//	delete [] vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
232
233
234
235

	return flag;
}

236
bool GLSLShader::loadFragmentShader(const std::string& filename )
Pierre Kraemer's avatar
Pierre Kraemer committed
237
238
{
	bool	flag;
239
//	char	*fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
240

241
242
	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;
243
	m_fragment_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
244

245
	m_fragment_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
246

247
	if( !m_fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
248
	{
249
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
250
251
252
		return false;
	}

253
254
	flag = loadFragmentShaderSourceString( m_fragment_shader_source );
//	delete [] fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
255
256
257
258
259
260
261

	return flag;
}

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

264
265
	if (m_geom_shader_source)
		delete [] m_geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
266

267
	m_geom_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
268

269
	if( !m_geom_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
270
	{
271
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
272
273
274
		return false;
	}

275
276
	flag = loadGeometryShaderSourceString( m_geom_shader_source );
//	delete [] geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
277
278
279
280

	return flag;
}

Sylvain Thery's avatar
Sylvain Thery committed
281
bool GLSLShader::logError(GLuint handle, const std::string& nameSrc, const char *src)
Pierre Kraemer's avatar
Pierre Kraemer committed
282
{
Sylvain Thery's avatar
Sylvain Thery committed
283
	char *info_log;
284
	info_log = getInfoLogShader( handle );
Sylvain Thery's avatar
Sylvain Thery committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
	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
306

Sylvain Thery's avatar
Sylvain Thery committed
307
308
bool GLSLShader::loadVertexShaderSourceString( const char *vertex_shader_source )
{
309
	if (*m_vertex_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
310
	{
311
312
		glDeleteShader(*m_vertex_shader_object);
		*m_vertex_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
313
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
314
315

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

318
	if( !*m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
319
	{
320
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
321
322
323
324
325
326
		return false;
	}

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

329
		glDeleteShader(*m_vertex_shader_object );
330
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
331
332
333
334

		return false;
	}

335
	glShaderSource( *m_vertex_shader_object, 1, (const char**)&vertex_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
336
337

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

Sylvain Thery's avatar
Sylvain Thery committed
340
	if (!logError(*m_vertex_shader_object, m_nameVS, vertex_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
341
	{
342
		glDeleteShader( *m_vertex_shader_object );
343
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
344
345
346
347
348
349
350
		return false;
	}

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

351
bool GLSLShader::loadFragmentShaderSourceString( const char *fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
352
{
353
	if (*m_fragment_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
354
	{
355
356
		glDeleteShader(*m_fragment_shader_object);
		*m_fragment_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
357
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
358
359

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

362
	if( !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
363
	{
364
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
365
366
367
368
		return false;
	}

	/*** load source file ***/
369
	if( !fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
370
	{
371
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
372

373
		glDeleteShader( *m_fragment_shader_object );
374
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
375
376
377
378

		return false;
	}

379
	glShaderSource( *m_fragment_shader_object, 1, (const char**)&fragment_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
380
381

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

Sylvain Thery's avatar
Sylvain Thery committed
384
	if (!logError(*m_fragment_shader_object, m_nameFS, fragment_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
385
	{
386
		glDeleteShader( *m_fragment_shader_object );
387
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
388
389
390
391
392
393
394
395
396
		return false;
	}

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

bool GLSLShader::loadGeometryShaderSourceString( const char *geom_shader_source )
{
397
	if (*m_geom_shader_object!=0)
Sylvain Thery's avatar
Sylvain Thery committed
398
	{
399
400
		glDeleteShader(*m_geom_shader_object);
		*m_geom_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
401
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
402
	/*** create shader object ***/
Sylvain Thery's avatar
Sylvain Thery committed
403
	*m_geom_shader_object = glCreateShader(GL_GEOMETRY_SHADER);
Pierre Kraemer's avatar
Pierre Kraemer committed
404

405
	if( !*m_geom_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
406
	{
407
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
408
409
410
411
412
413
		return false;
	}

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

416
		glDeleteShader( *m_geom_shader_object );
417
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
418
419
420
421

		return false;
	}

422
	glShaderSource( *m_geom_shader_object, 1, (const char**)&geom_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
423
424

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

Sylvain Thery's avatar
Sylvain Thery committed
427
	if (!logError(*m_geom_shader_object, m_nameGS, geom_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
428
	{
429
		glDeleteShader( *m_geom_shader_object );
430
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
431
432
433
434
435
436
437
		return false;
	}

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

438
char* GLSLShader::getInfoLog( GLuint obj )
Pierre Kraemer's avatar
Pierre Kraemer committed
439
{
440
441
442
	char* info_log;
	int info_log_length;
	int length;
Pierre Kraemer's avatar
Pierre Kraemer committed
443

444
	glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &info_log_length);
Sylvain Thery's avatar
Sylvain Thery committed
445
446
447

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

	info_log = new char [info_log_length];
450
	glGetProgramInfoLog( obj, info_log_length, &length, info_log );
Pierre Kraemer's avatar
Pierre Kraemer committed
451
452
453
454

	return info_log;
}

455
456
char* GLSLShader::getInfoLogShader( GLuint obj )
{
457
458
459
	char* info_log;
	int	info_log_length;
	int	length;
460
461
462
463
464
465
466
467
468
469
470
471

    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
472
bool GLSLShader::create(GLint inputGeometryPrimitive,GLint outputGeometryPrimitive, int nb_max_vertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
473
{
474
475
	int	status;
	char* info_log;
Pierre Kraemer's avatar
Pierre Kraemer committed
476

477
	if (nb_max_vertices != -1)
Sylvain Thery's avatar
Sylvain Thery committed
478
479
		m_nbMaxVertices = nb_max_vertices;

480
481
	m_geom_inputPrimitives = inputGeometryPrimitive;
	m_geom_outputPrimitives = outputGeometryPrimitive;
Pierre Kraemer's avatar
Pierre Kraemer committed
482
483

	/*** check if shaders are loaded ***/
484
	if( !*m_vertex_shader_object || !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
485
	{
486
		CGoGNerr << "ERROR - GLSLShader::create() - shaders are not defined." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
487
488
489
490
		return false;
	}

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

493
	if( !*m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
494
	{
495
		CGoGNerr << "ERROR - GLSLShader::create() - unable to create program object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
496
497
498
499
		return false;
	}

	/*** attach shaders to program object ***/
500
501
	glAttachShader( *m_program_object, *m_vertex_shader_object );
	glAttachShader( *m_program_object, *m_fragment_shader_object );
502
	if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
503
	{
504
		glAttachShader( *m_program_object, *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
505

506
507
508
509
510
511
		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
512
513
514
	}

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

517
	glGetProgramiv( *m_program_object, GL_LINK_STATUS, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
518
519
	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;
Pierre Kraemer's avatar
Pierre Kraemer committed
523
524
		delete [] info_log;

525
526
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
527
		if (*m_geom_shader_object)
528
529
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
530
		*m_program_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
531
532
533
534

		return false;
	}

535
536
537
538
	*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");
539

Pierre Kraemer's avatar
Pierre Kraemer committed
540
541
542
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
543
544
545
bool GLSLShader::changeNbMaxVertices(int nb_max_vertices)
{
	m_nbMaxVertices = nb_max_vertices;
546
	if ((*m_geom_shader_object) && (CURRENT_OGL_VERSION == 2))
Sylvain Thery's avatar
Sylvain Thery committed
547
	{
548
		glProgramParameteriEXT(*m_program_object, GL_GEOMETRY_VERTICES_OUT_EXT, m_nbMaxVertices);
Sylvain Thery's avatar
Sylvain Thery committed
549
550
551
552
553
554
		// need to relink
		return true;
	}
	return false;
}

555
556
557
558
559
560
bool GLSLShader::link()
{
	int		status;
	char	*info_log;

	/*** link program object ***/
561
	glLinkProgram( *m_program_object );
562

563
	glGetProgramiv( *m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
564
565
	if( !status )
	{
566
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
567
		info_log = getInfoLog( *m_program_object );
568
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
569
570
		delete [] info_log;

571
572
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
573
		if (*m_geom_shader_object)
574
575
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
576
		*m_program_object = 0;
577
578
579
580
581
582

		return false;
	}
	return true;
}

583
bool GLSLShader::bind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
584
{
585
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
586
	{
587
		glUseProgram( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
588
589
		return true;
	}
590
591
	else
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
592
593
}

594
void GLSLShader::unbind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
595
{
596
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
597
	{
598
		glUseProgram( 0 );
Pierre Kraemer's avatar
Pierre Kraemer committed
599
600
601
602
603
	}
}

bool GLSLShader::isBinded()
{
604
605
606
607
608
609
	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
610
611
612
613
}

GLSLShader::~GLSLShader()
{
614
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
615
616
617
	{
		unbind();

618
		if( *m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
619
		{
620
621
			glDetachShader( *m_program_object, *m_vertex_shader_object );
			glDeleteShader( *m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
622
		}
623
		if( *m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
624
		{
625
626
			glDetachShader( *m_program_object, *m_fragment_shader_object );
			glDeleteShader( *m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
627
		}
628
		if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
629
		{
630
631
			glDetachShader( *m_program_object, *m_geom_shader_object );
			glDeleteShader( *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
632
633
		}

634
		glDeleteShader( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
635
	}
636

637
638
639
640
641
642
643
	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;

644
//	m_registeredShaders->erase(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
645
646
647
648
}

std::string GLSLShader::findFile(const std::string filename)
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
649
	// cherche d'abord dans le repertoire courant
Pierre Kraemer's avatar
Pierre Kraemer committed
650
651
652
653
654
655
656
657
658
	std::ifstream file;
	file.open(filename.c_str(),std::ios::in );
	if (!file.fail())
	{
		file.close();
		return filename;
	}
	file.close();

659
660
661
662
663
664
665
666
667
668
669
670
671
672
	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
673
	// LA MACRO SHADERPATH contient le chemin du repertoire qui contient les fichiers textes
Pierre Kraemer's avatar
Pierre Kraemer committed
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
	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()
{
690
#ifndef CGOGN_GLEW_MX
Pierre Kraemer's avatar
Pierre Kraemer committed
691
692
693
	GLenum error = glewInit();

	if (error != GLEW_OK)
694
		CGoGNerr << "Error: " << glewGetErrorString(error) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
695
	else
696
		CGoGNout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
697
698

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

701
702
703
	if  ((CURRENT_OGL_VERSION == 2) && (!glewIsSupported("GL_VERSION_2_0")))
	{
		CGoGNerr << " GL 2.0 not supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
704
705
		return false;
	}
706
707
708
709
710
711
712

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

713
714
715
#endif
	return true;

Pierre Kraemer's avatar
Pierre Kraemer committed
716
717
718
719
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
723
724
725
726
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;
	
	std::string pss = findFile(ps);
727
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
728
729

	if(!create()) {
730
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
731
732
		return false;
	}
733
	CGoGNout << "Shaders loaded (" << vs << "," << ps << ")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
734
735
736
	return true; 
}

Sylvain Thery's avatar
Sylvain Thery committed
737
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
738
{
Sylvain Thery's avatar
Sylvain Thery committed
739
740
741
742
	m_nameVS = vs;
	m_nameFS = ps;
	m_nameGS = gs;

Pierre Kraemer's avatar
Pierre Kraemer committed
743
744
745
746
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;

	std::string pss = findFile(ps);
747
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
748
749
750
751
752
753

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

	if (!geomShaderLoaded)
	{
754
		CGoGNerr << "Error while loading geometry shader" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
755
756
	}

Sylvain Thery's avatar
Sylvain Thery committed
757
	if(!create(inputGeometryPrimitive,outputGeometryPrimitive,nb_max_vertices))
Pierre Kraemer's avatar
Pierre Kraemer committed
758
	{
759
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
760
761
762
		return false;
	}

763
	CGoGNout << "Shaders loaded (" << vs << "," << ps << "," << gs <<")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
764
765
766
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
767
768
bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs)
{
769
770
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
771
	m_vertex_shader_source = NULL;
772

773
	size_t sz = strlen(vs);
774
	m_vertex_shader_source = new char[sz+1];
775
776
777
	memcpy(m_vertex_shader_source, vs,sz+1);
	

778
779
780
781
782
783

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

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

786
787
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
788

789
790
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
791
792
793

	if(!create())
	{
794
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
795
796
797
798
799
		return false;
	}
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
800
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
801
{
802
803
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
804
	m_vertex_shader_source = NULL;
805

806
	size_t sz = strlen(vs);
807
	m_vertex_shader_source = new char[sz+1];
808
	memcpy(m_vertex_shader_source,vs,sz+1);
809
810
811
812
813
814

	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;

	sz = strlen(fs);
	m_fragment_shader_source = new char[sz+1];
815
	memcpy(m_fragment_shader_source,fs,sz+1);
816
817
818
819
820
821

	if (m_geom_shader_source)
		delete [] m_geom_shader_source;

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

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
824
825
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
826

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
827
828
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
829

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
830
831
	if(!loadGeometryShaderSourceString(gs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
832

833
	if(!create(inputGeometryPrimitive, outputGeometryPrimitive, nb_max_vertices))
Sylvain Thery's avatar
Sylvain Thery committed
834
	{
835
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
836
837
838
839
840
841
		return false;
	}

	return true;
}

842
843
844
845
bool GLSLShader::reloadVertexShaderFromMemory(const char* vs)
{
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
846
	m_vertex_shader_source = NULL;
847

848
	size_t sz = strlen(vs);
849
	m_vertex_shader_source = new char[sz+1];
850
	memcpy(m_vertex_shader_source,vs,sz+1);
851
852
853
854
855
856
857
858
859

	return true;
}

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

860
	unsigned int sz = uint32(strlen(fs));
861
	m_fragment_shader_source = new char[sz+1];
862
	memcpy(m_fragment_shader_source,fs,sz+1);
863
864
865
866
867
868
869
870
871

	return true;
}

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

872
	unsigned int sz = uint32(strlen(gs));
873
	m_geom_shader_source = new char[sz+1];
874
	memcpy(m_geom_shader_source,gs,sz+1);
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889

	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))
	{
890
		CGoGNerr << "Unable to create the shaders !" << CGoGNendl;
891
892
893
		return false;
	}

894
895
896
	*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
897
	*m_uniMat_Normal	= glGetUniformLocation(*m_program_object,"NormalMatrix");
898
899
900

	restoreUniformsAttribs();

901
902
	updateClippingUniforms();

903
904
905
	return true;
}

906
907
bool GLSLShader::validateProgram()
{
908
	if(!*m_program_object)
909
910
		return false;

911
	glValidateProgram(*m_program_object);
912
	GLint Result = GL_FALSE;
913
	glGetProgramiv(*m_program_object, GL_VALIDATE_STATUS, &Result);
914
915
916

	if(Result == GL_FALSE)
	{
917
		CGoGNout << "Validate program:" << CGoGNendl;
918
		int InfoLogLength;
919
		glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
920
		std::vector<char> Buffer(InfoLogLength);
921
		glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
922
		CGoGNout <<  &(Buffer[0]) << CGoGNendl;
923
924
925
926
927
928
929
930
931
		return false;
	}

	return true;
}

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

	int InfoLogLength;
935
	glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
936
	std::vector<char> Buffer(std::max(InfoLogLength, int(1)));
937
	glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
938
	CGoGNout << &Buffer[0] << CGoGNendl;
939
940
941
942
943
944
945
946

	return Result == GL_TRUE;
}

bool GLSLShader::checkShader(int shaderType)
{
	GLint Result = GL_FALSE;
	int InfoLogLength;
947
	GLuint id;
948
949
950
951

	switch(shaderType)
	{
	case VERTEX_SHADER:
952
		id = *m_vertex_shader_object;
953
954
		break;
	case FRAGMENT_SHADER:
955
		id = *m_fragment_shader_object;
956
957
		break;
	case GEOMETRY_SHADER:
958
		id = *m_geom_shader_object;
959
960
		break;
	default:
961
		CGoGNerr << "Error unkown shader type" << CGoGNendl;
962
963
964
965
966
967
968
969
		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]);
970
	CGoGNout << &Buffer[0] << CGoGNendl;
971
972
973
974

	return Result == GL_TRUE;
}

Sylvain Thery's avatar
Sylvain Thery committed
975
void GLSLShader::bindAttrib(unsigned int att, const char* name) const
976
{
977
	glBindAttribLocation(*m_program_object, att, name);
978
979
}

980
981
982
983
984
void GLSLShader::addPathFileSeach(const std::string& path)
{
	m_pathes.push_back(path);
}

985
unsigned int GLSLShader::bindVA_VBO(const std::string& name, VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
986
{
987
	GLint idVA = glGetAttribLocation(*(this->m_program_object), name.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
988
989
990
	//valid ?
	if (idVA < 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
991
		CGoGNerr << "GLSLShader: Attribute " << name << " does not exist in shader" << CGoGNendl;
992
		return idVA;
Sylvain Thery's avatar
Sylvain Thery committed
993
	}
994

Sylvain Thery's avatar
Sylvain Thery committed
995
996
997
998
999
	// 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)
		{
1000
			it->vbo_ptr = vbo;
1001
			return uint32(it - m_va_vbo_binding.begin());
Sylvain Thery's avatar
Sylvain Thery committed
1002
1003
1004
1005
1006
		}
	}
	// new one:
	VAStr temp;
	temp.va_id = idVA;
1007
	temp.vbo_ptr = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
1008
	m_va_vbo_binding.push_back(temp);
1009

1010
	return uint32(m_va_vbo_binding.size() -1);
1011
1012
1013
1014
1015
}

void GLSLShader::changeVA_VBO(unsigned int id, VBO* vbo)
{
	m_va_vbo_binding[id].vbo_ptr = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
1016
1017
1018
1019
}

void GLSLShader::unbindVA(const std::string& name)
{
1020
	GLint idVA = glGetAttribLocation(*(this->m_program_object), name.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
1021
1022
1023
	//valid ?
	if (idVA < 0)
	{
1024
		CGoGNerr << "GLSLShader: Attribute " << name << " does not exist in shader, not unbinded" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
1025
1026
1027
		return;
	}
	// search if name already exist
1028
	unsigned int nb = uint32(m_va_vbo_binding.size());
1029
	for (unsigned int i = 0; i < nb; ++i)
Sylvain Thery's avatar
C