osgEarth 2.1.1
Public Member Functions | Protected Attributes

osgEarth::UpdateLightingUniformsHelper Class Reference

List of all members.

Public Member Functions

 UpdateLightingUniformsHelper (bool useUpdateTraversal=false)
 ~UpdateLightingUniformsHelper ()
void cullTraverse (osg::Node *node, osg::NodeVisitor *nv)
void updateTraverse (osg::Node *node)

Protected Attributes

int _maxLights
bool * _lightEnabled
bool _lightingEnabled
bool _dirty
bool _applied
bool _useUpdateTrav
OpenThreads::Mutex _stateSetMutex
osg::ref_ptr< osg::Uniform > _lightingEnabledUniform
osg::ref_ptr< osg::Uniform > _lightEnabledUniform

Detailed Description

A callback that will update the osgEarth lighting uniforms (based on the FFP lighting state) if necessary.

Definition at line 34 of file ShaderUtils.


Constructor & Destructor Documentation

UpdateLightingUniformsHelper::UpdateLightingUniformsHelper ( bool  useUpdateTraversal = false)

Definition at line 58 of file ShaderUtils.cpp.

                                                                               :
_lightingEnabled( true ),
_dirty( true ),
_applied( false ),
_useUpdateTrav( useUpdateTrav )
{
    _maxLights = Registry::instance()->getCapabilities().getMaxLights();

    _lightEnabled = new bool[ _maxLights ];
    if ( _maxLights > 0 )
        _lightEnabled[0] = 1;
    for(int i=1; i<_maxLights; ++i )
        _lightEnabled[i] = 0;

    _lightingEnabledUniform = new osg::Uniform( osg::Uniform::BOOL, "osgearth_LightingEnabled" );
    _lightEnabledUniform    = new osg::Uniform( osg::Uniform::INT,  "osgearth_LightEnabled", _maxLights );

    if ( !_useUpdateTrav )
    {
        // setting the data variance the DYNAMIC makes it safe to change the uniform values
        // during the CULL traversal.
        _lightingEnabledUniform->setDataVariance( osg::Object::DYNAMIC );
        _lightEnabledUniform->setDataVariance( osg::Object::DYNAMIC );
    }
}

Here is the call graph for this function:

UpdateLightingUniformsHelper::~UpdateLightingUniformsHelper ( )

Definition at line 84 of file ShaderUtils.cpp.

{
    delete [] _lightEnabled;
}

Member Function Documentation

void UpdateLightingUniformsHelper::cullTraverse ( osg::Node *  node,
osg::NodeVisitor *  nv 
)

Definition at line 90 of file ShaderUtils.cpp.

{
    osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>( nv );
    if ( cv )
    {
        StateSetStack stateSetStack;

        osgUtil::StateGraph* sg = cv->getCurrentStateGraph();
        while( sg )
        {
            const osg::StateSet* stateset = sg->getStateSet();
            if (stateset)
            {
                stateSetStack.push_front(stateset);
            }                
            sg = sg->_parent;
        }

        // Update the overall lighting-enabled value:
        bool lightingEnabled = 
            ( getModeValue(stateSetStack, GL_LIGHTING) & osg::StateAttribute::ON ) != 0;

        if ( lightingEnabled != _lightingEnabled || !_applied )
        {
            _lightingEnabled = lightingEnabled;
            if ( _useUpdateTrav )
                _dirty = true;
            else
                _lightingEnabledUniform->set( _lightingEnabled );
        }

        // Update the list of enabled lights:
        for( int i=0; i < _maxLights; ++i )
        {
            bool enabled =
                ( getModeValue( stateSetStack, GL_LIGHT0 + i ) & osg::StateAttribute::ON ) != 0;

            if ( _lightEnabled[i] != enabled || !_applied )
            {
                _lightEnabled[i] = enabled;
                if ( _useUpdateTrav )
                    _dirty = true;
                else
                    _lightEnabledUniform->setElement( i, _lightEnabled[i] );
            }
        }       

        // apply if necessary:
        if ( !_applied && !_useUpdateTrav )
        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _stateSetMutex );
            if (!_applied)
            {
                node->getOrCreateStateSet()->addUniform( _lightingEnabledUniform.get() );
                node->getStateSet()->addUniform( _lightEnabledUniform.get() );
                _applied = true;
            }
        }               
    }        
}

Here is the call graph for this function:

Here is the caller graph for this function:

void UpdateLightingUniformsHelper::updateTraverse ( osg::Node *  node)

Definition at line 152 of file ShaderUtils.cpp.

{
    if ( _dirty )
    {
        _lightingEnabledUniform->set( _lightingEnabled );

        for( int i=0; i < _maxLights; ++i )
            _lightEnabledUniform->setElement( i, _lightEnabled[i] );

        _dirty = false;

        if ( !_applied )
        {
            osg::StateSet* stateSet = node->getOrCreateStateSet();
            stateSet->addUniform( _lightingEnabledUniform.get() );
            stateSet->addUniform( _lightEnabledUniform.get() );
        }
    }
}

Member Data Documentation

Definition at line 48 of file ShaderUtils.

Definition at line 47 of file ShaderUtils.

Definition at line 45 of file ShaderUtils.

osg::ref_ptr<osg::Uniform> osgEarth::UpdateLightingUniformsHelper::_lightEnabledUniform [protected]

Definition at line 53 of file ShaderUtils.

Definition at line 46 of file ShaderUtils.

Definition at line 52 of file ShaderUtils.

Definition at line 44 of file ShaderUtils.

Definition at line 50 of file ShaderUtils.

Definition at line 49 of file ShaderUtils.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines