OpenWalnut  1.4.0
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes
WGEShader Class Reference

Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader. More...

#include <WGEShader.h>

+ Inheritance diagram for WGEShader:

List of all members.

Classes

class  SafeUpdaterCallback
 Update callback which handles the shader reloading. More...

Public Types

typedef osg::ref_ptr< WGEShaderRefPtr
 Convenience typedef for an osg::ref_ptr.
typedef osg::ref_ptr< const
WGEShader
ConstRefPtr
 Convenience typedef for an osg::ref_ptr; const.

Public Member Functions

 WGEShader (std::string name, boost::filesystem::path search=WPathHelper::getShaderPath())
 Default constructor.
virtual ~WGEShader ()
 Destructor.
virtual void apply (osg::ref_ptr< osg::Node > node)
 Apply this shader to the specified node.
virtual void applyDirect (osg::State &state)
 If enabled, activate our program in the GL pipeline, performing any rebuild operations that might be pending.
virtual void deactivate (osg::ref_ptr< osg::Node > node)
 Removes the shader from the specified node.
virtual void reload ()
 Initiate a reload of the shader during the next update cycle.
template<typename T >
WGEShaderDefine< T >::SPtr setDefine (std::string key, T value)
 Sets a define which is include into the shader source code.
WGEShaderDefineSwitch::SPtr setDefine (std::string key)
 Sets a define which is include into the shader source code.
void addPreprocessor (WGEShaderPreprocessor::SPtr preproc)
 Adds the specified preprocessor to this shader.
void removePreprocessor (WGEShaderPreprocessor::SPtr preproc)
 Removes the specified preprocessor.
void clearPreprocessors ()
 Removes all preprocessors.

Protected Types

typedef
WSharedAssociativeContainer
< std::map
< WGEShaderPreprocessor::SPtr,
boost::signals2::connection > > 
PreprocessorsList
 The list of preprocessors - Type.

Protected Member Functions

std::string processShaderRecursive (const std::string filename, bool optional=false, int level=0)
 This method searches and processes all includes in the shader source.
std::string processShader (const std::string filename, bool optional=false)
 This method searches and processes all includes in the shader source.
void reloadShader ()
 This completely reloads the shader file and processes it.
void updatePrograms ()
 Handles all state changes in m_reload and m_deactivated.

Protected Attributes

boost::filesystem::path m_shaderPath
 String that stores the location of all shader files.
std::string m_name
 The name of the shader.
bool m_reload
 Flag denoting whether a shader should be reloaded.
bool m_shaderLoaded
 True if the shaders have been loaded successfully previously.
bool m_deactivated
 Flag denoting whether a shader should be deactivated.
boost::signals2::connection m_reloadSignalConnection
 Connection object to the reload signal from WGraphbicsEngine.
PreprocessorsList m_preprocessors
 List of all pre-processing that need to be applied to this shader instance.
WGEShaderPreprocessor::SPtr m_versionPreprocessor
 This preprocessor needs to be run LAST.
osg::ref_ptr< osg::Shader > m_vertexShader
 the vertex shader object
osg::ref_ptr< osg::Shader > m_fragmentShader
 the fragment shader object
osg::ref_ptr< osg::Shader > m_geometryShader
 the geometry shader object

Detailed Description

Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.

Definition at line 47 of file WGEShader.h.


Member Typedef Documentation

typedef osg::ref_ptr< const WGEShader > WGEShader::ConstRefPtr

Convenience typedef for an osg::ref_ptr; const.

Definition at line 58 of file WGEShader.h.

typedef WSharedAssociativeContainer< std::map< WGEShaderPreprocessor::SPtr, boost::signals2::connection > > WGEShader::PreprocessorsList [protected]

The list of preprocessors - Type.

Definition at line 212 of file WGEShader.h.

typedef osg::ref_ptr< WGEShader > WGEShader::RefPtr

Convenience typedef for an osg::ref_ptr.

Definition at line 53 of file WGEShader.h.


