title: Cascaded Shadow Map shimmering
body: <p>I am currently trying to learn how cascaded shadow maps work so I've been trying to get one shadow map to fit to the view frustum without shimmering. I'm using a near/far plane of 1 to 10000 for my camera projection and this is the way I calculate the orthographic matrix for the light:</p>
  
  <pre><code>GLfloat far = -INFINITY;
  GLfloat near = INFINITY;
  
  //Multiply all the world space frustum corners with the view matrix of the light
  Frustum cameraFrustum = CameraMan.getActiveCamera()-&gt;mFrustum;
  lightViewMatrix = glm::lookAt((cameraFrustum.frustumCenter - glm::vec3(-0.447213620f, -0.89442790f, 0.0f)), cameraFrustum.frustumCenter, glm::vec3(0.0f, 0.0f, 1.0f));
  
  glm::vec3 arr[8];
  for (unsigned int i = 0; i &lt; 8; ++i)
      arr[i] = glm::vec3(lightViewMatrix * glm::vec4(cameraFrustum.frustumCorners[i], 1.0f));
  
  glm::vec3 minO = glm::vec3(INFINITY, INFINITY, INFINITY);
  glm::vec3 maxO = glm::vec3(-INFINITY, -INFINITY, -INFINITY);
  
  for (auto&amp; vec : arr)
  {
      minO = glm::min(minO, vec);
      maxO = glm::max(maxO, vec);
  }
  
  far = maxO.z;
  near = minO.z;
  
  //Get the longest diagonal of the frustum, this along with texel sized increments is used to keep the shadows from shimmering
  //far top right - near bottom left
  glm::vec3 longestDiagonal = cameraFrustum.frustumCorners[0] - cameraFrustum.frustumCorners[6];
  GLfloat lengthOfDiagonal = glm::length(longestDiagonal);
  longestDiagonal = glm::vec3(lengthOfDiagonal);
  
  glm::vec3 borderOffset = (longestDiagonal - (maxO - minO)) * glm::vec3(0.5f, 0.5f, 0.5f);
  
  borderOffset *= glm::vec3(1.0f, 1.0f, 0.0f);
  
  maxO += borderOffset;
  minO -= borderOffset;
  
  GLfloat worldUnitsPerTexel = lengthOfDiagonal / 1024.0f;
  glm::vec3 vWorldUnitsPerTexel = glm::vec3(worldUnitsPerTexel, worldUnitsPerTexel, 0.0f);
  minO /= vWorldUnitsPerTexel;
  minO = glm::floor(minO);
  minO *= vWorldUnitsPerTexel;
  
  maxO /= vWorldUnitsPerTexel;
  maxO = glm::floor(maxO);
  maxO *= vWorldUnitsPerTexel; 
  
  lightOrthoMatrix = glm::ortho(minO.x, maxO.x, minO.y, maxO.y, near, far);
  </code></pre>
  
  <p>The use of the longest diagonal to offset the frustum seems to be working as the shadow map doesn't seem to shrink/scale when looking around, however using the texel sized increments described by <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ee416324(v=vs.85).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/windows/desktop/ee416324(v=vs.85).aspx</a> has no effect whatsoever. I am using a pretty large scene for testing, which results in a low resolution on my shadow maps, but I wanted to get a stabilized shadow that fits a view frustum before I move on to splitting the frustum up. It's hard to tell from images, but the shimmering effect isn't reduced by the solution that microsoft presented: <a href="https://i.stack.imgur.com/1elMI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1elMI.png" alt="enter image description here"></a></p>