In some applications such as semiconductor layout programs, you want to display multiple different layers of materials and indicate where the materials overlap each other.
As a simple example, suppose you have three different substances that can be layered. At any point, eight possible combinations of layers can occur, as shown in Table 13-1 .
| Layer 1 | Layer 2 | Layer 3 | Color | |
|---|---|---|---|---|
0 | absent | absent | absent | black |
1 | present | absent | absent | red |
2 | absent | present | absent | green |
3 | present | present | absent | blue |
4 | absent | absent | present | pink |
5 | present | absent | present | yellow |
6 | absent | present | present | white |
7 | present | present | present | gray |
You want your program to display eight different colors, depending on the layers present. One arbitrary possibility is shown in the last column of the table. To use this method, use color-index mode and load your color map so that entry 0 is black, entry 1 is red, entry 2 is green, and so on. Note that if the numbers from 0 through 7 are written in binary, the 4 bit is turned on whenever layer 3 appears, the 2 bit whenever layer 2 appears, and the 1 bit whenever layer 1 appears.
To clear the window, set the writemask to 7 (all three layers) and set the clearing color to 0. To draw your image, set the color to 7, and then when you want to draw something in layer n, set the writemask to n. In other types of applications, it might be necessary to selectively erase in a layer, in which case you would use the same writemasks as above, but set the color to 0 instead of 7.
See "Masking Buffers" for more information about writemasks.
OpenGL Programming Guide