In RGBA mode, each pixel stores the following data: R value (Red component), G value (green component), B value (blue component), and A value (alpha component ). The combination of red, green, and blue colors gives us the various colors we need, while alpha does not directly affect the color. It will be introduced later.
In RGBA mode, color selection is very simple. You only need a function.
GlColor * series functions can be used to set colors. The version of the three parameters can specify the values of R, G, and B, while the value of A is the default value; the versions of the four parameters can specify the values of R, G, B, And A respectively. For example:
Void glColor3f (GLfloat red, GLfloat green, GLfloat blue );
Void glColor4f (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha );
(Remember? 3f indicates that there are three floating point parameters ~ See the description of the glVertex * function in the second lesson .)
The floating point number is used as a parameter. 0.0 indicates that this color is not used, and 1.0 indicates that this color is used most. For example:
GlColor3f (1.0f, 0.0f, 0.0f); indicates that green and blue are not used, but red is used the most, so the purest red is obtained.
GlColor3f (0.0f, 1.0f, 1.0f); indicates to use green, blue to the maximum, rather than red. The mixed effect is light blue.
GlColor3f (0.5f, 0.5f, 0.5f); indicates that each color is used in half and the effect is gray.
Note: floating-point numbers can be precise to several digits after the decimal point, which does not mean that the computer can display so many colors. In fact, the number of colors that a computer can display depends on the hardware. If OpenGL cannot find the exact color, it will perform a process similar to rounding.
You can change the glColor3f parameter value in the following code to draw rectangles of different colors.
Void mydisplay (void)
{
Glclear (gl_color_buffer_bit );
Glcolor3f (0.0f, 1.0f, 1.0f );
Glrectf (-0.5f,-0.5f, 0.5f, 0.5f );
Glflush ();
}
Note: For glcolor functions, different parameter types indicate different values of the "maximum" color.
Use F and D as the suffix function, and use 1.0 as the maximum value.
Use B as the suffix and use 127 as the maximum value.
Use UB as the suffix function, and use 255 as the maximum value.
Use s as the suffix function, and use 32767 as the maximum value.
Use us as the suffix function, and use 65535 as the maximum value.
These rules seem troublesome, but there is no obstacle in actual use after you are familiar with them.