Thanks, I will think about moving this to update or cull callbacks.

Wojtek Lewandowski


----- Original Message ----- From: "Robert Osfield" <[EMAIL PROTECTED]>
To: "osg users" <[email protected]>
Sent: Friday, November 17, 2006 12:20 PM
Subject: Re: [osg-users] Applying uniforms in DrawCallbacks. What isrecommended method ?


Hi Wojciech,

You need to use osg::StateSet to set up your uniforms, placing them in
draw callbacks is not the right way to mange OpenGL state with the
OSG.

Uniforms in particular are more critical because of need to know the
current glProgram this is in force to be able to set up the uniform
correctly.  Its not a straight apply like normal OpenGL state.

Robert.

On 11/17/06, Wojciech Lewandowski <[EMAIL PROTECTED]> wrote:


Hi,

We have situation where we want to change uniform values in DrawCallback
before rendering some geometry. It looks as there is no state::applyUniform
method.

We have come up with following code (our code was much more complex this is
simplified example ) :
-----------------------------------------------------------------------------------------------------
class CounterDrawCallback: public Drawable::DrawCallback
{
public:
    CounterDrawCallback( Uniform * uniform )
    {
        _counter = 0.0f;
        _uniform = uniform;
        _stateset = new StateSet;
        _stateset->addUniform( uniform );
    }

    void drawImplementation( State & state, Drawable * drawable )
    {
        _counter += 1.0f;
        _uniform->set( _counter );
        state.apply( _stateset.get() );
        drawable->drawImplementation( state );
    }


protected:
    ref_ptr< Uniform >  _uniform;
    ref_ptr< StateSet > _stateset;
    float                      _counter;
}
-----------------------------------------------------------------------------------------------------
But above code does not work. Uniform is not applied by state.apply(
_stateset.get() ). Is this a bug ? Somehow state:: _lastAppliedProgramObject
variable gets reset in state::apply before appying new uniform value.

Instaed we come up with updated version which applies Program as well:

-----------------------------------------------------------------------------------------------------
class CounterDrawCallback: public Drawable::DrawCallback
{
public:
    CounterDrawCallback( Program * program, Uniform * uniform )
    {
        _counter = 0.0f;
        _uniform = uniform;
        _stateset = new StateSet;
        _stateset->setAttributeAndModes( program,
StateAtribute::ON );
        _stateset->addUniform( uniform );
    }

    void drawImplementation( State & state, Drawable * drawable )
    {
        _counter += 1.0f;
        _uniform->set( _counter );
        state.apply( _stateset.get() );
        drawable->drawImplementation( state );
    }


protected:
    ref_ptr< Uniform >  _uniform;
    ref_ptr< StateSet > _stateset;
    float                      _counter;
}
-----------------------------------------------------------------------------------------------------
This method works but I suspect that is far from optimal. So what is
recomended method to minimize state changes ?


Regards,
Wojtek Lewandowski
_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/


_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/



_______________________________________________
osg-users mailing list
[email protected]
http://openscenegraph.net/mailman/listinfo/osg-users
http://www.openscenegraph.org/

Reply via email to