Context & stateGL 1.0

glClear

Resets the chosen buffers of the current framebuffer (color, depth, stencil).

Signature

void glClear(GLbitfield mask);

How it works

Called at the start of every frame so the new image does not draw on top of the old one. Each buffer is filled with its clear value: the color from glClearColor, depth 1.0 (the far plane) and stencil 0 by default.

Clearing is very cheap on modern GPUs — often cheaper than not clearing — so always clear what you use.

Parameters

maskGLbitfield

Which buffers to clear, combined with |.

Common values

GL_COLOR_BUFFER_BIT
Color attachments.
GL_DEPTH_BUFFER_BIT
Depth buffer — required when depth testing is on.
GL_STENCIL_BUFFER_BIT
Stencil buffer.

Tips & pitfalls

  • Forgetting GL_DEPTH_BUFFER_BIT with depth testing enabled usually shows only the first frame, or nothing at all.

  • glDepthMask(GL_FALSE) also blocks clearing the depth buffer — re-enable it before glClear.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_VALUE
mask has bits other than the three above.

Example

example.cpp
1while (!glfwWindowShouldClose(window)) {
2    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3    // ... draw ...
4    glfwSwapBuffers(window);
5    glfwPollEvents();
6}
Official Khronos documentation