From efdc026114435fd0de982d3f07a1f8417ed276c5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 17 Jan 2008 15:37:35 +0000 Subject: [PATCH] Fixed the computation of terrain bounding volume so that it properly accounts for elevation layers --- include/osgTerrain/Layer | 4 +- src/osgTerrain/Layer.cpp | 62 +++++++++++++++++++--------- src/osgTerrain/Terrain.cpp | 19 +++++---- src/osgWrappers/osgTerrain/Layer.cpp | 8 ++-- 4 files changed, 61 insertions(+), 32 deletions(-) diff --git a/include/osgTerrain/Layer b/include/osgTerrain/Layer index 290058d96..f93b848a0 100644 --- a/include/osgTerrain/Layer +++ b/include/osgTerrain/Layer @@ -160,7 +160,7 @@ class OSGTERRAIN_EXPORT Layer : public osg::Object /** Get modified count value. */ virtual unsigned int getModifiedCount() const { return 0; } - virtual osg::BoundingSphere computeBound() const; + virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const; protected: @@ -333,7 +333,7 @@ class OSGTERRAIN_EXPORT ProxyLayer : public Layer virtual void setModifiedCount(unsigned int value); virtual unsigned int getModifiedCount() const; - virtual osg::BoundingSphere computeBound() const; + virtual osg::BoundingSphere computeBound(bool treatAsElevationLayer) const; protected: diff --git a/src/osgTerrain/Layer.cpp b/src/osgTerrain/Layer.cpp index b72c25e40..9c7cec6c0 100644 --- a/src/osgTerrain/Layer.cpp +++ b/src/osgTerrain/Layer.cpp @@ -34,32 +34,56 @@ Layer::~Layer() { } -osg::BoundingSphere Layer::computeBound() const +osg::BoundingSphere Layer::computeBound(bool treatAsElevationLayer) const { osg::BoundingSphere bs; if (!getLocator()) return bs; - - osg::Vec3d v; - if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), v)) - { - bs.expandBy(v); - } - if (getLocator()->convertLocalToModel(osg::Vec3d(1.0,0.0,0.0), v)) + if (treatAsElevationLayer) { - bs.expandBy(v); - } + osg::BoundingBox bb; + unsigned int numColumns = getNumColumns(); + unsigned int numRows = getNumRows(); + for(unsigned int r=0;rconvertLocalToModel(osg::Vec3d(1.0,1.0,0.0), v)) - { - bs.expandBy(v); + if (getLocator()->convertLocalToModel(ndc, v)) + { + bb.expandBy(v); + } + } + } + } + bs.expandBy(bb); } - - if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,1.0,0.0), v)) + else { - bs.expandBy(v); - } + osg::Vec3d v; + if (getLocator()->convertLocalToModel(osg::Vec3d(0.5,0.5,0.0), v)) + { + bs.center() = v; + } + + if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), v)) + { + bs.expandBy(v); + + bs.radius() = (bs.center() - v).length(); + } + + } + return bs; } @@ -431,8 +455,8 @@ unsigned int ProxyLayer::getModifiedCount() const } -osg::BoundingSphere ProxyLayer::computeBound() const +osg::BoundingSphere ProxyLayer::computeBound(bool treatAsElevationLayer) const { - if (_implementation.valid()) return _implementation->computeBound(); + if (_implementation.valid()) return _implementation->computeBound(treatAsElevationLayer); } diff --git a/src/osgTerrain/Terrain.cpp b/src/osgTerrain/Terrain.cpp index 277aa5e6e..574ef7cfd 100644 --- a/src/osgTerrain/Terrain.cpp +++ b/src/osgTerrain/Terrain.cpp @@ -104,14 +104,19 @@ osg::BoundingSphere Terrain::computeBound() const { osg::BoundingSphere bs; - if (_elevationLayer.valid()) bs.expandBy(_elevationLayer->computeBound()); - - for(Layers::const_iterator itr = _colorLayers.begin(); - itr != _colorLayers.end(); - ++itr) + if (_elevationLayer.valid()) { - if (itr->layer.valid()) bs.expandBy(itr->layer->computeBound()); + bs.expandBy(_elevationLayer->computeBound(true)); } - + else + { + for(Layers::const_iterator itr = _colorLayers.begin(); + itr != _colorLayers.end(); + ++itr) + { + if (itr->layer.valid()) bs.expandBy(itr->layer->computeBound(false)); + } + } + return bs; } diff --git a/src/osgWrappers/osgTerrain/Layer.cpp b/src/osgWrappers/osgTerrain/Layer.cpp index 738629947..888a660e1 100644 --- a/src/osgWrappers/osgTerrain/Layer.cpp +++ b/src/osgWrappers/osgTerrain/Layer.cpp @@ -555,9 +555,9 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::Layer) __unsigned_int__getModifiedCount, "Get modified count value. ", ""); - I_Method0(osg::BoundingSphere, computeBound, + I_Method1(osg::BoundingSphere, computeBound, IN, bool, treatAsElevationLayer, Properties::VIRTUAL, - __osg_BoundingSphere__computeBound, + __osg_BoundingSphere__computeBound__bool, "", ""); I_SimpleProperty(const osg::Vec4 &, DefaultValue, @@ -693,9 +693,9 @@ BEGIN_OBJECT_REFLECTOR(osgTerrain::ProxyLayer) __unsigned_int__getModifiedCount, "Get modified count value. ", ""); - I_Method0(osg::BoundingSphere, computeBound, + I_Method1(osg::BoundingSphere, computeBound, IN, bool, treatAsElevationLayer, Properties::VIRTUAL, - __osg_BoundingSphere__computeBound, + __osg_BoundingSphere__computeBound__bool, "", ""); I_SimpleProperty(const std::string &, FileName,