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

发布于 2024-11-02 22:58:13 字数 1128 浏览 1 评论 0原文

我正在将场景渲染为纹理。除了深度测试不起作用之外,这工作得很好。如果渲染到离屏纹理,如何启用深度测试?我正在使用 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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2024-11-09 22:58:13

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文