openscenegraph
Geode
Go to the documentation of this file.
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
14#ifndef OSG_GEODE
15#define OSG_GEODE 1
16
17#include <osg/Node>
18#include <osg/NodeVisitor>
19#include <osg/Drawable>
20
21namespace osg {
22
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.
27*/
28class OSG_EXPORT Geode : public Group
29{
30 public:
31
32 Geode();
33
34 /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
35 Geode(const Geode&,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
36
37 META_Node(osg, Geode);
38
39 virtual Geode* asGeode() { return this; }
40 virtual const Geode* asGeode() const { return this; }
41
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.
49 */
50 virtual bool addDrawable( Drawable *drawable );
51
52 template<class T> bool addDrawable( const ref_ptr<T>& drawable ) { return addDrawable(drawable.get()); }
53
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
58 * otherwise.
59 */
60 virtual bool removeDrawable( Drawable *drawable );
61
62 template<class T> bool removeDrawable( const ref_ptr<T>& drawable ) { return removeDrawable( drawable.get() ); }
63
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
68 * remove.
69 * @return \c true if at least one \c Drawable was removed. \c false
70 * otherwise.
71 */
72 virtual bool removeDrawables(unsigned int i,unsigned int numDrawablesToRemove=1);
73
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.
77 */
78 virtual bool replaceDrawable( Drawable *origDraw, Drawable *newDraw );
79
80 template<class T, class R> bool replaceDrawable( const ref_ptr<T>& origDraw, const ref_ptr<R>& newDraw ) { return replaceDrawable(origDraw.get(), newDraw.get()); }
81
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).
91 */
92 virtual bool setDrawable( unsigned int i, Drawable* drawable );
93
94 template<class T> bool setDrawable( unsigned int i, const ref_ptr<T>& drawable ) { return setDrawable(i, drawable.get()); }
95
96 /** Return the number of <tt>Drawable</tt>s currently attached to the
97 * \c Geode.
98 */
99 inline unsigned int getNumDrawables() const { return getNumChildren(); }
100
101 /** Return the \c Drawable at position \c i.*/
102 inline Drawable* getDrawable( unsigned int i ) { return _children[i].valid() ? _children[i]->asDrawable() : 0; }
103
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; }
106
107 /** Return \c true if a given \c Drawable is contained within \c Geode.*/
108 inline bool containsDrawable(const Drawable* drawable) const
109 {
110 for (NodeList::const_iterator itr=_children.begin();
111 itr!=_children.end();
112 ++itr)
113 {
114 if (itr->get() == drawable) return true;
115 }
116 return false;
117 }
118
119 template<class T> bool containsDrawable(const ref_ptr<T>& drawable) const { return containsDrawable(drawable.get()); }
120
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.
125 */
126 inline unsigned int getDrawableIndex( const Drawable* drawable ) const
127 {
128 return getChildIndex(drawable);
129 }
130
131 template<class T> unsigned int getDrawableIndex( const ref_ptr<T>& drawable ) const { return getDrawableIndex(drawable.get()); }
132
133 /** Compile OpenGL Display List for each drawable.*/
134 void compileDrawables(RenderInfo& renderInfo);
135
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
139 {
140 if(!_boundingSphereComputed) getBound();
141 return _bbox;
142 }
143
144 virtual BoundingSphere computeBound() const;
145
146
147 protected:
148
149 virtual ~Geode();
150
151 mutable osg::BoundingBox _bbox;
152
153};
154
155}
156
157#endif