Constructor & Destructor Documentation

WGEShader::WGEShader ( std::string  name,
boost::filesystem::path  search = WPathHelper::getShaderPath() 
)

Default constructor.

Loads the specified shader programs. The path that can be specified is optional but allows modules to load their own local shaders. The search order for shader files is as follows: 1. search, 2. search/shaders, 3. WPathHelper::getShaderPath()

Parameters:
namethe name of the shader. It gets searched in the shader path.
searchthe local search path. If not specified, the global shader path is used.

Definition at line 49 of file WGEShader.cpp.

References WGraphicsEngine::getGraphicsEngine(), m_fragmentShader, m_geometryShader, m_reloadSignalConnection, m_versionPreprocessor, m_vertexShader, and reload().

WGEShader::~WGEShader ( ) [virtual]

Destructor.

Definition at line 73 of file WGEShader.cpp.

References m_reloadSignalConnection.


Member Function Documentation

Adds the specified preprocessor to this shader.

The preprocessor is able to force shader reloads.

Parameters:
preprocthe preprocessor to add.

Definition at line 359 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

Referenced by setDefine().

void WGEShader::apply ( osg::ref_ptr< osg::Node >  node) [virtual]

Apply this shader to the specified node.

Use this method to ensure, that reload events can be handled properly during the update cycle.

Parameters:
nodethe node where the program should be registered to.

Definition at line 79 of file WGEShader.cpp.

References m_deactivated, m_reload, and m_shaderLoaded.

Referenced by applyDirect().

void WGEShader::applyDirect ( osg::State &  state) [virtual]

If enabled, activate our program in the GL pipeline, performing any rebuild operations that might be pending.

In addition to the standard OSG functionality, it also loads/reloads the shader source from file.

Parameters:
statethe state to apply the shader program to.

Definition at line 92 of file WGEShader.cpp.

References apply(), and updatePrograms().

Removes all preprocessors.

Be careful with this one since it removes the WGESHaderVersionPreprocessor too, which is mandatory.

Definition at line 384 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

void WGEShader::deactivate ( osg::ref_ptr< osg::Node >  node) [virtual]

Removes the shader from the specified node.

Parameters:
nodethe node where the program is registered to.

Definition at line 98 of file WGEShader.cpp.

References m_deactivated, and m_shaderLoaded.

std::string WGEShader::processShader ( const std::string  filename,
bool  optional = false 
) [protected]

This method searches and processes all includes in the shader source.

The filenames in the include statement are assumed to be relative to this shader's path. It additionally applies preprocessors.

See also:
processShaderRecursive
Parameters:
filenamethe filename of the shader to process.
optionaldenotes whether a "file not found" is critical or not
Returns:
the processed source.

Definition at line 334 of file WGEShader.cpp.

References WSharedObject< T >::getReadTicket(), m_preprocessors, m_versionPreprocessor, and processShaderRecursive().

Referenced by reloadShader().

std::string WGEShader::processShaderRecursive ( const std::string  filename,
bool  optional = false,
int  level = 0 
) [protected]

This method searches and processes all includes in the shader source.

The filenames in the include statement are assumed to be relative to this shader's path. It simply unrolls the code.

Parameters:
filenamethe filename of the shader to process.
optionaldenotes whether a "file not found" is critical or not
levelthe inclusion level. This is used to avoid cycles.
Returns:
the processed source.

Definition at line 206 of file WGEShader.cpp.

References WLogger::addLogMessage(), WLogger::getLogger(), WPathHelper::getShaderPath(), and m_shaderPath.

Referenced by processShader().

void WGEShader::reload ( ) [virtual]

Initiate a reload of the shader during the next update cycle.

Definition at line 117 of file WGEShader.cpp.

References m_reload.

Referenced by addPreprocessor(), clearPreprocessors(), removePreprocessor(), and WGEShader().

void WGEShader::reloadShader ( ) [protected]

This completely reloads the shader file and processes it.

