OpenSceneGraph/src/osg/Geode.cpp

149 lines
3.2 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
}
const 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);
dirtyBound();
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
const 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);
// 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;
}
const 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
DrawableList::iterator itr = findDrawable(origDrawable);
if (itr!=_drawables.end())
2001-01-11 00:32:10 +08:00
{
// 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.
*itr = newDrawable;
// register as parent of child.
newDrawable->addParent(this);
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
}
const bool Geode::computeBound() const
{
2001-01-11 00:32:10 +08:00
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.isValid())
2001-01-11 00:32:10 +08:00
{
_bsphere._center = bb.center();
_bsphere._radius = 0.0f;
for(itr=_drawables.begin();
itr!=_drawables.end();
++itr)
2001-01-11 00:32:10 +08:00
{
const BoundingBox& bbox = (*itr)->getBound();
for(unsigned int c=0;c<8;++c)
{
_bsphere.expandRadiusBy(bbox.corner(c));
}
2001-01-11 00:32:10 +08:00
}
_bsphere_computed=true;
return true;
}
else
{
_bsphere.init();
_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
}
}