在渲染过程中,为什么要清除OpenGL中的深度缓冲区?

时间:2022-11-01 22:24:52

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?

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

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

我可能知道原因,为了清除GPU之前使用的深度缓冲区值,我只想确认一下。

1 个解决方案

#1


15  

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.

深度缓冲区保存场景中像素的“深度”。当OpenGL呈现几何图形时,每个片段(像素)都会与深度缓冲区的值进行比较。如果该片段的z值低于缓冲区中的值,则它将成为新的最低值,因此将呈现像素。如果不是,就不要渲染,因为有更接近的东西在阻挡它。这就是它的要点——你可以自己解读细节。

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.

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

#1


15  

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.

深度缓冲区保存场景中像素的“深度”。当OpenGL呈现几何图形时,每个片段(像素)都会与深度缓冲区的值进行比较。如果该片段的z值低于缓冲区中的值,则它将成为新的最低值,因此将呈现像素。如果不是,就不要渲染,因为有更接近的东西在阻挡它。这就是它的要点——你可以自己解读细节。

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.

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