Open GL Super Bible

Previous Table of Contents Next


This chapter covers one of the most important features supported by a graphics package: color. You have seen how to specify a color in terms of its RGB components, and how these components relate to one another in the RGB color cube. Your understanding of glColor has been expanded to include the coloring of vertices, and you have seen how this affects shading. We explained OpenGLís selection of colors in 4-, 8-, 16-, and 24-bit Windows color modes. We demonstrated the building of a 3-3-2 palette for use by OpenGL in 8-bit color modes. Finally, we took a brief look at color index mode and its utilization to gain better palette control in 8-bit color modes.

Good use of color and shading is a prerequisite for good 3D graphics. The upcoming chapter explains how OpenGL uses shading to produce lighting effects. Youíll learn how to specify material colors and lighting conditions and allow OpenGL to select the drawing colors.

Reference Section


Sets the clear value for the color index buffers.
Include File
void glClearIndex(GLfloat color);
This function specifies the color index to use in color index mode to clear the color buffers. This has the net effect of clearing the window and setting the background color to the color in the index specified by the color parameter.


GLfloat: The value to use when the color index buffers are cleared with glClear. The default is 0.
See the sample program INDEX in this chapter.
See Also
glClear, glGet


Sets the current color when in RGBA color mode.
Include File
void glColor3b(GLbyte red,GLbyte green, GLbyte blue);
void glColor3d(GLdouble red, GLdouble green, GLdouble blue);
void glColor3f(GLfloat red, GLfloat green, GLfloat blue);
void glColor3i(GLint red, GLint green, GLint blue);
void glColor3s(GLshort red, GLshort green, GLshort blue);
void glColor3ub(GLubyte red, GLubyte green, GLubyte blue);
void glColor3ui(GLuint red, GLuint green, GLuint blue);
void glColor3us(GLushort red, GLushort green, GLushort blue);
void glColor4b(GLbyte red, GLbyte green, GLbyte blue, GLbyte alpha);
void glColor4d(GLdouble red, GLdouble green, GLdouble blue, GLdouble alpha);
void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
void glColor4i(GLint red, GLint green, GLint blue, GLint alpha);
void glColor4s(GLshort red, GLshort green, GLshort blue, GLshort alpha);
void glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha);
void glColor4ui(GLuint red, GLuint green, GLuint blue, GLuint alpha);
void glColor4us(GLushort red, GLushort green, GLushort blue, GLushort alpha);
void glColor3bv(const GLbyte *v);
void glColor3dv(const GLdouble *v);
void glColor3fv(const GLfloat *v);
void glColor3iv(const GLint *v);
void glColor3sv(const GLshort *v);
void glColor3ubv(const GLubyte *v);
void glColor3uiv(const GLuint *v);
void glColor3usv(const GLushort *v);
void glColor4bv(const GLbyte *v);
void glColor4dv(const GLdouble *v);
void glColor4fv(const GLfloat *v);
void glColor4iv(const GLint *v);
void glColor4sv(const GLshort *v);
void glColor4ubv(const GLubyte *v);
void glColor4uiv(const GLuint *v);
void glColor4usv(const GLushort *v);
This function sets the current color by specifying separate red, green, and blue components of the color. Some functions also accept an alpha component. Each component represents the range of intensity from zero (0.0) to full intensity (1.0). Functions with the v suffix take a pointer to an array that specifies the components. Each element in the array must be of the same type. When the alpha component is not specified, it is implicitly set to 1.0. When non-floating point types are specified, the range from zero to the largest value represented by that type is mapped to the floating point range 0.0 to 1.0.


Specifies the red component of the color.
Specifies the green component of the color.
Specifies the blue component of the color.
Specifies the alpha component of the color. Used only in variations that take four arguments.
A pointer to an array of red, green, blue, and possibly alpha values.


The following code from the CCUBE example in this chapter sets one of the corners of the color cube to white.

     // Front face

            // White
            glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
See Also


Enables or disables modification of color components in the color buffers.
Include File
void glColorMask(GLboolean bRed, GLboolean bGreen, GLboolean bBlue, GLboolean bAlpha);
This function allows changes to individual color components in the color buffer to be disabled or enabled (all are enabled by default). For example, setting the bAlpha argument to GL_FALSE disallows changes to the alpha color component.


GLboolean: Specifies whether the red component may be modified.
GLboolean: Specifies whether the green component may be modified.
GLboolean: Specifies whether the blue component may be modified.
GLboolean: Specifies whether the alpha component may be modified.
See the sample program MASK on the CD for this chapter.
See Also
glColor, glIndex, glIndexMask, glDepthMask, glStencilMask

Previous Table of Contents Next