# How to clear a buffer to 1.0 instead of 0.0 in OpenGL?

Using glClear() you can set the buffer specified by the parameter to 0.0. This is useful say if you want pixels not covered by models to be black. Because vec3(0.0, 0.0, 0.0) results in black.

But for the depth buffer, clearing means to set it to the maximum value 1.0 since you don't want pixels not covered by models to have the depth 0.0 which is the camera's position but to have the depth 1.0 which means as far away as possible.

So how can I clear the depth buffer to 1.0 or come over this issue in another way?

You use:

• glClearColor(r, g, b, a) to set the color of glClear(GL_COLOR_BUFFER_BIT)
• glClearDepth(depth) for the depth buffer
• glClearStencil(stencil) for the stencil buffer

Alternatively, you can use glDepthFunc to change the way the depth buffer is tested.

• Since what I have isn't an actual depth buffer but a color buffer using glClearColor is what I was looking for. – danijar Mar 28 '13 at 17:30

You can use the function glClearDepth where you can specify what to clear the depth buffer to. http://www.opengl.org/sdk/docs/man/xhtml/glClearDepth.xml however I think glClear() clears the depth buffer to 1.0 by default.

glClear sets the bitplane area of the window to values previously selected by glClearColor, glClearDepth, and glClearStencil

As glClearDepth states

Specifies the depth value used when the depth buffer is cleared. The initial value is 1.