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

Source: Internet
Author: User

3Set Light Source

3.1Type of Light Source

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 such a light source comes from the same point and emits radiation in all directions, as shown in 5.3-1.

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 α.

3.2Composition 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.3Set 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.

3.3.1Set 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:

Void setlight

{
Int ambientlight [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.

3.3.2Set 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 ).

3.3.3Set 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.

 

3.4Set 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.

3.5Create a spotlight

3.5.1Set the light source clipping Angle

Call

Gllightf (gl_light0, gl_spot_cutoff, lightcutoff );

Lightcutoff is the Alpha angle shown in Figure 5.3-2 of the spotlight, that is, the top angle of the spotlight cone. By default, the value is 180 °, that is, a point light source.

3.5.2Set light attenuation coefficient

We know that when light is transmitted in the air, the light intensity will continuously decrease due to the refraction and absorption of the suspended particles and small water drops in the atmosphere. To achieve this effect, we can call

Gllightf (gl_light0, attenuationway, spotattenuation );

To set the attenuation coefficient of the light source. Here, attenuationway can take the following values:

Gl_constant_attenuation -- indicates that the light is reduced by Changshu (not related to distance)

Gl_linear_attenuation -- linear attenuation of light by distance

Gl_quadratic_attenuation -- indicates that the light fades by a quadratic function at a distance.

The spotattenuation parameter is the attenuation coefficient of the light.

3.5.3Set illumination direction

Yes

Gllightfv (gl_light0, gl_spot_direction ,@Spotdir);

Spotdir is a three-dimensional array and a vector that represents the direction of the spotlight.

3.5.4Set Concentration Index

Finally, call

Gllightfv (gl_light0, gl_spot_exponent,Spotexponent);

Set the concentration index. Spotexponent specifies the spotlight intensity.

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.