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()
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
// ref_ptr<> automactially decrements the reference count of all drawables.
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
const bool Geode::addDrawable( Drawable *gset )
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (gset && !containsDrawable(gset))
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
// note ref_ptr<> automatically handles incrementing gset's reference count.
|
2001-09-20 05:08:56 +08:00
|
|
|
_drawables.push_back(gset);
|
2001-01-11 00:32:10 +08:00
|
|
|
dirtyBound();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
const bool Geode::removeDrawable( Drawable *gset )
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +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.
|
2001-09-20 05:08:56 +08:00
|
|
|
_drawables.erase(itr);
|
2001-01-11 00:32:10 +08:00
|
|
|
dirtyBound();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
const bool Geode::replaceDrawable( Drawable *origGset, Drawable *newGset )
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (newGset==NULL || origGset==newGset) return false;
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
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-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
const bool Geode::computeBound() const
|
|
|
|
{
|
2001-01-11 00:32:10 +08:00
|
|
|
BoundingBox bb;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
DrawableList::const_iterator itr;
|
|
|
|
for(itr=_drawables.begin();
|
|
|
|
itr!=_drawables.end();
|
2001-01-11 00:32:10 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
bb.expandBy((*itr)->getBound());
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
if (bb.isValid())
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +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
|
|
|
{
|
2001-09-20 05:08:56 +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
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_bsphere_computed=true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_bsphere.init();
|
|
|
|
_bsphere_computed=true;
|
|
|
|
return false;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Geode::compileDrawables(State& state)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
for(DrawableList::iterator itr = _drawables.begin();
|
|
|
|
itr!=_drawables.end();
|
2001-01-11 00:32:10 +08:00
|
|
|
++itr)
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
(*itr)->compile(state);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|