Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have to clear depth buffer in OpenGL during rendering?

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?

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

like image 998
2am Avatar asked Oct 19 '13 17:10

2am


1 Answers

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.

like image 122
GraphicsMuncher Avatar answered Sep 27 '22 21:09

GraphicsMuncher