From 8b30a1037590411292c3d59113e5348e053fa4b1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 22 Apr 2002 19:41:33 +0000 Subject: [PATCH] Added code in hangglide to move the earth sky around with the eye point, use osg::Transform::ComputeTransformCallback. --- include/osgUtil/CullVisitor | 9 +++--- src/Demos/hangglide/hangglide.cpp | 54 +++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/include/osgUtil/CullVisitor b/include/osgUtil/CullVisitor index 5ffba59c3..a21b5e7c8 100644 --- a/include/osgUtil/CullVisitor +++ b/include/osgUtil/CullVisitor @@ -175,6 +175,11 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor const float getCalculatedFarPlane() const { return _computed_zfar; } + inline const osg::Vec3& getEyeLocal() const + { + return _eyePointStack.back(); + } + protected: /** prevent unwanted copy construction.*/ @@ -197,10 +202,6 @@ class OSGUTIL_EXPORT CullVisitor : public osg::NodeVisitor else acceptNode->accept(*this); } - inline const osg::Vec3& getEyeLocal() const - { - return _eyePointStack.back(); - } inline const osg::Vec3 getUpLocal() const { diff --git a/src/Demos/hangglide/hangglide.cpp b/src/Demos/hangglide/hangglide.cpp index 6e721515b..6bc224f42 100644 --- a/src/Demos/hangglide/hangglide.cpp +++ b/src/Demos/hangglide/hangglide.cpp @@ -3,6 +3,9 @@ #include #include #include +#include + +#include #include #include @@ -22,6 +25,31 @@ extern osg::Node *makeSky( void ); extern osg::Node *makeBase( void ); extern osg::Node *makeClouds( void ); +struct MoveEarthySkyWithEyePointCallback : public osg::Transform::ComputeTransformCallback +{ + /** Get the transformation matrix which moves from local coords to world coords.*/ + virtual const bool computeLocalToWorldMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const + { + osgUtil::CullVisitor* cv = dynamic_cast(nv); + if (cv) + { + osg::Vec3 eyePointLocal = cv->getEyeLocal(); + matrix.preMult(osg::Matrix::translate(eyePointLocal.x(),eyePointLocal.y(),0.0f)); + } + } + + /** Get the transformation matrix which moves from world coords to local coords.*/ + virtual const bool computeWorldToLocalMatrix(osg::Matrix& matrix,const osg::Transform* transform, osg::NodeVisitor* nv) const + { + osgUtil::CullVisitor* cv = dynamic_cast(nv); + if (cv) + { + osg::Vec3 eyePointLocal = cv->getEyeLocal(); + matrix.postMult(osg::Matrix::translate(-eyePointLocal.x(),-eyePointLocal.y(),0.0f)); + } + } +}; + int main( int argc, char **argv ) { @@ -56,11 +84,31 @@ int main( int argc, char **argv ) // osg::Depth, and setting their bin numbers to less than 0, // to force them to draw before the rest of the scene. - osg::EarthSky* earthSky = new osg::EarthSky; + osg::EarthSky* earthSky = osgNew osg::EarthSky; earthSky->setRequiresClear(false); // we've got base and sky to do it. - earthSky->addChild(makeSky()); // bin number -2 so drawn first. - earthSky->addChild(makeBase()); // bin number -1 so draw second. + // use a transform to make the sky and base around with the eye point. + osg::Transform* transform = osgNew osg::Transform; + + // transform's value isn't knowm until in the cull traversal so its bounding + // volume is can't be determined, therefore culling will be invalid, + // so switch it off, this cause all our paresnts to switch culling + // off as well. But don't worry culling will be back on once underneath + // this node or any other branch above this transform. + transform->setCullingActive(false); + + // set the compute transform callback to do all the work of + // determining the transform according to the current eye point. + transform->setComputeTransformCallback(osgNew MoveEarthySkyWithEyePointCallback); + + // add the sky and base layer. + transform->addChild(makeSky()); // bin number -2 so drawn first. + transform->addChild(makeBase()); // bin number -1 so draw second. + + // add the transform to the earth sky. + earthSky->addChild(transform); + + // add to earth sky to the scene. group->addChild(earthSky); // the rest of the scene drawn after the base and sky above.