HomeMultimedia Page 3 - Working with Colors in OpenGL for Game Programming with SDL
Manipulating Color - Multimedia
Colors magically bring a scene to life. No matter how interesting the plot is, how engrossing the special effects are, if the color combinations are not accurate or the colors are tepid, the liveliness of the game suffers. In short, it is the combination of and correct usage of colors that decide what the scene and eventually the whole game will be like.
The core of OpenGL color manipulation is two functions: glColor() and glIndex(). The former is for RGBA mode and the latter is for color-index/color-map mode. Here are the details:
1. glColor*():
The * is used to represent the fact that there are multiple versions of the function. In the case of glColor() there are 14 different versions, each taking different data-types as data-type parameters. The differentiation is done on the basis of a suffix applied to the function.
For example, the version that takes integer parameters has i as the suffix, i.e. glColor3i(). All the versions having 3 as part of the suffix take three parameters: the values of the red, blue, and green components. The alpha is set to max implicitly. To explicitly set the value of alpha, the version with 4 as a part of its suffix needs to be used. It takes one extra parameter: the value of alpha component. For example, to set the current color to green without alpha set to max and to have the values to be supplied in integers, the statement would be:
glColor3i(0,255,0,0);
To set the current color to red with alpha set to maximum using integer values, the statement would be:
glColor3i(255,0,0,1);
2. glIndex*():
This function comes into play when the requirement is color-index mode. It updates the current color index. The main thing to keep in mind is that the current index is saved as a floating point value. If the integer is passed as an argument, then it is converted to a floating point number.
Just like its RGBA counterpart, glIndex*() has different versions based on the data-type of the parameter. In all the variants, this function takes one parameter: the color index to be used as the current color index. The variants include integer, float, and vector. Vector specified with V accepts array values. The v suffix is used in conjunction with integer (i) and float (f). For example, to set the color index to 25 in integer, the statement would be:
glIndexi(25);
Just as with glColor*(), glIndex*() can be called any time to update the current index. Also, it can be used between glBegin and glEnd.
That covers the main APIs for color manipulation. In the next section, I will be testing the APIs in the created test bed, which was used in previous parts of the series.