root/OpenSceneGraph/trunk/src/osg/Geode.cpp @ 13041

Revision 13041, 6.5 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osg/Geode>
14#include <osg/Notify>
15
16#include <stdio.h>
17#include <math.h>
18
19#define square(x)   ((x)*(x))
20
21using namespace osg;
22
23Geode::Geode()
24{
25}
26
27Geode::Geode(const Geode& geode,const CopyOp& copyop):
28    Node(geode,copyop)
29{
30    for(DrawableList::const_iterator itr=geode._drawables.begin();
31        itr!=geode._drawables.end();
32        ++itr)
33    {
34        Drawable* drawable = copyop(itr->get());
35        if (drawable) addDrawable(drawable);
36    }
37}
38
39Geode::~Geode()
40{
41    // remove reference to this from children's parent lists.
42    for(DrawableList::iterator itr=_drawables.begin();
43        itr!=_drawables.end();
44        ++itr)
45    {
46       (*itr)->removeParent(this);
47    }
48}
49
50bool Geode::addDrawable( Drawable *drawable )
51{
52    if (drawable /* && !containsDrawable(drawable)*/)
53    {
54        // note ref_ptr<> automatically handles incrementing drawable's reference count.
55        _drawables.push_back(drawable);
56
57        // register as parent of drawable.
58        drawable->addParent(this);
59
60        if (drawable->requiresUpdateTraversal())
61        {
62            setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
63        }
64
65        if (drawable->requiresEventTraversal())
66        {
67            setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+1);
68        }
69
70        dirtyBound();
71
72        return true;
73    }
74    else return false;
75}
76
77
78bool Geode::removeDrawable( Drawable *drawable )
79{
80    return removeDrawables(getDrawableIndex(drawable),1);
81}
82
83bool Geode::removeDrawables(unsigned int pos,unsigned int numDrawablesToRemove)
84{
85    if (pos<_drawables.size() && numDrawablesToRemove>0)
86    {
87        unsigned int endOfRemoveRange = pos+numDrawablesToRemove;
88        if (endOfRemoveRange>_drawables.size())
89        {
90            OSG_DEBUG<<"Warning: Geode::removeDrawable(i,numDrawablesToRemove) has been passed an excessive number"<<std::endl;
91            OSG_DEBUG<<"         of drawables to remove, trimming just to end of drawable list."<<std::endl;
92            endOfRemoveRange=_drawables.size();
93        }
94
95        unsigned int updateCallbackRemoved = 0;
96        unsigned int eventCallbackRemoved = 0;
97        for(unsigned i=pos;i<endOfRemoveRange;++i)
98        {
99            // remove this Geode from the child parent list.
100            _drawables[i]->removeParent(this);
101            // update the number of app callbacks removed
102            if (_drawables[i]->requiresUpdateTraversal()) ++updateCallbackRemoved;
103            if (_drawables[i]->requiresEventTraversal()) ++eventCallbackRemoved;
104        }
105
106        _drawables.erase(_drawables.begin()+pos,_drawables.begin()+endOfRemoveRange);
107
108        if (updateCallbackRemoved)
109        {
110            setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()-updateCallbackRemoved);
111        }
112
113        if (eventCallbackRemoved)
114        {
115            setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()-eventCallbackRemoved);
116        }
117
118        dirtyBound();
119
120        return true;
121    }
122    else return false;
123}
124
125bool Geode::replaceDrawable( Drawable *origDrawable, Drawable *newDrawable )
126{
127    if (newDrawable==NULL || origDrawable==newDrawable) return false;
128
129    unsigned int pos = getDrawableIndex(origDrawable);
130    if (pos<_drawables.size())
131    {
132        return setDrawable(pos,newDrawable);
133    }
134    return false;
135}
136
137bool Geode::setDrawable( unsigned  int i, Drawable* newDrawable )
138{
139    if (i<_drawables.size() && newDrawable)
140    {
141
142        Drawable* origDrawable = _drawables[i].get();
143
144        int deltaUpdate = 0;
145        if (origDrawable->requiresUpdateTraversal()) --deltaUpdate;
146        if (newDrawable->requiresUpdateTraversal()) ++deltaUpdate;
147        if (deltaUpdate!=0)
148        {
149            setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+deltaUpdate);
150        }
151
152        int deltaEvent = 0;
153        if (origDrawable->requiresEventTraversal()) --deltaEvent;
154        if (newDrawable->requiresEventTraversal()) ++deltaEvent;
155        if (deltaEvent!=0)
156        {
157            setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+deltaEvent);
158        }
159
160
161        // remove from origDrawable's parent list.
162        origDrawable->removeParent(this);
163
164        // note ref_ptr<> automatically handles decrementing origGset's reference count,
165        // and incrementing newGset's reference count.
166        _drawables[i] = newDrawable;
167
168        // register as parent of child.
169        newDrawable->addParent(this);
170
171
172        dirtyBound();
173
174        return true;
175    }
176    else return false;
177
178}
179
180
181BoundingSphere Geode::computeBound() const
182{
183    BoundingSphere bsphere;
184
185    _bbox.init();
186
187    DrawableList::const_iterator itr;
188    for(itr=_drawables.begin();
189        itr!=_drawables.end();
190        ++itr)
191    {
192        _bbox.expandBy((*itr)->getBound());
193    }
194
195    if (_bbox.valid())
196    {
197        bsphere.expandBy(_bbox);
198    }
199    return bsphere;
200}
201
202void Geode::compileDrawables(RenderInfo& renderInfo)
203{
204    for(DrawableList::iterator itr = _drawables.begin();
205        itr!=_drawables.end();
206        ++itr)
207    {
208        (*itr)->compileGLObjects(renderInfo);
209    }
210}
211
212void Geode::setThreadSafeRefUnref(bool threadSafe)
213{
214    Node::setThreadSafeRefUnref(threadSafe);
215
216    for(DrawableList::const_iterator itr=_drawables.begin();
217        itr!=_drawables.end();
218        ++itr)
219    {
220        (*itr)->setThreadSafeRefUnref(threadSafe);
221    }
222}
223
224void Geode::resizeGLObjectBuffers(unsigned int maxSize)
225{
226    Node::resizeGLObjectBuffers(maxSize);
227
228    for(DrawableList::const_iterator itr=_drawables.begin();
229        itr!=_drawables.end();
230        ++itr)
231    {
232        (*itr)->resizeGLObjectBuffers(maxSize);
233    }
234}
235
236void Geode::releaseGLObjects(osg::State* state) const
237{
238    Node::releaseGLObjects(state);
239
240    for(DrawableList::const_iterator itr=_drawables.begin();
241        itr!=_drawables.end();
242        ++itr)
243    {
244        (*itr)->releaseGLObjects(state);
245    }
246}
Note: See TracBrowser for help on using the browser.