diff --git a/src/Demos/osgoccluder/osgoccluder.cpp b/src/Demos/osgoccluder/osgoccluder.cpp index 9840daad7..d80686fa7 100644 --- a/src/Demos/osgoccluder/osgoccluder.cpp +++ b/src/Demos/osgoccluder/osgoccluder.cpp @@ -107,6 +107,8 @@ osg::Node* createOccluder(const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec geoset->setPrimType(osg::GeoSet::QUADS); geoset->setNumPrims(1); +// geoset->setPrimType(osg::GeoSet::POINTS); +// geoset->setNumPrims(4); osg::Geode* geode = osgNew osg::Geode; geode->addDrawable(geoset); diff --git a/src/osg/BoundingSphere.cpp b/src/osg/BoundingSphere.cpp index 8aab06e79..df140908f 100644 --- a/src/osg/BoundingSphere.cpp +++ b/src/osg/BoundingSphere.cpp @@ -1,10 +1,11 @@ #include +#include using namespace osg; void BoundingSphere::expandBy(const Vec3& v) { - if (isValid()) + if (valid()) { Vec3 dv = v-_center; float r = dv.length(); @@ -13,7 +14,7 @@ void BoundingSphere::expandBy(const Vec3& v) float dr = (r-_radius)*0.5f; _center += dv*(dr/r); _radius += dr; - } // else do nothing as vertex is within sphere. + } // else do nothing as vertex is within sphere. } else { @@ -25,7 +26,7 @@ void BoundingSphere::expandBy(const Vec3& v) void BoundingSphere::expandRadiusBy(const Vec3& v) { - if (isValid()) + if (valid()) { float r = (v-_center).length(); if (r>_radius) _radius = r; @@ -41,15 +42,14 @@ void BoundingSphere::expandRadiusBy(const Vec3& v) void BoundingSphere::expandBy(const BoundingSphere& sh) { - if (sh.isValid()) + if (sh.valid()) { - if (isValid()) + if (valid()) { Vec3 dv = sh._center-_center; float dv_len = dv.length(); if (dv_len+sh._radius>_radius) { - Vec3 e1 = _center-(dv*(_radius/dv_len)); Vec3 e2 = sh._center+(dv*(sh._radius/dv_len)); _center = (e1+e2)*0.5f; @@ -68,9 +68,9 @@ void BoundingSphere::expandBy(const BoundingSphere& sh) void BoundingSphere::expandRadiusBy(const BoundingSphere& sh) { - if (sh.isValid()) + if (sh.valid()) { - if (isValid()) + if (valid()) { float r = (sh._center-_center).length()+sh._radius; if (r>_radius) _radius = r; @@ -83,3 +83,51 @@ void BoundingSphere::expandRadiusBy(const BoundingSphere& sh) } } } + +void BoundingSphere::expandBy(const BoundingBox& bb) +{ + if (bb.valid()) + { + if (valid()) + { + BoundingBox newbb(bb); + + for(unsigned int c=0;c<8;++c) + { + Vec3 v = bb.corner(c)-_center; // get the direction vector from corner + v.normalize(); // normalise it. + v *= -_radius; // move the vector in the opposite direction distance radius. + v += _center; // move to absolute position. + newbb.expandBy(v); // add it into the new bounding box. + } + + _center = newbb.center(); + _radius = newbb.radius(); + + } + else + { + _center = bb.center(); + _radius = bb.radius(); + } + } +} + +void BoundingSphere::expandRadiusBy(const BoundingBox& bb) +{ + if (bb.valid()) + { + if (valid()) + { + for(unsigned int c=0;c<8;++c) + { + expandRadiusBy(bb.corner(c)); + } + } + else + { + _center = bb.center(); + _radius = bb.radius(); + } + } +} diff --git a/src/osg/Geode.cpp b/src/osg/Geode.cpp index 9b892f771..74492bb21 100644 --- a/src/osg/Geode.cpp +++ b/src/osg/Geode.cpp @@ -111,25 +111,9 @@ const bool Geode::computeBound() const bb.expandBy((*itr)->getBound()); } - if (bb.isValid()) + if (bb.valid()) { - - _bsphere._center = bb.center(); - _bsphere._radius = 0.0f; - - for(itr=_drawables.begin(); - itr!=_drawables.end(); - ++itr) - { - const BoundingBox& bbox = (*itr)->getBound(); - for(unsigned int c=0;c<8;++c) - { - _bsphere.expandRadiusBy(bbox.corner(c)); - } - } - -// if (_occluder.valid()) _occluder->computeBound(_bsphere); - + _bsphere.expandBy(bb); _bsphere_computed=true; return true; } diff --git a/src/osg/OccluderNode.cpp b/src/osg/OccluderNode.cpp index e3935bf5b..c8d8a17ec 100644 --- a/src/osg/OccluderNode.cpp +++ b/src/osg/OccluderNode.cpp @@ -11,3 +11,27 @@ OccluderNode::OccluderNode(const OccluderNode& node,const CopyOp& copyop): _occluder(dynamic_cast(copyop(node._occluder.get()))) { } + +const bool OccluderNode::computeBound() const +{ + bool result = Group::computeBound(); + + if (getOccluder()) + { + BoundingBox bb; + const ConvexPlanerPolygon::VertexList& vertexList = getOccluder()->getOccluder().getVertexList(); + for(ConvexPlanerPolygon::VertexList::const_iterator itr=vertexList.begin(); + itr!=vertexList.end(); + ++itr) + { + bb.expandBy(*itr); + } + if (bb.valid()) + { + _bsphere.expandBy(bb); + _bsphere_computed=true; + result = true; + } + } + return result; +}