1 #ifndef OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
2 #define OPENMW_COMPONENTS_SCENEUTIL_STATESETCONTROLLER_H
3 
4 #include <osg/NodeCallback>
5 
6 namespace SceneUtil
7 {
8 
9     /// @brief Implements efficient per-frame updating of StateSets.
10     /// @par With a naive update there would be race conditions when the OSG draw thread of the last frame
11     ///     queues up a StateSet that we want to modify for the next frame. To solve this we could set the StateSet to
12     ///     DYNAMIC data variance but that would undo all the benefits of the threading model - having the cull and draw
13     ///     traversals run in parallel can yield up to 200% framerates.
14     /// @par Race conditions are prevented using a "double buffering" scheme - we have two StateSets that take turns,
15     ///     one StateSet we can write to, the second one is currently in use by the draw traversal of the last frame.
16     /// @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.
17     /// @note Do not add the same StateSetUpdater to multiple nodes.
18     /// @note Do not add multiple StateSetControllers on the same Node as they will conflict - instead use the CompositeStateSetUpdater.
19     class StateSetUpdater : public osg::NodeCallback
20     {
21     public:
22         StateSetUpdater();
23         StateSetUpdater(const StateSetUpdater& copy, const osg::CopyOp& copyop);
24 
25         META_Object(SceneUtil, StateSetUpdater)
26 
27         void operator()(osg::Node* node, osg::NodeVisitor* nv) override;
28 
29         /// Apply state - to override in derived classes
30         /// @note Due to the double buffering approach you *have* to apply all state
31         /// even if it has not changed since the last frame.
apply(osg::StateSet * stateset,osg::NodeVisitor * nv)32         virtual void apply(osg::StateSet* stateset, osg::NodeVisitor* nv) {}
33 
34         /// Set default state - optionally override in derived classes
35         /// @par May be used e.g. to allocate StateAttributes.
setDefaults(osg::StateSet * stateset)36         virtual void setDefaults(osg::StateSet* stateset) {}
37 
38     protected:
39         /// Reset mStateSets, forcing a setDefaults() on the next frame. Can be used to change the defaults if needed.
40         void reset();
41 
42     private:
43         osg::ref_ptr<osg::StateSet> mStateSets[2];
44     };
45 
46     /// @brief A variant of the StateSetController that can be made up of multiple controllers all controlling the same target.
47     class CompositeStateSetUpdater : public StateSetUpdater
48     {
49     public:
50         CompositeStateSetUpdater();
51         CompositeStateSetUpdater(const CompositeStateSetUpdater& copy, const osg::CopyOp& copyop);
52 
53         META_Object(SceneUtil, CompositeStateSetUpdater)
54 
55         unsigned int getNumControllers();
56         StateSetUpdater* getController(int i);
57 
58         void addController(StateSetUpdater* ctrl);
59 
60         void apply(osg::StateSet* stateset, osg::NodeVisitor* nv) override;
61 
62     protected:
63 
64         void setDefaults(osg::StateSet *stateset) override;
65 
66         std::vector<osg::ref_ptr<StateSetUpdater> > mCtrls;
67     };
68 
69 }
70 
71 #endif
72