| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield |
|---|
| 2 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 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. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGUTIL_UPDATEVISITOR |
|---|
| 15 | #define OSGUTIL_UPDATEVISITOR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/NodeVisitor> |
|---|
| 18 | #include <osg/Node> |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <osg/Billboard> |
|---|
| 21 | #include <osg/LOD> |
|---|
| 22 | #include <osg/Switch> |
|---|
| 23 | #include <osg/LightSource> |
|---|
| 24 | #include <osg/Transform> |
|---|
| 25 | #include <osg/Projection> |
|---|
| 26 | #include <osg/Impostor> |
|---|
| 27 | #include <osg/OccluderNode> |
|---|
| 28 | |
|---|
| 29 | #include <osgUtil/Export> |
|---|
| 30 | |
|---|
| 31 | namespace osgUtil { |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Basic UpdateVisitor implementation for animating a scene. |
|---|
| 35 | * This visitor traverses the scene graph, calling each nodes appCallback if |
|---|
| 36 | * it exists. |
|---|
| 37 | */ |
|---|
| 38 | class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor |
|---|
| 39 | { |
|---|
| 40 | public: |
|---|
| 41 | |
|---|
| 42 | UpdateVisitor(); |
|---|
| 43 | virtual ~UpdateVisitor(); |
|---|
| 44 | |
|---|
| 45 | virtual void reset(); |
|---|
| 46 | |
|---|
| 47 | /** During traversal each type of node calls its callbacks and its children traversed. */ |
|---|
| 48 | virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); } |
|---|
| 49 | |
|---|
| 50 | virtual void apply(osg::Geode& node) { handle_geode_callbacks(node); } |
|---|
| 51 | virtual void apply(osg::Billboard& node) { handle_geode_callbacks(node); } |
|---|
| 52 | |
|---|
| 53 | virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); } |
|---|
| 54 | |
|---|
| 55 | virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); } |
|---|
| 56 | virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); } |
|---|
| 57 | virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); } |
|---|
| 58 | virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); } |
|---|
| 59 | virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); } |
|---|
| 60 | virtual void apply(osg::Impostor& node) { handle_callbacks_and_traverse(node); } |
|---|
| 61 | virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | protected: |
|---|
| 65 | |
|---|
| 66 | // /** Prevent unwanted copy construction.*/ |
|---|
| 67 | // UpdateVisitor(const UpdateVisitor&):osg::NodeVisitor() {} |
|---|
| 68 | |
|---|
| 69 | /** Prevent unwanted copy operator.*/ |
|---|
| 70 | UpdateVisitor& operator = (const UpdateVisitor&) { return *this; } |
|---|
| 71 | |
|---|
| 72 | inline void handle_callbacks_and_traverse(osg::Node& node) |
|---|
| 73 | { |
|---|
| 74 | osg::NodeCallback* callback = node.getUpdateCallback(); |
|---|
| 75 | if (callback) (*callback)(&node,this); |
|---|
| 76 | else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | inline void handle_geode_callbacks(osg::Geode& node) |
|---|
| 80 | { |
|---|
| 81 | osg::NodeCallback* callback = node.getUpdateCallback(); |
|---|
| 82 | if (callback) (*callback)(&node,this); |
|---|
| 83 | /*else if (node.getNumChildrenRequiringUpdateTraversal()>0)*/ |
|---|
| 84 | traverseGeode(node); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | inline void traverseGeode(osg::Geode& geode) |
|---|
| 88 | { |
|---|
| 89 | traverse((osg::Node&)geode); |
|---|
| 90 | |
|---|
| 91 | // Call the app callbacks on the drawables. |
|---|
| 92 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 93 | { |
|---|
| 94 | osg::Drawable::UpdateCallback* callback = geode.getDrawable(i)->getUpdateCallback(); |
|---|
| 95 | if (callback) callback->update(this,geode.getDrawable(i)); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | #endif |
|---|
| 104 | |
|---|