title: Small artifacts with simple hard shadow mapping technique using OpenGL and GLSL
body: <p>I have a strange behaviour with my hard shadows I have never seen before because of the somber color of the textures I used (these artifacts was not visible). Here's some screenshots of my problem with 3 different shadowmap resolutions:</p>
  
  <p>1) Shadowmap 160 * 120</p>
  
  <p><img src="https://i.stack.imgur.com/jSNb8.png" alt="enter image description here"></p>
  
  <p>As you can see, there are kind of teeth on the surface of the cube.</p>
  
  <p>Here's the view of the scene from light position:</p>
  
  <p><img src="https://i.stack.imgur.com/HVblD.png" alt="enter image description here"></p>
  
  <p>Here's the shadowmap:</p>
  
  <p><img src="https://i.stack.imgur.com/xqHia.png" alt="enter image description here"></p>
  
  <p>2) Shadowmap 640 * 480</p>
  
  <p><img src="https://i.stack.imgur.com/jEJFS.png" alt="enter image description here"></p>
  
  <p>With a higher resolution the artifacts are less visible because the shadow map brings more precision.</p>
  
  <p>Here's the view of the scene from light position:</p>
  
  <p><img src="https://i.stack.imgur.com/vdE11.png" alt="enter image description here"></p>
  
  <p>Here's the shadowmap:</p>
  
  <p><img src="https://i.stack.imgur.com/zDx8y.png" alt="enter image description here"></p>
  
  <p>I tried with higher other resolution. Of course the artifacts are smaller and smaller but if you zoom on the surface they are still visible.</p>
  
  <p>Here's the code I use in my fragment shader during the second pass:</p>
  
  <pre><code>[...]
  
  uniform sampler2DShadow ShadowSampler[MAX_LIGHT_COUNT];
  
  [...]
  
  float GetDynamicShadowBias(vec3 normalDir_cs, vec3 lightDir_cs)
  {
      float cosTheta = clamp(dot(normalDir_cs, lightDir_cs), 0.0f, 1.0f);
      float bias = 0.005f * tan(acos(cosTheta));
  
      return (clamp(bias, 0.0f, 0.01f));
  }
  
  float GetBiased_Hard_ShadowFactor(vec3 normalDir, vec3 lightDir, int idx)
  {    
      if (ShadowCoords[idx].w &gt; 0.0f)
      {
          float bias = GetDynamicShadowBias(normalDir, lightDir);
  
          float LightToOccluderClipDist = texture(
              ShadowSampler[idx], ShadowCoords[idx].xyz/ShadowCoords[idx].w);
  
          float LightToVertexClipDist = (ShadowCoords[idx].z - bias)/ShadowCoords[idx].w;
  
          if (LightToOccluderClipDist &lt; LightToVertexClipDist)
              return (0.0f);
      }
      return (1.0f);
  }
  </code></pre>
  
  <p>So I wonder if it's possible to remove these artifacts on the edges of the cube. I tried to play with the shadow bias without real success (for information I use FRONT_FACE culling during the first pass (filling of the depth texture)).</p>