Problem with stable shadow mapping.

Started by
2 comments, last by gchewood 11 years, 5 months ago
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:
http://www.gamedev.n...ng-shadow-maps/

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!!!!


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;
}
Advertisement
http://msdn.microsoft.com/en-us/library/windows/desktop/ee416324(v=vs.85).aspx
Peter Panning eh? Thanks I'll see if I can figure it out.
Ok well I've tried playing with the near and far planes, and I've tried altering the depth bias. No luck so far.

Any advice is welcome!

This topic is closed to new replies.

Advertisement