title: OpenGL shadow mapping almost working
body: <p>I am trying to add real time shadow mapping to my Minecraft-like clone as per the tutorial: <a href="http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/" rel="nofollow noreferrer">here</a></p>
  
  <p>First what happens: </p>
  
  <p><img src="https://i.stack.imgur.com/N7l9R.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/1QRk6.png" alt="enter image description here"></p>
  
  <p>From what I can tell, the shadow map is rendering correctly? The problem seems to be during the final pass.</p>
  
  <p>I create the depth texture:</p>
  
  <pre><code>glGenFramebuffers(1, &amp;m_frameBufferId);
  glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
  glGenTextures(1, &amp;m_depthTexture);
  glBindTexture(GL_TEXTURE_2D, m_depthTexture);
  glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
  
  glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_depthTexture, 0);
  glDrawBuffer(GL_NONE);
  </code></pre>
  
  <p>Note: I also tried using GL_COMPARE_R_TO_TEXTURE with sampler2DShadow in the shader. Same outcome.</p>
  
  <p>Then run both passes:</p>
  
  <pre><code>// ====================    Shadow    =====================
  glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
  glViewport(0, 0, 1024, 1024);
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  ShadowShader-&gt;Bind();
  VCSceneGraph::Instance-&gt;RenderGraph();
  
  
  // ====================    Final    =======================
  glBindFramebuffer(GL_FRAMEBUFFER, 0);
  glViewport(0, 0, 1280, 800);
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  VoxelShader-&gt;Bind();
  VCSceneGraph::Instance-&gt;RenderGraph();
  </code></pre>
  
  <p>Shadow Shader code:</p>
  
  <pre><code>// ====================    Vert    =====================
  #version 330 core\n
  
  in vec4 vertexPosition_modelspace;
  uniform mat4 depthMVP;
  
  void main()
  {
      gl_Position =  depthMVP * vertexPosition_modelspace;
  }
  
  // ====================    Frag    =====================
  #version 330 core\n
  
  out float fragmentdepth;
  
  void main()
  {
      fragmentdepth = gl_FragCoord.z;
  }
  </code></pre>
  
  <p>Final-Pass (Voxel Shader) code:</p>
  
  <pre><code>// ====================    Vert    =====================
  #version 330 core\n
  
  in vec3 position;
  in int normal;
  in vec4 color;
  
  out vec4 colorVarying;
  out vec4 ShadowCoord;
  
  uniform mat4 modelViewProjectionMatrix;
  uniform mat4 DepthBiasMVP;
  
  void main()
  {
      gl_Position =  modelViewProjectionMatrix * vec4(position, 1);
      ShadowCoord = DepthBiasMVP * vec4(position, 1);
      colorVarying = color;
  }
  
  // ====================    Frag    =====================
  #version 330 core\n
  
  in vec4 colorVarying;
  in vec4 ShadowCoord;
  
  out vec4 color;
  
  uniform sampler2D  shadowMap;
  
  void main()
  {
      float visibility = 1.0;
  
      if ( texture(shadowMap, ShadowCoord.xy).z  &lt;  ShadowCoord.z)
      {
          visibility = 0.5;
      }
  
      color.xyz = colorVarying.xyz * visibility;
      color.w = colorVarying.w;
  }
  </code></pre>
  
  <p>I suspect my issue is in the code that updated the uniforms for the final pass:</p>
  
  <pre><code>// Re-create the exact same MVP matrix that was used for the shadow pass
  glm::mat4 depthProjectionMatrix = glm::ortho&lt;float&gt;( -30, 30, -30, 30, -100, 100);
  glm::mat4 depthViewMatrix = glm::lookAt(glm::vec3(0.5f, 2, 2), glm::vec3(0,0,0), glm::vec3(0,1,0));
  depthViewMatrix = glm::translate(depthViewMatrix, -15.0f, 30.0f, 0.0f);
  glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix;// * modelMatrix;
  
  // Multiply it be the bias matrix
  glm::mat4 biasMatrix
  (
      0.5, 0.0, 0.0, 0.0,
      0.0, 0.5, 0.0, 0.0,
      0.0, 0.0, 0.5, 0.0,
      0.5, 0.5, 0.5, 1.0
  );
  
  glm::mat4 depthBiasMVP = biasMatrix * depthMVP;
  
  // Create Camera's MVP
  VCCamera* currentCamera = VCSceneGraph::Instance-&gt;CurrentRenderingCamera;
  glm::mat4 ProjectionMatrix = currentCamera-&gt;ProjectionMatrix;
  glm::mat4 ViewMatrix = currentCamera-&gt;ViewMatrix;
  glm::mat4 MVP = ProjectionMatrix * ViewMatrix;// * modelMatrix;
  
  // Update uniforms
  glUniformMatrix4fv(m_unifMVP, 1, GL_FALSE, &amp;MVP[0][0]);
  glUniformMatrix4fv(m_unifDepthMVP, 1, GL_FALSE, &amp;depthBiasMVP[0][0]);
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I fixed the problem by making the Shadow texture the same resolution as the window. I cannot however explain WHY this fixes it. If anyone else want to take a crack at it I will accept your answer.  The expected output is this:
  <img src="https://i.stack.imgur.com/4WnRs.png" alt="enter image description here"></p>
  
  <p>I should have also noted that full source is available at: <a href="https://github.com/AThilenius/VoxelCraft/tree/Thilenius" rel="nofollow noreferrer">GitHub</a>.</p>