From 3d8cf5695de0eac61be8761b142e16285f8240b3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 2 Mar 2011 16:09:50 +0000 Subject: [PATCH] Implemented first cut of more flexible depth partitioning code. --- examples/osgshadow/osgshadow.cpp | 157 ++++++++++++++++++++++++++++--- 1 file changed, 143 insertions(+), 14 deletions(-) diff --git a/examples/osgshadow/osgshadow.cpp b/examples/osgshadow/osgshadow.cpp index a326a73dc..2773a21b3 100644 --- a/examples/osgshadow/osgshadow.cpp +++ b/examples/osgshadow/osgshadow.cpp @@ -47,6 +47,7 @@ #include #include +#include #include @@ -568,37 +569,155 @@ osg::Node* createTestModel(osg::ArgumentParser& arguments) } +struct DepthPartitionSettings : public osg::Referenced +{ + DepthPartitionSettings() {} + + enum DepthMode + { + FIXED_RANGE, + BOUNDING_VOLUME + }; + + bool getDepthRange(osg::View& view, unsigned int partition, double& zNear, double& zFar) + { + switch(_mode) + { + case(FIXED_RANGE): + { + if (partition==0) + { + zNear = _zNear; + zFar = _zMid; + return true; + } + else if (partition==1) + { + zNear = _zMid; + zFar = _zFar; + return true; + } + return false; + } + case(BOUNDING_VOLUME): + { + osgViewer::View* view_withSceneData = dynamic_cast(&view); + const osg::Node* node = view_withSceneData ? view_withSceneData->getSceneData() : 0; + if (!node) return false; + + const osg::Camera* masterCamera = view.getCamera(); + if (!masterCamera) return false; + + osg::BoundingSphere bs = node->getBound(); + const osg::Matrixd& viewMatrix = masterCamera->getViewMatrix(); + //osg::Matrixd& projectionMatrix = masterCamera->getProjectionMatrix(); + + osg::Vec3d lookVectorInWorldCoords = osg::Matrixd::transform3x3(viewMatrix,osg::Vec3d(0.0,0.0,-1.0)); + lookVectorInWorldCoords.normalize(); + + osg::Vec3d nearPointInWorldCoords = bs.center() - lookVectorInWorldCoords*bs.radius(); + osg::Vec3d farPointInWorldCoords = bs.center() + lookVectorInWorldCoords*bs.radius(); + + osg::Vec3d nearPointInEyeCoords = nearPointInWorldCoords * viewMatrix; + osg::Vec3d farPointInEyeCoords = farPointInWorldCoords * viewMatrix; + +#if 0 + OSG_NOTICE<setNodeMask(0x0); + return; + } + else + { + camera->setNodeMask(0xffffff); + } if (camera->getProjectionMatrix()(0,3)==0.0 && camera->getProjectionMatrix()(1,3)==0.0 && camera->getProjectionMatrix()(2,3)==0.0) { - //OSG_NOTICE<<"MyUpdateSlaveCallback othographic camera"<getProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar); - camera->setProjectionMatrixAsOrtho(left, right, bottom, top, _zNear, _zFar); + camera->setProjectionMatrixAsOrtho(left, right, bottom, top, computed_zNear, computed_zFar); } else { double left, right, bottom, top, zNear, zFar; camera->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar); - //OSG_NOTICE<<"MyUpdateSlaveCallback perpective camera zNear="< dps = new DepthPartitionSettings; + + dps->_mode = DepthPartitionSettings::BOUNDING_VOLUME; + dps->_zNear = 0.5; + dps->_zMid = partitionPosition; + dps->_zFar = 1000.0; // far camera { @@ -667,7 +789,7 @@ void setUpViewForDepthPartion(osgViewer::Viewer& viewer, double partitionPositio viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); - slave._updateSlaveCallback = new MyUpdateSlaveCallback(zMid, zFar); + slave._updateSlaveCallback = new MyUpdateSlaveCallback(dps.get(), 1); } // near camera @@ -690,7 +812,7 @@ void setUpViewForDepthPartion(osgViewer::Viewer& viewer, double partitionPositio viewer.addSlave(camera.get(), osg::Matrix::scale(1.0, 1.0, scale_z)*osg::Matrix::translate(0.0, 0.0, translate_z), osg::Matrix() ); osg::View::Slave& slave = viewer.getSlave(viewer.getNumSlaves()-1); - slave._updateSlaveCallback = new MyUpdateSlaveCallback(zNear, zMid); + slave._updateSlaveCallback = new MyUpdateSlaveCallback(dps.get(), 0); } } @@ -824,7 +946,12 @@ int main(int argc, char** argv) shadowedScene->setCastsShadowTraversalMask(CastsShadowTraversalMask); osg::ref_ptr msm = NULL; - if (arguments.read("--sv")) + if (arguments.read("--no-shadows")) + { + OSG_NOTICE<<"Not using a ShadowTechnique"<setShadowTechnique(0); + } + else if (arguments.read("--sv")) { // hint to tell viewer to request stencil buffer when setting up windows osg::DisplaySettings::instance()->setMinimumNumStencilBits(8); @@ -951,6 +1078,8 @@ int main(int argc, char** argv) msm->setBaseTextureUnit( baseTexUnit ); } + OSG_NOTICE<<"shadowedScene->getShadowTechnique()="<getShadowTechnique()< model = osgDB::readNodeFiles(arguments); if (model.valid()) {