osgPPU::ShaderAttribute Class Reference

Wrapper class to simplify work with shader and uniforms. More...


Public Member Functions

 META_StateAttribute (osgPPU, ShaderAttribute, PROGRAM)
 ShaderAttribute ()
 ShaderAttribute (const ShaderAttribute &, const osg::CopyOp &copyop=osg::CopyOp::DEEP_COPY_ALL)
virtual ~ShaderAttribute ()
void add (const std::string &name, osg::Uniform::Type type, unsigned int elementCount=1, osg::StateAttribute::OverrideValue mode=osg::StateAttribute::ON)
void add (osg::Uniform *uniform, osg::StateAttribute::OverrideValue mode=osg::StateAttribute::ON)
void add (osg::StateSet::RefUniformPair uniform)
void del (const std::string &name)
bool set (const std::string &name, bool b0)
bool set (const std::string &name, bool b0, bool b1)
bool set (const std::string &name, bool b0, bool b1, bool b2)
bool set (const std::string &name, bool b0, bool b1, bool b2, bool b3)
bool set (const std::string &name, int i0)
bool set (const std::string &name, int i0, int i1)
bool set (const std::string &name, int i0, int i1, int i2)
bool set (const std::string &name, int i0, int i1, int i2, int i3)
bool set (const std::string &name, float b0)
bool set (const std::string &name, float b0, float b1)
bool set (const std::string &name, float b0, float b1, float b2)
bool set (const std::string &name, float b0, float b1, float b2, float b3)
bool set (const std::string &name, const osg::Vec2 &v)
bool set (const std::string &name, const osg::Vec3 &v)
bool set (const std::string &name, const osg::Vec4 &v)
bool set (const std::string &name, const osg::Matrix2 &m)
bool set (const std::string &name, const osg::Matrix3 &m)
bool set (const std::string &name, const osg::Matrixf &m)
bool set (unsigned int index, const std::string &name, bool b0)
bool set (unsigned int index, const std::string &name, bool b0, bool b1)
bool set (unsigned int index, const std::string &name, bool b0, bool b1, bool b2)
bool set (unsigned int index, const std::string &name, bool b0, bool b1, bool b2, bool b3)
bool set (unsigned int index, const std::string &name, int i0)
bool set (unsigned int index, const std::string &name, int i0, int i1)
bool set (unsigned int index, const std::string &name, int i0, int i1, int i2)
bool set (unsigned int index, const std::string &name, int i0, int i1, int i2, int i3)
bool set (unsigned int index, const std::string &name, float b0)
bool set (unsigned int index, const std::string &name, float b0, float b1)
bool set (unsigned int index, const std::string &name, float b0, float b1, float b2)
bool set (unsigned int index, const std::string &name, float b0, float b1, float b2, float b3)
bool set (unsigned int index, const std::string &name, const osg::Vec2 &v)
bool set (unsigned int index, const std::string &name, const osg::Vec3 &v)
bool set (unsigned int index, const std::string &name, const osg::Vec4 &v)
bool set (unsigned int index, const std::string &name, const osg::Matrix2 &m)
bool set (unsigned int index, const std::string &name, const osg::Matrix3 &m)
bool set (unsigned int index, const std::string &name, const osg::Matrixf &m)
bool bindTexture (unsigned int index, const std::string &name, osg::Texture *tex, int unit=-1)
bool bindTexture (const std::string &name, osg::Texture *tex, int unit=-1)
 Bind a texture to the specified uniform. This method do siplify your life ;-)
Parameters:
index If uniform is an array element, then specify the index here
name Name of the uniform to which to bound the texture
tex Texture to bound
unit Texture unit to which to bound the texture. Specify -1 to automagically choose free texture unit

bool bindAttribute (const std::string &name, unsigned int index)
bool bindFragData (const std::string &name, unsigned int index)
virtual void apply (osg::State &state) const
virtual int compare (const osg::StateAttribute &sa) const
osg::Uniform * get (const std::string &name)
void setUniformList (const osg::StateSet::UniformList &list)
const osg::StateSet::UniformList & getUniformList () const
void setMaximalSupportedTextureUnits (int i)
int getMaximalSupportedTextureUnits () const
void dirty ()

Protected Types

typedef std::map< std::string,
std::map< int, TexUnit > > 
TexUnitDb

Protected Member Functions

void addParameter (const std::string &name, osg::Uniform *param, osg::StateAttribute::OverrideValue mode)
void resetTextureUniforms ()
osg::Uniform::Type convertToUniformType (const std::string &name)
 Convert string type name into type.

Protected Attributes

TexUnitDb mTexUnits
 Database to hold the texture to uniform bindings.
osg::StateSet::UniformList mUniforms
 List of all added parameters.
bool mDirtyTextureBindings
 mark if boundings are dirty
int mMaxTextureUnits
 maximal possible number of supported texture units

Data Structures

struct  TexUnit


Detailed Description

Wrapper class to simplify work with shader and uniforms.

ShaderAttribute class is a simple wrapper for the osg::Shader, osg::Program and osg::Uniform classes to simplify the work with shaders. It can be used to define shader programs for the osgPPU::Unit or in external applications as well.

