From bc83e63bb492337890a22fe93882f75b53683139 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 25 Apr 2005 11:05:02 +0000 Subject: [PATCH] Futher work on adding event and update callbacks to StateSet, Uniform and StateAttributes --- include/osg/StateAttribute | 2 +- include/osg/StateSet | 28 ++++++++--- include/osg/Uniform | 2 +- include/osgGA/EventVisitor | 14 ++++++ include/osgUtil/UpdateVisitor | 36 +++++++++----- src/osg/Drawable.cpp | 47 ++++++++++++++++-- src/osg/Node.cpp | 32 +++++++++++-- src/osg/StateSet.cpp | 89 +++++++++++++++++++++++++++++++++++ 8 files changed, 220 insertions(+), 30 deletions(-) diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index 810870b99..a07f50053 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -278,7 +278,7 @@ class OSG_EXPORT StateAttribute : public Object META_Object(osg,Callback); /** do customized update code.*/ - virtual void operator () (NodeVisitor*, StateAttribute*) {} + virtual void operator () (StateAttribute*, NodeVisitor*) {} }; /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/ diff --git a/include/osg/StateSet b/include/osg/StateSet index bcfd722e3..847ee1092 100644 --- a/include/osg/StateSet +++ b/include/osg/StateSet @@ -392,7 +392,7 @@ class OSG_EXPORT StateSet : public Object META_Object(osg,Callback); /** do customized callback code.*/ - virtual void operator() (NodeVisitor*, StateSet*) {} + virtual void operator() (StateSet*, NodeVisitor*) {} }; /** Set the Update Callback which allows users to attach customize the updating of an object during the update traversal.*/ @@ -404,9 +404,15 @@ class OSG_EXPORT StateSet : public Object /** Get the const Update Callback.*/ const Callback* getUpdateCallback() const { return _updateCallback.get(); } + /** Return whether this StateSet has update callbacks associated with it, and therefore must be traversed.*/ + bool requiresUpdateTraversal() const { return _updateCallback.valid() || getNumChildrenRequiringUpdateTraversal()!=0; } + /** Get the number of Objects of this StateSet which require Update traversal, * since they have an Update Callback attached to them or their children.*/ - inline unsigned int getNumObjectsRequiringUpdateTraversal() const { return _numObjectsRequiringUpdateTraversal; } + inline unsigned int getNumChildrenRequiringUpdateTraversal() const { return _numChildrenRequiringUpdateTraversal; } + + /** Run the update callbacks attached directly to this StateSet or to its children.*/ + void runUpdateCallbacks(osg::NodeVisitor* nv); /** Set the Event Callback which allows users to attach customize the updating of an object during the event traversal.*/ @@ -418,9 +424,15 @@ class OSG_EXPORT StateSet : public Object /** Get the const Event Callback.*/ const Callback* getEventCallback() const { return _eventCallback.get(); } + /** Return whether this StateSet has event callbacks associated with it, and therefore must be traversed.*/ + bool requiresEventTraversal() const { return _eventCallback.valid() || getNumChildrenRequiringEventTraversal()!=0; } + /** Get the number of Objects of this StateSet which require Event traversal, * since they have an Eevnt Callback attached to them or their children.*/ - inline unsigned int getNumObjectsRequiringEventTraversal() const { return _numObjectsRequiringEventTraversal; } + inline unsigned int getNumChildrenRequiringEventTraversal() const { return _numChildrenRequiringEventTraversal; } + + /** Run the event callbacks attached directly to this StateSet or to its children.*/ + void runEventCallbacks(osg::NodeVisitor* nv); /** call compile on all StateAttributes contained within this StateSet.*/ @@ -483,11 +495,13 @@ class OSG_EXPORT StateSet : public Object int _binNum; std::string _binName; - ref_ptr _updateCallback; - unsigned int _numObjectsRequiringUpdateTraversal; + ref_ptr _updateCallback; + unsigned int _numChildrenRequiringUpdateTraversal; + void setNumChildrenRequiringUpdateTraversal(unsigned int num); - ref_ptr _eventCallback; - unsigned int _numObjectsRequiringEventTraversal; + ref_ptr _eventCallback; + unsigned int _numChildrenRequiringEventTraversal; + void setNumChildrenRequiringEventTraversal(unsigned int num); }; diff --git a/include/osg/Uniform b/include/osg/Uniform index dd52d86f0..e1a16beff 100644 --- a/include/osg/Uniform +++ b/include/osg/Uniform @@ -276,7 +276,7 @@ class OSG_EXPORT Uniform : public Object META_Object(osg,Callback); /** do customized update code.*/ - virtual void operator () (NodeVisitor*, Uniform*) {} + virtual void operator () (Uniform*, NodeVisitor*) {} }; /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/ diff --git a/include/osgGA/EventVisitor b/include/osgGA/EventVisitor index 761087a9b..3586f23d8 100644 --- a/include/osgGA/EventVisitor +++ b/include/osgGA/EventVisitor @@ -93,8 +93,18 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor /** Prevent unwanted copy operator.*/ EventVisitor& operator = (const EventVisitor&) { return *this; } + inline void handle_callbacks(osg::StateSet* stateset) + { + if (stateset && stateset->requiresEventTraversal()) + { + stateset->runEventCallbacks(this); + } + } + inline void handle_callbacks_and_traverse(osg::Node& node) { + handle_callbacks(node.getStateSet()); + osg::NodeCallback* callback = node.getEventCallback(); if (callback) (*callback)(&node,this); else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node); @@ -102,6 +112,8 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor inline void handle_geode_callbacks(osg::Geode& node) { + handle_callbacks(node.getStateSet()); + osg::NodeCallback* callback = node.getEventCallback(); if (callback) (*callback)(&node,this); /*else if (node.getNumChildrenRequiringEventTraversal()>0)*/ @@ -117,6 +129,8 @@ class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor { osg::Drawable::EventCallback* callback = geode.getDrawable(i)->getEventCallback(); if (callback) callback->event(this,geode.getDrawable(i)); + + handle_callbacks(geode.getDrawable(i)->getStateSet()); } } diff --git a/include/osgUtil/UpdateVisitor b/include/osgUtil/UpdateVisitor index 6e4f1b2a5..e7c04940e 100644 --- a/include/osgUtil/UpdateVisitor +++ b/include/osgUtil/UpdateVisitor @@ -69,33 +69,43 @@ class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor /** Prevent unwanted copy operator.*/ UpdateVisitor& operator = (const UpdateVisitor&) { return *this; } + inline void handle_callbacks(osg::StateSet* stateset) + { + if (stateset && stateset->requiresUpdateTraversal()) + { + stateset->runUpdateCallbacks(this); + } + } + inline void handle_callbacks_and_traverse(osg::Node& node) { + handle_callbacks(node.getStateSet()); + osg::NodeCallback* callback = node.getUpdateCallback(); if (callback) (*callback)(&node,this); else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node); } - inline void handle_geode_callbacks(osg::Geode& node) + inline void handle_geode_callbacks(osg::Geode& geode) { - osg::NodeCallback* callback = node.getUpdateCallback(); - if (callback) (*callback)(&node,this); - /*else if (node.getNumChildrenRequiringUpdateTraversal()>0)*/ - traverseGeode(node); - } - - inline void traverseGeode(osg::Geode& geode) - { - traverse((osg::Node&)geode); - + handle_callbacks(geode.getStateSet()); + + osg::NodeCallback* callback = geode.getUpdateCallback(); + if (callback) (*callback)(&geode,this); + // Call the app callbacks on the drawables. for(unsigned int i=0;igetUpdateCallback(); if (callback) callback->update(this,geode.getDrawable(i)); + + handle_callbacks(geode.getDrawable(i)->getStateSet()); } - } - + + // should we traverse just in case a subclass of Geode adds children?? Won't for now as + // Geode's arn't designed to have children. + // traverse(geode); + } }; } diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index 4f997d424..4a5df30d5 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -312,14 +312,53 @@ void Drawable::setStateSet(osg::StateSet* stateset) // do nothing if nothing changed. if (_stateset==stateset) return; + // track whether we need to account for the need to do a update or event traversal. + int delta_update = 0; + int delta_event = 0; + // remove this node from the current statesets parent list - if (_stateset.valid()) _stateset->removeParent(this); + if (_stateset.valid()) + { + _stateset->removeParent(this); + if (_stateset->requiresUpdateTraversal()) --delta_update; + if (_stateset->requiresEventTraversal()) --delta_event; + } // set the stateset. _stateset = stateset; // add this node to the new stateset to the parent list. - if (_stateset.valid()) _stateset->addParent(this); + if (_stateset.valid()) + { + _stateset->addParent(this); + if (_stateset->requiresUpdateTraversal()) ++delta_update; + if (_stateset->requiresEventTraversal()) ++delta_event; + } + + + // only inform parents if change occurs and drawable doesn't already have an update callback + if (delta_update!=0 && !_updateCallback) + { + for(ParentList::iterator itr=_parents.begin(); + itr!=_parents.end(); + ++itr) + { + (*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta_update); + } + } + + // only inform parents if change occurs and drawable doesn't already have an event callback + if (delta_event!=0 && !_eventCallback) + { + for(ParentList::iterator itr=_parents.begin(); + itr!=_parents.end(); + ++itr) + { + (*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta_event); + } + } + + } osg::StateSet* Drawable::getOrCreateStateSet() @@ -502,7 +541,7 @@ void Drawable::setUpdateCallback(UpdateCallback* ac) _updateCallback = ac; - if (delta!=0) + if (delta!=0 && !(_stateset.valid() && _stateset->requiresUpdateTraversal())) { for(ParentList::iterator itr=_parents.begin(); itr!=_parents.end(); @@ -523,7 +562,7 @@ void Drawable::setEventCallback(EventCallback* ac) _eventCallback = ac; - if (delta!=0) + if (delta!=0 && !(_stateset.valid() && _stateset->requiresEventTraversal())) { for(ParentList::iterator itr=_parents.begin(); itr!=_parents.end(); diff --git a/src/osg/Node.cpp b/src/osg/Node.cpp index 5b5e04011..437df5bb5 100644 --- a/src/osg/Node.cpp +++ b/src/osg/Node.cpp @@ -91,14 +91,38 @@ void Node::setStateSet(osg::StateSet* stateset) // do nothing if nothing changed. if (_stateset==stateset) return; + // track whether we need to account for the need to do a update or event traversal. + int delta_update = 0; + int delta_event = 0; + // remove this node from the current statesets parent list - if (_stateset.valid()) _stateset->removeParent(this); + if (_stateset.valid()) + { + _stateset->removeParent(this); + if (_stateset->requiresUpdateTraversal()) --delta_update; + if (_stateset->requiresEventTraversal()) --delta_event; + } // set the stateset. _stateset = stateset; // add this node to the new stateset to the parent list. - if (_stateset.valid()) _stateset->addParent(this); + if (_stateset.valid()) + { + _stateset->addParent(this); + if (_stateset->requiresUpdateTraversal()) ++delta_update; + if (_stateset->requiresEventTraversal()) ++delta_event; + } + + if (delta_update!=0) + { + setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+delta_update); + } + + if (delta_event!=0) + { + setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+delta_event); + } } osg::StateSet* Node::getOrCreateStateSet() @@ -113,7 +137,7 @@ void Node::setUpdateCallback(NodeCallback* nc) // if no changes just return. if (_updateCallback==nc) return; - // app callback has been changed, will need to update + // updated callback has been changed, will need to update // both _updateCallback and possibly the numChildrenRequiringAppTraversal // if the number of callbacks changes. @@ -191,7 +215,7 @@ void Node::setEventCallback(NodeCallback* nc) // if no changes just return. if (_eventCallback==nc) return; - // app callback has been changed, will need to Event + // event callback has been changed, will need to Event // both _EventCallback and possibly the numChildrenRequiringAppTraversal // if the number of callbacks changes. diff --git a/src/osg/StateSet.cpp b/src/osg/StateSet.cpp index 79d2b2a46..3f7924c98 100644 --- a/src/osg/StateSet.cpp +++ b/src/osg/StateSet.cpp @@ -73,6 +73,9 @@ StateSet::StateSet() _renderingHint = DEFAULT_BIN; + _numChildrenRequiringUpdateTraversal = 0; + _numChildrenRequiringEventTraversal = 0; + setRenderBinToInherit(); } @@ -141,6 +144,10 @@ StateSet::StateSet(const StateSet& rhs,const CopyOp& copyop):Object(rhs,copyop) _binMode = rhs._binMode; _binNum = rhs._binNum; _binName = rhs._binName; + + _numChildrenRequiringUpdateTraversal = rhs._numChildrenRequiringUpdateTraversal; + _numChildrenRequiringEventTraversal = rhs._numChildrenRequiringEventTraversal; + } StateSet::~StateSet() @@ -1243,6 +1250,47 @@ void StateSet::setUpdateCallback(Callback* ac) } } +void StateSet::runUpdateCallbacks(osg::NodeVisitor* nv) +{ + if (_updateCallback.valid()) (*_updateCallback)(this,nv); + + if (_numChildrenRequiringUpdateTraversal!=0) + { + // run attribute callbacks + for(AttributeList::iterator itr=_attributeList.begin(); + itr!=_attributeList.end(); + ++itr) + { + StateAttribute::Callback* callback = itr->second.first->getUpdateCallback(); + if (callback) (*callback)(itr->second.first.get(),nv); + } + + + // run texture attribute callbacks. + for(unsigned int i=0;i<_textureAttributeList.size();++i) + { + AttributeList& attributeList = _textureAttributeList[i]; + for(AttributeList::iterator itr=attributeList.begin(); + itr!=attributeList.end(); + ++itr) + { + StateAttribute::Callback* callback = itr->second.first->getUpdateCallback(); + if (callback) (*callback)(itr->second.first.get(),nv); + } + } + + + // run uniform callbacks. + for(UniformList::iterator uitr = _uniformList.begin(); + uitr != _uniformList.end(); + ++uitr) + { + Uniform::Callback* callback = uitr->second.first->getUpdateCallback(); + if (callback) (*callback)(uitr->second.first.get(),nv); + } + } +} + void StateSet::setEventCallback(Callback* ac) { if (_eventCallback==ac) return; @@ -1265,3 +1313,44 @@ void StateSet::setEventCallback(Callback* ac) #endif } } + +void StateSet::runEventCallbacks(osg::NodeVisitor* nv) +{ + if (_eventCallback.valid()) (*_eventCallback)(this,nv); + + if (_numChildrenRequiringEventTraversal!=0) + { + // run attribute callbacks + for(AttributeList::iterator itr=_attributeList.begin(); + itr!=_attributeList.end(); + ++itr) + { + StateAttribute::Callback* callback = itr->second.first->getEventCallback(); + if (callback) (*callback)(itr->second.first.get(),nv); + } + + + // run texture attribute callbacks. + for(unsigned int i=0;i<_textureAttributeList.size();++i) + { + AttributeList& attributeList = _textureAttributeList[i]; + for(AttributeList::iterator itr=attributeList.begin(); + itr!=attributeList.end(); + ++itr) + { + StateAttribute::Callback* callback = itr->second.first->getEventCallback(); + if (callback) (*callback)(itr->second.first.get(),nv); + } + } + + + // run uniform callbacks. + for(UniformList::iterator uitr = _uniformList.begin(); + uitr != _uniformList.end(); + ++uitr) + { + Uniform::Callback* callback = uitr->second.first->getEventCallback(); + if (callback) (*callback)(uitr->second.first.get(),nv); + } + } +}