OpenSceneGraph/src/osg/Geode.cpp

166 lines
3.6 KiB
C++
Raw Normal View History

2001-01-11 00:32:10 +08:00
#include <stdio.h>
#include <math.h>
#include <osg/Geode>
2001-01-11 00:32:10 +08:00
#define square(x) ((x)*(x))
using namespace osg;
Geode::Geode()
{
}
Geode::Geode(const Geode& geode,const CopyOp& copyop):
Node(geode,copyop)
{
for(DrawableList::const_iterator itr=geode._drawables.begin();
itr!=geode._drawables.end();
++itr)
{
Drawable* drawable = copyop(itr->get());
if (drawable) addDrawable(drawable);
}
}
2001-01-11 00:32:10 +08:00
Geode::~Geode()
{
// remove reference to this from children's parent lists.
for(DrawableList::iterator itr=_drawables.begin();
itr!=_drawables.end();
++itr)
{
(*itr)->removeParent(this);
}
2001-01-11 00:32:10 +08:00
}
bool Geode::addDrawable( Drawable *drawable )
2001-01-11 00:32:10 +08:00
{
if (drawable && !containsDrawable(drawable))
2001-01-11 00:32:10 +08:00
{
// note ref_ptr<> automatically handles incrementing drawable's reference count.
_drawables.push_back(drawable);
// register as parent of drawable.
drawable->addParent(this);
2002-07-10 23:35:47 +08:00
if (drawable->getAppCallback())
{
setNumChildrenRequiringAppTraversal(getNumChildrenRequiringAppTraversal()+1);
}
dirtyBound();
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
bool Geode::removeDrawable( Drawable *drawable )
2001-01-11 00:32:10 +08:00
{
DrawableList::iterator itr = findDrawable(drawable);
if (itr!=_drawables.end())
2001-01-11 00:32:10 +08:00
{
// remove this Geode from the child parent list.
drawable->removeParent(this);
2002-07-10 23:35:47 +08:00
if (drawable->getAppCallback())
{
setNumChildrenRequiringAppTraversal(getNumChildrenRequiringAppTraversal()-1);
}
// note ref_ptr<> automatically handles decrementing drawable's reference count.
_drawables.erase(itr);
2001-01-11 00:32:10 +08:00
dirtyBound();
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
bool Geode::replaceDrawable( Drawable *origDrawable, Drawable *newDrawable )
2001-01-11 00:32:10 +08:00
{
if (newDrawable==NULL || origDrawable==newDrawable) return false;
2001-01-11 00:32:10 +08:00
unsigned int pos = findDrawableNum(origDrawable);
if (pos<_drawables.size())
2001-01-11 00:32:10 +08:00
{
return setDrawable(pos,newDrawable);
}
return false;
}
bool Geode::setDrawable( unsigned int i, Drawable* newDrawable )
{
if (i<_drawables.size() && newDrawable)
{
Drawable* origDrawable = _drawables[i].get();
2002-07-10 23:35:47 +08:00
int delta = 0;
if (origDrawable->getAppCallback()) --delta;
if (newDrawable->getAppCallback()) ++delta;
if (delta!=0)
{
setNumChildrenRequiringAppTraversal(getNumChildrenRequiringAppTraversal()+delta);
}
// remove from origDrawable's parent list.
origDrawable->removeParent(this);
2001-01-11 00:32:10 +08:00
// note ref_ptr<> automatically handles decrementing origGset's reference count,
// and inccrementing newGset's reference count.
_drawables[i] = newDrawable;
// register as parent of child.
newDrawable->addParent(this);
2002-07-10 23:35:47 +08:00
2001-01-11 00:32:10 +08:00
dirtyBound();
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
2001-01-11 00:32:10 +08:00
}
bool Geode::computeBound() const
{
_bsphere.init();
BoundingBox bb;
DrawableList::const_iterator itr;
for(itr=_drawables.begin();
itr!=_drawables.end();
2001-01-11 00:32:10 +08:00
++itr)
{
bb.expandBy((*itr)->getBound());
}
if (bb.valid())
2001-01-11 00:32:10 +08:00
{
_bsphere.expandBy(bb);
_bsphere_computed=true;
return true;
}
else
{
_bsphere_computed=true;
return false;
}
2001-01-11 00:32:10 +08:00
}
void Geode::compileDrawables(State& state)
2001-01-11 00:32:10 +08:00
{
for(DrawableList::iterator itr = _drawables.begin();
itr!=_drawables.end();
2001-01-11 00:32:10 +08:00
++itr)
{
(*itr)->compile(state);
2001-01-11 00:32:10 +08:00
}
}