OpenGL supports two color modes: rgba and color index.
In either color mode, the computer must save some data for each pixel. The difference is that in the rgba mode, data directly represents the color, while in the color index mode, data represents an index, to get the true color, you must also query the index table.
1. rgba color
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.
2. index color
In the index color mode, OpenGL requires a color table. This table is equivalent to the painter's palette: although many colors can be called up, the number of colors that exist on the palette at the same time will not exceed the number of cells in the palette. Imagine each item of the color table as a grid on the color palette: it saves a color.
When I used the index color mode to draw a picture, I said, "I set the I-th color to XX." In fact, it is equivalent to setting the I-th color of the color palette to XX. "I need the K color to draw a picture", then use a paint brush to dip the K color palette.
The size of the color table is very limited, generally between 256 and ~ Between 4096 and is always an integer power of 2. When you use the index color method for drawing, you always set the color table first and then select the color.
2.1 select the color
You can use the glindex * series functions to select colors in the color table. The most common one is glindexi. Its parameter is an integer. Void glindexi (glint C );
Yes, it is really easy.
2.2 set the color table
OpenGL does not directly provide a method to set the color table. Therefore, you must use the operating system to set the color table. The windows and most other graphic operating systems we use have this function, but the functions used are different. Just as I did not describe how to write code to create a window in windows, I will not describe how to set a color table in windows.
The glut toolkit provides a function to set the color table, but I have always encountered problems in the test. Now, I want to introduce you to another OpenGL Toolkit: aux to show you the color of the index. This toolkit is provided by Visual Studio and does not need to be installed separately, but it is outdated. Here we just try it and you don't have to go into depth.
#include <windows.h>#include <GL/gl.h>#include <GL/glaux.h>#pragma comment (lib, "opengl32.lib")#pragma comment (lib, "glaux.lib")#include <math.h>const GLdouble Pi = 3.1415926536;void myDisplay(void){ int i; for(i=0; i<8; ++i) auxSetOneColor(i, (float)(i&0x04), (float)(i&0x02), (float)(i&0x01)); glShadeModel(GL_FLAT); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_TRIANGLE_FAN); glVertex2f(0.0f, 0.0f); for(i=0; i<=8; ++i) { glIndexi(i); glVertex2f(cos(i*Pi/4), sin(i*Pi/4)); } glEnd(); glFlush();}int main(void){ auxInitDisplayMode(AUX_SINGLE|AUX_INDEX); auxInitPosition(0, 0, 400, 400); auxInitWindow(L""); myDisplay(); Sleep(10 * 1000); return 0;}
You can ignore the other parts, just look at the mydisplay function. First, use auxsetonecolor to set a grid in the color table. You can set the eight cells after eight cycles.
Glshademodel.
Then, set the vertex with glvertex in the loop, and change the color represented by the vertex with glindexi.
The final result is eight triangles of the same shape and different colors.
Although the index color is a little bit more. The main advantage of index color is that it occupies a small amount of space (each pixel does not have to save its own color, and only a few binary digits can represent its color position in the color table), which consumes less system resources, the graphic operation speed is fast, but it is slightly less convenient to program, and the image effect is also worse than the RGB color. StarCraft may represent the screen effect of a 256-color table. Although it can run smoothly on a very bad PC, from the current perspective, the screen effect is insufficient.
Currently, the performance of PC is sufficient to use RGB color in various scenarios. Therefore, the index color is not mainstream in PC program development. Of course, some small devices, such as GBA and mobile phones, still have their own index colors.
3. Specify the color used to clear the screen
We wrote: glclear (gl_color_buffer_bit), which means to clear the color of the screen.
But what is actually "null? In the universe, black represents "empty"; on a White Paper, white represents "empty"; on the envelope, the envelope color is "empty ".
OpenGL uses the following function to define the color of the screen after the screen.
In RGB mode, glclearcolor is used to specify the "null" color. It requires four parameters. The parameter meaning is similar to glcolor4f.
In the index color mode, glclearindex is used to specify the index where the "null" color is located. It requires a parameter and its meaning is similar to that of glindexi.
void myDisplay(void){ glClearColor(1.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush();}
Oh, this is really simple ~
4. Specify the coloring model
OpenGL allows you to specify different colors for different vertices of the same polygon. For example:
# Include <math. h> const gldouble Pi = 3.1415926536; void mydisplay (void) {int I; // glshademodel (gl_flat); glclear (gl_color_buffer_bit); glbegin (gl_triangle_fan ); // triangle glcolor3f (1.0f, 1.0f, 1.0f); // V1 glvertex2f (0.0f, 0.0f); for (I = 0; I <= 8; ++ I) {glcolor3f (I & 0x04, I & 0x02, I & 0x01); glvertex2f (COS (I * PI/4 ), sin (I * PI/4); // V2, 3,4, 5, 6, 7, 8} glend (); glflush ();}
By default, OpenGL calculates other vertices between two vertices and fills them with "appropriate" Colors to bring the color values of adjacent points closer. If the RGB mode is used, the gradient effect will appear. If the color index mode is used, the index values of neighboring points are close. If the items in the color table are set to close colors, the gradient effect will appear. However, if the color of the item in the color table is very different, it may seem very strange.
You can use the glshademodel function to disable this calculation. If the color of the vertex is different, set all other vertices between the vertex to be the same as a vertex. (The Color of the vertex specified after the straight line prevails, and the polygon will be subject to the color of any vertex, which is determined by implementation .) To avoid this uncertainty, try to use the same color in the polygon.
How to Use glshademodel:
Glshademodel (gl_smooth); // smooth mode, which is also the default mode
Glshademodel (gl_flat); // monochrome mode
Summary:
This lesson describes how to set colors. The RGB color mode is a common method on PC.
You can set the remaining color of the screen after glclear is cleared.
You can set the color filling mode: smooth or monochrome.
================================= End of Lesson 4 ========================== =====
================================= To be continued ======================== =====
OpenGL Note 3