如何在渲染到屏幕外缓冲区然后渲染到纹理时使用深度测试

时间:2011-04-22 00:04:00

标签: opengl graphics framebuffer depth-buffer

我正在将场景渲染为纹理。这工作正常,但深度测试不起作用。如果渲染到屏幕外纹理,如何启用深度测试?我正在使用FrameBuffer类http://www.opengl.org/news/comments/framebuffer_object_fbo_c_class_available_with_example_application/

glGetIntegerv(GL_DRAW_BUFFER, &drawBuffer);
frameBuffer->Bind();
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
rAngle += 0.3f;
glUseProgram(0);
drawSpinningTeapot();
FramebufferObject::Disable();

glDrawBuffer(drawBuffer);
glViewport(0, 0, WINDOW_WIDTH,WINDOW_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(g_program);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,tex1);
texSampler = glGetUniformLocation(g_program,"texture");
glUniform1f(texSampler, 0);
glActiveTexture(GL_TEXTURE0);
glBegin(GL_QUADS);
{
    glTexCoord2f(0, 0); glVertex3f(-1, -1, -0.5f);
    glTexCoord2f(1, 0); glVertex3f( 1, -1, -0.5f);
    glTexCoord2f(1, 1); glVertex3f( 1,  1, -0.5f);
    glTexCoord2f(0, 1); glVertex3f(-1,  1, -0.5f);
}
glEnd();
glDisable(GL_TEXTURE_2D);

1 个答案:

答案 0 :(得分:12)

除了颜色附件之外,您还需要将渲染缓冲区或纹理附加到GL_DEPTH_ATTACHMENT。这是一个很好的教程,可以帮助您入门:

http://www.songho.ca/opengl/gl_fbo.html