about 图形学中的着色过程,重点描述光照模型。

属于图形学系列文章

物理中的光

光源分为:环境光、平行光、点光源、聚光灯光源

光源与物体接触有两种现象:散射 和 吸收,散射到物体内部:折射/散射;散射到外部:反射,其余部分被吸收

散射不改变光线密度和颜色,只改变方向;吸收不改变方向,只改变光线密度和颜色,

反射模型

Ambient Reflection 环境反射

Approximate interreflection (i.e., light bouncing off multiple surfaces) 很多物体之间相互反射,很不精确

Not a physically meaningful quantity, just to make the scene not too dark even if no light reaches a point.

I=IakaIa= intensity of ambient light ka= percentage of the light reflected by the object 

Diffuse Reflection 漫反射

Approximate “body reflection” of rough surfaces (大多数塑料材质)

Also called Lambertian surface

当光线照射到物体表面上,the energy of light will spread around the area,所以需要乘以 cosθ

I=IpkdcosθIp= intensity of light kd= diffuse coefficient 

If light is at infinity, L is constant over the whole surface:

I=Ipkd(NL)

其中的 NL 见本文第一张图


上面的公式没有考虑到光源到物体表面的距离

I=Ipfattkd(NL)fatt=1a+bd+cd2d= distance of light from the surface a,b and c are user defined constants 

Specular Reflection 镜面反射

光照在金属材质表面,往往会产生高光。Amount of reflection changes with viewpoint 从不同的角度观察物体结果都不一样。

Phong模型

Ipkscosn(α)cos(α): fall off as V moves away from Rn gives the sharpness 

Blinn-Phong模型

课件没有介绍,但挺有名的,从这篇文章摘录一些内容作为补充。

Blinn-Phong模型是对phong模型的改进,解决了Phong模型存在的一些失真情况,并且提高了运算效率。在Blinn-Phong模型中,使用H单位矢量代替了R,称作”HalfWay”:

Bidirectional Reflectance Distribution Functions

Phong reflectance model 不遵循真实的物理世界,该模型就是它的补充。仅做补充,考试应该不考。

实际上光照模型分为三种,第一种纯手工测量[MERL lab],第二种就是前面的那些经验公式(Phong, Blinn-Phong, Ward),此外还有物理上的分析(Cook-Torrance, Torrance-Sparrow, …)

如何计算反射方向?

注意这里 LN 都是单位向量

S=NcosθLR=Ncosθ+S=2NcosθL=2N(NL)L

以上考虑的都是单个光源 (ambient light source),如果有多个光源的话,我们可以把每个光源的结果相加,但如果光源太多,渲染过程会很慢。

着色模型

确定材质上的光照效果的这种操作被称为着色。在 Rendering Pipeline 过程中,我们是怎么着色的呢?

首先,我们会计算三角形每个顶点的颜色,这个过程叫 顶点着色,发生在model-view transformation 之后的几何处理阶段

顶点着色后的结果会传递到光栅化阶段,接着我们会使用 插值(interpolation) 对三角形的内部着色。

如何计算 normal 法线?

Normal of a triangle:

vertices are in anticlockwise direction with respect to normal

N=(BA)×(CA)

Normal of a vertex:

Average of all the triangle incident on the vertex

Nv=N1+N2+N3+N44

Constant/Flat/Faceted Shading

对于每个三角形只计算一次,使用 Normal of a triangle 着色,最终的效果是每个三角形都很均匀。

Gouraud/Smooth Shading

使用 Normal of a vertex 着色,Interpolating illumination between vertices,Bilinear interpolation across the triangle,也就是说,对每个顶点进行颜色计算,将这个颜色作为输入传递给片段着色器,由它对两点之间的点进行插值计算。

这种方法计算的 edge 能够得到相同的颜色,不论它是从那边的三角形渲染的,Shading is continuous at edges

Tends to spread sharp illumination spots over the triangle,所以该方法对于线性的颜色均匀的图像处理的比较好,但这同时是该模型的缺点,对于镜面高光的效果不好,因为插值是插入两个数之间的值,不可能大于较大的顶点值,所以高光只能在顶点出现。

Phong Shading

不要和 Phong reflectance model 混淆。使用 Normal of a triangle 着色,在 rasterization 的时候使用 interpolated normal 计算每个像素的颜色。

相较于 Flat Shading 的区别在于,Flat Shading 顶点着色器传递给片段着色器的值是已经计算好的 color,而该模型传递的是 normal data 和 position data。

缺点是比 Gouraud Shading 慢(但现在硬件发展快,不再care这个问题),优点是解决了高光问题。