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
GL_TRUE writes red (default).
GL_TRUE writes green.
GL_TRUE writes blue.
GL_TRUE writes alpha.
Tips & pitfalls
glClearrespects 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
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);