Question

I am running into issues when sampling points outside my light's view and therefore my shadow map. This is what I get whenever my shadow map is created with the texture parameter for GL_TEXTURE_WRAP set to GL_CLAMP or GL_CLAMP_TO_EDGE:

What I have determined is happening here is that every point outside the shadow map on the side of the map where there are shadows are considered in shadow and every point outside the map on the side where it is unshadowed is considered unshadowed as well. I think this is expected behavior for GL_CLAMP and GL_CLAMP_TO_EDGE, but I have not seen this issue on the various shadow mapping examples I have found around the web. Some of these issues are removed by using GL_CLAMP_TO_BORDER and setting a border color, but again this is not a fix I have seen in any of the examples I have looked at. Is this an expected issue when shadowing spotlights, and if so, what is the usual fix?

Was it helpful?

Solution

With GL_CLAMP_TO_EDGE, your shadow texture will get first and last pixel repeated in each dirrection, wich leads quite unpredictable results

enter image description here

if you clamp to border and set border color to (1,1,1,1), you'll get clean border but without shadow at all.

GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor,new float[] {1f,1f,1f,1f});

Extending your Projection size is the only way to have shadowing as expected on the whole scene (orthographic example where lightSize is the relevant size.):

lightProjection = Matrix4.CreateOrthographicOffCenter(-lightSize, lightSize, -lightSize, lightSize, lightZNear, lightZFar); 

but doing this you loose resolution on your shadow map, which have to be filtered with some common techniques, see 'soft shadow filtering' like pcf.

The step forward is than to use technique like 'cascading shadow' to handle multiple shadow map resolution depending on the scene distance...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top