Prepare for lighting before enabling in OpenGL--Specifying Normals

in addition to intensity and color, we need to specify the position and orientation of the light source, and the position and orientation of the lights will greatly affect the appearance of the scene.

OpenGL supports a minimum of 8 independent light sources. When we specify a light source, we tell OpenGL the location of the light source and its direction of illumination. The light source often shines around, but it can also be illuminated in one direction. In either case, the light from any light source (except the purely ambient light source) will collide with the surface of the polygon that makes up the object for any object we draw. To calculate the shading effect around the polygon surface, OpenGL must be able to calculate the angle between the light and the polygon surface.

Imagine that a polygon is irradiated by a beam of light from a light source, and when the light hits the surface of the polygon, it forms an angle to the plane (a), and then the light is reflected to the observer at an angle (B) (The observer is not necessarily able to see it). With these angles, together with the lighting parameters and material properties we discussed earlier, we can calculate the appearance color of this position,

Since OpenGL calculates the appearance color based on each vertex, how do we calculate the angle between the vertex and the light when each vertex is irradiated by a beam of light from an angle? Of course, we cannot find the angle of a point and a straight line in 3D space by means of geometry, because it has infinite possibilities. To solve this problem, we have to associate each vertex with some information, which is what we're going to say next, a vector that represents the vertical upward of each vertex-the normal.

surface normalson an imaginary plane (or polygon), a vertically upward vector is called the normal vector. In fact, it is a straight line (vector) pointing in a direction that is 90 degrees from the surface of the polygon.

Specifying Normalsin OpenGL, the following code specifies a normal vector: Glbegin (gl_triangles);glnormal3f (0.0f, -1.0f, 0.0f);glvertex3f (0.0f, 0.0f, 60.0f);glvertex3f ( -15.0f, 0.0f, 30.0f);glvertex3f (15.0f, 0.0f, 30.0f);glend ();

The glnormal3f function accepts 3 values representing coordinates (x, y, z), which specifies a normal vector perpendicular to the surface of the triangle. Here, the normals of all three vertices have the same direction, all along the negative direction of the y-axis downward. This is a simple example, because the triangle lies flat on the x-z plane.

When we start drawing, it is almost impossible to assign a normal to each vertex or polygon, especially if only a very small portion of the surface is parallel to the three main planes. Of course, we have a solution that can generate the normals we need by invoking a method in the Gltools library.

Generating Normalswe can calculate its normal vector by taking 3 points on the polygon plane. , 3 points P1, P2, P3 on the plane, we can define two vectors, p2 from P1 to V1, and vector P1 from P3 to V2. From a mathematical standpoint, two vectors in three-dimensional space define a plane. We cross-product the two vectors (V1XV2), and the resulting vectors are perpendicular to this plane, that is, we need the normal vectors, as shown in the vector V3:

The Gltool library contains a function that is designed to calculate a normal vector based on 3 points on a polygon:

void M3dfindnormal (m3dvector3f vnormal,Const m3dvector3f vP1,Const m3dvector3f VP2,const m3dvector3f vP3);

The first parameter of the method is used to store the evaluated normal vector, and another 3 vectors are passed to it, representing the points taken from the polygon or triangle (specified in a counterclockwise direction). Note that the normal vector returned by the method is not necessarily of the unit length.

Unit normalsThe so-called unit normal, is the length of 1 normal. The process of converting normals to unit normals is called normalization of normals. In OpenGL implementations, for lighting calculations, all normal vectors must be normalized before being involved in the calculation.

**This article is from Du Xiaomeng's blog, do not use for any commercial purposes, reproduced please maintain integrity and identify the source: Http://blog.csdn.net/haohan_meng**

Prepare for lighting before enabling in OpenGL--Specifying Normals