2015-04-14 13:55:56 +00:00
# ifndef OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
# define OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
# include <osg/NodeCallback>
namespace SceneUtil
{
2015-09-21 15:58:57 +00:00
/// @brief Implements efficient per-frame updating of StateSets.
2015-04-14 13:55:56 +00:00
/// @par With a naive update there would be race conditions when the OSG draw thread of the last frame
/// queues up a StateSet that we want to modify for the next frame. To solve this we could set the StateSet to
/// DYNAMIC data variance but that would undo all the benefits of the threading model - having the cull and draw
/// traversals run in parallel can yield up to 200% framerates.
/// @par Race conditions are prevented using a "double buffering" scheme - we have two StateSets that take turns,
2015-12-03 22:44:15 +00:00
/// one StateSet we can write to, the second one is currently in use by the draw traversal of the last frame.
2020-05-12 13:37:00 +00:00
/// @par Must be set as UpdateCallback or CullCallback on a Node. If set as a CullCallback, the StateSetUpdater operates on an empty StateSet, otherwise it operates on a clone of the node's existing StateSet.
2015-11-02 22:38:34 +00:00
/// @note Do not add the same StateSetUpdater to multiple nodes.
2015-04-14 15:29:12 +00:00
/// @note Do not add multiple StateSetControllers on the same Node as they will conflict - instead use the CompositeStateSetUpdater.
class StateSetUpdater : public osg : : NodeCallback
2015-04-14 13:55:56 +00:00
{
public :
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
2015-04-14 15:29:12 +00:00
META_Object ( SceneUtil , StateSetUpdater )
2015-04-14 14:41:06 +00:00
2020-10-16 18:18:54 +00:00
void operator ( ) ( osg : : Node * node , osg : : NodeVisitor * nv ) override ;
2015-04-14 13:55:56 +00:00
/// Apply state - to override in derived classes
/// @note Due to the double buffering approach you *have* to apply all state
/// even if it has not changed since the last frame.
2015-04-14 14:41:06 +00:00
virtual void apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv ) { }
2015-04-14 13:55:56 +00:00
/// Set default state - optionally override in derived classes
/// @par May be used e.g. to allocate StateAttributes.
virtual void setDefaults ( osg : : StateSet * stateset ) { }
2015-06-01 15:02:44 +00:00
protected :
/// Reset mStateSets, forcing a setDefaults() on the next frame. Can be used to change the defaults if needed.
void reset ( ) ;
2015-04-14 13:55:56 +00:00
private :
osg : : ref_ptr < osg : : StateSet > mStateSets [ 2 ] ;
} ;
2015-04-14 14:41:06 +00:00
/// @brief A variant of the StateSetController that can be made up of multiple controllers all controlling the same target.
2015-04-14 15:29:12 +00:00
class CompositeStateSetUpdater : public StateSetUpdater
2015-04-14 14:41:06 +00:00
{
public :
2015-04-14 15:29:12 +00:00
CompositeStateSetUpdater ( ) ;
CompositeStateSetUpdater ( const CompositeStateSetUpdater & copy , const osg : : CopyOp & copyop ) ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
META_Object ( SceneUtil , CompositeStateSetUpdater )
2015-04-14 14:41:06 +00:00
unsigned int getNumControllers ( ) ;
2015-04-14 15:29:12 +00:00
StateSetUpdater * getController ( int i ) ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
void addController ( StateSetUpdater * ctrl ) ;
2015-04-14 14:41:06 +00:00
2020-10-16 18:18:54 +00:00
void apply ( osg : : StateSet * stateset , osg : : NodeVisitor * nv ) override ;
2015-04-14 14:41:06 +00:00
protected :
2020-10-16 18:18:54 +00:00
void setDefaults ( osg : : StateSet * stateset ) override ;
2015-04-14 14:41:06 +00:00
2015-04-14 15:29:12 +00:00
std : : vector < osg : : ref_ptr < StateSetUpdater > > mCtrls ;
2015-04-14 14:41:06 +00:00
} ;
2015-04-14 13:55:56 +00:00
}
# endif