渲染到屏幕外缓冲区然后渲染到纹理时如何使用深度测试 [英] How to use depth testing when rendering to an offscreen buffer then onto texture
问题描述
我将场景渲染为纹理。这工作正常,但深度测试不起作用。如果渲染到离屏纹理,我如何启用深度测试?我正在使用FrameBuffer类 http://www.opengl.org/news/comments/framebuffer_object_fbo_c_class_available_with_example_application /
I'm rendering my scene to a texture. This works fine except that depth testing does not work. How do I enable depth testing if rendering to an offscreen texture? I'm using the FrameBuffer class 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);
推荐答案
您需要附加渲染缓冲区或纹理除了颜色附件之外,还有 GL_DEPTH_ATTACHMENT
。这里有一个很好的教程,让你开始:
You need to attach a render buffer or a texture to the GL_DEPTH_ATTACHMENT
in addition to the color attachment. Here's a good tutorial to get you started:
http://www.songho.ca/opengl/gl_fbo.html
这篇关于渲染到屏幕外缓冲区然后渲染到纹理时如何使用深度测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!