2015-04-14 17:29:12 +02:00
# include "statesetupdater.hpp"
2015-04-14 15:55:56 +02:00
# include <osg/Node>
2015-06-07 23:51:54 +02:00
# include <osg/NodeVisitor>
2020-05-12 13:37:00 +00:00
# include <osgUtil/CullVisitor>
2015-04-14 15:55:56 +02:00
namespace SceneUtil
{
2015-04-14 17:29:12 +02:00
void StateSetUpdater : : operator ( ) ( osg : : Node * node , osg : : NodeVisitor * nv )
2015-04-14 15:55:56 +02:00
{
2020-05-12 13:37:00 +00:00
bool isCullVisitor = nv - > getVisitorType ( ) = = osg : : NodeVisitor : : CULL_VISITOR ;
2015-04-14 15:55:56 +02:00
if ( ! mStateSets [ 0 ] )
{
2020-05-12 13:37:00 +00:00
for ( int i = 0 ; i < 2 ; + + i )
2015-04-14 15:55:56 +02: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 15:55:56 +02:00
setDefaults ( mStateSets [ i ] ) ;
}
}
2020-06-30 08:59:21 +04:00
osg : : ref_ptr < osg : : StateSet > stateset = mStateSets [ nv - > getTraversalNumber ( ) % 2 ] ;
2015-12-03 23:44:15 +01:00
apply ( stateset , nv ) ;
2015-04-14 15:55:56 +02: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 15:55:56 +02:00
traverse ( node , nv ) ;
2020-05-12 13:37:00 +00:00
if ( isCullVisitor )
static_cast < osgUtil : : CullVisitor * > ( nv ) - > popStateSet ( ) ;
2015-04-14 15:55:56 +02:00
}
2015-06-01 17:02:44 +02:00
void StateSetUpdater : : reset ( )
{
2018-10-09 10:21:12 +04:00
mStateSets [ 0 ] = nullptr ;
mStateSets [ 1 ] = nullptr ;
2015-06-01 17:02:44 +02:00
}
2015-04-14 17:29:12 +02:00
StateSetUpdater : : StateSetUpdater ( )
2015-04-14 16:41:06 +02:00
{
}
2015-04-14 17:29:12 +02:00
StateSetUpdater : : StateSetUpdater ( const StateSetUpdater & copy , const osg : : CopyOp & copyop )
2015-04-14 16:41:06 +02:00
: osg : : NodeCallback ( copy , copyop )
{
}
// ----------------------------------------------------------------------------------
2015-04-14 17:29:12 +02:00
void CompositeStateSetUpdater : : apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv )
2015-04-14 16:41:06 +02:00
{
for ( unsigned int i = 0 ; i < mCtrls . size ( ) ; + + i )
mCtrls [ i ] - > apply ( stateset , nv ) ;
}
2015-04-14 17:29:12 +02:00
void CompositeStateSetUpdater : : setDefaults ( osg : : StateSet * stateset )
2015-04-14 16:41:06 +02:00
{
for ( unsigned int i = 0 ; i < mCtrls . size ( ) ; + + i )
mCtrls [ i ] - > setDefaults ( stateset ) ;
}
2015-04-14 17:29:12 +02:00
CompositeStateSetUpdater : : CompositeStateSetUpdater ( )
2015-04-14 16:41:06 +02:00
{
}
2015-04-14 17:29:12 +02:00
CompositeStateSetUpdater : : CompositeStateSetUpdater ( const CompositeStateSetUpdater & copy , const osg : : CopyOp & copyop )
: StateSetUpdater ( copy , copyop )
2015-04-14 16:41:06 +02:00
{
2015-04-14 17:29:12 +02:00
for ( unsigned int i = 0 ; i < copy . mCtrls . size ( ) ; + + i )
2020-10-17 12:26:35 +04:00
mCtrls . emplace_back ( osg : : clone ( copy . mCtrls [ i ] . get ( ) , copyop ) ) ;
2015-04-14 16:41:06 +02:00
}
2015-04-14 17:29:12 +02:00
unsigned int CompositeStateSetUpdater : : getNumControllers ( )
2015-04-14 16:41:06 +02:00
{
return mCtrls . size ( ) ;
}
2015-04-14 17:29:12 +02:00
StateSetUpdater * CompositeStateSetUpdater : : getController ( int i )
{
return mCtrls [ i ] ;
}
void CompositeStateSetUpdater : : addController ( StateSetUpdater * ctrl )
2015-04-14 16:41:06 +02:00
{
2020-10-17 12:26:35 +04:00
mCtrls . emplace_back ( ctrl ) ;
2015-04-14 16:41:06 +02:00
}
2015-04-14 15:55:56 +02:00
}