From cb811aa7cd7125ae5974635f2bc09355ae01135a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 28 Mar 2007 16:28:20 +0000 Subject: [PATCH] Added support for compute the extents on local NDC coordiantes of the elevation and colour layers --- examples/osgterrain/osgterrain.cpp | 7 ++- include/osgTerrain/Locator | 15 +++++- src/osgTerrain/GeometryTechnique.cpp | 71 ++++++++++++++++++++++++++-- src/osgTerrain/Locator.cpp | 41 +++++++++++++++- 4 files changed, 123 insertions(+), 11 deletions(-) diff --git a/examples/osgterrain/osgterrain.cpp b/examples/osgterrain/osgterrain.cpp index b65f58fbe..2b043d103 100644 --- a/examples/osgterrain/osgterrain.cpp +++ b/examples/osgterrain/osgterrain.cpp @@ -39,7 +39,6 @@ int main(int argc, char** argv) osg::ref_ptr terrain = new osgTerrain::TerrainNode; osg::ref_ptr locator = new osgTerrain::EllipsoidLocator(-osg::PI, -osg::PI*0.5, 2.0*osg::PI, osg::PI, 0.0); - bool readParameter = false; do { @@ -57,7 +56,7 @@ int main(int argc, char** argv) { readParameter = true; - osg::notify(osg::NOTICE)<<"--hf "< hf = osgDB::readHeightFieldFile(filename); if (hf.valid()) @@ -78,10 +77,10 @@ int main(int argc, char** argv) } - if (arguments.read("-h",filename) || arguments.read("--elevation-image",filename)) + if (arguments.read("-d",filename) || arguments.read("--elevation-image",filename)) { readParameter = true; - osg::notify(osg::NOTICE)<<"--elevation-image "< image = osgDB::readImageFile(filename); if (image.valid()) diff --git a/include/osgTerrain/Locator b/include/osgTerrain/Locator index 21dc65118..1a54d9c27 100644 --- a/include/osgTerrain/Locator +++ b/include/osgTerrain/Locator @@ -34,7 +34,18 @@ class OSGTERRAIN_EXPORT Locator : public osg::Object META_Object(osgTerrain, Locator); virtual bool convertLocalToModel(const osg::Vec3d& /*local*/, osg::Vec3d& /*world*/) const { return false; } - virtual bool convertModelToWorld(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const { return false; } + virtual bool convertModelToLocal(const osg::Vec3d& /*world*/, osg::Vec3d& /*local*/) const { return false; } + + static bool convertLocalCoordBetween(const Locator& source, const osg::Vec3d& sourceNDC, + const Locator& destination, osg::Vec3d& destinationNDC) + { + osg::Vec3d model; + if (!source.convertLocalToModel(sourceNDC, model)) return false; + if (!destination.convertModelToLocal(model, destinationNDC)) return false; + return true; + } + + bool computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight); protected: @@ -63,7 +74,7 @@ public: const osg::EllipsoidModel* getEllipsoidModel() const { return _em.get(); } bool convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const; - bool convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) const; + bool convertModelToLocal(const osg::Vec3d& world, osg::Vec3d& local) const; protected: diff --git a/src/osgTerrain/GeometryTechnique.cpp b/src/osgTerrain/GeometryTechnique.cpp index 629efdf63..c2b11b999 100644 --- a/src/osgTerrain/GeometryTechnique.cpp +++ b/src/osgTerrain/GeometryTechnique.cpp @@ -12,6 +12,8 @@ */ #include +#include +#include using namespace osgTerrain; @@ -32,24 +34,86 @@ void GeometryTechnique::init() { osg::notify(osg::NOTICE)<<"Doing init()"<getElevationLayer(); + osgTerrain::Layer* colorLayer = _terrainNode->getColorLayer(); + osg::TransferFunction* colorTF = _terrainNode->getColorTransferFunction(); + + osg::notify(osg::NOTICE)<<"elevationLayer = "<computeLocalBounds(*elevationLocator, bottomLeftNDC, topRightNDC); + } + else + { + bottomLeftNDC.x() = osg::minimum(bottomLeftNDC.x(), 0.0); + bottomLeftNDC.y() = osg::minimum(bottomLeftNDC.y(), 0.0); + topRightNDC.x() = osg::maximum(topRightNDC.x(), 1.0); + topRightNDC.y() = osg::maximum(topRightNDC.y(), 1.0); + } + } + + if (colorLayer) + { + if (colorLocator!= masterLocator) + { + masterLocator->computeLocalBounds(*colorLocator, bottomLeftNDC, topRightNDC); + } + else + { + bottomLeftNDC.x() = osg::minimum(bottomLeftNDC.x(), 0.0); + bottomLeftNDC.y() = osg::minimum(bottomLeftNDC.y(), 0.0); + topRightNDC.x() = osg::maximum(topRightNDC.x(), 1.0); + topRightNDC.y() = osg::maximum(topRightNDC.y(), 1.0); + } + } + + osg::notify(osg::NOTICE)<<"bottomLeftNDC = "<accept(*nv); @@ -58,7 +122,6 @@ void GeometryTechnique::cull(osgUtil::CullVisitor* nv) void GeometryTechnique::cleanSceneGraph() { - osg::notify(osg::NOTICE)<<"Cleaning scene graph"< +#include + using namespace osgTerrain; ////////////////////////////////////////////////////////////////////////////// @@ -32,6 +34,43 @@ Locator::~Locator() { } +bool Locator::computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) +{ + typedef std::list Corners; + Corners corners; + + osg::Vec3d cornerNDC; + if (Locator::convertLocalCoordBetween(source, osg::Vec3d(0.0,0.0,0.0), *this, cornerNDC)) + { + corners.push_back(cornerNDC); + } + + if (Locator::convertLocalCoordBetween(source, osg::Vec3d(1.0,0.0,0.0), *this, cornerNDC)) + { + corners.push_back(cornerNDC); + } + + if (Locator::convertLocalCoordBetween(source, osg::Vec3d(0.0,1.0,0.0), *this, cornerNDC)) + { + corners.push_back(cornerNDC); + } + + if (Locator::convertLocalCoordBetween(source, osg::Vec3d(1.0,1.0,0.0), *this, cornerNDC)) + { + corners.push_back(cornerNDC); + } + + + for(Corners::iterator itr = corners.begin(); + itr != corners.end(); + ++itr) + { + bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x()); + bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y()); + topRight.x() = osg::maximum( topRight.x(), itr->x()); + topRight.y() = osg::maximum( topRight.y(), itr->y()); + } +} ////////////////////////////////////////////////////////////////////////////// // @@ -70,7 +109,7 @@ bool EllipsoidLocator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& return true; } -bool EllipsoidLocator::convertModelToWorld(const osg::Vec3d& world, osg::Vec3d& local) const +bool EllipsoidLocator::convertModelToLocal(const osg::Vec3d& world, osg::Vec3d& local) const { double longitude, latitude, height;