It also resets m_reload to false.

Definition at line 122 of file WGEShader.cpp.

References WLogger::addLogMessage(), WLogger::getLogger(), m_fragmentShader, m_geometryShader, m_name, m_reload, m_shaderLoaded, m_vertexShader, and processShader().

Referenced by updatePrograms().

Removes the specified preprocessor.

Changes inside the preprocessor won't cause any updates anymore.

Parameters:
preprocthe preprocessor to remove. If not exists: nothing is done.

Definition at line 372 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

template<typename T >
WGEShaderDefine< T >::SPtr WGEShader::setDefine ( std::string  key,
value 
)

Sets a define which is include into the shader source code.

This allows the preprocessor to turn on/off several parts of your code. In GLSL defines are a better choice when compared with a lot of branches (if-statements).

Parameters:
keyThe name of the define
valueThe value of the define. If this is not specified, the define can be used as simple ifdef switch.
Returns:
the define object allowing later control

Definition at line 274 of file WGEShader.h.

References addPreprocessor(), WGEShaderDefine< ValueType >::getName(), WSharedObject< T >::getReadTicket(), m_preprocessors, and WGEShaderDefine< ValueType >::setValue().

Sets a define which is include into the shader source code.

This allows the preprocessor to turn on/off several parts of your code. In GLSL defines are a better choice when compared with a lot of branches (if-statements).

Parameters:
keyThe name of the define
Returns:
the switch allowing to control the define

Definition at line 398 of file WGEShader.cpp.

void WGEShader::updatePrograms ( ) [protected]

Handles all state changes in m_reload and m_deactivated.

It ensure that the shader programs are bound properly or deactivated.

Definition at line 177 of file WGEShader.cpp.

References m_deactivated, m_fragmentShader, m_geometryShader, m_reload, m_vertexShader, and reloadShader().

Referenced by applyDirect().


Member Data Documentation

bool WGEShader::m_deactivated [protected]

Flag denoting whether a shader should be deactivated.

Definition at line 202 of file WGEShader.h.

Referenced by apply(), deactivate(), and updatePrograms().

osg::ref_ptr< osg::Shader > WGEShader::m_fragmentShader [protected]

the fragment shader object

Definition at line 232 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().

osg::ref_ptr< osg::Shader > WGEShader::m_geometryShader [protected]

the geometry shader object

Definition at line 237 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().

std::string WGEShader::m_name [protected]

The name of the shader.

It is used to construct the actual filename to load.

Definition at line 187 of file WGEShader.h.

Referenced by reloadShader().

List of all pre-processing that need to be applied to this shader instance.

Definition at line 217 of file WGEShader.h.

Referenced by addPreprocessor(), clearPreprocessors(), processShader(), removePreprocessor(), and setDefine().

bool WGEShader::m_reload [protected]

Flag denoting whether a shader should be reloaded.

Definition at line 192 of file WGEShader.h.

Referenced by apply(), reload(), reloadShader(), and updatePrograms().

boost::signals2::connection WGEShader::m_reloadSignalConnection [protected]

Connection object to the reload signal from WGraphbicsEngine.

Definition at line 207 of file WGEShader.h.

Referenced by WGEShader(), and ~WGEShader().

bool WGEShader::m_shaderLoaded [protected]

True if the shaders have been loaded successfully previously.

Definition at line 197 of file WGEShader.h.

Referenced by apply(), deactivate(), and reloadShader().

boost::filesystem::path WGEShader::m_shaderPath [protected]

String that stores the location of all shader files.

Definition at line 182 of file WGEShader.h.

Referenced by processShaderRecursive().

This preprocessor needs to be run LAST.

It handles version-statements in GLSL.

Definition at line 222 of file WGEShader.h.

Referenced by processShader(), and WGEShader().

osg::ref_ptr< osg::Shader > WGEShader::m_vertexShader [protected]

the vertex shader object

Definition at line 227 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().


The documentation for this class was generated from the following files: