From 9cfb248b4622d7d202502fa8f10993ffd470ab3a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 23 Feb 2011 16:45:44 +0000 Subject: [PATCH] From Wojciech Lewandowski, various tweaks to osgShadow shadow mapping classes to improve consistency and enable more debug info output --- include/osgShadow/DebugShadowMap | 21 ++++++--- .../osgShadow/LightSpacePerspectiveShadowMap | 6 +-- include/osgShadow/MinimalCullBoundsShadowMap | 2 +- include/osgShadow/MinimalDrawBoundsShadowMap | 2 +- include/osgShadow/MinimalShadowMap | 2 +- include/osgShadow/ProjectionShadowMap | 2 +- include/osgShadow/StandardShadowMap | 2 +- .../osgShadow/ViewDependentShadowTechnique | 2 +- src/osgShadow/DebugShadowMap.cpp | 3 +- .../LightSpacePerspectiveShadowMap.cpp | 12 ++--- src/osgShadow/MinimalShadowMap.cpp | 31 ++++++++++++- src/osgShadow/StandardShadowMap.cpp | 46 +++++++++++++++++++ 12 files changed, 107 insertions(+), 24 deletions(-) diff --git a/include/osgShadow/DebugShadowMap b/include/osgShadow/DebugShadowMap index a472bc2dd..992de2be6 100644 --- a/include/osgShadow/DebugShadowMap +++ b/include/osgShadow/DebugShadowMap @@ -63,7 +63,7 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique DebugShadowMap(const DebugShadowMap& dsm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, DebugShadowMap ); + META_Object( osgShadow, DebugShadowMap ); /** Turn on/off debuging hud & rendering of debug volumes in main view */ void setDebugDraw( bool draw ) { _doDebugDraw = draw; } @@ -71,6 +71,12 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique /** Tell if debuging hud & rendering of debug volumes is active */ bool getDebugDraw( void ) const { return _doDebugDraw; } + /** Get the file name of debuging dump */ + std::string getDebugDump( void ) const { return _debugDump; } + + /** Set the file name of debuging dump */ + void setDebugDump( const std::string & debugDumpFile ) { _debugDump = debugDumpFile; } + protected: /** Classic protected OSG destructor */ virtual ~DebugShadowMap(); @@ -86,6 +92,7 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique osg::Vec2s _orthoOrigin; bool _doDebugDraw; + std::string _debugDump; osg::ref_ptr< osg::Shader > _depthColorFragmentShader; @@ -120,16 +127,17 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique typedef std::map< std::string, PolytopeGeometry > PolytopeGeometryMap; - osg::Vec2s _hudSize; - osg::Vec2s _hudOrigin; + osg::Vec2s _hudSize; + osg::Vec2s _hudOrigin; osg::Vec2s _viewportSize; osg::Vec2s _viewportOrigin; osg::Vec2s _orthoSize; osg::Vec2s _orthoOrigin; bool *_doDebugDrawPtr; + std::string *_debugDumpPtr; - PolytopeGeometryMap _polytopeGeometryMap; + PolytopeGeometryMap _polytopeGeometryMap; osg::ref_ptr< osg::Geode > _geode[2]; osg::ref_ptr< osg::MatrixTransform > _transform[2]; @@ -139,7 +147,8 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique osg::ref_ptr _cameraDebugHUD; - bool getDebugDraw() { return *_doDebugDrawPtr; } + bool getDebugDraw() { return *_doDebugDrawPtr; } + std::string * getDebugDump() { return _debugDumpPtr; } virtual void init( ThisClass * st, osgUtil::CullVisitor * cv ); @@ -172,7 +181,7 @@ class OSGSHADOW_EXPORT DebugShadowMap : public ViewDependentShadowTechnique const osg::Camera * shadowCam, const ConvexPolyhedron * hull ); - void dump( const char * filename = "DebugShadowDump.osg" ); + void dump( const std::string & filename ); }; META_ViewDependentShadowTechniqueData( ThisClass, ViewData ) diff --git a/include/osgShadow/LightSpacePerspectiveShadowMap b/include/osgShadow/LightSpacePerspectiveShadowMap index 4853bffbe..4b6fc7d56 100644 --- a/include/osgShadow/LightSpacePerspectiveShadowMap +++ b/include/osgShadow/LightSpacePerspectiveShadowMap @@ -66,7 +66,7 @@ class OSGSHADOW_EXPORT LightSpacePerspectiveShadowMapDB: public ProjectionShadow } /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, LightSpacePerspectiveShadowMapDB ); + META_Object( osgShadow, LightSpacePerspectiveShadowMapDB ); }; // Optimized for cull traversal shadow bounds @@ -89,7 +89,7 @@ class OSGSHADOW_EXPORT LightSpacePerspectiveShadowMapCB: public ProjectionShadow } /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, LightSpacePerspectiveShadowMapCB ); + META_Object( osgShadow, LightSpacePerspectiveShadowMapCB ); }; // Optimized for view frustum bounds @@ -112,7 +112,7 @@ class OSGSHADOW_EXPORT LightSpacePerspectiveShadowMapVB: public ProjectionShadow } /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, LightSpacePerspectiveShadowMapVB ); + META_Object( osgShadow, LightSpacePerspectiveShadowMapVB ); }; typedef LightSpacePerspectiveShadowMapDB LightSpacePerspectiveShadowMap; diff --git a/include/osgShadow/MinimalCullBoundsShadowMap b/include/osgShadow/MinimalCullBoundsShadowMap index 262f79e90..a00ad756f 100644 --- a/include/osgShadow/MinimalCullBoundsShadowMap +++ b/include/osgShadow/MinimalCullBoundsShadowMap @@ -39,7 +39,7 @@ class OSGSHADOW_EXPORT MinimalCullBoundsShadowMap const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, MinimalCullBoundsShadowMap ); + META_Object( osgShadow, MinimalCullBoundsShadowMap ); protected: /** Classic protected OSG destructor */ diff --git a/include/osgShadow/MinimalDrawBoundsShadowMap b/include/osgShadow/MinimalDrawBoundsShadowMap index 483201e20..b889729fb 100644 --- a/include/osgShadow/MinimalDrawBoundsShadowMap +++ b/include/osgShadow/MinimalDrawBoundsShadowMap @@ -40,7 +40,7 @@ class OSGSHADOW_EXPORT MinimalDrawBoundsShadowMap const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, MinimalDrawBoundsShadowMap ); + META_Object( osgShadow, MinimalDrawBoundsShadowMap ); protected: /** Classic protected OSG destructor */ diff --git a/include/osgShadow/MinimalShadowMap b/include/osgShadow/MinimalShadowMap index 52d915784..fe43de129 100644 --- a/include/osgShadow/MinimalShadowMap +++ b/include/osgShadow/MinimalShadowMap @@ -38,7 +38,7 @@ class OSGSHADOW_EXPORT MinimalShadowMap : public StandardShadowMap const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, MinimalShadowMap ); + META_Object( osgShadow, MinimalShadowMap ); void setModellingSpaceToWorldTransform( const osg::Matrix & modellingSpaceToWorld ) { _modellingSpaceToWorld = modellingSpaceToWorld; } diff --git a/include/osgShadow/ProjectionShadowMap b/include/osgShadow/ProjectionShadowMap index 44d807c9b..612a8a431 100644 --- a/include/osgShadow/ProjectionShadowMap +++ b/include/osgShadow/ProjectionShadowMap @@ -43,7 +43,7 @@ class OSGSHADOW_EXPORT ProjectionShadowMap : public MinimalBoundsBaseClass } /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, ProjectionShadowMap ); + META_Object( osgShadow, ProjectionShadowMap ); protected: diff --git a/include/osgShadow/StandardShadowMap b/include/osgShadow/StandardShadowMap index a40f79ca0..336472de2 100644 --- a/include/osgShadow/StandardShadowMap +++ b/include/osgShadow/StandardShadowMap @@ -37,7 +37,7 @@ class OSGSHADOW_EXPORT StandardShadowMap : public DebugShadowMap const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, StandardShadowMap ); + META_Object( osgShadow, StandardShadowMap ); void setBaseTextureUnit( unsigned int unit ) { _baseTextureUnit = unit; dirty(); } diff --git a/include/osgShadow/ViewDependentShadowTechnique b/include/osgShadow/ViewDependentShadowTechnique index 6044ab745..8c7b86960 100644 --- a/include/osgShadow/ViewDependentShadowTechnique +++ b/include/osgShadow/ViewDependentShadowTechnique @@ -119,7 +119,7 @@ class OSGSHADOW_EXPORT ViewDependentShadowTechnique /** Declaration of standard OSG object methods */ - META_Object( ViewDependentShadow, ViewDependentShadowTechnique ); + META_Object( osgShadow, ViewDependentShadowTechnique ); /** Dirty view data bits and force update of view data resources */ virtual void dirty(); diff --git a/src/osgShadow/DebugShadowMap.cpp b/src/osgShadow/DebugShadowMap.cpp index 37f73878d..d5c6e4827 100644 --- a/src/osgShadow/DebugShadowMap.cpp +++ b/src/osgShadow/DebugShadowMap.cpp @@ -388,6 +388,7 @@ void DebugShadowMap::ViewData::init( ThisClass *st, osgUtil::CullVisitor *cv ) BaseClass::ViewData::init( st, cv ); _doDebugDrawPtr = &st->_doDebugDraw; + _debugDumpPtr = &st->_debugDump; _hudSize = st->_hudSize; _hudOrigin = st->_hudOrigin; @@ -456,7 +457,7 @@ public: osg::ref_ptr< osg::Texture > _pTexture; }; -void DebugShadowMap::ViewData::dump( const char * filename ) +void DebugShadowMap::ViewData::dump( const std::string & filename ) { osg::ref_ptr< osg::Group > root = new osg::Group; osgUtil::CullVisitor * cv = _cv.get(); diff --git a/src/osgShadow/LightSpacePerspectiveShadowMap.cpp b/src/osgShadow/LightSpacePerspectiveShadowMap.cpp index 99224f3bc..1cbe79b60 100644 --- a/src/osgShadow/LightSpacePerspectiveShadowMap.cpp +++ b/src/osgShadow/LightSpacePerspectiveShadowMap.cpp @@ -169,12 +169,12 @@ void LightSpacePerspectiveShadowMapAlgorithm::operator() osg::Matrix lightViewToWorld = cameraShadow->getInverseViewMatrix(); - osg::Vec3 eyePos = osg::Vec3( 0, 0, 0 ) * eyeViewToWorld; + osg::Vec3d eyePos = osg::Vec3d( 0, 0, 0 ) * eyeViewToWorld; - osg::Vec3 viewDir( osg::Matrix::transform3x3( osg::Vec3(0,0,-1), eyeViewToWorld ) ); + osg::Vec3d viewDir( osg::Matrix::transform3x3( osg::Vec3d(0,0,-1), eyeViewToWorld ) ); - osg::Vec3 lightDir( osg::Matrix::transform3x3( osg::Vec3( 0,0,-1), lightViewToWorld ) ); - osg::Vec3 up( osg::Matrix::transform3x3( osg::Vec3(0,1,0), lightViewToWorld ) ); + osg::Vec3d lightDir( osg::Matrix::transform3x3( osg::Vec3d( 0,0,-1), lightViewToWorld ) ); + osg::Vec3d up( osg::Matrix::transform3x3( osg::Vec3d(0,1,0), lightViewToWorld ) ); osg::Matrix lightView; // compute coarse light view matrix lightView.makeLookAt( eyePos, eyePos + lightDir, up ); @@ -237,8 +237,8 @@ void LightSpacePerspectiveShadowMapAlgorithm::operator() bb = computeScenePolytopeBounds ( cameraShadow->getViewMatrix() * cameraShadow->getProjectionMatrix() ); - if( !osg::equivalent( 0.f, (bb._min - osg::Vec3(-1,-1,-1)).length2() ) || - !osg::equivalent( 0.f, (bb._max - osg::Vec3( 1, 1, 1)).length2() ) ) + if( !osg::equivalent( 0.f, (bb._min - osg::Vec3d(-1,-1,-1)).length2() ) || + !osg::equivalent( 0.f, (bb._max - osg::Vec3d( 1, 1, 1)).length2() ) ) { bb = computeScenePolytopeBounds ( cameraShadow->getViewMatrix() * cameraShadow->getProjectionMatrix() ); diff --git a/src/osgShadow/MinimalShadowMap.cpp b/src/osgShadow/MinimalShadowMap.cpp index 4170563bb..e13dcad7e 100644 --- a/src/osgShadow/MinimalShadowMap.cpp +++ b/src/osgShadow/MinimalShadowMap.cpp @@ -143,7 +143,7 @@ void MinimalShadowMap::ViewData::aimShadowCastingCamera // We compute this vector on -ZY view plane, perpendicular to light direction // Matrix m = ViewToWorld #if 0 - osg::Matrix m = osg::Matrix::inverse( *cv.getModelViewMatrix() ); + osg::Matrix m = osg::Matrix::inverse( _cv->getModelViewMatrix() ); osg::Vec3 camFw( -m( 2, 0 ), -m( 2, 1 ), -m( 2, 2 ) ); camFw.normalize(); @@ -244,6 +244,34 @@ void MinimalShadowMap::ViewData::frameShadowCastingCamera ( cameraMain, cameraShadow, &_sceneReceivingShadowPolytope ); #endif + if( pass == _frameShadowCastingCameraPasses - 1 ) + { +#if 1 + { + osg::Matrix mvp = cameraShadow->getViewMatrix() * cameraShadow->getProjectionMatrix(); + ConvexPolyhedron frustum; + frustum.setToUnitFrustum(); + frustum.transform( osg::Matrix::inverse( mvp ), mvp ); + + setDebugPolytope( "shadowCamFrustum", frustum, osg::Vec4(0,0,1,1) ); + } + + { + osg::Matrix mvp = cameraMain->getViewMatrix() * cameraMain->getProjectionMatrix(); + ConvexPolyhedron frustum; + frustum.setToUnitFrustum(); + frustum.transform( osg::Matrix::inverse( mvp ), mvp ); + + setDebugPolytope( "mainCamFrustum", frustum, osg::Vec4(1,1,1,1) ); + } +#endif + std::string * filename = getDebugDump( ); + if( filename && !filename->empty() ) + { + dump( *filename ); + filename->clear(); + } + } } void MinimalShadowMap::ViewData::cullShadowReceivingScene( ) @@ -294,7 +322,6 @@ void MinimalShadowMap::ViewData::cullShadowReceivingScene( ) setDebugPolytope ( "frustum", _sceneReceivingShadowPolytope, osg::Vec4(1,0,1,1)); - } void MinimalShadowMap::ViewData::init( ThisClass *st, osgUtil::CullVisitor *cv ) diff --git a/src/osgShadow/StandardShadowMap.cpp b/src/osgShadow/StandardShadowMap.cpp index 3634215a3..d1581678b 100644 --- a/src/osgShadow/StandardShadowMap.cpp +++ b/src/osgShadow/StandardShadowMap.cpp @@ -29,6 +29,7 @@ using namespace osgShadow; #define DISPLAY_SHADOW_TEXEL_TO_PIXEL_ERROR 0 +#define FRAGMENT_SHADERS_ONLY 1 StandardShadowMap::StandardShadowMap(): @@ -42,6 +43,50 @@ StandardShadowMap::StandardShadowMap(): _shadowTextureCoordIndex( 1 ) { +#if FRAGMENT_SHADERS_ONLY + _mainFragmentShader = new osg::Shader( osg::Shader::FRAGMENT, + " // following expressions are auto modified - do not change them: \n" + " // gl_TexCoord[0] 0 - can be subsituted with other index \n" + " \n" + "float DynamicShadow( ); \n" + " \n" + "uniform sampler2D baseTexture; \n" + " \n" + "void main(void) \n" + "{ \n" + " vec4 colorAmbientEmissive = gl_FrontLightModelProduct.sceneColor; \n" + " // Add ambient from Light of index = 0 \n" + " colorAmbientEmissive += gl_FrontLightProduct[0].ambient; \n" + " vec4 color = texture2D( baseTexture, gl_TexCoord[0].xy ); \n" + " color *= mix( colorAmbientEmissive, gl_Color, DynamicShadow() ); \n" +#if DISPLAY_SHADOW_TEXEL_TO_PIXEL_ERROR + " color.xy = abs( dFdy( gl_TexCoord[1].xy / gl_TexCoord[1].w ) )* 1024.0; \n" + " color.z = color.y; \n" + " color.x = color.z; \n" + " color.y = color.z; \n" + " color.a = 1.0; \n" +#endif +// " float fog = clamp((gl_Fog.end - gl_FogFragCoord)*gl_Fog.scale, 0.,1.);\n" +// " color.rgb = mix( gl_Fog.color.rgb, color.rgb, fog ); \n" + " gl_FragColor = color; \n" + "} \n" ); + + _shadowFragmentShader = new osg::Shader( osg::Shader::FRAGMENT, + " // following expressions are auto modified - do not change them: \n" + " // gl_TexCoord[1] 1 - can be subsituted with other index \n" + " \n" + "uniform sampler2DShadow shadowTexture; \n" + " \n" + "float DynamicShadow( ) \n" + "{ \n" + " return shadow2DProj( shadowTexture, gl_TexCoord[1] ).r; \n" + "} \n" ); + + + _shadowVertexShader = NULL; + _mainVertexShader = NULL; + +#else _mainFragmentShader = new osg::Shader( osg::Shader::FRAGMENT, " // following expressions are auto modified - do not change them: \n" " // gl_TexCoord[0] 0 - can be subsituted with other index \n" @@ -287,6 +332,7 @@ StandardShadowMap::StandardShadowMap(): " \n" " gl_FogFragCoord = ecLen; \n" "} \n" ); +#endif } StandardShadowMap::StandardShadowMap(const StandardShadowMap& copy, const osg::CopyOp& copyop) :