Geode 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 OSG_GEODE
  14. #define OSG_GEODE 1
  15. #include <osg/Node>
  16. #include <osg/NodeVisitor>
  17. #include <osg/Drawable>
  18. namespace osg {
  19. /** A \c Geode is a "geometry node", that is, a leaf node on the scene graph
  20. * that can have "renderable things" attached to it. In OSG, renderable things
  21. * are represented by objects from the \c Drawable class, so a \c Geode is a
  22. * \c Node whose purpose is grouping <tt>Drawable</tt>s.
  23. */
  24. class OSG_EXPORT Geode : public Group
  25. {
  26. public:
  27. Geode();
  28. /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
  29. Geode(const Geode&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  30. META_Node(osg, Geode);
  31. virtual Geode* asGeode() { return this; }
  32. virtual const Geode* asGeode() const { return this; }
  33. /** Add a \c Drawable to the \c Geode.
  34. * If \c drawable is not \c NULL and is not contained in the \c Geode
  35. * then increment its reference count, add it to the drawables list and
  36. * dirty the bounding sphere to force it to be recomputed on the next
  37. * call to \c getBound().
  38. * @param drawable The \c Drawable to be added to the \c Geode.
  39. * @return \c true for success; \c false otherwise.
  40. */
  41. virtual bool addDrawable( Drawable *drawable );
  42. template<class T> bool addDrawable( const ref_ptr<T>& drawable ) { return addDrawable(drawable.get()); }
  43. /** Remove a \c Drawable from the \c Geode.
  44. * Equivalent to <tt>removeDrawable(getDrawableIndex(drawable)</tt>.
  45. * @param drawable The drawable to be removed.
  46. * @return \c true if at least one \c Drawable was removed. \c false
  47. * otherwise.
  48. */
  49. virtual bool removeDrawable( Drawable *drawable );
  50. template<class T> bool removeDrawable( const ref_ptr<T>& drawable ) { return removeDrawable( drawable.get() ); }
  51. /** Remove <tt>Drawable</tt>(s) from the specified position in
  52. * <tt>Geode</tt>'s drawable list.
  53. * @param i The index of the first \c Drawable to remove.
  54. * @param numDrawablesToRemove The number of <tt>Drawable</tt> to
  55. * remove.
  56. * @return \c true if at least one \c Drawable was removed. \c false
  57. * otherwise.
  58. */
  59. virtual bool removeDrawables(unsigned int i,unsigned int numDrawablesToRemove=1);
  60. /** Replace specified Drawable with another Drawable.
  61. * Equivalent to <tt>setDrawable(getDrawableIndex(origDraw),newDraw)</tt>,
  62. * see docs for \c setDrawable() for further details on implementation.
  63. */
  64. virtual bool replaceDrawable( Drawable *origDraw, Drawable *newDraw );
  65. template<class T, class R> bool replaceDrawable( const ref_ptr<T>& origDraw, const ref_ptr<R>& newDraw ) { return replaceDrawable(origDraw.get(), newDraw.get()); }
  66. /** Set \c Drawable at position \c i.
  67. * Decrement the reference count origGSet and increments the
  68. * reference count of newGset, and dirty the bounding sphere
  69. * to force it to recompute on next getBound() and returns true.
  70. * If origDrawable is not found then return false and do not
  71. * add newGset. If newGset is NULL then return false and do
  72. * not remove origGset.
  73. * @return \c true if set correctly, \c false on failure
  74. * (if node==NULL || i is out of range).
  75. */
  76. virtual bool setDrawable( unsigned int i, Drawable* drawable );
  77. template<class T> bool setDrawable( unsigned int i, const ref_ptr<T>& drawable ) { return setDrawable(i, drawable.get()); }
  78. /** Return the number of <tt>Drawable</tt>s currently attached to the
  79. * \c Geode.
  80. */
  81. inline unsigned int getNumDrawables() const { return getNumChildren(); }
  82. /** Return the \c Drawable at position \c i.*/
  83. inline Drawable* getDrawable( unsigned int i ) { return _children[i].valid() ? _children[i]->asDrawable() : 0; }
  84. /** Return the \c Drawable at position \c i.*/
  85. inline const Drawable* getDrawable( unsigned int i ) const { return _children[i].valid() ? _children[i]->asDrawable() : 0; }
  86. /** Return \c true if a given \c Drawable is contained within \c Geode.*/
  87. inline bool containsDrawable(const Drawable* drawable) const
  88. {
  89. for (NodeList::const_iterator itr=_children.begin();
  90. itr!=_children.end();
  91. ++itr)
  92. {
  93. if (itr->get() == drawable) return true;
  94. }
  95. return false;
  96. }
  97. template<class T> bool containsDrawable(const ref_ptr<T>& drawable) const { return containsDrawable(drawable.get()); }
  98. /** Get the index number of \c drawable.
  99. * @return A value between 0 and <tt>getNumDrawables()-1</tt> if
  100. * \c drawable is found; if not found, then
  101. * <tt>getNumDrawables()</tt> is returned.
  102. */
  103. inline unsigned int getDrawableIndex( const Drawable* drawable ) const
  104. {
  105. return getChildIndex(drawable);
  106. }
  107. template<class T> unsigned int getDrawableIndex( const ref_ptr<T>& drawable ) const { return getDrawableIndex(drawable.get()); }
  108. /** Compile OpenGL Display List for each drawable.*/
  109. void compileDrawables(RenderInfo& renderInfo);
  110. /** Return the Geode's bounding box, which is the union of all the
  111. * bounding boxes of the geode's drawables.*/
  112. inline const BoundingBox& getBoundingBox() const
  113. {
  114. if(!_boundingSphereComputed) getBound();
  115. return _bbox;
  116. }
  117. virtual BoundingSphere computeBound() const;
  118. protected:
  119. virtual ~Geode();
  120. mutable osg::BoundingBox _bbox;
  121. };
  122. }
  123. #endif