1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGUTIL_UPDATEVISITOR
15#define OSGUTIL_UPDATEVISITOR 1
17#include <osg/NodeVisitor>
19#include <osg/Billboard>
22#include <osg/LightSource>
23#include <osg/Transform>
24#include <osg/Projection>
25#include <osg/OccluderNode>
26#include <osg/ScriptEngine>
28#include <osgUtil/Export>
33 * Basic UpdateVisitor implementation for animating a scene.
34 * This visitor traverses the scene graph, calling each nodes appCallback if
37class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor
42 virtual ~UpdateVisitor();
44 META_NodeVisitor(osgUtil, UpdateVisitor)
46 /** Convert 'this' into a osgUtil::UpdateVisitor pointer if Object is a osgUtil::UpdateVisitor, otherwise return 0.
47 * Equivalent to dynamic_cast<osgUtil::UpdateVisitor*>(this).*/
48 virtual osgUtil::UpdateVisitor* asUpdateVisitor() { return this; }
50 /** convert 'const this' into a const osgUtil::UpdateVisitor pointer if Object is a osgUtil::UpdateVisitor, otherwise return 0.
51 * Equivalent to dynamic_cast<const osgUtil::UpdateVisitor*>(this).*/
52 virtual const osgUtil::UpdateVisitor* asUpdateVisitor() const { return this; }
56 /** During traversal each type of node calls its callbacks and its children traversed. */
57 virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
59 virtual void apply(osg::Drawable& drawable)
61 osg::Callback* callback = drawable.getUpdateCallback();
64 osg::DrawableUpdateCallback* drawable_callback = callback->asDrawableUpdateCallback();
65 osg::NodeCallback* node_callback = callback->asNodeCallback();
67 if (drawable_callback) drawable_callback->update(this,&drawable);
68 if (node_callback) (*node_callback)(&drawable, this);
70 if (!drawable_callback && !node_callback) callback->run(&drawable, this);
73 handle_callbacks(drawable.getStateSet());
76 // The following overrides are technically redundant as the default implementation would eventually trickle down to
77 // apply(osg::Node&); - however defining these explicitly should save a couple of virtual function calls
78 virtual void apply(osg::Geode& node) { handle_callbacks_and_traverse(node); }
79 virtual void apply(osg::Billboard& node) { handle_callbacks_and_traverse(node); }
80 virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
81 virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
82 virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
83 virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
84 virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
85 virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
86 virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
91// /** Prevent unwanted copy construction.*/
92// UpdateVisitor(const UpdateVisitor&):osg::NodeVisitor() {}
94 /** Prevent unwanted copy operator.*/
95 UpdateVisitor& operator = (const UpdateVisitor&) { return *this; }
97 inline void handle_callbacks(osg::StateSet* stateset)
99 if (stateset && stateset->requiresUpdateTraversal())
101 stateset->runUpdateCallbacks(this);
105 inline void handle_callbacks_and_traverse(osg::Node& node)
107 handle_callbacks(node.getStateSet());
109 osg::Callback* callback = node.getUpdateCallback();
110 if (callback) callback->run(&node,this);
111 else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node);