Basic OpenGL concepts 4-color, light, and material 3

Source: Internet
Author: User

4Specify a normal vector for the element

OpenGL must use the normal vector of the element to determine the brightness and shade of the element. Only when the objects in a scenario have different light and shade can the scenario be stereoscopic.

It is quite simple to determine the normal vector of a plane. On a plane, two vectors that are not parallel to each other are randomly searched. Their outer product (cross product) is the normal of the plane.

Formulas can be used to calculate the outer product of two vectors:

SX =UYVZ-UZVY
SY =UZVX-UXVZ
SZ =UXVY-UYVX

In the above formula, U and V are two unbalanced row vectors, and s is the outer product of U and V (that is, u × V ). After the outer product is calculated, we also need to convert the obtained vector to the unit vector. You only need to divide all X, Y, and Z factors of a vector by the modulo of the vector to obtain the unit vector. Therefore, you can use the following code to calculate the normal vector of a triangle.

Note that the vertex of the triangle should be passed to the function in a counter-clockwise order to obtain the correct result.

After obtaining the normal vector through calculation, we need to call the glnormal function to specify the normal for the vertex or element before drawing the vertex. For example:

Glbegin (gl_triangles );
Glnormal3f (, 1); // specify the normal for the triangle to be drawn
Glvertex3f (, 1); glvertex3f (, 1); glvertex3f (, 0 );
Glend ();

In addition, different normal vectors can be specified for different vertices of the same element. For example:

Glbegin (gl_triangles );
Glnormal3f (1, 1 );
Glvertex3f (1, 0, 1 );

Glnormal3f (1, 0, 1 );
Glvertex3f (1, 1 );

Glnormal3f (0, 1 );
Glvertex3f (1, 0, 0 );
Glend ();

5Material

After specifying the normal of the element, we also need to specify the corresponding material for it to determine the degree of reflection of the object on the light of various colors, which will affect the color of the object. Similar to the components of a light source, materials can also be divided into diffuse light and parallel light. The diffuse optical composition of the material determines the degree of reflection of the material on the diffuse light in the environment. Similar, the parallel optical composition determines the degree of reflection of the material on the parallel light. Similar to setting the light source, you only need to call the glmaterialf or glmaterialfv function before drawing the elements to set the parameters of the materials of the elements to be drawn.

5.1Sets the reflective ratio of materials to various types of light.

Call:

Glmaterialfv (gl_front, gl_diffuse, @ diffuse );

You can specify the reflectivity of materials to diffuse light. The first parameter determines whether the material is used on the front or back of the element. You can use gl_front (Front), gl_back (back), and gl_front_and_back (both sides ). The first value indicates the type of light to be set, and gl_diffuse indicates the diffuse light reflectivity. gl_ambient (ambient light), gl_diffuse (diffuse light), and gl_ambient_and_diffuse (ambient light and diffuse light) can be used), gl_specular (parallel/mirror light ). The third parameter is a four-dimensional array, which describes the rgba value of the Back-rate. Each item has a value between 0 and 1. For example

Diffuse [4] = {1, 0, 1 };

Glmaterialfv (gl_front, gl_diffuse, diffuse );

It will make the object red in the light of a red element.

5.2Use color tracking

It is inconvenient to specify the color for the elements after the lighting system is enabled. First, we need to create an array, and then call the glmaterial function to pass the array to the material to determine the color of the object. To simplify the code, we can turn on color tracking. Call

Glenable (gl_corlor_material );

Start color tracing and then call

Glcolormaterial (gl_front, gl_ambient_and_diffuse );

To determine whether the front or back of the object, and to track the color of the ambient light, mirror light, or diffuse light. The first parameter can be gl_front, gl_back, or gl_front_and_back. The second parameter can be gl_ambient, gl_diffuse, gl_ambient_and_diffuse, or gl_specular.

After the color tracking is started, we can use the glcolor function to specify the color of the elements as before. In this case, OpenGL automatically determines the object material based on the color passed by the glcolor function, saving the trouble of manually specifying the material. For example, to draw a Red Triangle while enabling the lighting system, you can do this:

Glable (gl_color_material );
Glcolormaterial (gl_front, gl_ambient_and_diffuse );
Glcolor3ub (255, 0, 0 );
Glbegin (gl_triangles );
Glvertex3f (, 1); glvertex3f (, 1); glvertex3f (, 0 );
Glend ();

5.3Mirror index of material

When the light source contains a mirror light, and the mirror light is strong, some smooth objects will have some highlighted focus.

We can set the mirror index of the material to determine the size and intensity of the spot. Call

Glmateriali (gl_front, gl_shininess,N);

You can set the mirror index. If the N value is greater, the smaller the spot size, the more shiny the object is. The N value can be any integer between 1 and.

5.4Radiant Material

Some objects emit light. We can set the emission component of the material to make the object look luminous. You only need to add the following code:

Int lightemission [4] = {1, 1, 1}

Glmaterialfv (gl_front, gl_emission, lightemission );

Here, lightemission indicates the rgba value of the color in which the object emits light.

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.