3.2 Light Chapter (1)

Source: Internet
Author: User

Http://book.51cto.com/art/201104/255654.htm

3.2 Light Chapter (1)

The reason why we can see things is that there is light coming into everyone's eyes, regardless of the source of the light. If an object does not shine and does not reflect any light, then put it in the strongest sunlight and you will not see anything. As a result, OpenGL sets both the light nature and the reflective nature of the object (tell OpenGL how the light enters our eyes).

1. Classification and setting of light

There are 4 main types of light in OpenGL: spontaneous light, ambient light (ambient), scattered light (diffuse), specular light (specular). Different book translation methods are not the same, some books will be self-luminous separate classification. Some books also translate them into emitted light, ambient light, scattered light, and specular reflection light. There are also some books that translate specular light into mirrored or reflected light, which I personally think is not quite complete. Our translation here is mainly to connect with the optical knowledge in junior middle school physics.

Let's review the knowledge about light in junior high school physics. The light is "emitted" by the light source, the light from the light source to the object is called the "incident Light" in junior physics (it does not reach our eyes), and the object "processing" after the light in junior physics there are two "reflected light and refraction light"-a small part of them to reach our eyes.

Self-illumination: the light emitted by the object itself (most objects are not self-luminous, only the sun, lights, candles and other objects have spontaneous light).

Ambient light: The object's reflection on ambient light is equal in any direction, at exactly the same intensity from any direction in the space. In layman's terms, there is no direction when you come, and there is no direction when you go. So, in the case of only ambient light exposure, we look at objects from any angle, and the brightness is the same. We think it only plays an illuminating role, which does little to increase the 3D effect. The light source that can produce the environment light in life is nonexistent, the shadowless lamp of medicine is barely considered.

In fact, OpenGL believes that the environment is a comprehensive product of light. Like a cube placed on a table in a room, the light it receives includes light from the sun's light to the wall, light from the sun's light to the table, light from the sun's light to the dust particles in the air, light from the sunlight through the glass, direct illumination of the lamp, light emitted by the lamp on different objects, and so on. Although there is no instance of the ambient light source in life, OpenGL assumes such a light source, called an ambient light.

Scattering light: The object is shone from a fixed direction, but the object is reflected in any direction evenly. Of course, the part that it shines directly is brighter, and the part that is swept is relatively dark. In layman's terms, when you come to a fixed direction, there is no direction when you go.

Specular light: A light that comes from a light source in a particular direction and reflects in a particular direction. In layman's terms, when it comes to the fixed direction, the time to go is also fixed.

Enable lighting: Glenable (gl_lighting). This command is required for any lighting effect, and is equivalent to the door where lighting is enabled. If you do not add any other lighting code after lighting is enabled, the object you see is black and the color set by the Glcolor does not work.

Set the lighting mode (Gllightmodel): Some books translate it into a light model, which is not appropriate. GLLIGHTMODELFV (GL_LIGHT_MODEL_AMBIENT,V): Used to set ambient light, where V is required to be a four-element vector. And all of its component values range from [0,1], indicating the intensity of red, green, blue, and transparency respectively. Its default value is (0,0,0,1). I had to admit that I had been misled by this order, and I thought that replacing "gl_light_model_ambient" with "Gl_light_model_diffuse" could be used to set scattered light and replace it with "gl_light_model_ Specular "can be used to set specular reflection light, but that is not the case. Looking at some of the data, we found that the two types of light settings to be placed in the content of the light source. In fact, the most appropriate name for the ambient light enabled by this command is the global ambient light, which sets the brightness of the entire scene. After learning the knowledge of light source you will find that any light emitted by a light source can be divided into ambient light, scattered light, specular reflection light.

This command also has other uses, the most commonly used is:

Gllightmodelfi (gl_light_model_two_side,gl_true): Enables polygon double-sided illumination. That is, the polygon's two faces can be illuminated. The default value is Gl_false, which is polygon frontal illumination.

Below we will change the example of the cube in the 2nd chapter, add a little global ambient light effect. Just add the following code in front of the original program:

    1. Glenable (gl_lighting); Enable lighting
    2. Float v[]={1,1,1,1}; Light intensity Array
    3. GLLIGHTMODELFV (GL_LIGHT_MODEL_AMBIENT,V); Illumination mode (this refers to the global ambient light)
    4. Glenable (gl_color_material); Enable color tracking

The last line of code needs more explanation: Without this command, the subsequent color settings will not work. In fact, the object's reflection on the light is enabled here. Although there is ambient light shining on the object, if the object does not reflect, the light will not reach our eyes, of course, we see a black screen. When color tracking is enabled, OpenGL reflects the light based on the color set by the Glcolor later. The color traced here is the color set by the Glcolor.

The program runs as follows, where figure 3-3 of v[]={0.5,0.5,0.5,0.5}, figure 3-4 of v[]={1,1,1,1}; You can see the difference in brightness between the two. The difference here is mainly caused by the global ambient light. In addition, for a single graphic, rotate, you can see the brightness is the same everywhere.

Figure 3-3 v[]={0.5,0.5,0.5,0.5}

Figure 3-4 v[]={1,1,1,1}

3.2 Light Chapter (1)

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.