为什么我们必须在渲染期间在OpenGL中清除深度缓冲区? [英] Why do we have to clear depth buffer in OpenGL during rendering?

查看:934
本文介绍了为什么我们必须在渲染期间在OpenGL中清除深度缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个OpenGL代码,它没有在glClear()中清除 GL_DEPTH_BUFFER_BIT ,因为我无法渲染我的场景。我添加了这个位,并且场景被呈现。为什么有必要使用这个清除位?

I was trying to run an OpenGL code, it didn't have GL_DEPTH_BUFFER_BIT cleared in glClear(), because of which I couldn't render my scene. I added this bit, and the scene was rendered. Why is it necessary to use this clear bit?

我可能知道原因,以前清除GPU使用的深度缓冲区值,但我只是想确认。

I may know the reason for this, to clear the depth buffers values used by GPU previously, but i just want to confirm.

推荐答案

深度缓冲区保存场景中像素的深度。当OpenGL渲染您的几何体时,每个片段(像素)与该点处的深度缓冲器的值进行比较。如果该片段具有低于缓冲器中的z值的z值,则它变成新的最低值,并且因此成为要渲染的像素。如果没有,不要渲染它 - 有更近的东西阻塞它。

The Depth Buffer holds the "depth" of the pixel in the scene. When OpenGL renders your geometry, each fragment (pixel) is compared against the depth buffer's value at that point. If that fragment has a z value lower than the one in the buffer, it becomes the new lowest value, and thus the pixel to be rendered. If not, don't render it - there's something closer that's blocking it. That's the gist of it - you can read into the specifics yourself.

现在,当场景变化时,会发生什么?您要清除屏幕,以便重绘所有内容,但也要清除深度缓冲区。为什么?因为否则所有新的像素将与来自前一帧的深度值进行比较。这没有意义 - 他们应该与框架中的他们进行比较!您的推理是正确的。

Now, what happens when the scene changes? You want to clear the screen so you redraw everything, but you also want to clear the depth buffer. Why? Because otherwise all the new pixels will be compared against the depth values from the previous frame. That doesn't make sense - they should be compared against those in the frame they're in! You are correct in your reasoning.

这篇关于为什么我们必须在渲染期间在OpenGL中清除深度缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