Depth, blending & cullingGL 1.0

glColorMask

Turns writing to each colour channel on or off; all GL_FALSE draws depth and stencil only.

Signature

void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);

How it works

The colour version of glDepthMask. Fragments are still shaded and tested, but the channels set to GL_FALSE are not changed in the framebuffer.

The main use is a depth pre-pass: draw the scene once with every channel off, so only the depth buffer is filled; then draw it again with glDepthFunc(GL_LEQUAL) (or GL_EQUAL) and the colour on. Each pixel's expensive lighting then runs once, for the visible surface only. Writing a stencil mask without touching the image is the other common case.

Parameters

redGLboolean

GL_TRUE writes red (default).

greenGLboolean

GL_TRUE writes green.

blueGLboolean

GL_TRUE writes blue.

alphaGLboolean

GL_TRUE writes alpha.

Tips & pitfalls

  • glClear respects the mask: turn the channels back on before clearing the colour buffer.

  • A depth-only pass still runs the fragment shader; use a cheap shader for it.

Example

example.cpp
1// depth pre-pass
2glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
3drawScene(depthOnlyShader);
4glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5
6glDepthFunc(GL_LEQUAL);
7drawScene(litShader);
Official Khronos documentation