File: OglShader.cpp

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (514 lines) | stat: -rw-r--r-- 15,208 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* 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.          *
*******************************************************************************
*                               SOFA :: Modules                               *
*                                                                             *
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
//
// C++ Implementation: Shader
//
// Description:
//
//
// Author: The SOFA team </www.sofa-framework.org>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include <sofa/component/visualmodel/OglShader.h>
#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/system/FileRepository.h>


namespace sofa
{

namespace component
{

namespace visualmodel
{


SOFA_DECL_CLASS(OglShader)

//Register OglShader in the Object Factory
int OglShaderClass = core::RegisterObject("OglShader")
.add< OglShader >()
;

OglShader::OglShader():
	turnOn(initData(&turnOn, (bool) true, "turnOn", "Turn On the shader?")),
	passive(initData(&passive, (bool) false, "passive", "Will this shader be activated manually or automatically?")),
	vertFilename(initData(&vertFilename, (std::string) "shaders/toonShading.vert", "vertFilename", "Set the vertex shader filename to load")),
    fragFilename(initData(&fragFilename, (std::string) "shaders/toonShading.frag", "fragFilename", "Set the fragment shader filename to load")),
	geoFilename(initData(&geoFilename, (std::string) "", "geoFilename", "Set the geometry shader filename to load")),
	geometryInputType(initData(&geometryInputType, (int) -1, "geometryInputType", "Set input types for the geometry shader")),
	geometryOutputType(initData(&geometryOutputType, (int) -1, "geometryOutputType", "Set output types for the geometry shader")),
	geometryVerticesOut(initData(&geometryVerticesOut, (int) -1, "geometryVerticesOut", "Set max number of vertices in output for the geometry shader")),
	indexActiveShader(initData(&indexActiveShader, (unsigned int) 0, "indexActiveShader", "Set current active shader")),
	hasGeometryShader(false)
{


}

OglShader::~OglShader()
{
  if (shaderVector.size() == 0) return;
	shaderVector[indexActiveShader.getValue()]->TurnOff();
	for (unsigned int i=0 ; i<shaderVector.size() ; i++)
	{
              if (shaderVector[i])
              {
		shaderVector[i]->Release();
		delete shaderVector[i];
              }
	}
}

void OglShader::init()
{

	///Vertex filenames parsing
        std::string tempStr = vertFilename.getFullPath();
	std::string file;
	const std::string SEPARATOR = ";";
	unsigned long int pos = 0;
	unsigned long int oldPos = 0;

	pos = tempStr.find(SEPARATOR, oldPos);

	while (pos != std::string::npos)
	{
		file = tempStr.substr( oldPos, pos - oldPos);

		if (!helper::system::DataRepository.findFile(file))
		{
			serr << "OglShader : vertex shader file " << file <<" was not found." << sendl;
			return;
		}
		vertexFilenames.push_back( file );

		oldPos = pos + SEPARATOR.size();
		pos = tempStr.find(SEPARATOR, oldPos + SEPARATOR.size());
	}

	file = tempStr.substr( oldPos );

	if (!helper::system::DataRepository.findFile(file))
	{
		serr << "OglShader : vertex shader file " << file <<" was not found." << sendl;
		return;
	}
	vertexFilenames.push_back( file );

	///Fragment filenames parsing
	pos = oldPos = 0;
        tempStr = fragFilename.getFullPath();

	pos = tempStr.find(SEPARATOR, oldPos);

	while (pos != std::string::npos)
	{
		file = tempStr.substr( oldPos, pos - oldPos );

		if (!helper::system::DataRepository.findFile(file))
		{
				serr << "OglShader : fragment shader file " << file <<" was not found." << sendl;
				return;
		}
		fragmentFilenames.push_back( file );

		oldPos = pos + SEPARATOR.size();
		pos = tempStr.find(SEPARATOR, oldPos + SEPARATOR.size());
	}

	file = tempStr.substr( oldPos );
	if (!helper::system::DataRepository.findFile(file))
	{
			serr << "OglShader : fragment shader file " << file <<" was not found." << sendl;
			return;
	}
	fragmentFilenames.push_back( file );


	///Geometry filenames parsing
	pos = oldPos = 0;
        tempStr = geoFilename.getFullPath();

	if (geoFilename.getValue() == "" )
	{
		//shaderVector[i]->InitShaders(helper::system::DataRepository.getFile(vertFilename.getValue()),
		//		             helper::system::DataRepository.getFile(fragFilename.getValue()));

		if (fragmentFilenames.size() != vertexFilenames.size())
		{
			serr << "OglShader : The number of Vertex shaders is different from the number of Fragment Shaders." << sendl;
			return;
		}

		shaderVector.resize(vertexFilenames.size());

		for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
		{
			shaderVector[i] = new sofa::helper::gl::GLSLShader();
		}

	}
	else
	{
		pos = tempStr.find(SEPARATOR, oldPos);

		while (pos != std::string::npos)
		{
			pos = tempStr.find(SEPARATOR, oldPos);
			if (pos != std::string::npos)
			{
				file = tempStr.substr( oldPos, pos - oldPos );

				if (!helper::system::DataRepository.findFile(file))
				{
						serr << "OglShader : geometry shader file " << file <<" was not found." << sendl;
						return;
				}
				geometryFilenames.push_back( file );
			}
			oldPos = pos + SEPARATOR.size();
			pos = tempStr.find(SEPARATOR, oldPos + SEPARATOR.size());
		}

		file = tempStr.substr( oldPos );
		if (!helper::system::DataRepository.findFile(file))
		{
				serr << "OglShader : geometry shader file " << file <<" was not found." << sendl;
				return;
		}
		geometryFilenames.push_back( file );


		if (fragmentFilenames.size() != vertexFilenames.size() && geometryFilenames.size() !=  vertexFilenames.size())
		{
			serr << "OglShader : The number of indicated shaders is not coherent (not the same number for each triplet." << sendl;
			return;
		}

		shaderVector.resize(vertexFilenames.size());
		for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
			shaderVector[i] = new sofa::helper::gl::GLSLShader();

		hasGeometryShader = true;
	}

}

void OglShader::reinit()
{

}

void OglShader::initVisual()
{

	if (!sofa::helper::gl::GLSLShader::InitGLSL())
	{
		serr << "OglShader : InitGLSL failed" << sendl;
		return;
	}

	if (!hasGeometryShader)
	{
		for (unsigned int i=0 ; i<shaderVector.size() ; i++)
		{
			shaderVector[i]->InitShaders(helper::system::DataRepository.getFile(vertexFilenames[i]),
							            helper::system::DataRepository.getFile(fragmentFilenames[i]));
		}
	}
	else
	{
		if (geometryInputType.getValue() != -1)
		{
			for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
				setGeometryInputType(i, geometryInputType.getValue());
		}
		if (geometryOutputType.getValue() != -1)
		{
			for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
				setGeometryOutputType(i, geometryOutputType.getValue());
		}
#ifdef GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT
        GLint maxV;
        glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT, &maxV);
        if (geometryVerticesOut.getValue() < 0 || geometryVerticesOut.getValue() > maxV)
        {
        	for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
        		geometryVerticesOut.setValue(3);
        }
#endif
        if (geometryVerticesOut.getValue() >= 0)
        {
        	for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
        		setGeometryVerticesOut(i, geometryVerticesOut.getValue());
        }



		for (unsigned int i=0 ; i<vertexFilenames.size() ; i++)
		{
			shaderVector[i]->InitShaders(helper::system::DataRepository.getFile(vertexFilenames[i]),
										 helper::system::DataRepository.getFile(geometryFilenames[i]),
										 helper::system::DataRepository.getFile(fragmentFilenames[i]));
		}
	}

}

void OglShader::drawVisual()
{

}

void OglShader::stop()
{
	if(turnOn.getValue())
		shaderVector[indexActiveShader.getValue()]->TurnOff();
}

void OglShader::start()
{
	if(turnOn.getValue())
		shaderVector[indexActiveShader.getValue()]->TurnOn();
}

bool OglShader::isActive()
{
	return !passive.getValue();
}

void OglShader::updateVisual()
{

}
unsigned int OglShader::getNumberOfShaders()
{
	return shaderVector.size();
}

unsigned int OglShader::getCurrentIndex()
{
	return indexActiveShader.getValue();
}

void OglShader::setCurrentIndex(const unsigned int index)
{
	if (index < shaderVector.size())
	{
		shaderVector[indexActiveShader.getValue()]->TurnOff();
		indexActiveShader.setValue(index);
		shaderVector[indexActiveShader.getValue()]->TurnOn();
	}
}

void OglShader::addDefineMacro(const unsigned int index, const std::string &name, const std::string &value)
{
	shaderVector[index]->AddDefineMacro(name, value);
}

void OglShader::setTexture(const unsigned int index, const char* name, unsigned short unit)
{
	start();
	shaderVector[index]->SetInt(shaderVector[index]->GetVariable(name), unit);
	stop();
}
void OglShader::setInt(const unsigned int index, const char* name, int i)
{
	start();
	shaderVector[index]->SetInt(shaderVector[index]->GetVariable(name), i);
	stop();
}

void OglShader::setInt2(const unsigned int index, const char* name, int i1, int i2)
{
	start();
	shaderVector[index]->SetInt2(shaderVector[index]->GetVariable(name), i1, i2);
	stop();
}
void OglShader::setInt3(const unsigned int index, const char* name, int i1, int i2, int i3)
{
	start();
	shaderVector[index]->SetInt3(shaderVector[index]->GetVariable(name), i1, i2, i3);
	stop();
}
void OglShader::setInt4(const unsigned int index, const char* name, int i1, int i2, int i3, int i4)
{
	start();
	shaderVector[index]->SetInt4(shaderVector[index]->GetVariable(name), i1, i2, i3, i4);
	stop();
}

void OglShader::setFloat(const unsigned int index, const char* name, float f1)
{
	start();
	shaderVector[index]->SetFloat(shaderVector[index]->GetVariable(name), f1);
	stop();
}
void OglShader::setFloat2(const unsigned int index, const char* name, float f1, float f2)
{
	start();
	shaderVector[index]->SetFloat2(shaderVector[index]->GetVariable(name), f1, f2);
	stop();
}
void OglShader::setFloat3(const unsigned int index, const char* name, float f1, float f2, float f3)
{
	start();
	shaderVector[index]->SetFloat3(shaderVector[index]->GetVariable(name), f1, f2, f3);
	stop();
}
void OglShader::setFloat4(const unsigned int index, const char* name, float f1, float f2, float f3, float f4)
{
	start();
	shaderVector[index]->SetFloat4(shaderVector[index]->GetVariable(name), f1, f2, f3, f4);
	stop();
}

void OglShader::setIntVector(const unsigned int index, const char* name, int count, const GLint* i)
{
	start();
	shaderVector[index]->SetIntVector(shaderVector[index]->GetVariable(name), count, i);
	stop();
}
void OglShader::setIntVector2(const unsigned int index, const char* name, int count, const GLint* i)
{
	start();
	shaderVector[index]->SetIntVector2(shaderVector[index]->GetVariable(name), count, i);
	stop();
}
void OglShader::setIntVector3(const unsigned int index, const char* name, int count, const GLint* i)
{
	start();
	shaderVector[index]->SetIntVector3(shaderVector[index]->GetVariable(name), count, i);
	stop();
}
void OglShader::setIntVector4(const unsigned int index, const char* name, int count, const GLint* i)
{
	start();
	shaderVector[index]->SetIntVector4(shaderVector[index]->GetVariable(name), count, i);
	stop();
}

void OglShader::setFloatVector(const unsigned int index, const char* name, int count, const float* f)
{
	start();
	shaderVector[index]->SetFloatVector(shaderVector[index]->GetVariable(name), count, f);
	stop();
}
void OglShader::setFloatVector2(const unsigned int index, const char* name, int count, const float* f)
{
	start();
	shaderVector[index]->SetFloatVector2(shaderVector[index]->GetVariable(name), count, f);
	stop();
}
void OglShader::setFloatVector3(const unsigned int index, const char* name, int count, const float* f)
{
	start();
	shaderVector[index]->SetFloatVector3(shaderVector[index]->GetVariable(name), count, f);
	stop();
}
void OglShader::setFloatVector4(const unsigned int index, const char* name, int count, const float* f)
{
	start();
	shaderVector[index]->SetFloatVector4(shaderVector[index]->GetVariable(name), count, f);
	stop();
}


GLint OglShader::getAttribute(const unsigned int index, const char* name)
{
	start();
	GLint res = shaderVector[index]->GetAttributeVariable(name);
	stop();

	return res;
}

GLint OglShader::getUniform(const unsigned int index, const char* name)
{
	start();
	GLint res = shaderVector[index]->GetVariable(name);
	stop();

	return res;
}

GLint OglShader::getGeometryInputType(const unsigned int index)
{
	return shaderVector[index]->GetGeometryInputType();
}
void  OglShader::setGeometryInputType(const unsigned int index, GLint v)
{
	shaderVector[index]->SetGeometryInputType(v);
}

GLint OglShader::getGeometryOutputType(const unsigned int index)
{
	return shaderVector[index]->GetGeometryOutputType();
}

void  OglShader::setGeometryOutputType(const unsigned int index, GLint v)
{
	shaderVector[index]->SetGeometryOutputType(v);
}

GLint OglShader::getGeometryVerticesOut(const unsigned int index)
{
	return shaderVector[index]->GetGeometryVerticesOut();
}

void  OglShader::setGeometryVerticesOut(const unsigned int index, GLint v)
{
	shaderVector[index]->SetGeometryVerticesOut(v);
}

OglShaderElement::OglShaderElement()
: id(initData(&id, (std::string) "id", "id", "Set an ID name"))
, indexShader(initData(&indexShader, (unsigned int) 0, "indexShader", "Set the index of the desired shader you want to apply this parameter"))
{

}

void OglShaderElement::init()
{
	sofa::core::objectmodel::BaseContext* context = this->getContext();
	shader = context->core::objectmodel::BaseContext::get<OglShader>();

	if (!shader)
	{
		serr << "OglShaderElement: shader not found "<< sendl;
		return;
	}
}


}//namespace visualmodel

} //namespace component

} //namespace sofa