#### Transcript 第二课

```第二课
Overview of this Section


Lighting (光照) & Material (材质)
OpenGL Implementation
Lighting enabled
Lighting disabled
Lighting

Simple illumination 简单光照模型




Local illumination 局部光照模型



OpenGL、Direct3D直接支持
BRDF(双向反射分布函数)等
Global illumination 整体光照模型


Simple Illumination


Reflection, Transmission, Absorption
Reflection



Specular Reflection （镜面反射）
Diffuse Reflection (漫反射)
Phong 模型


Phong

Diffuse （理想漫反射）



Specular (镜面反射)



View Independent （视点无关）
Id = IpKd*(L∙N)
View Dependent (视点相关)
Is = IpKs(R∙V)n
Ambient (环境光)



Ie = IaKa
Phong


I = IaKa + IpKs(R∙V)n + IpKd*(L∙N)



H∙N替代R∙V
H为L和V的平分向量
Phong



Gourand & Phong 明暗处理





Gourand明暗处理





Phong模型计算顶点光强

Phong明暗处理







Gourand插值计算

Phong插值计算量

Light in OpenGL

Ambient Color


Diffuse Color



Specular Color


Light
Ambient : Green
Diffuse : Red
Specular : Blue
Material in OpenGL

Material



Diffuse



Ambient


Material in OpenGL

Specular & Shininess


Shininess控制高光的程度
Emission


OpenGL Lighting




Emission + Global Light Ambient *
Material Ambient + ∑(Lighti Ambient *
Material Ambient + Lighti Specular *
Material Specular + Lighti Diffuse *
Material Diffuse)

Put Light into OpenGL







Assign Normal Vector



glNormal{34}{isfd}[v](TYPE* normal);


Assign normal vector for vertices (the normal
vector should be assigned before you assign
vertices).
Normal vectors must be normalize. OpenGL
can automatically normalize normal vector by
glEnable(GL_NORMALIZE);
Sample： Assign Normal Vector
glBegin(GL_TRIANGLE);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, -1.0);
glVertex3f(-1.0, 0.0, 0.0);
glEnd();
Light Source




Use glEnable(GL_LIGHT0); to enable light zero.
You can use at most 8 light sources in OpenGL
spec. (GL_LIGHT0~GL_LIGHT7)
glLight{if}[v](GLenum light, Glenum pname,
TYPE param);




Specify the attribute of light
light can be GL_LIGHT0 ~ GL_LIGHT7
pname is the characteristic of the light
param is the value of pname
Light Source
Pname
Def. Value
Meaning
GL_AMBIENT
GL_DIFFUSE
GL_SPECULAR
GL_POSITION
GL_SPOT_DIRECTION
GL_SPOT_EXPONENT
GL_SPOT_CUTOFF
GL_CONSTANT_ATTENUATION
GL_LINEAR_ATTENUATION
(0.0, 0.0, 0.0, 0.0)
(1.0, 1.0, 1.0, 1.0)
(1.0, 1.0, 1.0, 1.0)
(0.0, 0.0, 1.0, 0.0)
(0.0, 0.0, -1.0)
0.0
180.0
1.0
0.0
0.0

spotlight cutoff angle
constant attenuation factor
linear attenuation factor
Light Source

Color


The default values listed for GL_DIFFUSE and
GL_SPECULAR apply only to GL_LIGHT0.
For other lights, the default value is (0.0, 0.0, 0.0, 1.0 ) for
both GL_DIFFUSE and GL_SPECULAR.

Ex.



GLfloat light_ambient[4] = {0.0, 0.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
Position ( x, y, z, w )



W is ZERO, Directional light, (x,y,z) specify its direction.
W is NONZERO, Positional light, (x,y,z) specify the
location of the light source.
ModelView Enabled
Light Source

Attenuation (衰减系数)
Attenuatio n Facror 





1
kc  kld  kqd
2
d = 光源到顶点位置
kc = GL_CONSTANT_ATTENUATION
kl = GL_LINEAR_ATTENUATION

Material

Define material properties for objects
in the scene.

void glMaterial{if}[v](GLenum face,
GLenum pname, TYPE[*] param);

Specifies a current material property for use
in lighting calculations.
Material
Pname
Def. Value
Meaning
GL_AMBIENT
GL_DIFFUSE
GL_AMBIENT_AND_DIFFUSE
(0.2, 0.2, 0.2, 1.0)
(0.8, 0.8, 0.8, 1.0)
GL_SPECULAR
GL_SHININESS
GL_EMISSION
GL_COLOR_INDEXES
(0.0, 0.0, 0.0, 1.0)
0.0
(0.0, 0.0, 0.0, 1.0)
(0, 1, 1)
ambient color of material
diffuse color of material
ambient and diffuse color of
material
specular color of material
specular exponent
emissive color of material
ambient, diffuse, and specular color
indices
Material
No
Ambient
Gray
Ambient
Blue
Ambient
Diffuse
Only
Specular
Higher
Shininess
Emission
Light


Demo, Light Position
Demo, Light Material
```