1

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?

jbills
  • 694
  • 1
  • 7
  • 22

1 Answers1

6

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

j-p
  • 1,622
  • 10
  • 18
  • Ok, that makes sense, and that solution fixes almost all the problems. I still have one more with shadowing behind the light. Would the solution for that be some sort of z check? – jbills Mar 19 '14 at 23:56
  • can you explain a bit more, maybe post a new screen shot – j-p Mar 19 '14 at 23:57
  • Ok, so here is a screenshot with your solution applied: http://i.imgur.com/bhlrjFZ.png. Everything behind the light fustrum is put in shadow. Would some kind of z check be used to fix this? – jbills Mar 20 '14 at 00:03
  • Your projection bottom is the straight line thru your plane. So you have to center your frustum target and move your light position accordingly. thank you for the vote :-) – j-p Mar 20 '14 at 00:06
  • Ok maybe I used the wrong term; Everything behind the spotlight (z < 0 in light view space) is being placed in shadow. Can I fix this by using a z check? If not then what is the usual solution? – jbills Mar 20 '14 at 00:12
  • If it's behind, it will not be rendered at all in your shadow map. Z range is bound to z near and z far plane which have to be wisely set. According to opengl spec, specifying negative z near will cause error or unpredictable result, so consider it's not allowed. – j-p Mar 20 '14 at 00:35