Computer Graphics (iii) _ Elements of attributes _3_opengl color function (bottom)

Source: Internet
Author: User

OpenGL color function (bottom)

OpenGL Color Array

We can also specify the color values of the scene in the vertex array together with the coordinate values (see section 2.17). This can be done in both RGB mode and in Rgba mode. As with the vertex array, you must first activate the OpenGL color array:
Glenableclientstate (Gl_color_array);
Then, to specify the position and format of the color component for the RGB mode:
Glcolorpointer (Ncolorcomponents,datatype,offset,colorarray);
Parameters ncolorcomponentsAn assignment of 3 or 4 depends on whether the array ColorarrayThe RGB or RGBA color components are listed in. OpenGL symbol constants such as GL INT or GL float are assigned to the parameter datatype to point to the data type of the color value. For a single color array, we can assign 0 to the parameter Offset。 However, if you combine the color data and the vertex data in the same array, the OffsetThe value is the number of bytes in each set of color components in the array.
The following program section sets the color of all vertices in front of the cube to blue, setting the color of all vertices of the back polygon to red.
typedef glint VERTEX3 [3], Color3 [3];vertex3 pt [8] = {{0,0,0}, {0,1,0}, {1,0,0}, {1,1,0}, {0,0,1}, {0,1,1}, {1,0,1}, {1, 1,1}};color3 hue [8] = {{1,0,0}, {1,0,0}, {0,0,1}, {0,0,1}, {1,0,0}, {1,0,0}, {0,0,1}, {0,0,1}};glenableclientstate} (gl_ve Rtex_array); glenableclientstate (Gl_color_array); Glvertexpointer (3, Gl_int, 0, PT); Glcolorpointer (3, GL_INT, 0, Hue);
We can load the color and vertex coordinates together into a jagged array ( Interlaced Array)。 Each pointer points to a single jagged array with the appropriate displacement value. For example:
Static Glint HUEANDPT [] = {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0 , 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1};glvertexpointer (3, gl_int, 6 * sizeof (Glint), HUEANDPT [3]); Glcolorpoin ter (3, gl_int, 6 * sizeof (Glint), HUEANDPT [0]);
The first three elements of the array specify an RGB color value, and the next three elements specify a vertex coordinate (x, y, z) so that it is staggered down to the end. Sets offset to the number of bytes between adjacent colors or vertex values, that is, both are 6 * sizeof (GLINT)。 The color value from the first element of a jagged array is hueandpt[0] Start, the vertex value is from the fourth element Hueandpt[3]Begin.
Because a scene typically consists of several objects, each with multiple flat surfaces, OpenGL provides a function that can specify all vertices and color arrays and other types of information at once. If we want to change the color and vertex values in the example above to floating-point numbers, we need to use the function in the following format:
Glinterleavedarrays (GL_C3F_V3F,0,HUEANDPT);
The first parameter is an OpenGL constant that specifies the ternary floating point description for the color (C) and Vertex (V). The array hueandpt is interleaved by the way each vertex's color is placed before its coordinates. The function also automatically activates vertices and color arrays. In color index mode, use the following statement to define an array of color indexes:
Glindexpointer (type, stride, ColorIndex);
Color index in array ColorIndex, and the parameters type and stride are the same as in G1coiorpointer.because the color table index is described with a single value, the size parameter is not required.

Other OpenGL color functions

This function is used to select the RGB color component for the display window.
  G1clearcolor (red, green, blue, alpha);
each component (red, green, blue) and the A parameter are assigned floating-point values in the range 0.0 to 1.0. The default values for the four parameters are 0.0, and they generate a black。 If each color component is set to 1.0, the net color is white. A variety of grays are obtained when the color component is an equal value between 0.0 and 1.0. The fourth parameter α provides options for reconciling the front color with the current color.This only occurs when OpenGL's harmonic features are activated, and color blending cannot be applied to the values specified in the color table。
As we noted in section 3.19, OpenGL has several color caches that can be used to display the current refresh cache of the scene, while the function g1ciearcolor specifies the color of all color caches. Then use the following command to set the net colorused for these color caches:
Glclear (Gl_color_buffer_bit);
We can also use the Glclear function to set the initial value of other caches that are valid in OpenGL. They are the cumulative cache (accumulation buffer) that holds the harmonic color information, the depth cache (depth buffer) that holds the depth value of the scene object (distance from the viewing location), and the template cache (stencil buffer) that defines the graphics range.
In the color index mode, use the followingfunction (instead of g1clearcolor) to set the display window color。
Glclearindex (index);
The window background color is specified with the color stored in the index position in the color table. Perform glclear (gl_color_buffer_bit)Letter
The window is displayed in this color after counting.
There are other color functions in the OpenGL Library to handle a variety of tasks, such as changing the color pattern, setting the scene lighting effect, describing the camera effect, and drawing the object surface. We examine other color functions when analyzing various processing of computer graphics systems. But for now, we're limiting the discussion to the range of functions that are relevant to the color description of the entity.

Computer Graphics (iii) _ Elements of attributes _3_opengl color function (bottom)

Related Article

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.