From Wojciech Lewandowski, various tweaks to osgShadow shadow mapping classes to improve consistency and enable more debug info output

This commit is contained in:
Robert Osfield
2011-02-23 16:45:44 +00:00
parent c45717fef0
commit 9cfb248b46
12 changed files with 107 additions and 24 deletions

View File

@@ -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<osg::Camera> _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 )

View File

@@ -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;

View File

@@ -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 */

View File

@@ -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 */

View File

@@ -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; }

View File

@@ -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:

View File

@@ -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(); }

View File

@@ -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();

View File

@@ -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();

View File

@@ -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() );

View File

@@ -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 )

View File

@@ -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) :