Hello,
I am having great difficulties getting a shader to read a depth texture from an FBO. I always get a constant depth value, the same result I get when no texture is bound.
Before using it, I detached it from the FBO, and unbound the FBO.
I created it like this:
// Create depth texture with stencil glGenTextures(1, &m_depthTextureID);
glBindTexture(GL_TEXTURE_2D, m_depthTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_width, m_height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
// Make it readable in a shader
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
//glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_depthTextureID, 0);
glBindTexture(GL_TEXTURE_2D, 0);
In the shader, it is declared as:
uniform sampler2D gDepth;
and read like this:
texture2D(gDepth, uv).x;
I tried all sorts of combinations of settings, but I always get just one constant value from the depth texture.
It isn’t a linearization problem, since I am comparing it to projected z coordinates.
How does one properly read a depth texture in a shader?
Thank you for any help you can offer.