GLSLShader.cpp 29.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
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 35
#include "glm/gtx/inverse_transpose.hpp"

Sylvain Thery's avatar
Sylvain Thery committed
36

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

namespace Utils
{
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43

unsigned int GLSLShader::CURRENT_OGL_VERSION = 2;
Sylvain Thery's avatar
Sylvain Thery committed
44 45 46

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

Pierre Kraemer's avatar
Pierre Kraemer committed
54

Sylvain Thery's avatar
Sylvain Thery committed
55
std::string GLSLShader::DEFINES_GL3=\
56
"#version 150\n"
57
"#define PRECISON precision highp float\n"
Sylvain Thery's avatar
Sylvain Thery committed
58 59 60 61
"#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"
62
"#define INVARIANT_POS invariant gl_Position\n";
Sylvain Thery's avatar
Sylvain Thery committed
63

Pierre Kraemer's avatar
Pierre Kraemer committed
64 65

std::string* GLSLShader::DEFINES_GL = NULL;
66 67 68

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

Pierre Kraemer's avatar
Pierre Kraemer committed
69 70 71
std::set< std::pair<void*, GLSLShader*> > GLSLShader::m_registeredShaders;


72 73 74
//glm::mat4* GLSLShader::s_current_matrices=NULL;
Utils::GL_Matrices* GLSLShader::s_current_matrices=NULL;

Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77 78
GLSLShader::GLSLShader() :
	m_vertex_shader_source(NULL),
	m_fragment_shader_source(NULL),
	m_geom_shader_source(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
79
{
80 81 82 83 84 85 86 87 88
	*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
Pierre Kraemer committed
89
	if (DEFINES_GL == NULL)
90
		DEFINES_GL = &DEFINES_GL2;
Sylvain Thery's avatar
Sylvain Thery committed
91 92

	m_nbMaxVertices = 16;
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94
}

Pierre Kraemer's avatar
Pierre Kraemer committed
95
void GLSLShader::registerShader(void* ptr, GLSLShader* shader)
96
{
Pierre Kraemer's avatar
Pierre Kraemer committed
97
	m_registeredShaders.insert(std::pair<void*,GLSLShader*>(ptr, shader));
98 99
}

Pierre Kraemer's avatar
Pierre Kraemer committed
100
void GLSLShader::unregisterShader(void* ptr, GLSLShader* shader)
101
{
Pierre Kraemer's avatar
Pierre Kraemer committed
102
	m_registeredShaders.erase(std::pair<void*,GLSLShader*>(ptr, shader));
103 104 105 106
}

std::string GLSLShader::defines_Geom(const std::string& primitivesIn, const std::string& primitivesOut, int maxVert)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
107
	if (CURRENT_OGL_VERSION == 3)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	{
		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");
125
		str.append("#define NBVERTS_IN gl_in.length()\n");
126 127 128 129
		return str;
	}
	else
	{
Sylvain Thery's avatar
Sylvain Thery committed
130 131 132
		std::string str("#version 110\n");
		str.append("#extension GL_EXT_geometry_shader4 : enable\n");
		str.append("#define PRECISON float pipo_PRECISION\n");
133 134 135 136
		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");
137
		str.append("#define NBVERTS_IN gl_VerticesIn\n");
138 139 140 141
		return str;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
142 143
bool GLSLShader::areGeometryShadersSupported()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
	if (!glewGetExtension("GL_EXT_geometry_shader4"))
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147 148 149 150
	return true;
}

bool GLSLShader::areShadersSupported()
{
Sylvain Thery's avatar
Sylvain Thery committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	if ( ! glewIsSupported("GL_ARB_vertex_shader"))
	{
		CGoGNerr << " vertex shaders not supported!" <<CGoGNendl;
		return false;
	}

	if ( ! glewIsSupported("GL_ARB_fragment_shader"))
	{
		CGoGNerr << " fragment shaders not supported!" << CGoGNendl;
		return false;
	}

	if ( ! glewIsSupported("GL_ARB_geometry_shader4"))
	{
		CGoGNerr << " geometry shaders not supported!" << CGoGNendl;
		return false;
	}

	if ( ! glewIsSupported("GL_ARB_shader_objects"))
	{
		CGoGNerr << " shaders not supported!" << CGoGNendl;
		return false;
	}

	if ( ! glewIsSupported("GL_ARB_shading_language_100"))
	{
		CGoGNerr << " GLSL no supported!" << CGoGNendl;
		return false;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182 183 184 185 186 187 188 189 190

	return true;
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
191
char* GLSLShader::loadSourceFile(const std::string& filename)
Pierre Kraemer's avatar
Pierre Kraemer committed
192 193 194 195 196 197 198 199 200 201
{
	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() )
	{
202
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - unable to open the file " << filename << "." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		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 )
	{
223
		CGoGNerr << "ERROR - GLSLShader::loadSourceFile() - " << io_exception.what() << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
224 225 226 227 228 229 230 231 232 233 234 235
		file.close();
		return NULL;
	}

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

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

238 239
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
240
	m_vertex_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
241

242
	m_vertex_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
243

244
	if( !m_vertex_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
245
	{
246
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249 250
		return false;
	}


251 252
	flag = loadVertexShaderSourceString( m_vertex_shader_source );
//	delete [] vertex_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255 256

	return flag;
}

257
bool GLSLShader::loadFragmentShader(const std::string& filename )
Pierre Kraemer's avatar
Pierre Kraemer committed
258 259
{
	bool	flag;
260
//	char	*fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
261

262 263
	if (m_fragment_shader_source)
		delete [] m_fragment_shader_source;
264
	m_fragment_shader_source = NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
265

266
	m_fragment_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
267

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

274 275
	flag = loadFragmentShaderSourceString( m_fragment_shader_source );
//	delete [] fragment_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
276 277 278 279 280 281 282 283


	return flag;
}

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

286 287
	if (m_geom_shader_source)
		delete [] m_geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
288

289
	m_geom_shader_source = loadSourceFile( filename );
Pierre Kraemer's avatar
Pierre Kraemer committed
290

291
	if( !m_geom_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
292
	{
293
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - error occured while loading source file." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
294 295 296
		return false;
	}

297 298
	flag = loadGeometryShaderSourceString( m_geom_shader_source );
//	delete [] geom_shader_source;
Pierre Kraemer's avatar
Pierre Kraemer committed
299 300 301 302

	return flag;
}

Sylvain Thery's avatar
Sylvain Thery committed
303
bool GLSLShader::logError(GLuint handle, const std::string& nameSrc, const char *src)
Pierre Kraemer's avatar
Pierre Kraemer committed
304
{
Sylvain Thery's avatar
Sylvain Thery committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	char *info_log;
	info_log = getInfoLog( handle );
	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
328

Sylvain Thery's avatar
Sylvain Thery committed
329 330 331

bool GLSLShader::loadVertexShaderSourceString( const char *vertex_shader_source )
{
332
	if (*m_vertex_shader_object==0)
Sylvain Thery's avatar
Sylvain Thery committed
333
	{
334 335
		glDeleteShader(*m_vertex_shader_object);
		*m_vertex_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
336
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
337 338

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

341
	if( !*m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
342
	{
343
		CGoGNerr << "ERROR - GLSLShader::loadVertexShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347 348 349
		return false;
	}

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

352
		glDeleteShader(*m_vertex_shader_object );
353
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
354 355 356 357

		return false;
	}

358
	glShaderSource( *m_vertex_shader_object, 1, (const char**)&vertex_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
359 360

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

Sylvain Thery's avatar
Sylvain Thery committed
363
	if (!logError(*m_vertex_shader_object, m_nameVS, vertex_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	{
365
		glDeleteShader( *m_vertex_shader_object );
366
		*m_vertex_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368 369 370 371 372 373
		return false;
	}

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

374
bool GLSLShader::loadFragmentShaderSourceString( const char *fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
375
{
376
	if (*m_fragment_shader_object==0)
Sylvain Thery's avatar
Sylvain Thery committed
377
	{
378 379
		glDeleteShader(*m_fragment_shader_object);
		*m_fragment_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
380
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
381 382

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

385
	if( !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
386
	{
387
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
388 389 390 391
		return false;
	}

	/*** load source file ***/
392
	if( !fragment_shader_source )
Pierre Kraemer's avatar
Pierre Kraemer committed
393
	{
394
		CGoGNerr << "ERROR - GLSLShader::loadFragmentShader() - source string is empty." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
395

396
		glDeleteShader( *m_fragment_shader_object );
397
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
398 399 400 401

		return false;
	}

402
	glShaderSource( *m_fragment_shader_object, 1, (const char**)&fragment_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
403 404

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

Sylvain Thery's avatar
Sylvain Thery committed
407
	if (!logError(*m_fragment_shader_object, m_nameFS, fragment_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
408
	{
409
		glDeleteShader( *m_fragment_shader_object );
410
		*m_fragment_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
411 412 413
		return false;
	}

Sylvain Thery's avatar
Sylvain Thery committed
414

Pierre Kraemer's avatar
Pierre Kraemer committed
415 416 417 418 419 420
	/*** termination ***/
	return true;
}

bool GLSLShader::loadGeometryShaderSourceString( const char *geom_shader_source )
{
421
	if (*m_geom_shader_object==0)
Sylvain Thery's avatar
Sylvain Thery committed
422
	{
423 424
		glDeleteShader(*m_geom_shader_object);
		*m_geom_shader_object=0;
Sylvain Thery's avatar
Sylvain Thery committed
425
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
426
	/*** create shader object ***/
Sylvain Thery's avatar
Sylvain Thery committed
427
	*m_geom_shader_object = glCreateShader(GL_GEOMETRY_SHADER);
Pierre Kraemer's avatar
Pierre Kraemer committed
428

429
	if( !*m_geom_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
430
	{
431
		CGoGNerr << "ERROR - GLSLShader::loadGeometryShader() - unable to create shader object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
432 433 434 435 436 437
		return false;
	}

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

440
		glDeleteShader( *m_geom_shader_object );
441
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
442 443 444 445

		return false;
	}

446
	glShaderSource( *m_geom_shader_object, 1, (const char**)&geom_shader_source, NULL );
Pierre Kraemer's avatar
Pierre Kraemer committed
447 448

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

Sylvain Thery's avatar
Sylvain Thery committed
451
	if (!logError(*m_geom_shader_object, m_nameGS, geom_shader_source))
Pierre Kraemer's avatar
Pierre Kraemer committed
452
	{
453
		glDeleteShader( *m_geom_shader_object );
454
		*m_geom_shader_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
455 456 457
		return false;
	}

Sylvain Thery's avatar
Sylvain Thery committed
458

Pierre Kraemer's avatar
Pierre Kraemer committed
459 460 461 462
	/*** termination ***/
	return true;
}

463
char* GLSLShader::getInfoLog( GLuint obj )
Pierre Kraemer's avatar
Pierre Kraemer committed
464 465 466 467 468
{
	char	*info_log;
	int		info_log_length;
	int		length;

Sylvain Thery's avatar
Sylvain Thery committed
469 470 471 472
	glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &info_log_length);

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

	info_log = new char [info_log_length];
Sylvain Thery's avatar
Sylvain Thery committed
475
	glGetShaderInfoLog( obj, info_log_length, &length, info_log );
Pierre Kraemer's avatar
Pierre Kraemer committed
476 477 478 479

	return info_log;
}

Sylvain Thery's avatar
Sylvain Thery committed
480 481


Sylvain Thery's avatar
Sylvain Thery committed
482
bool GLSLShader::create(GLint inputGeometryPrimitive,GLint outputGeometryPrimitive, int nb_max_vertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
483 484 485 486
{
	int		status;
	char	*info_log;

487
	if (nb_max_vertices != -1)
Sylvain Thery's avatar
Sylvain Thery committed
488 489
		m_nbMaxVertices = nb_max_vertices;

490 491
	m_geom_inputPrimitives = inputGeometryPrimitive;
	m_geom_outputPrimitives = outputGeometryPrimitive;
Pierre Kraemer's avatar
Pierre Kraemer committed
492 493

	/*** check if shaders are loaded ***/
494
	if( !*m_vertex_shader_object || !*m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
495
	{
496
		CGoGNerr << "ERROR - GLSLShader::create() - shaders are not defined." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
497 498 499 500
		return false;
	}

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

503
	if( !*m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
504
	{
505
		CGoGNerr << "ERROR - GLSLShader::create() - unable to create program object." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
506 507 508 509
		return false;
	}

	/*** attach shaders to program object ***/
510 511
	glAttachShader( *m_program_object, *m_vertex_shader_object );
	glAttachShader( *m_program_object, *m_fragment_shader_object );
512
	if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
513
	{
514
		glAttachShader( *m_program_object, *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
515

516 517 518
		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
519 520 521
	}

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

524
	glGetProgramiv( *m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
Pierre Kraemer's avatar
Pierre Kraemer committed
525 526
	if( !status )
	{
527
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
528
		info_log = getInfoLog( *m_program_object );
529
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
530 531
		delete [] info_log;

532 533
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
534
		if (*m_geom_shader_object)
535 536
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
537
		*m_program_object = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
538 539 540 541

		return false;
	}

542 543 544 545
	*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");
546

Pierre Kraemer's avatar
Pierre Kraemer committed
547 548 549
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569


bool GLSLShader::changeNbMaxVertices(int nb_max_vertices)
{
	m_nbMaxVertices = nb_max_vertices;
	if (*m_geom_shader_object)
	{
		glProgramParameteriEXT(*m_program_object,GL_GEOMETRY_VERTICES_OUT_EXT,m_nbMaxVertices);
		// need to relink
		return true;
	}
	return false;
}







570 571 572 573 574 575
bool GLSLShader::link()
{
	int		status;
	char	*info_log;

	/*** link program object ***/
576
	glLinkProgram( *m_program_object );
577

578
	glGetProgramiv( *m_program_object, GL_OBJECT_LINK_STATUS_ARB, &status );
579 580
	if( !status )
	{
581
		CGoGNerr << "ERROR - GLSLShader::create() - error occured while linking shader program." << CGoGNendl;
582
		info_log = getInfoLog( *m_program_object );
583
		CGoGNerr << "  LINK " << info_log << CGoGNendl;
584 585
		delete [] info_log;

586 587
		glDetachShader( *m_program_object, *m_vertex_shader_object );
		glDetachShader( *m_program_object, *m_fragment_shader_object );
588
		if (*m_geom_shader_object)
589 590
			glDetachShader( *m_program_object, *m_geom_shader_object );
		glDeleteShader( *m_program_object );
591
		*m_program_object = 0;
592 593 594 595 596 597 598

		return false;
	}

	return true;
}

599
bool GLSLShader::bind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
600
{
601
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
602
	{
603
		glUseProgram( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
604 605
		return true;
	}
606 607
	else
		return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
608 609
}

610
void GLSLShader::unbind() const
Pierre Kraemer's avatar
Pierre Kraemer committed
611
{
612
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
613
	{
614
		glUseProgram( 0 );
Pierre Kraemer's avatar
Pierre Kraemer committed
615 616 617 618 619
	}
}

bool GLSLShader::isBinded()
{
620 621 622 623 624 625
	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
626 627 628 629
}

GLSLShader::~GLSLShader()
{
630
	if( *m_program_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
631 632 633
	{
		unbind();

634
		if( *m_vertex_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
635
		{
636 637
			glDetachShader( *m_program_object, *m_vertex_shader_object );
			glDeleteShader( *m_vertex_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
638
		}
639
		if( *m_fragment_shader_object )
Pierre Kraemer's avatar
Pierre Kraemer committed
640
		{
641 642
			glDetachShader( *m_program_object, *m_fragment_shader_object );
			glDeleteShader( *m_fragment_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
643
		}
644
		if (*m_geom_shader_object)
Pierre Kraemer's avatar
Pierre Kraemer committed
645
		{
646 647
			glDetachShader( *m_program_object, *m_geom_shader_object );
			glDeleteShader( *m_geom_shader_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
648 649
		}

650
		glDeleteShader( *m_program_object );
Pierre Kraemer's avatar
Pierre Kraemer committed
651
	}
652

653 654 655 656 657 658 659
	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;

Pierre Kraemer's avatar
Pierre Kraemer committed
660
//	m_registeredShaders.erase(this);
Pierre Kraemer's avatar
Pierre Kraemer committed
661 662 663 664
}

std::string GLSLShader::findFile(const std::string filename)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
665
	// cherche d'abord dans le repertoire courant
Pierre Kraemer's avatar
Pierre Kraemer committed
666 667 668 669 670 671 672 673 674
	std::ifstream file;
	file.open(filename.c_str(),std::ios::in );
	if (!file.fail())
	{
		file.close();
		return filename;
	}
	file.close();

675 676 677 678 679 680 681 682 683 684 685 686 687 688
	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
Pierre Kraemer committed
689
	// LA MACRO SHADERPATH contient le chemin du repertoire qui contient les fichiers textes
Pierre Kraemer's avatar
Pierre Kraemer committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
	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()
{
706
#ifndef GLEW_MX
Pierre Kraemer's avatar
Pierre Kraemer committed
707 708 709
	GLenum error = glewInit();

	if (error != GLEW_OK)
710
		CGoGNerr << "Error: " << glewGetErrorString(error) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
711
	else
712
		CGoGNout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
713 714

	if (!areVBOSupported())
715
		CGoGNout << "VBO not supported !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
716 717

	if(!areShadersSupported()) {
718
		CGoGNout << "Shaders not supported !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
719 720
		return false;
	}
721 722 723
#endif
	return true;

Pierre Kraemer's avatar
Pierre Kraemer committed
724 725 726 727
}

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

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

	if(!create()) {
738
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
739 740
		return false;
	}
741
	CGoGNout << "Shaders loaded (" << vs << "," << ps << ")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
742 743 744
	return true; 
}

Sylvain Thery's avatar
Sylvain Thery committed
745
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
746
{
Sylvain Thery's avatar
Sylvain Thery committed
747 748 749 750
	m_nameVS = vs;
	m_nameFS = ps;
	m_nameGS = gs;

Pierre Kraemer's avatar
Pierre Kraemer committed
751 752 753 754
	std::string vss = findFile(vs);
	if(!loadVertexShader(vss)) return false;

	std::string pss = findFile(ps);
755
	if(!loadFragmentShader(pss)) return false;
Pierre Kraemer's avatar
Pierre Kraemer committed
756 757 758 759 760 761

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

	if (!geomShaderLoaded)
	{
762
		CGoGNerr << "Error while loading geometry shader" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
763 764
	}

Sylvain Thery's avatar
Sylvain Thery committed
765
	if(!create(inputGeometryPrimitive,outputGeometryPrimitive,nb_max_vertices))
Pierre Kraemer's avatar
Pierre Kraemer committed
766
	{
767
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
768 769 770
		return false;
	}

771
	CGoGNout << "Shaders loaded (" << vs << "," << ps << "," << gs <<")" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
772 773 774
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
775 776
bool GLSLShader::loadShadersFromMemory(const char* vs, const char* fs)
{
777 778
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
779
	m_vertex_shader_source = NULL;
780 781 782

	unsigned int sz = strlen(vs);
	m_vertex_shader_source = new char[sz+1];
Pierre Kraemer's avatar
Pierre Kraemer committed
783
	strcpy(m_vertex_shader_source, vs);
784 785 786 787 788 789

	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
Pierre Kraemer committed
790
	strcpy(m_fragment_shader_source, fs);
791

792 793
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
794

795 796
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
797 798 799

	if(!create())
	{
800
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
801 802 803 804 805
		return false;
	}
	return true;
}

Sylvain Thery's avatar
Sylvain Thery committed
806
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
807
{
808 809
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
810
	m_vertex_shader_source = NULL;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829

	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
Pierre Kraemer committed
830 831
	if(!loadVertexShaderSourceString(vs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
832

Pierre Kraemer's avatar
Pierre Kraemer committed
833 834
	if(!loadFragmentShaderSourceString(fs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
835

Pierre Kraemer's avatar
Pierre Kraemer committed
836 837
	if(!loadGeometryShaderSourceString(gs))
		return false;
Sylvain Thery's avatar
Sylvain Thery committed
838

839
	if(!create(inputGeometryPrimitive, outputGeometryPrimitive, nb_max_vertices))
Sylvain Thery's avatar
Sylvain Thery committed
840
	{
841
		CGoGNout << "Unable to create the shaders !" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
842 843 844 845 846 847
		return false;
	}

	return true;
}

848 849 850 851
bool GLSLShader::reloadVertexShaderFromMemory(const char* vs)
{
	if (m_vertex_shader_source)
		delete [] m_vertex_shader_source;
852
	m_vertex_shader_source = NULL;
853 854 855

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

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	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))
	{
897
		CGoGNerr << "Unable to create the shaders !" << CGoGNendl;
898 899 900
		return false;
	}

901 902 903
	*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
904
	*m_uniMat_Normal	= glGetUniformLocation(*m_program_object,"NormalMatrix");
905 906 907

	restoreUniformsAttribs();

908 909
	updateClippingUniforms();

910 911 912
	return true;
}

913 914
bool GLSLShader::validateProgram()
{
915
	if(!*m_program_object)
916 917
		return false;

918
	glValidateProgram(*m_program_object);
919
	GLint Result = GL_FALSE;
920
	glGetProgramiv(*m_program_object, GL_VALIDATE_STATUS, &Result);
921 922 923

	if(Result == GL_FALSE)
	{
924
		CGoGNout << "Validate program:" << CGoGNendl;
925
		int InfoLogLength;
926
		glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
927
		std::vector<char> Buffer(InfoLogLength);
928
		glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
929
		CGoGNout <<  &(Buffer[0]) << CGoGNendl;
930 931 932 933 934 935 936 937 938
		return false;
	}

	return true;
}

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

	int InfoLogLength;
942
	glGetProgramiv(*m_program_object, GL_INFO_LOG_LENGTH, &InfoLogLength);
943
	std::vector<char> Buffer(std::max(InfoLogLength, int(1)));
944
	glGetProgramInfoLog(*m_program_object, InfoLogLength, NULL, &Buffer[0]);
945
	CGoGNout << &Buffer[0] << CGoGNendl;
946 947 948 949 950 951 952 953

	return Result == GL_TRUE;
}

bool GLSLShader::checkShader(int shaderType)
{
	GLint Result = GL_FALSE;
	int InfoLogLength;
954
	GLuint id;
955 956 957 958

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

	return Result == GL_TRUE;
}

Sylvain Thery's avatar
Sylvain Thery committed
982
void GLSLShader::bindAttrib(unsigned int att, const char* name) const
983
{
984
	glBindAttribLocation(*m_program_object, att, name);
985 986
}

987 988 989 990 991
void GLSLShader::addPathFileSeach(const std::string& path)
{
	m_pathes.push_back(path);
}

992
unsigned int GLSLShader::bindVA_VBO(const std::string& name, VBO* vbo)
Sylvain Thery's avatar
Sylvain Thery committed
993
{
994
	GLint idVA = glGetAttribLocation(*(this->m_program_object), name.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
995 996 997
	//valid ?
	if (idVA < 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
998
		CGoGNerr << "GLSLShader: Attribute " << name << " does not exist in shader" << CGoGNendl;
999
		return idVA;
Sylvain Thery's avatar
Sylvain Thery committed
1000 1001 1002 1003 1004 1005
	}
	// 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)
		{
1006 1007
			it->vbo_ptr = vbo;
			return (it - m_va_vbo_binding.begin());
Sylvain Thery's avatar
Sylvain Thery committed
1008 1009 1010 1011 1012
		}
	}
	// new one:
	VAStr temp;
	temp.va_id = idVA;
1013
	temp.vbo_ptr = vbo;
Sylvain Thery's avatar
Sylvain Thery committed
1014
	m_va_vbo_binding.push_back(temp);
1015 1016 1017 1018 1019 1020
	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
1021 1022 1023 1024
}

void GLSLShader::unbindVA(const std::string& name)
{
1025
	GLint idVA = glGetAttribLocation(*(this->m_program_object), name.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
1026 1027 1028
	//valid ?
	if (idVA < 0)
	{
1029
		CGoGNerr << "GLSLShader: Attribute " << name << " does not exist in shader, not unbinded" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
1030 1031 1032 1033
		return;
	}
	// search if name already exist
	unsigned int nb = m_va_vbo_binding.size();
1034
	for (unsigned int i = 0; i < nb; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
1035 1036 1037 1038 1039 1040 1041 1042 1043
	{
		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;
		}
	}
1044
	CGoGNerr << "GLSLShader: Attribute "<<name<< " not binded"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
1045 1046 1047 1048
}

void GLSLShader::setCurrentOGLVersion(unsigned int version)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
1049
	CURRENT_OGL_VERSION = version;
Sylvain Thery's avatar
Sylvain Thery committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	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)
{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1066
    this->bind();
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

	if (*m_uniMat_Proj >= 0)
		glUniformMatrix4fv(*m_uniMat_Proj, 1, false, &projection[0][0]);

	if (*m_uniMat_Model >= 0)
		glUniformMatrix4fv(*m_uniMat_Model,	1, false, &modelview[0][0]);

	if (*m_uniMat_ModelProj >= 0)
	{
		glm::mat4 PMV = projection * modelview;
		glUniformMatrix4fv(*m_uniMat_ModelProj,	1 , false, &PMV[0][0]);
	}

	if (*m_uniMat_Normal >= 0)
	{
		glm::mat4 normalMatrix = glm::gtx::inverse_transpose::inverseTranspose(modelview);
		glUniformMatrix4fv(*m_uniMat_Normal, 	1 , false, &normalMatrix[0][0]);
	}
1085 1086

	this->unbind();
Sylvain Thery's avatar
Sylvain Thery committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
}

void GLSLShader::updateMatrices(const glm::mat4& projection, const glm::mat4& modelview, const glm::mat4& PMV, const glm::mat4& normalMatrix)
{
	this->bind();

	if (*m_uniMat_Proj >= 0)
		glUniformMatrix4fv(*m_uniMat_Proj, 1, false, &projection[0][0]);

	if (*m_uniMat_Model >= 0)
		glUniformMatrix4fv(*m_uniMat_Model,	1, false, &modelview[0][0]);

	if (*m_uniMat_ModelProj >= 0)
		glUniformMatrix4fv(*m_uniMat_ModelProj,	1 , false, &PMV[0][0]);
1101

Sylvain Thery's avatar
Sylvain Thery committed
1102 1103
	if (*m_uniMat_Normal >= 0)
		glUniformMatrix4fv(*m_uniMat_Normal, 	1 , false, &normalMatrix[0][0]);
1104 1105

	this->unbind();
1106 1107
}

Sylvain Thery's avatar
Sylvain Thery committed
1108 1109 1110



1111
void GLSLShader::enableVertexAttribs(unsigned int stride, unsigned int begin) const
1112 1113
{
	this->bind();
1114
	for (std::vector<Utils::GLSLShader::VAStr>::const_iterator it = m_va_vbo_binding.begin(); it != m_va_vbo_binding.end(); ++it)
1115 1116 1117
	{
		glBindBuffer(GL_ARRAY_BUFFER, it->vbo_ptr->id());
		glEnableVertexAttribArray(it->va_id);
1118
		assert((it->vbo_ptr->dataSize()!=0) || !"dataSize of VBO is 0 ! could not draw");
1119
		glVertexAttribPointer(it->va_id, it->vbo_ptr->dataSize(), GL_FLOAT, false, stride, (const GLvoid*)((unsigned long)(begin)));
1120
	}
1121
//	this->unbind();
1122
}
Sylvain Thery's avatar
Sylvain Thery committed
1123

1124 1125
void GLSLShader::disableVertexAttribs() const
{
1126
//	this->bind();
1127
	for (std::vector<Utils::GLSLShader::VAStr>::const_iterator it = m_va_vbo_binding.begin(); it != m_va_vbo_binding.end(); ++it)
1128 1129 1130 1131
		glDisableVertexAttribArray(it->va_id);
	this->unbind();
}

Sylvain Thery's avatar
Sylvain Thery committed
1132

1133 1134 1135 1136 1137
void GLSLShader::updateCurrentMatrices()
{
	glm::mat4 model(currentModelView());
	model *= currentTransfo();

Sylvain Thery's avatar
Sylvain Thery committed
1138 1139 1140
	currentPMV() = currentProjection() * model;
	currentNormalMatrix() = glm::gtx::inverse_transpose::inverseTranspose(model);

1141
	for(std::set< std::pair<void*, GLSLShader*> >::iterator it = m_registeredShaders.begin(); it != m_registeredShaders.end(); ++it)
Sylvain Thery's avatar
Sylvain Thery committed
1142
		it->second->updateMatrices(currentProjection(), model, currentPMV(), currentNormalMatrix());
Sylvain Thery's avatar
Sylvain Thery committed
1143
}
Pierre Kraemer's avatar
Pierre Kraemer committed
1144

1145 1146 1147 1148 1149 1150 1151
void GLSLShader::updateAllFromGLMatrices()
{
	GLdouble modelview[16];
	GLdouble projection[16];
	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
	glGetDoublev( GL_PROJECTION_MATRIX, projection );

1152 1153
	glm::mat4& model = currentModelView();
	glm::mat4& proj = currentProjection();
1154 1155

	for (unsigned int i=0; i< 4; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
1156
	{
1157 1158 1159 1160 1161
		for (unsigned int j=0; j<4; ++j)
		{
			proj[i][j] = float(projection[4*i+j]);
			model[i][j] = float(modelview[4*i+j]);
		}
Sylvain Thery's avatar
Sylvain Thery committed
1162
	}
Sylvain Thery's avatar
Sylvain Thery committed
1163
	model = currentTransfo() * model;
Sylvain Thery's avatar
Sylvain Thery committed
1164 1165 1166

	currentPMV() = proj * model;
	currentNormalMatrix() = glm::gtx::inverse_transpose::inverseTranspose(model);
1167 1168

	for(std::set< std::pair<void*, GLSLShader*> >::iterator it = m_registeredShaders.begin(); it != m_registeredShaders.end(); ++it)
Sylvain Thery's avatar
Sylvain Thery committed
1169
		it->second->updateMatrices(proj, model, currentPMV(), currentNormalMatrix());
1170 1171 1172
}


Pierre Kraemer's avatar
Pierre Kraemer committed
1173
} // namespace Utils
Pierre Kraemer's avatar
Pierre Kraemer committed
1174

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