Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: glClearDepth

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2012


    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?


  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Mar 2009
    Karachi, Pakistan
    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) 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.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2012
    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?

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    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.

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2012
    I was referring to mobeen saying:

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

  6. #6
    Super Moderator OpenGL Guru
    Join Date
    Feb 2000
    Montreal, Canada
    In reality, the actual depth values won't be 1.0 when you call glClear.

    Unless if you have a floating point format depth buffer.
    an open source GLU replacement library. Much more modern than GLU.
    float matrix[16], inverse_matrix[16];
    glhTranslatef2(matrix, 0.0, 0.0, 5.0);
    glhRotateAboutXf2(matrix, angleInRadians);
    glhScalef2(matrix, 1.0, 1.0, -1.0);
    glhQuickInvertMatrixf2(matrix, inverse_matrix);
    glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
    glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts