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明暗处理
Gourand明暗处理




计算多边形顶点平均法向
Phong模型计算顶点光强
插值计算边上的各点光强
插值计算多边形内部各点光强
Phong明暗处理




计算多边形顶点平均法向
插值离散边上各点法向
插值多边形内部法向
计算各像素光强度
插值效果


Gourand插值计算
量小,但会影响高
光效果绘制
Phong插值计算量
大,真实感更高
Light in OpenGL

Ambient Color


Diffuse Color


光源的环境光,一般为 零
可被认为是光源的颜色
Specular Color

一般与Diffuse颜色相当
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
GL_QUADRATIC_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
quadratic 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
kq = GL_QUADRATIC_ATTENUATION
方向光源,Attenuation is 1
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