From: http://www.cnblogs.com/opengl/archive/2012/11/14/2770745.html
To add light to a scene, perform the following steps:
(1) set one or more light sources and related properties;
(2) Select a illumination model;
(3) set the material attributes of an object.
The details are as follows:
1. Set the light source
(1) types of light sources
Ambient Light
Environment light is a ubiquitous light. The light emitted by the ambient light source is considered to come from any direction. Therefore, when you only specify the ambient light for the scenario, all objects, regardless of the normal vector, will show the same degree of light and shade.
Point Light Source
The light emitted by this light source comes from the same point, and the direction of radiation is from all directions.
Parallel Light
Parallel Light, also known as mirror light, is parallel to each other. The light emitted from flashlight, sun, and other objects belongs to parallel light.
Spotlight
This light source emits light from a cone and produces a concentrating effect on the target object. To use this light source, you must specify the direction of the light and the pyramid's top angle α.
(2) Composition of light
Each light source has two components: diffuse light and parallel light. In OpenGL, ambient light is also viewed as a special light source component. A diffuse light refers to the color components of light that can be reflected in the light source (white includes all colors), while a parallel light is the color components of all light that can be reflected in the mirror. By specifying the colors of these two components, you can determine whether the light source is a parallel light source or a point light source.
(3) set the light source component
OpenGL can provide 8 effective light sources at the same time. That is to say, we can enable up to 8 light sources at the same time. They are gl_light0, gl_light1, gl_light2 ...... Gl_light0 is the most special light source. We can specify the ambient light composition for gl_light0.
A) set the ambient light.
For gl_light0, we can specify the ambient light composition for it. Call
Gllightfv (gl_light0, gl_ambient, ambientlight );
To set the environment light of the scenario. In the above function call, the first parameter indicates that we want to set gl_light0, the second parameter indicates that we want to set the environmental light composition, and the third parameter is an array, it has four values, indicating that the light source contains three elements: Red, green, and blue. Generally, the value is 1, and the last one is the transparency value, which is also 1. The complete code is as follows:
Intambientlight [4] = {1, 1, 1 };
Gllightfv (gl_light0, gl_ambient, ambientlight );
Glable (gl_light0 );
Glable (gl_lighting );
Note that in the third and fourth lines of the above Code, we call the glable function to enable the gl_light0 light source and illumination system respectively.
B) set the diffuse optical component.
By setting the diffuse optical composition, we can generate a point light source. The method is similar to setting the ambient light composition. You only need to call
Gllightfv (gl_light0, gl_diffuse, diffuselight );
You can. Diffuselight is the color component of the diffuse light. Generally, it is ).
C) set the mirror light Composition
By setting the mirror light composition, we can generate a parallel light source. The method is similar to setting the diffuse optical component. You only need to call
Gllightfv (gl_light0, gl_specular, specularlight );
You can. Specularlight is the color component of the diffuse light. You can specify different colors as needed.
(4) set the position of the light source
For point and line light sources, we often need to specify the position of the light source to produce the desired effect. The method is still to call the gllightfv function, just to replace the parameters:
Gllightfv (gl_light0, gl_position, @ lightposition );
Lightposition is also a four-dimensional array. The first three items of the four-dimensional array are the X, Y, and Z components of the light source position in sequence. The fourth value is special, generally 1 or-1. When lightposition [4] =-1, it indicates that the light source is infinitely far from the scene, regardless of the value of X, Y, and Z set above. When lightposition [4] = 1, the position of the light source is the position specified by the first three items.
2. Illumination Model
The OpenGL illumination model is used to simulate the illumination in real life.
3. Material settings
(1) Material color
OpenGL uses the reflectivity of the three primary colors of light, red, green, and blue to approximate the color of the material. Like a light source, the color of the material is divided into environment, diffuse reflection, and mirror reflection components, which determine the degree of reflection of the material on the ambient light, diffuse reflection light, and mirror light. In light computing, the reflectivity of materials to ambient light is combined with the ambient light of each incoming light source, and the reflectivity of diffuse light is combined with that of each incoming light source, the reflectivity of the mirror light is combined with the reflected light of each incoming light source. The degree of reflection of ambient light and diffuse light determines the color of the material, and they are very similar. The reflectivity of the reflected light is usually white or gray (that is, the reflectivity of red, green, and blue in the reflected light is the same ). The brightest part of a mirrored highlight is the color of the light source. For example, for a bright red plastic ball, most of the ball is red, and the bright highlight will be white.
(2) material definition
The definition of a material is similar to that of a light source. Its function is:
Void glmaterial {if} [v] (glenum face, glenum pname, type PARAM );
Defines the current material used in illumination calculation. Face can be gl_front, gl_back, and gl_front_and_back. It indicates which face the current material should be applied to. pname indicates a specific material. Param indicates the specific value of the material, if the function is a vector, Param is a pointer to a set of values, and vice versa. The non-vector form is only used to set gl_shiness. The following table describes the pname parameter values. In addition, the gl_ambient_and_diffuse parameter indicates that the ambient and diffuse colors can be set with the same RGB values.
___________________________________________________________________
Parameter Name default value description
Ambient light color of gl_ambient (0.2, 0.2, 0.2, 1.0)
Diffuse Light Color of gl_diffuse (0.8, 0.8, 0.8, 1.0) Materials
Ambient Light and diffuse light color of gl_ambient_and_diffuse
The reflected light color of gl_specular (0.0, 0.0, 0.0, 1.0) material.
Gl_shiness 0.0 mirror index (brightness)
Radiant color of gl_emission (0.0, 0.0, 0.0, 1.0) Materials
Environmental light, diffuse light, and mirror light color of gl_color_indexes (, 1) Materials
_______________________________________________
(3) Relationship between material RGB values and light source RGB values
The color of the material is somewhat different from that of the light source. For a light source, the R, G, and B values are equal to the ratio of R, G, and B to the maximum intensity. If the R, G, and B values of the light source are both 1.0, It is the strongest white light. If the value changes to 0.5, the color is still white, but the intensity is half of the original, so it is displayed as gray; if R = G = 1.0, B = 0.0, the light source is yellow. For materials, the R, G, and B values are the reflectivity of the R, G, and B components of the light. For example, if the r = 1.0, G = 0.5, and B = 0.0 of a material, the material reflects all the red components, half of the green components, and does not reflect the blue components. That is to say, if the light source color of OpenGL is (LR, LG, LB) and the material color is (MR, Mg, MB), when all other reflection effects are ignored, the final color of light reaching the eyes is (LR * Mr, LG * Mg, LB * MB ). Similarly, if there are two beams of light with the corresponding values (R1, G1, B1) and (R2, G2, B2), OpenGL adds each color component, (R1 + R2, G1 + G2, B1 + B2). If the value of any component is greater than 1 (beyond the brightness displayed by the device), the value is reduced to 1.0.