2015-04-14 15:29:12 +00:00
# include "statesetupdater.hpp"
2015-04-14 13:55:56 +00:00
# include <osg/Node>
2015-06-07 21:51:54 +00:00
# include <osg/NodeVisitor>
2020-05-12 13:37:00 +00:00
# include <osgUtil/CullVisitor>
2015-04-14 13:55:56 +00:00
namespace SceneUtil
{
2015-04-14 15:29:12 +00:00
void StateSetUpdater : : operator ( ) ( osg : : Node * node , osg : : NodeVisitor * nv )
2015-04-14 13:55:56 +00:00
{
2020-05-12 13:37:00 +00:00
bool isCullVisitor = nv - > getVisitorType ( ) = = osg : : NodeVisitor : : CULL_VISITOR ;
2015-04-14 13:55:56 +00:00
if ( ! mStateSets [ 0 ] )
{
2020-05-12 13:37:00 +00:00
for ( int i = 0 ; i < 2 ; + + i )
2015-04-14 13:55:56 +00:00
{
2020-05-12 13:37:00 +00:00
if ( ! isCullVisitor )
mStateSets [ i ] = new osg : : StateSet ( * node - > getOrCreateStateSet ( ) , osg : : CopyOp : : SHALLOW_COPY ) ; // Using SHALLOW_COPY for StateAttributes, if users want to modify it is their responsibility to set a non-shared one first in setDefaults
else
mStateSets [ i ] = new osg : : StateSet ;
2015-04-14 13:55:56 +00:00
setDefaults ( mStateSets [ i ] ) ;
}
}
2020-06-30 04:59:21 +00:00
osg : : ref_ptr < osg : : StateSet > stateset = mStateSets [ nv - > getTraversalNumber ( ) % 2 ] ;
2015-12-03 22:44:15 +00:00
apply ( stateset , nv ) ;
2015-04-14 13:55:56 +00:00
2020-05-12 13:37:00 +00:00
if ( ! isCullVisitor )
node - > setStateSet ( stateset ) ;
else
static_cast < osgUtil : : CullVisitor * > ( nv ) - > pushStateSet ( stateset ) ;
2015-04-14 13:55:56 +00:00
traverse ( node , nv ) ;
2020-05-12 13:37:00 +00:00
if ( isCullVisitor )
static_cast < osgUtil : : CullVisitor * > ( nv ) - > popStateSet ( ) ;
2015-04-14 13:55:56 +00:00
}
2015-06-01 15:02:44 +00:00
void StateSetUpdater : : reset ( )
{
2018-10-09 06:21:12 +00:00
mStateSets [ 0 ] = nullptr ;
mStateSets [ 1 ] = nullptr ;
2015-06-01 15:02:44 +00:00
}
2015-04-14 15:29:12 +00:00
StateSetUpdater : : StateSetUpdater ( )
2015-04-14 14:41:06 +00:00
{
}
2015-04-14 15:29:12 +00:00
StateSetUpdater : : StateSetUpdater ( const StateSetUpdater & copy , const osg : : CopyOp & copyop )
2015-04-14 14:41:06 +00:00
: osg : : NodeCallback ( copy , copyop )
{
}
// ----------------------------------------------------------------------------------
2015-04-14 15:29:12 +00:00
void CompositeStateSetUpdater : : apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv )
2015-04-14 14:41:06 +00:00
{
for ( unsigned int i = 0 ; i < mCtrls . size ( ) ; + + i )
mCtrls [ i ] - > apply ( stateset , nv ) ;
}
2015-04-14 15:29:12 +00:00
void CompositeStateSetUpdater : : setDefaults ( osg : : StateSet * stateset )
2015-04-14 14:41:06 +00:00
{
for ( unsigned int i = 0 ; i < mCtrls . size ( ) ; + + i )
mCtrls [ i ] - > setDefaults ( stateset ) ;
}
2015-04-14 15:29:12 +00:00
CompositeStateSetUpdater : : CompositeStateSetUpdater ( )
2015-04-14 14:41:06 +00:00
{
}
2015-04-14 15:29:12 +00:00
CompositeStateSetUpdater : : CompositeStateSetUpdater ( const CompositeStateSetUpdater & copy , const osg : : CopyOp & copyop )
: StateSetUpdater ( copy , copyop )
2015-04-14 14:41:06 +00:00
{
2015-04-14 15:29:12 +00:00
for ( unsigned int i = 0 ; i < copy . mCtrls . size ( ) ; + + i )
2016-08-14 14:44:11 +00:00
mCtrls . push_back ( osg : : clone ( copy . mCtrls [ i ] . get ( ) , copyop ) ) ;
2015-04-14 14:41:06 +00:00
}
2015-04-14 15:29:12 +00:00
unsigned int CompositeStateSetUpdater : : getNumControllers ( )
2015-04-14 14:41:06 +00:00
{
return mCtrls . size ( ) ;
}
2015-04-14 15:29:12 +00:00
StateSetUpdater * CompositeStateSetUpdater : : getController ( int i )
{
return mCtrls [ i ] ;
}
void CompositeStateSetUpdater : : addController ( StateSetUpdater * ctrl )
2015-04-14 14:41:06 +00:00
{
mCtrls . push_back ( ctrl ) ;
}
2015-04-14 13:55:56 +00:00
}