glClearDepth

I am new to OpenGL, and I am using Qt Creator as an IDE. I am wondering what the glClearDepth() function does. I do not quite understand the description on the documentary, but when I change my code “glClearDepth(1.0)” to “glClearDepth(0.0)”, the polygon widget I have no longer appears. Could someone explain this function to me in simple words?

Thanks,
Flurite

glClearDepth sets the depth value in the depth buffer when the depth buffer is cleared similar to how glClearColor sets the clear color for the color buffer. When you put glClearDepth(1.0), it means that when the next glClear call is given (i.e. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT):wink: the depth buffer will be filled with value 1. Now the incoming pixels depth value will be compared to 1. If you do not modify the depth function (glDepthFunc(…)) which defaults to GL_LESS), the incoming value will be passed when the depth is < 1. Now when you change the clear depth to 0, it is already the lowest value and thus no fragment is <0 hence no pixel is written.

  1. if using glClearDepth(1) change the depth func. to GL_LESS (glDepthFunc(GL_LESS)) this will render the nearest fragment to the viewer to screen.
  2. if using glClearDepth(0) change the depth func to GL_GREATER (glDepthFunc(GL_GREATER)) this will render the furthest fragment from the viewer to screen.

So it all depends on what you want to achieve.

Cool, so the higher up the pixel is, the higher the depth value? Is a depth of 1 at the top of the screen and 0 at the bottom?

Ehm, why do you expect the depth at a fragment to change based only on the pixel it’s projected to? The values of the depth buffer are a result of rendering, not a function of pixel position.

I was referring to mobeen saying:

Now the incoming pixels depth value will be compared to 1.

In reality, the actual depth values won’t be 1.0 when you call glClear. Common Mistakes - OpenGL Wiki

Unless if you have a floating point format depth buffer.