OpenGL basic graphics programming-OpenGL color

Source: Internet
Author: User
Almost all OpenGL applications aim to draw color images in the screen window, so Color plays an important role in OpenGL programming. The color here is different from the color concept in the painting. It belongs to the RGB color space and is only displayed on the monitor screen. In addition, the screen window coordinates are measured in pixels. Therefore, each pixel in a graph has its own color. This color value is calculated by processing a series of OpenGL function commands. This chapter describes the concept of computer color and the color mode, definition, and application scenarios of OpenGL, you will be able to enter the colorful world and enjoy endless fun.

9.1. Computer Color

  9.1.1 color generation Principle
The computer color is different from the color in painting or printing. The color of each point displayed on the computer screen is three different colors of light (red, green, and blue) inspired by the electron gun inside the monitor) therefore, computer colors are represented by R (red), g (green), and B (blue) values. These values are also called color components. The color generation principle is shown in Figure 9-1.

Figure 9-1 Computer Color Generation Principle

  9.1.2 RGB stereo(RGB color cube)
The colors of all monitor screens belong to the RGB color space. If a cube is used to represent the RGB color composition, the cube is called an RGB color stereo, as shown in 9-2.

Figure 9-2 RGB stereo

In the figure, the values of R, G, and B are from 0.0 to 1.0. If a color component is larger, it indicates that the color component is brighter, that is, the more color components it contributes at this point; otherwise, the darker or less. When the values of R, G, and B are both 0.0, the color of this point is black. When all three are 1.0, the color of this point is white ); when the values of the three colors are equal, the contribution of the three is the same, so the gray (grey) is displayed. In the figure, the diagonal line from the black vertex to the white vertex is displayed; when r = 1.0, G = 1.0, B = 0.0, the color of this vertex is yellow (yellow); similarly, r = 1.0, G = 0.0, B = 1.0 is a foreign red, it is also called magenta. When r = 0.0, G = 1.0, and B = 1.0, it is blue (Cyan ).

9.2. color mode
There are two OpenGL color modes: RGB (rgba) mode and color table mode. In RGB mode, all color definitions are expressed by R, G, and B values, and sometimes the Alpha value (related to transparency) is added, that is, the rgba mode. In the color table mode, the color of each pixel is represented by a color index value in the color table, which points to the corresponding R, G, and B values. Such a table becomes a color map ).

  9.2.1 rgba Mode(Rgba Mode)
In rgba mode, you can use glcolor * () to define the current color. The function form is:

Void glcolor3 {B s I f d UB us UI} (type R, type G, type B );
Void glcolor4 {B s I f d UB us UI} (type R, type G, type B, type );
Void glcolor3 {B s I f d UB us UI} V (type * V );
Void glcolor4 {B s I f d UB us UI} V (type * V );

Set the current R, G, B, And a values. There are 3 and 4 methods for this function. In the previous method, the default a value is 1.0, and the latter Alpha value is set by the user. The range is from 0.0 to 1.0. Similarly, it can pass parameters through pointers. In addition, the corresponding parameter values and ranges vary depending on the use of the second extension of the function, as shown in table 9-1. Although these parameter values are different, OpenGL has automatically mapped them within the range of 0.0 to 1.0 or-1.0. Therefore, flexible use of these suffixes will bring great convenience to your programming.

Suffix Data Type Minimum value Minimum value ing Maximum Value Maximum Value ing
B 1-byte integer -128 -1.0 127 1.0
S 2-byte integer -32,768 -1.0 32,767 1.0
I 4-byte integer -2,147,483,648 -1.0 2,147,483,647 1.0
UB 1-byte unsigned integer 0 0.0 255 1.0
Us 2-byte unsigned integer 0 0.0 65,535 1.0
UI 4-byte unsigned integer 0 0.0 4,294,967,295 1.0
Table 9-1 integer color value to floating point number Conversion

  9.2.2 color table mode(Color_index Mode)
You can call the glindex * () function to select the current color from the color table. The function form is:

