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.
finalColor = vec4(depth);
Here you are setting avec4
using a singlefloat
value.... \$\endgroup\$vec4
will getdepth
. \$\endgroup\$