From bdb0dfe27d53f89e67cbfdcf9892759d9f0beae8 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 17 Oct 2011 14:40:29 +0000 Subject: [PATCH] Improvements to osgShadow::ViewDependentShadowMap and ShadowSettings --- include/osgShadow/ShadowSettings | 10 +- src/osgShadow/ShadowSettings.cpp | 4 +- src/osgShadow/ViewDependentShadowMap.cpp | 226 +++++++++++++++++++---- 3 files changed, 197 insertions(+), 43 deletions(-) diff --git a/include/osgShadow/ShadowSettings b/include/osgShadow/ShadowSettings index 12f86d504..cea3ee161 100644 --- a/include/osgShadow/ShadowSettings +++ b/include/osgShadow/ShadowSettings @@ -36,7 +36,14 @@ class OSGSHADOW_EXPORT ShadowSettings : public osg::Object void setBaseShadowTextureUnit(unsigned int unit) { _baseShadowTextureUnit = unit; } unsigned int getBaseShadowTextureUnit() const { return _baseShadowTextureUnit; } - + + /** Set whether to use osg::StateAttribute::OVERRIDE for the shadow map texture. + * Enabling override will force the shadow map texture to override any texture set on the shadow maps texture unit.*/ + void setUseOverrideForShadowMapTexture(bool useOverride) { _useShadowMapTextureOverride = useOverride; } + + /** Get whether to use osg::StateAttribute::OVERRIDE for the shadow map texture. */ + bool getUseOverrideForShadowMapTexture() const { return _useShadowMapTextureOverride; } + /** Set the size of the shadow map textures.*/ void setTextureSize(const osg::Vec2s& textureSize) { _textureSize = textureSize; } @@ -84,6 +91,7 @@ class OSGSHADOW_EXPORT ShadowSettings : public osg::Object int _lightNum; unsigned int _baseShadowTextureUnit; + bool _useShadowMapTextureOverride; osg::Vec2s _textureSize; double _minimumShadowMapNearFarRatio; diff --git a/src/osgShadow/ShadowSettings.cpp b/src/osgShadow/ShadowSettings.cpp index 25c5e19e7..6634181db 100644 --- a/src/osgShadow/ShadowSettings.cpp +++ b/src/osgShadow/ShadowSettings.cpp @@ -18,8 +18,9 @@ using namespace osgShadow; ShadowSettings::ShadowSettings(): _lightNum(-1), _baseShadowTextureUnit(1), + _useShadowMapTextureOverride(true), _textureSize(2048,2048), - _minimumShadowMapNearFarRatio(0.01), + _minimumShadowMapNearFarRatio(0.05), _shadowMapProjectionHint(PERSPECTIVE_SHADOW_MAP), _perspectiveShadowMapCutOffAngle(2.0), _shaderHint(NO_SHADERS), @@ -32,6 +33,7 @@ ShadowSettings::ShadowSettings(const ShadowSettings& ss, const osg::CopyOp& copy Object(ss,copyop), _lightNum(ss._lightNum), _baseShadowTextureUnit(ss._baseShadowTextureUnit), + _useShadowMapTextureOverride(ss._useShadowMapTextureOverride), _textureSize(ss._textureSize), _minimumShadowMapNearFarRatio(ss._minimumShadowMapNearFarRatio), _shadowMapProjectionHint(ss._shadowMapProjectionHint), diff --git a/src/osgShadow/ViewDependentShadowMap.cpp b/src/osgShadow/ViewDependentShadowMap.cpp index 172a7a44e..1b4839391 100644 --- a/src/osgShadow/ViewDependentShadowMap.cpp +++ b/src/osgShadow/ViewDependentShadowMap.cpp @@ -330,6 +330,9 @@ ViewDependentShadowMap::ShadowData::ShadowData(ViewDependentShadowMap::ViewDepen _camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES); //_camera->setComputeNearFarMode(osg::Camera::COMPUTE_NEAR_FAR_USING_PRIMITIVES); + // swtich off small feature culling as this can cull out geometry that will still be large enough once perspective correction takes effect. + _camera->setCullingMode(_camera->getCullingMode() & ~osg::CullSettings::SMALL_FEATURE_CULLING); + // set viewport _camera->setViewport(0,0,textureSize.x(),textureSize.y()); @@ -506,7 +509,7 @@ ViewDependentShadowMap::Frustum::Frustum(osgUtil::CullVisitor* cv, double minZNe ViewDependentShadowMap::ViewDependentData::ViewDependentData(ViewDependentShadowMap* vdsm): _viewDependentShadowMap(vdsm) { - OSG_NOTICE<<"ViewDependentData::ViewDependentData()"<osg::Group::traverse(cv); return; } @@ -594,7 +597,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) if (!vdd) { - OSG_NOTICE<<"Warning, now ViewDependentData created, unable to create shadows."<osg::Group::traverse(cv); return; } @@ -642,6 +645,7 @@ void ViewDependentShadowMap::cull(osgUtil::CullVisitor& cv) if (cv.getComputeNearFarMode()!=osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR) { + OSG_INFO<<"Just done main subgraph traversak"<=side_y.length2()) ? side_x : side_y; + side.normalize(); + + osg::Vec3d up = side ^ normal; + up.normalize(); + + osg::Vec3d center; + for(Vertices::iterator itr = intersections.begin(); + itr != intersections.end(); + ++itr) + { + center += *itr; + } + + center /= double(intersections.size()); + + typedef std::map VertexMap; + VertexMap vertexMap; + for(Vertices::iterator itr = intersections.begin(); + itr != intersections.end(); + ++itr) + { + osg::Vec3d dv = (*itr-center); + double h = dv * side; + double v = dv * up; + double angle = atan2(h,v); + // OSG_NOTICE<<"angle = "<max_z) { max_z=ls.z(); /* OSG_NOTICE<<" + ";*/ } + + // OSG_NOTICE<<" bb.z() in ls = "<setTextureAttributeAndModes(sd._textureUnit, sd._texture.get(), osg::StateAttribute::ON/* | osg::StateAttribute::OVERRIDE*/); + stateset->setTextureAttributeAndModes(sd._textureUnit, sd._texture.get(), shadowMapModeValue); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); stateset->setTextureMode(sd._textureUnit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON);