简体   繁体   中英

Viewing depth buffer in OpenGL

I am having trouble viewing depth rendered to a texture. I can view it when I render depth as RGB to texture, but when I try to render depth only I get only black. I have searched for many hours and implemented fixes from other similar questions, but to no avail. I've disabled GL_TEXTURE_COMPARE_MODE, added glDrawBuffer(GL_NONE) and glReadBuffer(GL_NONE).

I'm running on OSX 10.9 and OpenGL 4.1. I've removed gl error checking from the pasted code for conciseness.

The setup for the depth FBO/texture is this:

glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);

glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
glTexImage2D(GL_TEXTURE_2D, 0,
             GL_DEPTH_COMPONENT,
             fbWidth,
             fbHeight,
             0,
             GL_DEPTH_COMPONENT,
             GL_FLOAT,
             0);

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_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
//    glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, renderedTexture, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    assert(false && "framebuffer NOT OK");

depth fragment - I can see depth when rendering RGB

#version 150 core
#if RENDER_RGB
out vec4 outColor;

void main(){
    vec3 color = gl_FragCoord.z);
    outColor = vec4(color, 1.0);
}

#else

void main(){
    gl_FragDepth = gl_FragCoord.z;
}
#endif

Reading back the depth texture, I am using this fragment shader:

#version 150 core
in vec2 UV;

uniform sampler2D renderedTexture;
out vec4 outColor;

void main(){
    vec3 color = vec3( texture(renderedTexture, UV).r );
    outColor = vec4(color, 1.0);
}

Nevermind, it does work! My problem was the depth-only FBO setup for the depth texture was never called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM