Sign in to follow this  
gchewood

Problem with stable shadow mapping.

Recommended Posts

I'm using a basic orthographic projection to create shadow mapping for a direction light in XNA.

I ran into the (I assume common) problem of the shimmering edges of the shadows as is described in this post:
[url="http://www.gamedev.net/topic/591684-xna-40---shimmering-shadow-maps/"]http://www.gamedev.n...ng-shadow-maps/[/url]

I implemented the solution described in the post and it works great. HOWEVER, It seems to cut some of the shadows off. I've attached an image showing the problem:

[attachment=12403:problem.jpg]

The code I'm using to calculate the matrices can be found below. Playing with a few of the variables, it seems that this gap in the shadow is down to the ExtraBackup variable being too high. If I reduce it from 20 to 0, the shadows are nearly back to normal. However, this obviously creates different problems as not all onscreen models will be caught in the bounding sphere. Someone help!!!!

[CODE]
public void UpdateShadows(Vector3 lightDir, Matrix viewProjection)
{

BoundingFrustum frustum = new BoundingFrustum(viewProjection);


BoundingSphere sphere = BoundingSphere.CreateFromFrustum(frustum);
const float ExtraBackup = 20.0f;
const float NearClip = 1.0f;
float backupDist = ExtraBackup + NearClip + sphere.Radius;
Vector3 shadowCamPos = sphere.Center - (lightDir * backupDist);
lightViewMatrix = Matrix.CreateLookAt(shadowCamPos, sphere.Center, Vector3.Up);
float bounds = sphere.Radius * 2.0f;
float farClip = backupDist + sphere.Radius;
lightProjectionMatrix = Matrix.CreateOrthographic(bounds, bounds, NearClip, farClip);
lightViewProjectionMatrix = lightViewMatrix * lightProjectionMatrix;

//Make shadows stable under camera translation
float ShadowMapSize = 1024.0f;
Vector3 shadowOrigin = Vector3.Transform(Vector3.Zero, lightViewProjectionMatrix);
shadowOrigin *= (ShadowMapSize / 2.0f);
Vector2 roundedOrigin =new Vector2((float)Math.Round(shadowOrigin.X), (float)Math.Round(shadowOrigin.Y));
Vector2 rounding = roundedOrigin - new Vector2(shadowOrigin.X, shadowOrigin.Y); ;
rounding /= (ShadowMapSize / 2.0f);
Matrix roundMatrix = Matrix.CreateTranslation(rounding.X, rounding.Y, 0.0f);
lightViewProjectionMatrix *= roundMatrix;
}
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this