[glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

Started by
4 comments, last by dpadam450 1 year, 11 months ago

Hello guys,

I hope you're all doing well.

I was wondering what's the utility of using glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).

I read about glClear(GL_COLOR_BUFFER_BIT) and how it cleans the color of the window and sets it to the value specified by glClear().

But I don't really get what does glClear(GL_COLOR_BUFFER_BIT) do. I read that for example GL Less test passes if the incoming pixels depth value is less than the stored z value. Does that mean if I write glClearDepth(1.0) the test passes only if all the incoming pixels depth values are less than 1 and the objects are then rendered ? Thing is I commented glClearDepth(1.0) in my code and nothing changed, the scene was rendered perfectly and nothing was missing.

But before drawing, I enabled the depth test of course.

Another thing is that I tried to comment glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) in my code and ran it, everything seemed to work fine, the scene didn't disappear so why use that line when it doesn't really influence the scene. ?

SO to sum up why do I have to use glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) and glClearDepth(1.0) when rendering a scene ?

Thank you all.

Advertisement

ogldev1 said:
why do I have to use glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) and glClearDepth(1.0) when rendering a scene ?

As always, read the docs here for glClearDepth(), here for glClear(), or other sources if you prefer.

As you wrote, glClear() clears the selected buffers, in your case both the color buffer and the depth buffer. The clear color is whatever you set it to, and the range of the depth buffer is clamped from 0 to 1, and you're setting it to 1.

Clearing the color buffer can be optional if you completely overwrite the scene, with some caveats. There are various techniques that use additional color buffers that use the buffer, and it can also be useful to use a clear color to help diagnose issues, so there are good reasons to do it.

Clearing the depth buffer sets it to whatever value you specify. The range of the depth buffer is clamped from 0 to 1, which is why there are several algorithms available for depth which change how the precision scale is used. There are ways to avoid the call to clear as well, such as drawing a skybox and setting the depth function to GL_ALWAYS, with overwrites the distant values with wherever your sky is.

ogldev1 said:
Thing is I commented glClearDepth(1.0) in my code and nothing changed, the scene was rendered perfectly and nothing was missing.

Then you are probably misusing something, or you have a scene where things are never moving away. Did you enable depth testing? Do you have a consistent depth function, which almost always it should be GL_LESS? Are you overwriting it by some other means, such as a skybox with a depth function of GL_ALWAYS? Each can have an effect.

There are also some ways to mess it up where objects will still appear correct in some way. If depth testing was accidentally disabled but your scene is already laid out in depth order you might not notice any difference because you happen to be drawing them back to front.

A useful debugging aid is a fragment shader that draws the depth buffer. It's useful to have as something you can toggle to visualize what's going on with depth. Google can find lots of examples, but it can be a one-line fragment shader to draw gl_FragCoord.z instead of the pixel's normal color, or slightly more complex versions that let you scale it to highlight differences in depth.

@frob First of all, Thank you for the explanation.

Yes I enabled the depth test of course.

When I comment glClearDepth(1.0) nothing happens but when I change the value to 0, the object disappears and there is only a black background. Also when I keep changing the values for example glClearDepth(0.75) only parts of the object are rendered and not the whole object ( here in my case the object is a textured cube).

However I still don't get why when I comment glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT), this doesn't influence my scene. Nothing disappears. So if everything works fine, why can't I just remove it from my program.

Without knowing everything else going on it your program it would just be guesswork.

As I wrote above, it is possible to remove the clear under certain scenarios, and many professional games never call it. If you would overwrite the buffers entirely over the regular course of processing the scene there is no point in setting them to a specific value, even if the clear operation is optimized to take very little time it is non-zero.

If your scene does not update/move around, then once you draw something into it (say 1 triangle) regardless of depth information or color information, its always going to maintain being there and displayed because its the closest thing in the camera. if you turn color clear off and/or depth testing, its just going to redraw the triangle over and over again.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement