OpenGL basic Graphics Programming (ix) OpenGL colors

Source: Internet
Author: User

Nine, OpenGL color

Almost all OpenGL applications aim to draw color graphics within the screen window, so color plays an important role in OpenGL programming. The color here is not the same as the color concept in painting, it belongs to the RGB color space and is only displayed on the monitor screen. In addition, the screen window coordinates are in pixels, so each pixel that makes up the graph has its own color, and this color value is computed by processing a series of OpenGL function commands. This chapter will describe the concept of computer color and OpenGL color model, color definition and two modes of application, and so on, if you master the color of the application, you can go into the colorful world of color, to enjoy endless fun.


9.1. Computer Color

9.1.1    Color Generation principle
computer colors are different from the colors in painting or printing, and the color of each point displayed on the computer screen is a mixture of three different colors of light (red, green, blue) that are fired by the electronic gun inside the monitor, so the computer color is usually R (Red), G (Green), B (Blue) Three values to indicate that these three values are also known as color components. The principle of color generation is shown in Figure 9-1.

Figure 9-1 Principle of computer color generation

9.1.2 RGB color Stereoscopic ( rgb Color Cube )
All monitor screen colors are In the RGB color space, if you use a cube to represent the RGB color composition of the relationship, then the cube is called the RGB color stereoscopic, 9-2 is shown.

Figure 9-2 RGB Color Stereo

in the figure, the values of R, G, and b three range from 0.0 to 1.0. If a color component is larger, it indicates that the corresponding color component is brighter, that is, the more color components it contributes at this point, the darker or less the lower. When R, G, b Three values are all 0.0, this point color is black (Black); When all three are 1.0, the point color is white ( White); When the values of the three color components are equal, the three contributions are the same, thus rendering the gray (Grey), which is shown as the diagonal line from the black vertex to the white vertex, when r=1.0, g=1.0, b=0.0, the point color is yellow (YellowSimilarly, r=1.0, g=0.0, b=1.0 are magenta, also known as PINKCO (Magenta), r=0.0, g=1.0, b=1.0 Cyan (Cyan).


9.2. Color Mode
OpenGL Color Mode has a total of two: RGB (RGBA) mode and color table mode. In RGB mode, all color definitions are represented by R, G, b three values, and sometimes alpha values (related to transparency), or 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, b values. Such a table becomes a color map (Color Map).

   9.2.1 Rgba modes (rgbamode)
in Rgba mode, you can use glcolor* () to define the current color. Its function 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 a);  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);
sets the current R, G, B, and a values. This function has 3 and 42 ways, in the previous way, the A value defaults to 1.0, the latter alpha value is set by the user, ranging from 0.0 to 1.0. Similarly, it can also pass parameters using pointers. In addition, the function of the second suffix of the different use, its corresponding parameter values and range, see table 9-1 below. Although these parameter values are different, OpenGL is actually automatically mapped to 0.0 to 1.0 or-1.0 or range. Therefore, flexible use of these suffixes will give you a lot of programming convenience.


   9.2.2 Color Table mode ( Color_index Mode )
in the color table mode, you can call the glindex* () function to select the current color from the color table. Its function is:   
void Glindex{sifd} (type C); void Glindex{sifd}v (type *c);
sets the current color index value, which is the palette number. Modulo if the value is greater than the number of color bit polygons.

   9.2.32 Modes of Application
in most cases, the use of Rgba mode is more than the color table mode, especially many effects processing, such as shadow, lighting, fog, anti-aliasing, mixing, etc., using the RGBA mode effect will be better, in addition, texture mapping can only be done in RGBA mode. Here are a few scenarios for using the color table pattern (for reference only):
1) If the original application is in color table mode, it is best to keep this mode for portability when you go to OpenGL.
2) Use the color table mode if the color used is not within the default color range provided.

3) In many other special treatments, such as color animation, the use of this mode will result in a singular effect.


9.3 , color application Example
color is an attractive application, in the previous chapters has introduced the RGBA mode of application, there is no more. The following emphasis on the application of color table mode, see the example:
( cindex.c " Span style= "font-family:arial; Line-height:26px ">"
#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 result of this program is that eight different color triangles are displayed on the screen, each with a color table pattern. Where the auxiliary library function Auxsetonecolor () is called to load the color map table, which is the palette. Because a color is loaded into a color lookup table (Color Lookup Tablethe process must depend on the windowing system, and the OpenGL function is not related to the windowing system, so this is called the function of the auxiliary library to complete the process, before calling OpenGL's own function Glindex () to set the current color number.

Figure 9-3 Customizing the Palette

OpenGL basic Graphics Programming (ix) OpenGL colors

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.