UpdateVisitor 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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. #ifndef OSGUTIL_UPDATEVISITOR
  14. #define OSGUTIL_UPDATEVISITOR 1
  15. #include <osg/NodeVisitor>
  16. #include <osg/Geode>
  17. #include <osg/Billboard>
  18. #include <osg/LOD>
  19. #include <osg/Switch>
  20. #include <osg/LightSource>
  21. #include <osg/Transform>
  22. #include <osg/Projection>
  23. #include <osg/OccluderNode>
  24. #include <osg/ScriptEngine>
  25. #include <osgUtil/Export>
  26. namespace osgUtil {
  27. /**
  28. * Basic UpdateVisitor implementation for animating a scene.
  29. * This visitor traverses the scene graph, calling each nodes appCallback if
  30. * it exists.
  31. */
  32. class OSGUTIL_EXPORT UpdateVisitor : public osg::NodeVisitor
  33. {
  34. public:
  35. UpdateVisitor();
  36. virtual ~UpdateVisitor();
  37. META_NodeVisitor(osgUtil, UpdateVisitor)
  38. /** Convert 'this' into a osgUtil::UpdateVisitor pointer if Object is a osgUtil::UpdateVisitor, otherwise return 0.
  39. * Equivalent to dynamic_cast<osgUtil::UpdateVisitor*>(this).*/
  40. virtual osgUtil::UpdateVisitor* asUpdateVisitor() { return this; }
  41. /** convert 'const this' into a const osgUtil::UpdateVisitor pointer if Object is a osgUtil::UpdateVisitor, otherwise return 0.
  42. * Equivalent to dynamic_cast<const osgUtil::UpdateVisitor*>(this).*/
  43. virtual const osgUtil::UpdateVisitor* asUpdateVisitor() const { return this; }
  44. virtual void reset();
  45. /** During traversal each type of node calls its callbacks and its children traversed. */
  46. virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
  47. virtual void apply(osg::Drawable& drawable)
  48. {
  49. osg::Callback* callback = drawable.getUpdateCallback();
  50. if (callback)
  51. {
  52. osg::DrawableUpdateCallback* drawable_callback = callback->asDrawableUpdateCallback();
  53. osg::NodeCallback* node_callback = callback->asNodeCallback();
  54. if (drawable_callback) drawable_callback->update(this,&drawable);
  55. if (node_callback) (*node_callback)(&drawable, this);
  56. if (!drawable_callback && !node_callback) callback->run(&drawable, this);
  57. }
  58. handle_callbacks(drawable.getStateSet());
  59. }
  60. // The following overrides are technically redundant as the default implementation would eventually trickle down to
  61. // apply(osg::Node&); - however defining these explicitly should save a couple of virtual function calls
  62. virtual void apply(osg::Geode& node) { handle_callbacks_and_traverse(node); }
  63. virtual void apply(osg::Billboard& node) { handle_callbacks_and_traverse(node); }
  64. virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
  65. virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
  66. virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
  67. virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
  68. virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
  69. virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
  70. virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
  71. protected:
  72. // /** Prevent unwanted copy construction.*/
  73. // UpdateVisitor(const UpdateVisitor&):osg::NodeVisitor() {}
  74. /** Prevent unwanted copy operator.*/
  75. UpdateVisitor& operator = (const UpdateVisitor&) { return *this; }
  76. inline void handle_callbacks(osg::StateSet* stateset)
  77. {
  78. if (stateset && stateset->requiresUpdateTraversal())
  79. {
  80. stateset->runUpdateCallbacks(this);
  81. }
  82. }
  83. inline void handle_callbacks_and_traverse(osg::Node& node)
  84. {
  85. handle_callbacks(node.getStateSet());
  86. osg::Callback* callback = node.getUpdateCallback();
  87. if (callback) callback->run(&node,this);
  88. else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node);
  89. }
  90. };
  91. }
  92. #endif