前言
明天考图形学了,推一下矩阵,顺便练下 Latex

数学公式警告⚠
模型矩阵
模型矩阵负责将物体进行三个变换:平移,选择和缩放。
此外,我们认为原坐标为 x,变换后则得到 x’ 坐标(接下来的变换都用该形式表示),即:
x → x ′ x \rightarrow x' x→x′
话不多说,开冲!
缩放
缩放矩阵非常简单,各个坐标直接乘以对应的缩放因子即可:
S ( s x , s y , s z ) = [ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] S(s_x,s_y,s_z)= \begin{bmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} S(sx,sy,sz)=⎣⎢⎢⎡sx0000sy0000sz00001⎦⎥⎥⎤
旋转
旋转矩阵则分为绕 x,y,z 轴的旋转。以绕 z 轴的旋转为例,我们有如下的极坐标:
将极坐标展开就是:
x ′ = r c o s ϕ c o s θ − r s i n ϕ s i n θ y ′ = r s i n ϕ c o s θ + r c o s ϕ s i n θ x' = r \ cos\phi \ cos\theta - r \ sin\phi \ sin\theta \\ y' = r \ sin\phi \ cos\theta + r \ cos\phi \ sin\theta x′=r cosϕ cosθ−r sinϕ sinθy′=r sinϕ cosθ+r cosϕ sinθ
将原本 xy 的极坐标:
x = r cos ϕ y = r sin ϕ x = r\cos\phi \\ y = r\sin\phi x=rcosϕy=rsinϕ
带入 x’ 的极坐标展开,那么有
x ′ = x cos θ − y sin θ y ′ = x sin θ + y cos θ x' = x\cos\theta-y\sin\theta \\ y' = x\sin\theta+y\cos\theta \\ x′=xcosθ−ysinθy′=xsinθ+ycosθ
又因为是在 xoy 平面上进行旋转,那么有:
z ′ = z z' = z z′=z
即最终的变换矩阵有:
R z ( θ ) = [ c o s θ − s i n θ 0 0 s i n θ c o s θ 0 0 0 0 1 0 0 0 0 1 ] R_z(\theta)= \begin{bmatrix} cos\theta & -sin\theta & 0 & 0 \\ sin\theta & cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} Rz(θ)=⎣⎢⎢⎡cosθsinθ00−sinθcosθ0000100001⎦⎥⎥⎤
同理,得到绕 x,y 轴的变换矩阵(原理一样的):
R x ( θ ) = [ 1 0 0 0 0 c o s θ − s i n θ 0 0 s i n θ c o s θ 0 0 0 0 1 ] R_x(\theta)= \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & cos\theta & -sin\theta & 0 \\ 0 & sin\theta & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} Rx(θ)=⎣⎢⎢⎡10000cosθsinθ00−sinθcosθ00001⎦⎥⎥⎤
R y ( θ ) = [ c o s θ 0 − s i n θ 0 0 1 0 0 s i n θ 0 c o s θ 0 0 0 0 1 ] R_y(\theta)= \begin{bmatrix} cos\theta & 0 & -sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ sin\theta & 0 & cos\theta & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} Ry(θ)=⎣⎢⎢⎡cosθ0sinθ00100−sinθ0cosθ00001⎦⎥⎥⎤
平移
平移矩阵也简单,即在原坐标的基础上,加上一个常数即可:
T ( t x , t y , t z ) = [ 1 0 0 t x 0 1 0 t y 0 0 1 t z 0 0 0 1 ] T(t_x,t_y,t_z)= \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} T(tx,ty,tz)=⎣⎢⎢⎡100001000010txtytz1⎦⎥⎥⎤
视图矩阵
视图矩阵负责将世界坐标系下的坐标转换到相机坐标系,即标架的转换!
相机坐标系基底
首先来确定相机坐标系的基底,即基向量。我们可以通过三个变量,确定相机坐标系的(在世界坐标表示下)基,他们是:
- up: 一般为竖直向上向量 (0, 1, 0)
- eye:相机的世界坐标下的位置
- at:相机的视点(就是看向的点)的位置,也是世界坐标系下
即:
那么我们首先通过:
n ⃗ = e y e − a t \vec{n} = eye-at n=eye−at
得到指向摄像机视平面法向 n
注:
事实上 n 是反的,因为相机看向 z 轴负方向
这也是为何我们要用 eye 减去 at
然后我们再通过 n 和 up 的叉乘,得到指向相机视平面右侧的向量 u:
u ⃗ = u p ⃗ × n ⃗ \vec{u} = \vec{up} \times \vec{n} u=up×n
即:
最最后我们通过将 u 和 n 进行叉乘,得到指向相机视平面上方的向量 v:
v ⃗ = n ⃗ × u ⃗ \vec{v} = \vec{n} \times \vec{u} v=n×