Jump to content
  • Advertisement
Sign in to follow this  
Alundra

ShadowMap ViewProj Matrix

This topic is 2399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I'm curently working on shadow mapping.
ShadowMap shader and ShadowOcclusion works good, I have tried them with a faked LightViewProj.
Now its time to have a real LightViewProj, but i have some problem with him.
My ShadowMap is a R32F, i clear it : R = 1.0.
When i render using the LightViewProj i computed, i don't see anything in the shadow map.

I let you see the code i do to compute the LightViewProj.


// Light direction.
const CVector3 LightDir = CVector3( 0.0f, -1.0f, 0.0f );

// Create the light view matrix.
CMatrix4 LightViewMatrix;
LightViewMatrix.LookAt( -LightDir, CVector3( 0.0f, 0.0f, 0.0f ), CVector3( 0.0f, 1.0f, 0.0f ) );

// Extract frustum points.
const float ScaleXInv = 1.0f / Camera->GetProjectionMatrix().a11;
const float ScaleYInv = 1.0f / Camera->GetProjectionMatrix().a22;

// Transform frustum corners into light view space.
CMatrix4 CameraViewToLightProj = Camera->GetViewMatrix().Inverse() * LightViewMatrix;

// Compute corners.
CVector3 Corners[ 8 ];

// Near corners ( view space ).
const float NearX = ScaleXInv * Camera->GetNear();
const float NearY = ScaleYInv * Camera->GetNear();

Corners[ 0 ] = CVector3( -NearX, NearY, Camera->GetNear() );
Corners[ 1 ] = CVector3( NearX, NearY, Camera->GetNear() );
Corners[ 2 ] = CVector3( -NearX, -NearY, Camera->GetNear() );
Corners[ 3 ] = CVector3( NearX, -NearY, Camera->GetNear() );

// Far corners ( view space ).
const float FarX = ScaleXInv * Camera->GetFar();
const float FarY = ScaleYInv * Camera->GetFar();

Corners[ 4 ] = CVector3( -FarX, FarY, Camera->GetFar() );
Corners[ 5 ] = CVector3( FarX, FarY, Camera->GetFar() );
Corners[ 6 ] = CVector3( -FarX, -FarY, Camera->GetFar() );
Corners[ 7 ] = CVector3( FarX, -FarY, Camera->GetFar() );

// Transform corners to light space.
CVector3 CornersLS[ 8 ];
for( UInt32 i = 0; i < 8; ++i )
CornersLS[ i ] = CameraViewToLightProj.Transform( Corners[ i ] );

// Compute min/max.
CVector3 Min = CornersLS[ 0 ];
CVector3 Max = CornersLS[ 0 ];
for( UInt32 i = 0; i < 8; ++i )
{
Min = VectorMinimize( Min, CornersLS[ i ] );
Max = VectorMaximize( Max, CornersLS[ i ] );
}

// First adjust the light matrix to be centered on the extents in x/y and behind everything in z.
const CVector3 Center = 0.5f * ( Min + Max );

CMatrix4 CenterTransform;
CenterTransform.SetTranslation( -Center.x, -Center.y, -Min.z );

LightViewMatrix *= CenterTransform;

// Create the light projection matrix.
const CVector3 Dimensions = Max - Min;

CMatrix4 LightProjectionMatrix;
LightProjectionMatrix.OrthoOffCenter( -Dimensions.x, Dimensions.x, -Dimensions.y, Dimensions.y, 0.0f, Dimensions.z );


Thanks for the help,
Alundra

Share this post


Link to post
Share on other sites
Advertisement
Is the last parameter of your LookAt method the up vector? If so, you can't use an up vector of (0, 1, 0) when your forward direction is (0, -1, 0). You need to be able to cross the up and forward vectors to get the right vector, which you can't do when they point in the same direction (or the opposite direction).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!