63 #include "ShaderNode.hh"
69 namespace SceneGraph {
76 const std::string& _name)
83 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
84 if ( it->second.initialized ) {
86 if ( it->second.program != 0 )
87 delete it->second.program;
89 if ( it->second.vertexShader != 0 )
90 delete it->second.vertexShader;
92 if ( it->second.fragmentShader != 0 )
93 delete it->second.fragmentShader;
104 std::cerr <<
"hasShader: Error, draw mode not atomic!" << std::endl;
108 std::map<size_t,ShaderInfo>::iterator it;
111 it = pickShaders.find(_drawmode.
getIndex());
112 if ( it == pickShaders.end() )
115 it = shaders.find(_drawmode.
getIndex());
116 if ( it == shaders.end() )
120 return it->second.initialized;
129 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
131 it->second.program->use();
141 for ( std::map<size_t,ShaderInfo>::iterator it = pickShaders.begin(); it != pickShaders.end(); ++it) {
143 it->second.program->use();
153 std::cerr <<
"vertexShaderName: Error, draw mode not atomic!" << std::endl;
154 return std::string(
"");
157 std::map<size_t,ShaderInfo>::iterator it;
160 it = pickShaders.find(_drawmode.
getIndex());
161 if ( it == pickShaders.end() )
162 return std::string(
"");
164 it = shaders.find(_drawmode.
getIndex());
165 if ( it == shaders.end() )
166 return std::string(
"");
169 if ( it->second.initialized )
170 return it->second.vertexShaderFile;
172 return std::string(
"");
177 ShaderNode::fragmentShaderName(DrawModes::DrawMode _drawmode,
bool _pick) {
179 if ( !_drawmode.isAtomic() ) {
180 std::cerr <<
"fragmentShaderName: Error, draw mode not atomic!" << std::endl;
181 return std::string(
"");
184 std::map<size_t,ShaderInfo>::iterator it;
187 it = pickShaders.find(_drawmode.getIndex());
188 if ( it == pickShaders.end() )
189 return std::string(
"");
191 it = shaders.find(_drawmode.getIndex());
192 if ( it == shaders.end() )
193 return std::string(
"");
196 if ( it->second.initialized )
197 return it->second.fragmentShaderFile;
199 return std::string(
"");
208 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it)
210 it->second.program->disable();
218 for ( std::map<size_t,ShaderInfo>::iterator it = pickShaders.begin(); it != pickShaders.end(); ++it)
220 it->second.program->disable();
231 std::cerr <<
"getShader: Error, draw mode not atomic!" << std::endl;
235 std::map<size_t,ShaderInfo>::iterator it;
238 it = pickShaders.find(_drawmode.
getIndex());
240 if ( it == pickShaders.end() )
243 if ( it->second.initialized )
244 return it->second.program;
249 it = shaders.find(_drawmode.
getIndex());
251 if ( it == shaders.end() )
254 if ( it->second.initialized )
255 return it->second.program;
272 std::cerr <<
"disableShader: Error, draw mode not atomic!" << std::endl;
276 size_t index = _drawmode.
getIndex();
279 if ( shaders[index].initialized ) {
280 if ( shaders[index].program != 0 )
281 delete shaders[index].program;
283 if ( shaders[index].vertexShader != 0 )
284 delete shaders[index].vertexShader;
286 if ( shaders[index].fragmentShader != 0 )
287 delete shaders[index].fragmentShader;
289 shaders[index].initialized =
false;
298 std::string _vertexShader,
299 std::string _fragmentShader,
300 std::string _pickVertexShader,
301 std::string _pickFragmentShader) {
304 std::cerr <<
"Shaders not supported with OpenGL Version less than 2.0" << std::endl;
308 if ( shaderDir_ ==
"" ) {
309 std::cerr <<
"No shader dir set for shadernode. Unable to load shaders!" << std::endl;
314 std::cerr <<
"setShader: Error, draw mode not atomic!" << std::endl;
319 size_t index = _drawmode.
getIndex();
321 shaders[index].vertexShaderFile = shaderDir_ + _vertexShader;
322 shaders[index].fragmentShaderFile = shaderDir_ + _fragmentShader;
324 const char* vertexShaderFilePath = shaders[index].vertexShaderFile.c_str();
325 const char* fragmentShaderFilePath = shaders[index].fragmentShaderFile.c_str();
330 if ( (shaders[index].vertexShader == 0) ||
331 (shaders[index].fragmentShader == 0) ||
332 (shaders[index].program == 0) ) {
333 std::cerr <<
"Unable to load shaders" << shaders[index].vertexShaderFile <<
334 " or " << shaders[index].fragmentShaderFile << std::endl;
335 shaders[index].vertexShader = 0;
336 shaders[index].fragmentShader = 0;
337 shaders[index].program = 0;
338 shaders[index].initialized =
false;
342 shaders[index].program->attach(shaders[index].vertexShader);
343 shaders[index].program->attach(shaders[index].fragmentShader);
344 shaders[index].program->link();
346 shaders[index].initialized =
true;
350 if ( pickShaders[index].initialized ) {
351 if ( pickShaders[index].program != 0 )
352 delete pickShaders[index].program;
354 if ( pickShaders[index].vertexShader != 0 )
355 delete pickShaders[index].vertexShader;
357 if ( pickShaders[index].fragmentShader != 0 )
358 delete pickShaders[index].fragmentShader;
360 pickShaders[index].initialized =
false;
363 if (_pickVertexShader.length () > 0 && _pickFragmentShader.length () > 0)
365 pickShaders[index].vertexShaderFile = shaderDir_ + _pickVertexShader;
366 pickShaders[index].fragmentShaderFile = shaderDir_ + _pickFragmentShader;
368 const char* vertexShaderFilePath = pickShaders[index].vertexShaderFile.c_str();
369 const char* fragmentShaderFilePath = pickShaders[index].fragmentShaderFile.c_str();
374 if ( (pickShaders[index].vertexShader == 0) ||
375 (pickShaders[index].fragmentShader == 0) ||
376 (pickShaders[index].program == 0) ) {
377 std::cerr <<
"Unable to load pick shaders" << pickShaders[index].vertexShaderFile <<
378 " or " << pickShaders[index].fragmentShaderFile << std::endl;
379 pickShaders[index].vertexShader = 0;
380 pickShaders[index].fragmentShader = 0;
381 pickShaders[index].program = 0;
382 pickShaders[index].initialized =
false;
386 pickShaders[index].program->attach(pickShaders[index].vertexShader);
387 pickShaders[index].program->attach(pickShaders[index].fragmentShader);
388 pickShaders[index].program->link();
390 pickShaders[index].initialized =
true;
399 shaderDir_ = _shaderDir;
408 for ( std::map<size_t,ShaderInfo>::const_iterator it = shaders.begin(); it != shaders.end(); ++it) {
411 if ( it->second.initialized) {
void enter(GLState &, const DrawModes::DrawMode &_drawmode)
set shader
bool hasShader(DrawModes::DrawMode _drawmode, bool _pick=false)
Check if a shader is available for the given drawMode.
Namespace providing different geometric functions concerning angles.
size_t getIndex() const
get an index of the current drawMode
PickTarget
What target to use for picking.
bool containsAtomicDrawMode(DrawMode _atomicDrawMode) const
Check whether an Atomic DrawMode is active in this draw Mode.
void disableShader(DrawModes::DrawMode _drawmode)
virtual ~ShaderNode()
Destructor.
GLSL::PtrVertexShader ACGDLLEXPORT loadVertexShader(const char *name, const GLSL::StringList *macros=0, bool verbose=true)
Loads, compiles and installs a new vertex shader.
bool openGLVersion(const int _major, const int _minor)
void setShader(DrawModes::DrawMode _drawmode, std::string _vertexShader, std::string _fragmentShader, std::string _pickVertexShader="", std::string _pickFragmentShader="")
void leavePick(GLState &, PickTarget _target, const DrawModes::DrawMode &_drawmode)
disable shader
void enterPick(GLState &, PickTarget _target, const DrawModes::DrawMode &_drawmode)
set shader
GLSL::PtrProgram getShader(DrawModes::DrawMode _drawmode, bool _pick=false)
Get the shader for the given drawMode.
void setShaderDir(std::string _shaderDir)
Sets the shader dir.
bool isAtomic() const
Check if this is an atomic draw Mode.
ShaderNode(BaseNode *_parent=0, const std::string &_name="<ShaderNode>")
Default constructor. Applies all properties.
GLSL::PtrFragmentShader ACGDLLEXPORT loadFragmentShader(const char *name, const GLSL::StringList *macros=0, bool verbose=true)
Loads, compiles and installs a new vertex shader.
void leave(GLState &, const DrawModes::DrawMode &_drawmode)
disable shader
DrawMode NONE
not a valid draw mode
DrawModes::DrawMode availableDrawModes() const
return available draw modes