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

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