57 #include "ShaderNode.hh" 70 const std::string& _name)
77 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
78 if ( it->second.initialized ) {
80 if ( it->second.program != 0 )
81 delete it->second.program;
83 if ( it->second.vertexShader != 0 )
84 delete it->second.vertexShader;
86 if ( it->second.fragmentShader != 0 )
87 delete it->second.fragmentShader;
98 std::cerr <<
"hasShader: Error, draw mode not atomic!" << std::endl;
102 std::map<size_t,ShaderInfo>::iterator it;
105 it = pickShaders.find(_drawmode.
getIndex());
106 if ( it == pickShaders.end() )
109 it = shaders.find(_drawmode.
getIndex());
110 if ( it == shaders.end() )
114 return it->second.initialized;
123 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
125 it->second.program->use();
135 for ( std::map<size_t,ShaderInfo>::iterator it = pickShaders.begin(); it != pickShaders.end(); ++it) {
137 it->second.program->use();
147 std::cerr <<
"vertexShaderName: Error, draw mode not atomic!" << std::endl;
148 return std::string(
"");
151 std::map<size_t,ShaderInfo>::iterator it;
154 it = pickShaders.find(_drawmode.
getIndex());
155 if ( it == pickShaders.end() )
156 return std::string(
"");
158 it = shaders.find(_drawmode.
getIndex());
159 if ( it == shaders.end() )
160 return std::string(
"");
163 if ( it->second.initialized )
164 return it->second.vertexShaderFile;
166 return std::string(
"");
174 std::cerr <<
"fragmentShaderName: Error, draw mode not atomic!" << std::endl;
175 return std::string(
"");
178 std::map<size_t,ShaderInfo>::iterator it;
181 it = pickShaders.find(_drawmode.
getIndex());
182 if ( it == pickShaders.end() )
183 return std::string(
"");
185 it = shaders.find(_drawmode.
getIndex());
186 if ( it == shaders.end() )
187 return std::string(
"");
190 if ( it->second.initialized )
191 return it->second.fragmentShaderFile;
193 return std::string(
"");
202 for ( std::map<size_t,ShaderInfo>::iterator it = shaders.begin(); it != shaders.end(); ++it)
204 it->second.program->disable();
212 for ( std::map<size_t,ShaderInfo>::iterator it = pickShaders.begin(); it != pickShaders.end(); ++it)
214 it->second.program->disable();
225 std::cerr <<
"getShader: Error, draw mode not atomic!" << std::endl;
229 std::map<size_t,ShaderInfo>::iterator it;
232 it = pickShaders.find(_drawmode.
getIndex());
234 if ( it == pickShaders.end() )
237 if ( it->second.initialized )
238 return it->second.program;
243 it = shaders.find(_drawmode.
getIndex());
245 if ( it == shaders.end() )
248 if ( it->second.initialized )
249 return it->second.program;
266 std::cerr <<
"disableShader: Error, draw mode not atomic!" << std::endl;
270 size_t index = _drawmode.
getIndex();
273 if ( shaders[index].initialized ) {
274 if ( shaders[index].program != 0 )
275 delete shaders[index].program;
277 if ( shaders[index].vertexShader != 0 )
278 delete shaders[index].vertexShader;
280 if ( shaders[index].fragmentShader != 0 )
281 delete shaders[index].fragmentShader;
283 shaders[index].initialized =
false;
292 const std::string& _vertexShader,
293 const std::string& _fragmentShader,
294 std::string _pickVertexShader,
295 std::string _pickFragmentShader) {
298 std::cerr <<
"Shaders not supported with OpenGL Version less than 2.0" << std::endl;
302 if ( shaderDir_ ==
"" ) {
303 std::cerr <<
"No shader dir set for shadernode. Unable to load shaders!" << std::endl;
308 std::cerr <<
"setShader: Error, draw mode not atomic!" << std::endl;
313 size_t index = _drawmode.
getIndex();
315 shaders[index].vertexShaderFile = shaderDir_ + _vertexShader;
316 shaders[index].fragmentShaderFile = shaderDir_ + _fragmentShader;
318 const char* vertexShaderFilePath = shaders[index].vertexShaderFile.c_str();
319 const char* fragmentShaderFilePath = shaders[index].fragmentShaderFile.c_str();
324 if ( (shaders[index].vertexShader == 0) ||
325 (shaders[index].fragmentShader == 0) ||
326 (shaders[index].program == 0) ) {
327 std::cerr <<
"Unable to load shaders" << shaders[index].vertexShaderFile <<
328 " or " << shaders[index].fragmentShaderFile << std::endl;
329 shaders[index].vertexShader = 0;
330 shaders[index].fragmentShader = 0;
331 shaders[index].program = 0;
332 shaders[index].initialized =
false;
336 shaders[index].program->attach(shaders[index].vertexShader);
337 shaders[index].program->attach(shaders[index].fragmentShader);
338 shaders[index].program->link();
340 shaders[index].initialized =
true;
344 if ( pickShaders[index].initialized ) {
345 if ( pickShaders[index].program != 0 )
346 delete pickShaders[index].program;
348 if ( pickShaders[index].vertexShader != 0 )
349 delete pickShaders[index].vertexShader;
351 if ( pickShaders[index].fragmentShader != 0 )
352 delete pickShaders[index].fragmentShader;
354 pickShaders[index].initialized =
false;
357 if (_pickVertexShader.length () > 0 && _pickFragmentShader.length () > 0)
359 pickShaders[index].vertexShaderFile = shaderDir_ + _pickVertexShader;
360 pickShaders[index].fragmentShaderFile = shaderDir_ + _pickFragmentShader;
362 const char* vertexShaderFilePath = pickShaders[index].vertexShaderFile.c_str();
363 const char* fragmentShaderFilePath = pickShaders[index].fragmentShaderFile.c_str();
368 if ( (pickShaders[index].vertexShader == 0) ||
369 (pickShaders[index].fragmentShader == 0) ||
370 (pickShaders[index].program == 0) ) {
371 std::cerr <<
"Unable to load pick shaders" << pickShaders[index].vertexShaderFile <<
372 " or " << pickShaders[index].fragmentShaderFile << std::endl;
373 pickShaders[index].vertexShader = 0;
374 pickShaders[index].fragmentShader = 0;
375 pickShaders[index].program = 0;
376 pickShaders[index].initialized =
false;
380 pickShaders[index].program->attach(pickShaders[index].vertexShader);
381 pickShaders[index].program->attach(pickShaders[index].fragmentShader);
382 pickShaders[index].program->link();
384 pickShaders[index].initialized =
true;
393 shaderDir_ = _shaderDir;
402 for ( std::map<size_t,ShaderInfo>::const_iterator it = shaders.begin(); it != shaders.end(); ++it) {
405 if ( it->second.initialized) {
void enterPick(GLState &, PickTarget _target, const DrawModes::DrawMode &_drawmode)
set shader
GLSL::PtrVertexShader loadVertexShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
Namespace providing different geometric functions concerning angles.
void enter(GLState &, const DrawModes::DrawMode &_drawmode)
set shader
bool isAtomic() const
Check if this is an atomic draw Mode.
void leave(GLState &, const DrawModes::DrawMode &_drawmode)
disable shader
DrawModes::DrawMode availableDrawModes() const
return available draw modes
void disableShader(DrawModes::DrawMode _drawmode)
void setShader(DrawModes::DrawMode _drawmode, const std::string &_vertexShader, const std::string &_fragmentShader, std::string _pickVertexShader="", std::string _pickFragmentShader="")
DrawMode NONE
not a valid draw mode
ShaderNode(BaseNode *_parent=0, const std::string &_name="<ShaderNode>")
Default constructor. Applies all properties.
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.
void leavePick(GLState &, PickTarget _target, const DrawModes::DrawMode &_drawmode)
disable shader
bool hasShader(DrawModes::DrawMode _drawmode, bool _pick=false)
Check if a shader is available for the given drawMode.
PickTarget
What target to use for picking.
size_t getIndex() const
get an index of the current drawMode
GLSL::PtrFragmentShader loadFragmentShader(const char *name, const GLSL::StringList *macros, bool verbose)
Loads, compiles and installs a new vertex shader.
virtual ~ShaderNode()
Destructor.
bool containsAtomicDrawMode(const DrawMode &_atomicDrawMode) const
Check whether an Atomic DrawMode is active in this draw Mode.
bool openGLVersion(const int _major, const int _minor, bool _verbose)