计算机图形学(五)Lighting and Shading
1. Lighting Conception
- Need to consider:
- Surface orientation – normal
- Material properties
- Light sources
- Location of viewer
- Light which shines from light source to the objects transmits the reflective light, transparent light and abosrptive light (not be seen)
- Reflective light includes diffuse and specular which more depends on the material of the object.
2. Lighting Model
Local Lighting Model: all light comes from lights defined within the scene.
three components:
- Ambient light 环境光
- Diffuse light 漫反射光
- Specular light 镜面反射光
Ambient Light 环境光
- 虚拟的光,使空间获得均匀的照明,不考虑光源位置、观察者位置和表面法向量
- $A=L_0+\sum_{lights}(L_A*C_A)$
- ambient light $L_A$, material ambient value $C_A$
Diffuse Light 漫反射光
- $D=\sum_{lights}L_D*C_D(L\cdot N)$
- diffuse value $L_D$; material ambient value $C_D$;
- light vector $L$; normal vector $N$
- $D=\sum_{lights}L_D*C_D(L\cdot N)$
Specular Light 镜面反射
- $S=\sum_{lights}L_S*C_S(V\cdot R)^K$
- specular value $L_S$; material value $C_S$; material shininess $K$ (聚集度 0~127)
- eye vector $V$; the reflection of a light vector $R$
- $R=L-2(N(N\cdot L))$
- $S=\sum_{lights}L_S*C_S(V\cdot R)^K$
Local Illumination Model
- $I=L_0+\sum_{light}(LAC_A)+\sum_{lights}L_D*C_D(L\cdot N)+\sum_{lights}L_SD_S(V\cdot(L-2(N(N\cdot L))))^K$
- four vectors:
- To Source $l$
- To viewer $v$
- Normal $n$
- Perfect reflector $r$
Blinn-Phong Lighting Model
- the modified Blinn-Phong lighting model
- 半角向量 $h=\frac{l+v}{|l+v|}$,将$(r\cdot v)^e$替换成$(n\cdot h)^{e’}$
- $S=\sum_{lights}L_SC_S(n\cdot h)^{K_1}$
- the modified Blinn-Phong lighting model
3. Surface orientation and Material
- two need things: normals & material
4. Light Properties
Types of Light Sources
- Point lights: $(x,y,z,1)^T$
- Directional/parallel lights: $(x,y,z,0)^T$
- Spotlights: position, direction, angle(cutoff)
- Ambient Lights
Light Position
- $n$: the normal of the surface
- $(x,y,z)$: the coordinates of a point on the surface
- $s$: the direction of the light source at $(x,y,z)$
Directional Sources
- $s$ remains unchangeed across a surface
The spot of the Light Source
- Define:
- the position $P$
- direction $D$
- cutoff $\theta $
- dropoff $d$
- Q is in cutoff $\theta$: $l_Q=\cos^d(\phi)=((Q-P)\cdot D)^d$, if $\phi<\theta$
- Q is out of range cutoff $\theta$: $I_Q=0$, if$\phi>\theta$
- Define:
Light Attenuation
- For a point light source, the attenuation factor is
- $A_f=\frac{1}{d^2}$ (in theory)
- $A_f=\frac{1}{a+bd+cd^2}$ (in practice)
- For a directional light source, $A_f=1$
- $I=L+\sum_{lights}A_f\times L_A\times C_A+\sum_{lights}A_f\times L_D\times C_D\times(L\cdot N)+\sum_{lights}A_f\times L_S\times C_S(N\cdot H)^K$
- For a point light source, the attenuation factor is
Color for a Light Source – RGB values
vec4 diffuse0 =vec4(1.0, 1.0, 1.0, 1.0); //white color vec4 ambient0 = vec4(1.0, 1.0, 1.0, 1.0); //white color vec4 specular0 = vec4(1.0, 1.0, 1.0, 1.0); //white color // specify the position of a point light source vec4 light0_pos =vec4(1.0, 2.0, 3,0, 1.0); //specify the direction of a directional light source vec4 light1_pos =vec4(1.0, 1.0, 1,0, 0.0);
5. Moving Light Sources
- 移动物体或光源,受视图-模型矩阵影响
6. Shading 着色、明暗绘制
- Shading
- 计算每个图形对象的颜色
- 使用每个顶点的属性计算对象上每个像素的颜色
- 意味着颜色可以通过光照模型来设置和计算
- OpenGL可以使用均匀着色(flat shading)和平滑着色(smooth shading, Gouraud)两种着色
- Constant (Flat) Shading
- 多边形上每个点的颜色值都相同,对每个多边形只需要进行一次明暗计算
- 效果不好,因为人视觉系统有侧抑制(laeral inhibition)的性质,Mach带效应(March band)。
- Gouraud (Smooth) Shading
- 每个顶点都分配一个法向量(可能是多边形的法向量,也可能是相邻面的法向量的平均值)
- 插值计算
- $n_c(\alpha)=(1-\alpha)n_A+\alpha n_B$,$\alpha=\frac{distance\ of\ AC}{distance\ of\ AB}$
- $n(\alpha,\beta)=(1-\beta)n_C+\beta n_D$
- 基于片元的着色(per-fragment shading)
- $I_{P2}=I_{P1}+\Delta I_{RQ}\times\Delta t$
- Other Shading Models
- Phong Shading
- interpolate the normals across a polygon,对多边形上的法向量进行插值
- 计算每个像素颜色使用独立的局部光照模型
- $n_{P2}=n_{P1}+\Delta n_{RQ}\times\Delta t$
- Phong Shading
7. Computing Light in OpenGL
- Light: $(LR,LG,LB)$,material: $(MR,MG,MB)$
- The color of a material mainly depends on the diffuse reflectance.
- The diffuse reflection alone provides most information about the curvature and the depth of an object
- The ambient reflectance of a material is often the same as the diffuse reflectance.
- The highlights produced in specular reflections are the blurred images of the light sources.
8. Front and Back Faces
- Emissive Term
- If we want to simulate a lamp, light source in OpenGL is defined as an emissive component
- Transparency
- RGBA
9. Implementing Light Model
State-based shading functions have been deprecated ($glNormal$, $glMaterial$, $glLight$)
Method I
- Vertex Shader
void main() { // Transform vertex position into eye coordinates vec3 pos = (ModelView * vPosition).xyz; vec3 L = normalize( LightPosition.xyz - pos ); vec3 E = normalize( -pos ); vec3 H = normalize( L + E ); // Transform vertex normal into eye coordinates vec3 N = normalize( ModelView*vec4(vNormal, 0.0) ).xyz; // Compute terms in the illumination equation vec4 ambient = AmbientProduct; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = Kd*DiffuseProduct; float Ks = pow( max(dot(N, H), 0.0), Shininess ); vec4 specular = Ks * SpecularProduct; if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_Position = Projection * ModelView * vPosition; color = ambient + diffuse + specular; color.a = 1.0; }
- Fragment Shader
// fragment shader in vec4 color; void main() { gl_FragColor = color; }
Method II
- Vertex Shader
// vertex shader in vec4 vPosition; in vec3 vNormal; // output values that will be interpolatated per-fragment out vec3 fN; out vec3 fE; out vec3 fL; uniform mat4 ModelView; uniform mat4 Projection; uniform vec4 LightPosition; void main() { fN = vNormal; fE = vPosition.xyz; fL = LightPosition.xyz; if( LightPosition.w != 0.0 ) { fL = LightPosition.xyz - vPosition.xyz; } gl_Position = Projection*ModelView*vPosition; }
- Fragment Shader
// fragment shader // per-fragment interpolated values from the vertex shader in vec3 fN; in vec3 fL; in vec3 fE; uniform vec4 AmbientProduct, DiffuseProduct, SpecularProduct; uniform float Shininess; void main() { // Normalize the input lighting vectors vec3 N = normalize(fN); vec3 E = normalize(fE); vec3 L = normalize(fL); vec3 H = normalize( L + E ); vec4 ambient = AmbientProduct; float Kd = max(dot(L, N), 0.0); vec4 diffuse = Kd*DiffuseProduct; float Ks = pow(max(dot(N, H), 0.0), Shininess); vec4 specular = Ks*SpecularProduct; // discard the specular highlight if the light's behind the vertex if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_FragColor = ambient + diffuse + specular; gl_FragColor.a = 1.0; }
10. Other Techniques
- Anisotropic Shading 各向异性的光照
- CD、光盘的照亮
- Bump Mapping
- Global Illumination
- Ray Tracing: great specular, approx diffuse
- Radiosity: great diffuse, ignore specular