Void glindex {sifd} (type C );
Void glindex {sifd} V (type * C );

Set the current color index value, that is, the color palette number. If the value is greater than the color bit, the modulo operation is performed.

  9.2.3 application scenarios
In most cases, the rgba mode is more effective than the color table mode. In particular, many effects are processed, such as shadow, light, fog, anti-image, and mixing; in addition, texture ing can only be performed in rgba mode. The following describes how to use the color table mode (for your reference only ):
1) if the original application uses the color table mode, it is best to maintain this mode when it is switched to OpenGL for ease of transplantation.
2) If the color used is not within the color range provided by default, the color table mode is used.
3) in many other special operations, such as color animation, this mode will have a strange effect.

9.3 example of color application
Color is an attractive application. In the previous chapters, we have gradually introduced the application of the rgba mode. The following describes the application method of the color table mode. See the routine:

  Example 9-1 Application routine of the color table(CIndex. c)

# Include "Glos. H"
# Include <Gl/Gl. h>
# Include <Gl/Glaux. h>

Void myinit (void );
Void initpalette (void );
Void drawcolorfans (void );
Void callback myreshape (glsizei W, glsizei H );
Void callback display (void );

Void myinit (void)
{
Glclearcolor (0.0, 0.0, 0.0, 0.0 );
Glclear (gl_color_buffer_bit );
Glshademodel (gl_flat );
}

Void initpalette (void)
{
Glint J;
Static glfloat RGB [] [3] = {
{1.0, 0.0, 0.0}, {1.0, 0.0, 0.5}, {1.0, 0.0, 1.0}, {0.0, 0.0, 1.0 },
{0.0, 1.0, 1.0 },{ 0.0, 1.0, 0.0 },{ 1.0, 1.0, 0.0 },{ 1.0, 0.5, 0.0 }};

For (j = 0; j <8; j ++)
Auxsetonecolor (J + 1, RGB [J] [0], RGB [J] [1], RGB [J] [2]);
}

Void callback myreshape (glsizei W, glsizei H)
{
Glviewport (0, 0, W, H );
Glmatrixmode (gl_projection );
Glloadidentity ();
If (W <= H)
Glortho (-12.0, 12.0,-12.0 * (glfloat) h/(glfloat) W, 12.0 * (glfloat) h/(glfloat) W,-30.0, 30.0 );
Else
Glortho (-12.0 * (glfloat) h/(glfloat) W, 12.0 * (glfloat) h/(glfloat) W,-12.0, 12.0,-30.0, 30.0 );
Glmatrixmode (gl_modelview );
Glloadidentity ();
}

Void callback display (void)
{
Initpalette ();
Drawcolorfans ();
Glflush ();
}

Void drawcolorfans (void)
{
Glint N;
Glfloat PP [8] [2] = {
{7.0,-7.0}, {0.0,-10.0}, {-7.0,-7.0}, {-10.0, 0.0 },
{-7.0, 7.0 },{ 0.0, 10.0 },{ 7.0, 7.0 },{ 10.0, 0.0 }};

/* Draw some filled_fan_triangles */
Glbegin (gl_triangle_fan );
Glvertex2f (0.0, 0.0 );
Glvertex2f (10.0, 0.0 );
For (n = 0; n <8; n ++)
{
Glindexi (n + 1 );
Glvertex2fv (PP [N]);
}
Glend ();
}

Void main (void)
{
Auxinitdisplaymode (aux_single | aux_index );
Auxinitposition (0, 0, 500,500 );
Auxinitwindow ("color index ");
Myinit ();
Auxreshapefunc (myreshape );
Auxmainloop (Display );
}

The program running results show eight triangles of different colors connected to slices on the screen. The color of each triangle is defined in the color table mode. The auxiliary library function auxsetonecolor () is called to load the color ing table, that is, the color palette. Because the process of loading a color to a color lookup table must depend on the window system, and OpenGL functions are irrelevant to the window system, so here we call the Helper library function to complete this process, and then call OpenGL's own function glindex () to set the current color number.

Figure 9-3 custom palette

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.