From 1d540656465dc36c6a248984712b494063ab6c92 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 17 Aug 2011 16:09:17 +0000 Subject: [PATCH] intermin check-in of work on new LispSM implementation --- include/osgShadow/ViewDependentShadowMap | 4 + src/osgShadow/ViewDependentShadowMap.cpp | 256 +++++++++++++++++++++-- 2 files changed, 238 insertions(+), 22 deletions(-) diff --git a/include/osgShadow/ViewDependentShadowMap b/include/osgShadow/ViewDependentShadowMap index ba180f237..3d18e68c7 100644 --- a/include/osgShadow/ViewDependentShadowMap +++ b/include/osgShadow/ViewDependentShadowMap @@ -64,6 +64,7 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique typedef std::vector Edges; Edges edges; + osg::Vec3d eye; osg::Vec3d centerNearPlane; osg::Vec3d centerFarPlane; osg::Vec3d center; @@ -161,6 +162,9 @@ class OSGSHADOW_EXPORT ViewDependentShadowMap : public ShadowTechnique virtual bool perspectiveShadowMapCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Camera* camera); virtual bool lightSpacePerspectiveShadowMapCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Camera* camera); + virtual bool adjustPerspectiveShadowMapCameraSettings(Frustum& frustum, LightData& positionedLight, osg::Camera* camera); + + virtual bool assignTexGenSettings(osgUtil::CullVisitor* cv, osg::Camera* camera, unsigned int textureUnit, osg::TexGen* texgen); virtual void cullShadowReceivingScene(osgUtil::CullVisitor* cv) const; diff --git a/src/osgShadow/ViewDependentShadowMap.cpp b/src/osgShadow/ViewDependentShadowMap.cpp index d9d61fc25..d62724312 100644 --- a/src/osgShadow/ViewDependentShadowMap.cpp +++ b/src/osgShadow/ViewDependentShadowMap.cpp @@ -46,8 +46,11 @@ class VDSMCameraCullCallback : public osg::NodeCallback virtual void operator()(osg::Node*, osg::NodeVisitor* nv); + osg::RefMatrix* getProjectionMatrix() { return _projectionMatrix.get(); } + protected: + osg::ref_ptr _projectionMatrix; ViewDependentShadowMap* _vdsm; osg::Polytope _polytope; }; @@ -88,6 +91,9 @@ void VDSMCameraCullCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) if (cv->getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) { + // make sure that the near plane is computed correctly. + cv->computeNearPlane(); + osg::Matrixd projection = *(cv->getProjectionMatrix()); OSG_INFO<<"RTT Projection matrix "<setProjectionMatrix(projection); + + _projectionMatrix = cv->getProjectionMatrix(); } - } /////////////////////////////////////////////////////////////////////////////////////////////// @@ -168,7 +175,7 @@ void ViewDependentShadowMap::LightData::setLightData(osg::RefMatrix* lm, const o lightDir.normalize(); if (lightMatrix.valid()) { - OSG_NOTICE<<" Light matrix "<<*lightMatrix<setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); _camera->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); - //camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); + //_camera->setComputeNearFarMode(osg::Camera::DO_NOT_COMPUTE_NEAR_FAR); // set viewport _camera->setViewport(0,0,textureSize,textureSize); @@ -306,10 +313,13 @@ ViewDependentShadowMap::Frustum::Frustum(osgUtil::CullVisitor* cv): OSG_INFO<<" corner "<<*itr<osg::Group::traverse(cv); return; } - + + OSG_INFO<<"cv->getProjectionMatrix()="<<*cv.getProjectionMatrix()<getViewMatrix()); polytope.transformProvidingInverse(invertModelView); - camera->setCullCallback(new VDSMCameraCullCallback(this, polytope)); + osg::ref_ptr vdsmCallback = new VDSMCameraCullCallback(this, polytope); + camera->setCullCallback(vdsmCallback.get()); // 4.3 traverse RTT camera // @@ -573,6 +597,24 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) cv.popStateSet(); +#ifdef NEW_ADJUST + switch(_shadowMapProjectionHint) + { + case(PERSPECTIVE_SHADOW_MAP): + { + OSG_NOTICE<<"******************New adjustment of shadow"<getProjectionMatrix()) + { + vdsmCallback->getProjectionMatrix()->set(camera->getProjectionMatrix()); + } + break; + } + case(LIGHT_SPACE_PERSPECTIVE_SHADOW_MAP): break; + default: break; + } +#endif + // 4.4 compute main scene graph TexGen + uniform settings + setup state // @@ -600,6 +642,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) decoratorStateGraph->setStateSet(selectStateSetForRenderingShadow(*vdd)); } + OSG_NOTICE<<"End of shadow setup Projection matrix "<<*cv.getProjectionMatrix()< cull_face = new osg::CullFace; - cull_face->setMode(osg::CullFace::FRONT); - _shadowCastingStateSet->setAttribute(cull_face.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); - _shadowCastingStateSet->setMode(GL_CULL_FACE, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); -#endif +#if 0 + // note soft (attribute only no mode override) setting. When this works ? + // 1. for objects prepared for backface culling + // because they usually also set CullFace and CullMode on in their state + // For them we override CullFace but CullMode remains set by them + // 2. For one faced, trees, and similar objects which cannot use + // backface nor front face so they usually use CullMode off set here. + // In this case we will draw them in their entirety. + + _shadowCastingStateSet->setAttribute( new osg::CullFace( osg::CullFace::FRONT ), + osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE ); + + // make sure GL_CULL_FACE is off by default + // we assume that if object has cull face attribute set to back + // it will also set cull face mode ON so no need for override + _shadowCastingStateSet->setMode( GL_CULL_FACE, osg::StateAttribute::OFF ); +#endif + #if 1 float factor = 1.1; float units = 4.0; +#else + float factor = -1.1; + float units = -4.0; +#endif _polygonOffset = new osg::PolygonOffset(factor, units); _shadowCastingStateSet->setAttribute(_polygonOffset.get(), osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); _shadowCastingStateSet->setMode(GL_POLYGON_OFFSET_FILL, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE); -#endif + _uniforms.clear(); osg::ref_ptr baseTextureSampler = new osg::Uniform("baseTexture",(int)_baseTextureUnit); _uniforms.push_back(baseTextureSampler.get()); @@ -824,15 +881,21 @@ bool ViewDependentShadowMap::standardShadowMapCameraSettings(Frustum& frustum, L { OSG_INFO<<"standardShadowMapCameraSettings()"< lightSide_z.length()) ? lightSide_y : lightSide_z; lightSide.normalize(); - - //osg::Vec3d lightSide = positionedLight.lightDir ^ frustum.frustumCenterLine; lightSide.normalize(); +#else + osg::Vec3d lightSide = positionedLight.lightDir ^ frustum.frustumCenterLine; lightSide.normalize(); +#endif osg::Vec3d lightUp = lightSide ^ positionedLight.lightDir; + OSG_NOTICE<<"positionedLight.lightDir="<getProjectionMatrix(); + osg::Matrixd light_v = camera->getViewMatrix(); + osg::Matrixd light_vp = light_v * light_p; + osg::Vec3d lightdir(0.0,0.0,-1.0); + + OSG_NOTICE<<"light_v="<setMode(osg::TexGen::EYE_LINEAR);