Lecture 01 Overview
cg含义: 合成和操作视觉信息
应用 : video games, movies, animations, design, visualization, virtual reality, digital illustration, simulation, graphical user interfaces, typography(the quick brown fox jumps over the lazy dog) ... ...
Rasterization 光栅化 把三维物体投影到平面; 实时地,达到30fps
Ray Tracing 光线追踪 慢; 但是有实时光线追踪
Animation/Simulation
与cv的区别:
IDE Integrated Development Environment 集成开发环境
Lecture 02 Linear Algebra
dot product
a ⃗ = ( x 1 y 1 ) , b ⃗ = ( x 2 y 2 ) , 则 a ⃗ ⋅ b ⃗ = ∣ a ⃗ ∣ ∣ b ⃗ ∣ cos θ = x 1 x 2 + y 1 y 2 \vec{a} = \left( \begin{matrix} x_1\\ y_1 \end{matrix} \right) ,
\vec{b} = \left( \begin{matrix} x_2\\ y_2 \end{matrix} \right) , 则
\vec{a} \cdot \vec{b} = \lvert \vec{a} \rvert \lvert \vec{b} \rvert \cos\theta =
x_1x_2 + y_1y_2
a = ( x 1 y 1 ) , b = ( x 2 y 2 ) , 则 a ⋅ b = ∣ a ∣ ∣ b ∣ cos θ = x 1 x 2 + y 1 y 2
⊥ \bot ⊥ 读作 perp ; 含义为 perpendicular 垂直
点乘结果是数量
cross product
a ⃗ = ( x 1 y 1 ) , b ⃗ = ( x 2 y 2 ) , 则 ∣ a ⃗ × b ⃗ ∣ = ∣ a ⃗ ∣ ∣ b ⃗ ∣ sin θ \vec{a} = \left( \begin{matrix} x_1\\ y_1 \end{matrix} \right) ,
\vec{b} = \left( \begin{matrix} x_2\\ y_2 \end{matrix} \right) , 则
\lvert \vec{a} \times \vec{b} \rvert = \lvert \vec{a} \rvert \lvert \vec{b} \rvert \sin\theta
a = ( x 1 y 1 ) , b = ( x 2 y 2 ) , 则 ∣ a × b ∣ = ∣ a ∣ ∣ b ∣ sin θ
笛卡尔坐标系下的叉乘: a ⃗ × b ⃗ = ( 0 − z 1 y 1 z 1 0 − x 1 − y 1 x 1 0 ) ( x 2 y 2 z 2 ) \vec{a} \times \vec{b} =
\left( \begin {matrix}
0 & -z_1 & y_1 \\
z_1 & 0 & -x_1 \\
-y_1 & x_1 & 0
\end {matrix} \right)
\left( \begin{matrix}
x_2 \\ y_2 \\ z_2
\end {matrix}\right) a × b = ⎝ ⎛ 0 z 1 − y 1 − z 1 0 x 1 y 1 − x 1 0 ⎠ ⎞ ⎝ ⎛ x 2 y 2 z 2 ⎠ ⎞
使用右手螺旋方法,a ⃗ × b ⃗ = − b ⃗ × a ⃗ \vec{a} \times \vec{b} = -\vec{b} \times \vec{a} a × b = − b × a
叉乘结果是向量, a ⃗ × a ⃗ = 0 ⃗ \vec{a} \times \vec{a} = \vec{0} a × a = 0 结果也是零向量
叉乘应用
判断点P是否在三角形ABC内部:
A P ⃗ × A B ⃗ , B P ⃗ × B C ⃗ , C P ⃗ × C A ⃗ \vec{AP} \times \vec{AB}, \vec{BP} \times \vec{BC}, \vec{CP} \times \vec{CA} A P × A B , BP × BC , CP × C A
三者正负性一致,则在内部。
matrix
identity matrix 单位矩阵
( A B ) T = B T A T , ( A B ) − 1 = B − 1 A − 1 (AB)^{T} = B^T A^T, (AB)^{-1} = B^{-1} A^{-1} ( A B ) T = B T A T , ( A B ) − 1 = B − 1 A − 1
Lecture 03 Transformation
2D变换
x' 上标' 读作 prime
scale matrix
缩放倍数s_1s_2
( x ′ y ′ ) = ( s 1 0 0 s 2 ) ( x y ) \left(\begin{matrix} x'\\y' \end{matrix}\right) =
\left(\begin{matrix} s_1&0 \\ 0&s_2 \end{matrix}\right)
\left(\begin{matrix} x\\y \end{matrix}\right)
( x ′ y ′ ) = ( s 1 0 0 s 2 ) ( x y )
reflection matrix
沿Y轴对称
( x ′ y ′ ) = ( − 1 0 0 1 ) ( x y ) \left(\begin{matrix} x'\\y' \end{matrix}\right) =
\left(\begin{matrix} -1&0 \\ 0&1 \end{matrix}\right)
\left(\begin{matrix} x\\y \end{matrix}\right)
( x ′ y ′ ) = ( − 1 0 0 1 ) ( x y )
shear matrix
x轴方向a的切变
( x ′ y ′ ) = ( 1 a 0 1 ) ( x y ) \left(\begin{matrix} x'\\y' \end{matrix}\right) =
\left(\begin{matrix} 1&a \\ 0&1 \end{matrix}\right)
\left(\begin{matrix} x\\y \end{matrix}\right)
( x ′ y ′ ) = ( 1 0 a 1 ) ( x y )
rotation matrix
默认旋转:绕origin原点, 逆时针
极坐标推一下
( x ′ y ′ ) = ( cos θ − sin θ sin θ cos θ ) ( x y ) \left(\begin{matrix} x'\\y' \end{matrix}\right) =
\left(\begin{matrix} \cos\theta&-\sin\theta \\ \sin\theta& \cos\theta \end{matrix}\right)
\left(\begin{matrix} x\\y \end{matrix}\right)
( x ′ y ′ ) = ( cos θ sin θ − sin θ cos θ ) ( x y )
以上是线性变换,但平移不属于线性变换 ,因而引入了齐次坐标系。
Homogenous Coordinates
齐次坐标系中,
( x y 1 ) \left(\begin{matrix} x\\y\\1 \end{matrix}\right)
⎝ ⎛ x y 1 ⎠ ⎞ 表示point,
( x y 0 ) \left( \begin {matrix} x\\ y\\ 0 \end {matrix} \right)
⎝ ⎛ x y 0 ⎠ ⎞ 表示vector, 于是得到了以下规律 :
vector + vector = vector
vector + point = point
point - point = vector
point + point = 中点
在齐次坐标系中,仿射变换可以写为:
( x ′ y ′ 1 ) = ( a b t x c d t y 0 0 1 ) ( x y 1 ) \left(\begin{matrix} x'\\y'\\1 \end{matrix}\right) =
\left(\begin{matrix} a& b &t_x\\ c& d &t_y \\ 0& 0 &1 \end{matrix}\right)
\left(\begin{matrix} x\\y\\1 \end{matrix}\right)
⎝ ⎛ x ′ y ′ 1 ⎠ ⎞ = ⎝ ⎛ a c 0 b d 0 t x t y 1 ⎠ ⎞ ⎝ ⎛ x y 1 ⎠ ⎞
组合变换,先线性变换, 后平移 , 写成矩阵:
T ∗ R ∗ ( x y 1 ) T * R *
\left(\begin{matrix} x\\y\\1 \end{matrix}\right)
T ∗ R ∗ ⎝ ⎛ x y 1 ⎠ ⎞
注意左乘顺序, 比如绕着任意点 c 旋转,写成矩阵:
T ( c ) ∗ R ∗ T ( − c ) ∗ ( x y 1 ) T(c) * R * T(-c) *
\left(\begin{matrix} x\\y\\1 \end{matrix}\right)
T ( c ) ∗ R ∗ T ( − c ) ∗ ⎝ ⎛ x y 1 ⎠ ⎞
Lecture 04 Transformation Cont.
旋转矩阵是正交矩阵: R T = R − 1 R^T = R^{-1} R T = R − 1
3D变换
scale
( s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ) \left( \begin {matrix}
s_x & 0 & 0 & 0 \\
0 & s_y & 0 &0 \\
0 & 0 & s_z & 0 \\
0 & 0 & 0& 1
\end {matrix} \right )
⎝ ⎛ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ⎠ ⎞
translationn
( 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ) \left( \begin {matrix}
1 & 0 & 0 & t_x \\
0 & 1 & 0 & t_y \\
0 & 0 & 1 & t_z \\
0 & 0 & 0& 1
\end {matrix} \right )
⎝ ⎛ 1 0 0 0 0 1 0 0 0 0 1 0 t x t y t z 1 ⎠ ⎞
rotation
绕x : ( 1 0 0 0 0 cos θ − sin θ 0 0 sin θ cos θ 0 0 0 0 1 ) \left( \begin {matrix}
1 & 0 & 0 & 0 \\
0 & \cos\theta & -\sin\theta & 0 \\
0 & \sin\theta & \cos\theta & 0 \\
0 & 0 & 0& 1
\end {matrix} \right )
⎝ ⎛ 1 0 0 0 0 cos θ sin θ 0 0 − sin θ cos θ 0 0 0 0 1 ⎠ ⎞
绕y : ( cos θ 0 sin θ t x 0 1 0 t y − sin θ 0 cos θ t z 0 0 0 1 ) \left( \begin {matrix}
\cos\theta & 0 & \sin\theta & t_x \\
0 & 1 & 0 & t_y \\
-\sin\theta & 0 & \cos\theta & t_z \\
0 & 0 & 0& 1
\end {matrix} \right )
⎝ ⎛ cos θ 0 − sin θ 0 0 1 0 0 sin θ 0 cos θ 0 t x t y t z 1 ⎠ ⎞
绕z : ( cos θ − sin θ 0 t x sin θ cos θ 0 t y 0 0 1 t z 0 0 0 1 ) \left( \begin {matrix}
\cos\theta & -\sin\theta & 0 & t_x \\
\sin\theta & \cos\theta & 0 & t_y \\
0 & 0 & 1 & t_z \\
0 & 0 & 0& 1
\end {matrix} \right )
⎝ ⎛ cos θ sin θ 0 0 − sin θ cos θ 0 0 0 0 1 0 t x t y t z 1 ⎠ ⎞
Ridrigues's formula : 用一个旋转轴和旋转角度来刻画旋转
Quaternions 四元数
Viewing Transformation
视图变换
定义Camera:
position & gaze direction & up direction
约定相机位于原点,注视 -z 方向, up at Y
视图变换
将任意一个相机变换到约定位置:
Translate to origin → \rightarrow → Rotate g to -Z → \rightarrow → Rotate t to Y
M v i e w = R v i e w T v i e w M_{view} = R_{view} T_{view} M v i e w = R v i e w T v i e w
T v i e w = ( 1 0 0 − x e 0 1 0 − y e 0 0 1 − z e 0 0 0 1 ) T_{view} =
\left( \begin{matrix}
1 & 0 & 0 & -x_e \\
0 & 1 & 0 & -y_e \\
0 & 0 & 1 & -z_e \\
0 & 0 & 0 & 1
\end {matrix} \right)
T v i e w = ⎝ ⎛ 1 0 0 0 0 1 0 0 0 0 1 0 − x e − y e − z e 1 ⎠ ⎞
R v i e w = ( x g ^ × t ^ y g ^ × t ^ z g ^ × t ^ 0 x t y t z t 0 x − g y − g z − g 0 0 0 0 1 )
R_{view} =
\left( \begin{matrix}
x_{\hat{g} \times \hat{t}} & y_{\hat{g} \times \hat{t}} & z_{\hat{g} \times \hat{t}} & 0 \\
x_t & y_t & z_t & 0 \\
x_{-g} & y_{-g} & z_{-g} & 0 \\
0 & 0 & 0 & 1
\end {matrix} \right)
R v i e w = ⎝ ⎛ x g ^ × t ^ x t x − g 0 y g ^ × t ^ y t y − g 0 z g ^ × t ^ z t z − g 0 0 0 0 1 ⎠ ⎞
g to -Z, t to Y, (g x t) to X
投影变换
正交投影 Orthographic projection
Translate → \rightarrow → Scale
M o r t h o = ( 2 r − l 0 0 0 0 2 t − b 0 0 0 0 2 n − f 0 0 0 0 1 ) ( 1 0 0 − r + l 2 0 1 0 − t + b 2 0 0 1 − n + f 2 0 0 0 1 ) M_{ortho} =
\left( \begin{matrix}
\frac{2}{r-l} & 0 & 0 & 0 \\
0 & \frac{2}{t-b} & 0 & 0 \\
0 & 0 & \frac{2}{n-f} & 0 \\
0 & 0 & 0 & 1
\end {matrix} \right)
\left( \begin{matrix}
1 & 0 & 0 & -\frac{r+l}{2} \\
0 & 1 & 0 & -\frac{t+b}{2} \\
0 & 0 & 1 & -\frac{n+f}{2} \\
0 & 0 & 0 & 1
\end {matrix} \right) M or t h o = ⎝ ⎛ r − l 2 0 0 0 0 t − b 2 0 0 0 0 n − f 2 0 0 0 0 1 ⎠ ⎞ ⎝ ⎛ 1 0 0 0 0 1 0 0 0 0 1 0 − 2 r + l − 2 t + b − 2 n + f 1 ⎠ ⎞
透视投影 Perspective projection
M p e r s p = M o r t h o M p e r s p → o r t h o M_{persp} = M_{ortho} M_{persp \rightarrow ortho} M p ers p = M or t h o M p ers p → or t h o
M p e r s p → o r t h o = ( n 0 0 0 0 n 0 0 0 0 n + f − n f 0 0 1 0 ) M_{persp \rightarrow ortho} =
\left( \begin{matrix}
n & 0 & 0 & 0 \\
0 & n & 0 & 0 \\
0 & 0 & n+f & -nf \\
0 & 0 & 1 & 0
\end {matrix} \right) M p ers p → or t h o = ⎝ ⎛ n 0 0 0 0 n 0 0 0 0 n + f 1 0 0 − n f 0 ⎠ ⎞
Lecture 05 Rasterization
Triangles
screen space: pixel的下标在左下角,采样点取中间
光栅化:raster在德语里就是screen的意思,光栅化就是把3D图形在屏幕上显示出来
叉乘来判断点是否在三角形内部
Antialiasing
Sampling Artifacts : Jaggies 锯齿 Moire 摩尔纹 Wagon wheel effect 车轮效应
antialiasing solution : Pre-filtering, blurring before sampling
图片中的低频信息,高频信息;大多数是低频信息,高频信息意味着边界、意味着剧烈变化
采样Sampling 我的理解为分辨率,对应 屏幕空间中像素的数量;像素数量越多,采样率就越高
反走样Antialiasing 的寻常办法就是 先模糊, 即先过滤掉图像信息中的 高频信息,此时再对低频信息进行采样,就能起到反走样的效果。
MSAA MultiSampling Anti-Aliasing 多重采样抗锯齿
Approximate the effect of the 1-pixel box filter by sampling multiple locations within a pixel and averaging their values. for example, 4x4 supersampling :
FXAA Fast approximate AA 快速近似抗锯齿
对一幅图,通过图像匹配的方法找到有锯齿的边界,然后换成没有锯齿的边界。属于图像后期处理
TAA Temporal AA
DLSS Deep Learning Super Sampling
Z-Buffer
加上一个depth buffer来存深度,其中深度z 越大表示离得越远
Z-Buffer Algorithm:
for (each triangle T)
for (each sample (x,y,z) in T)
if (z < zBuffer[x,y])
zBuffer[x,y] = z;
frameBuffer[x,y] = rgb;
Lecture 06 Shading
Shading
定义:The process of applying a material to an object
着色具有局部性,不会产生阴影
Blinn-phong reflectance model 布林冯着色模型
Diffuse reflection
L d = k d ( I / r 2 ) m a x ( 0 , n ⋅ l ) L_d = k_d (I/r^2) max(0, \bold{n}\cdot\bold{l}) L d = k d ( I / r 2 ) ma x ( 0 , n ⋅ l )
L d L_d L d 是漫反射亮度,k d k_d k d 是漫反射系数,I I I 是点光源强度
接受到的点光源强度是与 距离 r 2 r^2 r 2 成反比
漫反射亮度与光线方向 l ⃗ \vec{l} l 和 法线 n ⃗ \vec{n} n 夹角的余弦成正比
漫反射向四周反射光线,是均匀的,与观察方向无关。
Specular
L s = k s ( I / r 2 ) m a x ( 0 , n ⋅ h ) p L_s = k_s (I/r^2) max(0, \bold{n}\cdot\bold{h})^p L s = k s ( I / r 2 ) ma x ( 0 , n ⋅ h ) p
其中 h \bold{h} h 是半程向量,h = l + v ∣ l + v ∣ \bold{h} = \frac{\bold{l}+\bold{v}}{|\bold{l}+\bold{v}|} h = ∣ l + v ∣ l + v
简化模型没有考虑 吸收率k d k_d k d
指数p 是高光系数,用来限定高光范围, 一般100~200
Ambient
L a = k a I a L_a = k_a I_a L a = k a I a
此处环境光是一种假设
Shading Frequency : Face、Vertex、Pixel
Shading Type: Flat, Gouraud, Phong
当模型三角形面足够多时, Flat Shading、Gouraud Shading的效果不会比Phong Shading 差
逐点着色的时候,顶点法线方向的计算:
N v = ∑ i N i ∣ ∑ i N i ∣ N_v = \frac{\sum_iN_i}{|\sum_iN_i|} N v = ∣ ∑ i N i ∣ ∑ i N i 分简单平均、加权平均
Pipeline
or Real-time Rendering
Texture Mapping
纹理映射
三角形顶点对应到纹理坐标系( u , v ) (u,v) ( u , v ) ,范围都是0~1
重心坐标系 barycentric coordinates
定义:( α , β , γ ) (\alpha,\beta,\gamma) ( α , β , γ ) , 可以用三角形ABC的3个顶点来表示 该平面所有 的点,即满足α + β + γ = 1 \alpha + \beta + \gamma = 1 α + β + γ = 1
作用: 用于三角形插值, 用于着色、纹理映射
求法: 每个系数 = 对应的小三角形面积 / 总面积
双线性插值 bilinear interpolation
定义:线性插值 l e r p ( s , v 0 , v 1 ) = v 0 + s ( v 1 − v 0 ) lerp(s,v_0, v_1) = v_0 + s(v_1 - v_0) l er p ( s , v 0 , v 1 ) = v 0 + s ( v 1 − v 0 ) ; 双线性插值 就是做两层
作用:纹理过小时插值
MipMap
定义: 新增l o g ( 原图分辨率 ) log(原图分辨率) l o g ( 原图分辨率 ) 个纹理图,分辨率依次对半
作用: 用于范围查询, 解决纹理过大时的走样
用法: 像素点对应到纹理图上, 通过计算对应的texel长度,
L = m a x ( ( d u d x ) 2 + ( d v d x ) 2 , ( d u d y ) 2 + ( d v d y ) 2 ) L = max(\sqrt{(\frac{du}{dx})^2 + (\frac{dv}{dx})^2},\sqrt{(\frac{du}{dy})^2 + (\frac{dv}{dy})^2} ) L = ma x ( ( d x d u ) 2 + ( d x d v ) 2 , ( d y d u ) 2 + ( d y d v ) 2 )
对应mipmap层级:D = l o g L D = logL D = l o gL
D不为整数,还要在做层与层的线性插值,即trilinear interpolation 三线性插值
Anisotropic Filtering
各向异性过滤,mipmap是均匀压缩,ripmap有水平竖直不同比例的压缩
像素点映射到纹理图上时,过扁的图形不适合采用正方形,会导致mipmap的overblur问题
ripmap允许对长方形的区域做 快速的范围查询
EWA Filtering
对斜向的长方形,各向异性 依旧没有解决问题;但EWA过滤开销大