title: OpenGL - Shadow map/Depth Map giving weird result
body: <p>I'm trying to render my depth map, for now I've rendered it onto the objects in the scene to get a overview of what the result I'm getting is. From what I've learned, the black values are objects that are close to the light, and white is further away. Please correct me if I've gotten it wrong. This is the result I'm getting:<a href="https://imgur.com/a/W1r24" rel="nofollow noreferrer">Shadow map rendered onto a cube</a></p>
  
  <p>With this result, I speculate that I've created the framebuffer in the wrong way. this is how I generate it:</p>
  
  <pre><code>void createFrameBuffer()
  {
      glGenFramebuffers(1, &amp;frameBuffer);
  
  
      glGenTextures(1, &amp;DepthMap);
      glBindTexture(GL_TEXTURE_2D, DepthMap);
  
      glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
          SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  
      glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
  
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,                                 
          GL_TEXTURE_2D, DepthMap, 0);
  
      glDrawBuffer(GL_NONE);
      glReadBuffer(GL_NONE);
  
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  }
  </code></pre>
  
  <p>We have followed the <a href="https://learnopengl.com/#!Advanced-Lighting/Shadows/Shadow-Mapping" rel="nofollow noreferrer">Learn OpenGL - Shadow Mapping</a> tutorial, and our shaders looks almost identical to his. So there shouldn't be any problem there. I think I've misunderstood something in the CPU part of shadow mapping, and not the GPU.This is how I draw everything, set up matrices and use programs. </p>
  
  <pre><code>float orthoValue = 20.0f;
  
  glm::mat4 lightProjection = glm::ortho(-orthoValue,
      orthoValue, -orthoValue, orthoValue, NEARPLANE, FARPLANE);
  glm::mat4 lightView = glm::lookAt(lightPos, glm::vec3(0.0f),
      glm::vec3(0.0, 1.0f, 0.0));
  glm::mat4 lightSpaceMatrix = lightProjection * lightView;
  //lightSpaceMatrix[3] = glm::vec4(lightPos, 1.0f);
  
  if (shadows == true) {
  
      glUseProgram(sShaderProgram);
      glBindVertexArray(mesh.VAO);
      glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
      glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
      glClear(GL_DEPTH_BUFFER_BIT);
      glActiveTexture(GL_TEXTURE0);
  
  
      glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "lightSpaceMatrix"),
          1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
      glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "modelMatrix"),
          1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));
  
      glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());
      glBindFramebuffer(GL_FRAMEBUFFER, 0);
      glBindVertexArray(0);
  
      setViewPort();
  }
  </code></pre>
  
  <p>I'm really grateful for all of the tips and help in advance. If I've left out crucial information, I'll add whatever's missing.</p>
  
  <p><strong>More code of the rendering part edit, also edited the way we draw:</strong></p>
  
  <p>This is the main loop</p>
  
  <pre><code>                glClearColor(0.0, 0.0, 0.5, 1);
                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                  glDisable(GL_CULL_FACE);
                  glEnable(GL_LIGHTING);
  
                  //Keyboard input
                  keyPressed();
                  //Mouse movement
                  MousePointer();
  
                  lightProjection = glm::ortho(-orthoValue,
                      orthoValue, -orthoValue, orthoValue, NEARPLANE, FARPLANE);
                  glm::lookAt(lightPos, glm::vec3(0.0f),
                      glm::vec3(0.0, 1.0, 0.0));
                  lightSpaceMatrix = lightProjection * lightView;
  
                  glUseProgram(sShaderProgram);
                  glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
                  glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
                  glClear(GL_DEPTH_BUFFER_BIT);
  
                  glActiveTexture(GL_TEXTURE0);
                  glBindTexture(GL_TEXTURE_2D, DepthMap);
  
                  GLint samplerLoc = glGetUniformLocation(sShaderProgram, "DepthMap");
                  glUniform1i(samplerLoc, 0);
  
                  glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "lightSpaceMatrix"),
                      1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
  
                  for (int i = 0; i &lt; objmesh.size(); i++) {
                      RenderShadows(*objmesh[i]);
                  }
                  glBindFramebuffer(GL_FRAMEBUFFER, 0);
                  setViewPort();
                  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                  //glDisable(GL_DEPTH_TEST);
  
                  for (int i = 0; i &lt; objmesh.size(); i++){
                      RenderVertices(gShaderProgram, *objmesh[i], true);
                  }
  
              }
  </code></pre>
  
  <p>Here we have the RenderShadows function that uses the shadow glsl program:</p>
  
  <pre><code>void RenderShadows(OBJMeshes mesh){
  glUniformMatrix4fv(glGetUniformLocation(sShaderProgram, "modelMatrix"),
      1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));
  
  glBindVertexArray(mesh.VAO);
  glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());
  glBindVertexArray(0);
  
  }
  </code></pre>
  
  <p>And finally the normal render function that renders the geometry:</p>
  
  <pre><code>void RenderVertices(GLuint shaderProgram, OBJMeshes mesh, bool shadows) {
  
  GLuint CPUValuesID = glGetUniformLocation(gUbo, "MVP");
  
  glBindBuffer(GL_UNIFORM_BUFFER, gUbo);
  glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(CPUMatricies), &amp;globalCPU);
  
  glBindBuffer(GL_UNIFORM_BUFFER, mtlBuff);
  glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(mtlValues), &amp;mtl);
  
  glUniformMatrix4fv(CPUValuesID, 1, GL_FALSE, &amp;globalCPU.MVP[0][0]);
  
  glUseProgram(shaderProgram);
  glBindVertexArray(mesh.VAO);
  
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, mesh.texture);
  
  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D, DepthMap);
  
  GLint samplerLoc = glGetUniformLocation(shaderProgram, "DepthMap");
  glUniform1i(samplerLoc, 1);
  
  glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "modelMatrix"),
      1, GL_FALSE, glm::value_ptr(mesh.modelMatrix));
  glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "lightSpaceMatrix"),
      1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
  
  glUniform3f(glGetUniformLocation(shaderProgram, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
  
  glDrawArrays(GL_TRIANGLES, 0, mesh.vertices.size());
  
  glBindVertexArray(0);
  
  }
  </code></pre>