2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2002-12-19 23:55:40 +08:00
|
|
|
#ifndef OSGUTIL_UPDATEVISITOR
|
|
|
|
#define OSGUTIL_UPDATEVISITOR 1
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/Node>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Billboard>
|
|
|
|
#include <osg/LOD>
|
|
|
|
#include <osg/Switch>
|
|
|
|
#include <osg/LightSource>
|
|
|
|
#include <osg/Transform>
|
2002-04-15 21:15:48 +08:00
|
|
|
#include <osg/Projection>
|
2001-09-20 07:41:39 +08:00
|
|
|
#include <osg/Impostor>
|
2002-06-12 02:41:57 +08:00
|
|
|
#include <osg/OccluderNode>
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
#include <osgUtil/Export>
|
|
|
|
|
|
|
|
namespace osgUtil {
|
|
|
|
|
|
|
|
/**
|
2002-12-19 23:55:40 +08:00
|
|
|
* Basic UpdateVisitor implementation for animating a scene.
|
2001-09-20 07:41:39 +08:00
|
|
|
* This visitor traverses the scene graph, call each nodes appCallback if
|
|
|
|
* it exists.
|
|
|
|
*/
|
2002-12-19 23:55:40 +08:00
|
|
|
class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor
|
2001-09-20 07:41:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2002-12-19 23:55:40 +08:00
|
|
|
UpdateVisitor();
|
|
|
|
virtual ~UpdateVisitor();
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
virtual void reset();
|
|
|
|
|
2002-06-12 02:41:57 +08:00
|
|
|
virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
|
2001-09-20 07:41:39 +08:00
|
|
|
|
2002-07-10 23:35:47 +08:00
|
|
|
virtual void apply(osg::Geode& node) { handle_geode_callbacks(node); }
|
|
|
|
virtual void apply(osg::Billboard& node) { handle_geode_callbacks(node); }
|
|
|
|
|
2002-06-12 02:41:57 +08:00
|
|
|
virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
|
2001-09-20 07:41:39 +08:00
|
|
|
|
2002-06-12 02:41:57 +08:00
|
|
|
virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::Impostor& node) { handle_callbacks_and_traverse(node); }
|
|
|
|
virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/** prevent unwanted copy construction.*/
|
2002-12-19 23:55:40 +08:00
|
|
|
UpdateVisitor(const UpdateVisitor&):osg::NodeVisitor() {}
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
/** prevent unwanted copy operator.*/
|
2002-12-19 23:55:40 +08:00
|
|
|
UpdateVisitor& operator = (const UpdateVisitor&) { return *this; }
|
2001-09-20 07:41:39 +08:00
|
|
|
|
2002-04-15 21:15:48 +08:00
|
|
|
inline void handle_callbacks_and_traverse(osg::Node& node)
|
2001-09-20 07:41:39 +08:00
|
|
|
{
|
2002-12-19 23:55:40 +08:00
|
|
|
osg::NodeCallback* callback = node.getUpdateCallback();
|
2001-09-20 07:41:39 +08:00
|
|
|
if (callback) (*callback)(&node,this);
|
2002-12-19 23:55:40 +08:00
|
|
|
else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node);
|
2001-09-20 07:41:39 +08:00
|
|
|
}
|
|
|
|
|
2002-07-10 23:35:47 +08:00
|
|
|
inline void handle_geode_callbacks(osg::Geode& node)
|
|
|
|
{
|
2002-12-19 23:55:40 +08:00
|
|
|
osg::NodeCallback* callback = node.getUpdateCallback();
|
2002-07-10 23:35:47 +08:00
|
|
|
if (callback) (*callback)(&node,this);
|
2003-03-22 17:48:43 +08:00
|
|
|
else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverseGeode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void traverseGeode(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
traverse((osg::Node&)geode);
|
2002-07-10 23:35:47 +08:00
|
|
|
|
|
|
|
// call the app callbacks on the drawables.
|
2003-03-22 17:48:43 +08:00
|
|
|
for(unsigned int i=0;i<geode.getNumDrawables();++i)
|
2002-07-10 23:35:47 +08:00
|
|
|
{
|
2003-03-22 17:48:43 +08:00
|
|
|
osg::Drawable::UpdateCallback* callback = geode.getDrawable(i)->getUpdateCallback();
|
|
|
|
if (callback) callback->update(this,geode.getDrawable(i));
|
2002-07-10 23:35:47 +08:00
|
|
|
}
|
|
|
|
}
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-09-20 07:41:39 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|