When exactly is glClear(GL_DEPTH_BUFFER_BIT) and glClear(GL_COLOR_BUFFER_BIT) necessary?

StackOverflow https://stackoverflow.com/questions/23125819

  •  05-07-2023
  •  | 
  •  

Frage

In a nutshell, when should the color buffer be cleared and when should the depth buffer be cleared? Is it always at the start of the drawing of the current scene/frame? Are there times when you would draw the next frame without clearing these? Are there other times when you would clear these?

War es hilfreich?

Lösung

Ouff... even though it's a simple question it's a bit hard to explain ^^

It should be cleared before drawing the scene again. Yes, every time if you want to avoid strange and nearly uncontrollable effect. You don't want to clear the two Buffers after swapping when you bind the scene to a texture () but right after that there's no more use for it.

The Color Buffer is as the name says a buffer, storing the computed color data. For better understanding, imagine you draw on a piece of paper. Each Point on the Paper knows which color was drawn on top of it - and that's basically all of it.

But: without the Depth Buffer, your Color Buffer is (except some special cases like multiple renderpasses for FX effects) nearly useless. It's like a second piece of paper but in a gray scale. How dark the gray is decides how far the last drawn pixel is away from the screen (relative to the zNear and zFar clipping plane). If you instruct OpenGl to draw another primitive, it goes pixel by pixel and checks, which depth value the pixel would have. If it's higher than the value stored in the Depth Buffer for that pixel, it draws over the Color Buffer in that position and does nothing if the value is lower.

To recap, the Color Buffer stores the picture to be drawn on your screen, and the Depth Buffer decides weather a part of your primitive get's drawn in this position. So clearing the Buffers for a new scene is basically changing the Paper to draw in top. If you want to mess with the old picture, then keep it, but it's in better hands on the monitor (or on the wall^^).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top