1
\$\begingroup\$

I am trying to implement shadow mapping into my project and am stuck at this problem: If I'm reading the shadow map values with glReadPixels and linearize those values, I am getting exactly the value I was looking for: ranging from 0 to 1. But when I am doing the exact same calculation in my shader, everything, where I rendered an object, is white. Another thing I noticed(which i assume is directly linked to my problem) is that if I am changing glClearDepth to anything different than 1, the output of my shader is black and the output of glReadPixels after linearizing is not anymore the value I wanted but anything else.

Here is the code where I am creating the depth texture:

glBindTexture(GL_TEXTURE_2D, _textures[GBTEXTURE_DEPTH]);  
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _textures[GBTEXTURE_DEPTH], 0);

And here is the fragment shader:

in vec2 fragUV;
uniform sampler2D tex;

float zfar = 1000.0f;
float znear = 1.0f;

float linearize(float depth)
{
    return (-zfar * znear / (depth * (zfar - znear) - zfar)) / zfar;
}

out vec4 finalColor;
void main(void){
    float depth = texture2D(tex, fragUV).r;
    depth = linearize(depth);

    finalColor = vec4(depth);
}

I am doing the exact same calculation on the CPU, with correct values as a result.

I hope I described my problem with enough details for you to help me.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is this correct? finalColor = vec4(depth); Here you are setting a vec4 using a single float value.... \$\endgroup\$ – Dan Oct 10 '14 at 23:35
  • \$\begingroup\$ Yes, that means that every component of vec4 will get depth. \$\endgroup\$ – Liess Jemai Oct 12 '14 at 0:00
  • 1
    \$\begingroup\$ You are also setting the alpha channel to this float value, are you aware of this? \$\endgroup\$ – Dan Oct 12 '14 at 6:21
  • \$\begingroup\$ Yes, I am. This shouldn't have any effect on the outcome. \$\endgroup\$ – Liess Jemai Oct 15 '14 at 12:28
0
\$\begingroup\$

Make sure that you call glEnable(GL_DEPTH_TEST) when you are binding your depth buffer for rendering. Preferably just before you call glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)Index);

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.