title: Basic shadow mapping artifacts using OpenGL and GLSL
body: <p>I've written a simple OpenGL test application about basic shadow mapping technique.</p>
  
  <p>I have removed most artifacts except for the one on the occluder back face. This back face is concerned by artifacts because during the first rendering pass (shadow depth map filling) I enable the front face culling. Consequently I have self-shadowing z-fighting artifacts.</p>
  
  <p>To solve this kind of problem it said on several tutorials the depth of the vertex position in light space need to be biased with a very small offset like 0.0005f.</p>
  
  <p>Here's a screenshot of my problem (for a sake of visibility I have increased the ambient light value for the box). Here without depth offset:</p>
  
  <p><img src="https://i.stack.imgur.com/iv3h6.png" alt="enter image description here"></p>
  
  <p>As you can see there is a strong self-shadowing.</p>
  
  <p>Here's a piece of code I use in my fragment shader:</p>
  
  <pre><code>if (ShadowCoords.w &gt; 0.0f)
  {
      vec4 tmp_shadow_coords = ShadowCoords;
  
      tmp_shadow_coords.z -= 0.0000f; //DEPTH OFFSET DISABLE HERE 
      shadowFactor = textureProj(ShadowMap, tmp_shadow_coords);
  }
  </code></pre>
  
  <p>Now let's see what happen when using an offset equal to 0.0002f:</p>
  
  <pre><code>tmp_shadow_coords.z -= 0.0002f;
  </code></pre>
  
  <p>The screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/AzNLo.png" alt="enter image description here"></p>
  
  <p>As you can see the self-shadowing is decreased. Now let's try with an offset equal to 0.0003f :</p>
  
  <pre><code>tmp_shadow_coords.z -= 0.0003f;
  </code></pre>
  
  <p>The screeshot:</p>
  
  <p><img src="https://i.stack.imgur.com/QFwvL.jpg" alt="enter image description here"></p>
  
  <p>As you can see self-shadowinh has disappeared! But if I zoom towards the limit between the occluder and the shadow we can see an unshaded area. This artifact is called Peter Panning artifact.</p>
  
  <p>So it's a strange situation : to avoid self-shadowing artifact I need to modify the vertex depth value in light space. But this modification of the depth value trains causes a Peter Panning artifact!</p>
  
  <p><img src="https://i.stack.imgur.com/nDttC.jpg" alt="enter image description here"></p>
  
  <p>The problem is worse if I increase the offset value.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I also tried adding an offset. For example:</p>
  
  <pre><code>tmp_shadow_coords.z += 0.0002f;
  </code></pre>
  
  <p>Screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/IMss9.png" alt="enter image description here"></p>
  
  <p>There's less self-shadowing but no Peter Panning artifact. But unfortunatly it appears some artifacts on the front faces of the cube. There's an overflow of the shadow on the front faces.</p>
  
  <p>If I increase the offset:</p>
  
  <pre><code>tmp_shadow_coords.z += 0.0003f;
  </code></pre>
  
  <p>Screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/8a7GL.png" alt="enter image description here"></p>
  
  <p>The self-shadowing artifacts have completely disappeared but the shadow overflow in front faces is worse.</p>
  
  <p>So I wonder if it's possible to have a render without both self-shadowing artifact and no Peter Panning artifact ?</p>