OpenGL ES lighting

Lighting Basics

From left to right are:

  1. ambient lighting
  2. Diffuse Lighting
  3. Specular Lighting
  4. Combining the above three is Feng's shading algorithm

image.png

Lighting properties

  1. Emitted light: emits light from the object itself
  2. Ambient light: is light that is sufficiently scattered in the environment and cannot tell its direction
  3. Diffuse light: light comes from a certain direction, but is reflected in all directions on the object
  4. Specular: Light comes from a specific direction and then bounces off the surface of the object in a specific direction

Material properties

  1. flood material
  2. Diffuse Material
  3. Specular Reflection Material
  4. emission material

Lighting calculation

  1. Calculation of ambient light (brightness)

Ambient light = ambient light color of the light source * material color of the object

image.png2. Calculated emission color of emitted light = object's reflective material color

Lighting comparison

image.png

Diffuse Lighting Calculation

image.pngThe light shines on the surface of the object, which determines the intensity of the light on the surface of the object, which is represented by the following figure:

image.png

The light intensity is the product of the intensity of the light itself and the angle cos between the light and the surface normal of the object, 有效的光照方向是与物体表面法线夹角在0~90度之间.

漫反射颜色 = 光源的漫反射颜色 * 物体的漫反射材质 * DiffuseFactor

DiffuseFactor = max(0, dot(N, L))

The diffuse factor DiffuseFactoris the dot product of the ray and the vertex normal vector

image.png

Specular light calculation

image.png N: plane normal

I: incident light

H: reflected light

E: sight

α: Angle between the viewpoint and the reflected light

镜面反射颜色 = 光源的镜面光的颜色 * 物体的镜面材质颜色 * SpecularFactor

SpecularFactor = power(max(0, dot(N,H)), shininess)

H: half vector of sight vector E and light vector L

dot(N,H): The dot product of H, N, the geometric meaning is the cos value of the angle between the square line and the normal line

shiniess: Reflectance of highlights

image.png 光照颜色 = (环境颜色 + 漫反射颜色 + 镜面反射颜色)* 衰减因子

Attenuation factor

image.png 衰减因子 = 1.0/(距离衰减常量 + 线性衰减常量 * 距离 + ⼆次衰减常量 * 距离的平方

The distance decay constant, linear decay constant and quadratic decay constant are all constant values.

注意: The intensity of ambient light, diffuse light and specular light will all be attenuated by distance, only the intensity of emitted light and global ambient light will not be affected.

Spotlight factor

聚光灯夹⻆cos值 = power(max(0,dot(单位光源位置,单位光线向量)),聚光灯指数);

  • The unit ray vector is the unit vector pointing from the light source to the vertex
  • Spotlight index, indicating the brightness of the spotlight
  • Interpretation of the formula: the power of the spotlight index of the dot product of the unit light source position and the unit light vector

Spotlight without transition and with transition processing

image.png

Add transition calculation

聚光灯因⼦ = clamp((外环的聚光灯⻆度cos值 - 当前顶点的聚光灯⻆度cos值)/ (外环的聚光灯⻆角度cos值 - 内环聚光灯的角度的cos值),0,1)

image.png

The ultimate formula for lighting calculations

光照颜色 = 发射颜色 + 全局环境颜色 + (环境颜色 + 漫反射颜色 + 镜⾯面反射颜色) * 聚光灯效果 * 衰减因子

Guess you like

Origin juejin.im/post/7085714693435686949