OpenGL | Light Sources & Materials

OpenGL divides the lighting system into three parts when dealing with lighting, namely light source, material and lighting model. Light sources, materials, and lighting modes all have their own properties, and although there are many types of properties, these properties are set with only a few functions.

  • Use the glLight* functions to set the properties of a light source.

  • Use the glMaterial* functions to set the properties of a material.

  • Use the glLightModel* functions to set the lighting mode.

The three properties of GL_AMBIENT, GL_DIFFUSE, and GL_SPECULAR are shared by light sources and materials.

1. Light source

1. Ambient light

Ambient light is a light that has no light source, no direction, and produces the same lighting effect on all objects in the scene. The characteristics of ambient light are: the light irradiated on the object comes from all directions around it, and is evenly reflected in all directions.

2. Diffuse light

Diffuse light emphasizes the influence of the angle at which the light hits the surface of the object on the brightness of the object. The most important characteristic of diffuse light is the direction of the light.

3. Specular reflection light

The specular reflection of light is not so much a property of the light itself, but a property of the object. This property is that when the incident light and the observer's viewing angle are both at a certain angle, the object will be highly luminous. For example, on a sunny day, you will see a certain edge of the car will be extraordinarily luminous.

The attribute GL_SPECULAR of the light source affects the color of the specular reflection area. Generally, the color of the specular reflection area of ​​an object is the color of the incident light. To achieve realism, its value should be set to be the same as GL_DIFFUSE.
    float whiteColor[] = { 1.0f,1.0f,1.0f,1.0f };       
    glLightfv(GL_LIGHT0, GL_AMBIENT, whiteColor);        //环境光
    glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteColor);        //漫反射
    glLightfv(GL_LIGHT0, GL_SPECULAR, whiteColor);       //镜面反射

2. Material

When we use lighting to describe a polygon, we always say that it is made of a material with certain reflective properties, not that it has a particular color. In this way, if we specify the color of the object, we must specify the reflection properties of the object material for ambient light, diffuse light and specular light source. Usually, we use the reflectivity of the material to the red, green and blue primary colors of light to approximately define the material properties.

Like light sources, material colors are divided into ambient, diffuse, and specular components, which determine how much the material reflects ambient, diffuse, and specular light.

   //环境光材质 
   float ambientMat[] = { 1.0f,0.0f,0.0f,1.0f };      
   glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMat);
   
   //漫反射材质,和法线有关
   float diffuseMat[] = { 0.0f,1.0f,0.0f,1.0f };      
   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
   
   //镜面反射材质,和法线有关
   float specularMat[] = { 0.0f,0.0f,1.0f,1.0f };
   glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
   glMaterialf(GL_FRONT, GL_SHININESS, 30.0f);  //镜面指数

3. Illumination calculation

When performing lighting calculations, the final color of an object is jointly determined by the RGB value of its material property and the RGB value of the light source property. For example, if the RGB value of the current ambient light source is (0.5f, 1.0f, 0.5f ), and the RGB value of the ambient reflection component of the object's material is (0.5f, 0.5f, 0.5f ), then the final color of the object for :

( 0.5 × 0.5 , 1.0 × 0.5 , 0.5 × 0.5 ) = ( 0.25 , 0.5 , 0.25 )

   float whiteColor[] = { 0.5f,1.0f,0.5f,1.0f };       
   glLightfv(GL_LIGHT0, GL_AMBIENT, whiteColor);        //环境光源
   float ambientMat[] = { 0.5f,0.5f,0.5f,1.0f };      
   glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMat);      //环境光材质

That is, each ambient light component is multiplied by the material's ambient reflectance. In this way, the color of the surface of the object is the superposition of three RGB values: the reflectivity of the material to ambient light and the RGB value combined with ambient light; the reflectivity of the material to diffuse light and the RGB value combined with diffuse light; the material to specular light The RGB value of reflectivity combined with specular light. Note that when the value of any color component in the superimposed RGB is greater than 1.0, then 1.0 is used for calculation.

4. Normal

1. The normal affects light and shade.

2. Diffuse reflection and specular reflection are related to light direction and normal.

おすすめ

転載: blog.csdn.net/weixin_39766005/article/details/129451924