Current version of the class do not support Uniform StateAttribute values - OVERRIDE and PROTECTED. This because the uniforms are applied directly in the apply method of the ShaderAttribute. In order to be able to use it you have to apply the uniforms directly with osg methods to the according StateSet. This wrapper is just a simpler version of osg's uniform handlings.


Constructor & Destructor Documentation

osgPPU::ShaderAttribute::ShaderAttribute (  ) 

Initialize shader to the fixed function pipeline per default

osgPPU::ShaderAttribute::ShaderAttribute ( const ShaderAttribute ,
const osg::CopyOp &  copyop = osg::CopyOp::DEEP_COPY_ALL 
)

Copy constructor to create a shader object from the other one. NOTE: copyop is currently ignored. The uniforms will be copied completely, hence after the copy you will get two uniforms with the same value.

virtual osgPPU::ShaderAttribute::~ShaderAttribute (  )  [virtual]

Release used memory and close all used shader programs


Member Function Documentation

void osgPPU::ShaderAttribute::add ( const std::string &  name,
osg::Uniform::Type  type,
unsigned int  elementCount = 1,
osg::StateAttribute::OverrideValue  mode = osg::StateAttribute::ON 
)

Add new uniform. The uniform can also represent an array. The uniform will be applied in the apply method. Unfortunately only the StateAttribute mode ON/OFF is checked. Hence PROTECTED and OVERRIDE are currently not supported.

Parameters:
name Name of the uniform
type Type of the uniform
elementCount Number of elements if you add an array, otherwise 1

void osgPPU::ShaderAttribute::add ( osg::Uniform *  uniform,
osg::StateAttribute::OverrideValue  mode = osg::StateAttribute::ON 
)

Add new uniform. The uniform value will be copied.

void osgPPU::ShaderAttribute::del ( const std::string &  name  ) 

Delete uniform. Uniforms which are deleted are removed from the parental StateSets too.

Parameters:
name Name of the uniform

bool osgPPU::ShaderAttribute::set ( const std::string &  name,
bool  b0 
)

Set uniform value.

bool osgPPU::ShaderAttribute::bindTexture ( unsigned int  index,
const std::string &  name,
osg::Texture *  tex,
int  unit = -1 
)

Bind a texture to the specified uniform. This method do siplify your life ;-)

Parameters:
index If uniform is an array element, then specify the index here
name Name of the uniform to which to bound the texture
tex Texture to bound
unit Texture unit to which to bound the texture. Specify -1 to automagically choose free texture unit

bool osgPPU::ShaderAttribute::bindAttribute ( const std::string &  name,
unsigned int  index 
)

Bind a vertex attribute to a uniform. You have to take care to deliver correct attribute data for the specified attribute.

See also:
osg::Programm::addBindAttribLocation()
Parameters:
name Name of the uniform variable
index Attribute index which to bound

bool osgPPU::ShaderAttribute::bindFragData ( const std::string &  name,
unsigned int  index 
)

For shader model 4.0 hardwares you can specify the frag data to bound

virtual void osgPPU::ShaderAttribute::apply ( osg::State &  state  )  const [virtual]

Apply the shader attribute to the given state. This will bind the shader program and set the uniforms. NOTE: The uniforms would be bound in this method, hence the appropriate uniform value might be propagated to the shader in the next frame.

virtual int osgPPU::ShaderAttribute::compare ( const osg::StateAttribute &  sa  )  const [virtual]

osg::Uniform* osgPPU::ShaderAttribute::get ( const std::string &  name  ) 

Get uniform by a its name. If uniform was previously added or created this method will return it.

Parameters:
name Name of the uniform

void osgPPU::ShaderAttribute::setUniformList ( const osg::StateSet::UniformList &  list  ) 

Set uniform list.

const osg::StateSet::UniformList& osgPPU::ShaderAttribute::getUniformList (  )  const

Get correpsonding uniform list.

void osgPPU::ShaderAttribute::setMaximalSupportedTextureUnits ( int  i  ) 

Set the number of maximal supported texture units. Per default this number is set to 8. This number is needed to setup automagic texture to uniform binding.

int osgPPU::ShaderAttribute::getMaximalSupportedTextureUnits (  )  const

Get maximum supported texture units. The results is the same as set by setMaximalSupportedTextureUnits() method

void osgPPU::ShaderAttribute::dirty (  ) 

Mark the ShaderAttribute as dirty. This will force to reset all the texture binding to parental StateSets on the next apply method.

See also:
bindTexture()

void osgPPU::ShaderAttribute::addParameter ( const std::string &  name,
osg::Uniform *  param,
osg::StateAttribute::OverrideValue  mode 
) [protected]

Set parameters as uniform values.

void osgPPU::ShaderAttribute::resetTextureUniforms (  )  [protected]

Reset texture bindings. Call this if you have rebound textures and want to force to recreate texture boundings within the shader. Normally this would be called automatically by the update function if you have previously called any bind function


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

Back to Homepage of osgPPU

Copyright (C) 2008 by Art Tevs (LGPL)