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