OpenSceneGraph/src/osg/Geode.cpp

120 lines
2.4 KiB
C++
Raw Normal View History

2001-01-11 00:32:10 +08:00
#include <stdio.h>
#include <math.h>
#include "osg/Geode"
#include <algorithm>
#ifdef __sgi
using std::find;
using std::for_each;
#endif
#define square(x) ((x)*(x))
using namespace osg;
Geode::Geode()
{
}
Geode::~Geode()
{
// ref_ptr<> automactially decrements the reference count of all drawables.
2001-01-11 00:32:10 +08:00
}
const bool Geode::addDrawable( Drawable *gset )
2001-01-11 00:32:10 +08:00
{
if (gset && !containsDrawable(gset))
2001-01-11 00:32:10 +08:00
{
// note ref_ptr<> automatically handles incrementing gset's reference count.
_drawables.push_back(gset);
2001-01-11 00:32:10 +08:00
dirtyBound();
return true;
}
else return false;
}
const bool Geode::removeDrawable( Drawable *gset )
2001-01-11 00:32:10 +08:00
{
DrawableList::iterator itr = findDrawable(gset);
if (itr!=_drawables.end())
2001-01-11 00:32:10 +08:00
{
// note ref_ptr<> automatically handles decrementing gset's reference count.
_drawables.erase(itr);
2001-01-11 00:32:10 +08:00
dirtyBound();
return true;
}
else return false;
}
const bool Geode::replaceDrawable( Drawable *origGset, Drawable *newGset )
2001-01-11 00:32:10 +08:00
{
if (newGset==NULL || origGset==newGset) return false;
DrawableList::iterator itr = findDrawable(origGset);
if (itr!=_drawables.end())
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 = newGset;
dirtyBound();
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
}
}