Games101笔记-Lecture04

3D Transformation

Affine Transformation

( x ′ y ′ z ′ 1 ) = ( a b c t x d e f t y g h i t z 0 0 0 1 ) ⋅ ( x y z 1 ) \begin{pmatrix}x'\\y'\\z'\\1\end{pmatrix}=\begin{pmatrix}a&b&c&t_x\\d&e&f&t_y\\g&h&i&t_z\\0&0&0&1\end{pmatrix}\cdot\begin{pmatrix}x\\y\\z\\1\end{pmatrix} xyz1=adg0beh0cfi0txtytz1xyz1

作用顺序是先进行线性变换(旋转、缩放)再进行平移变换,作用结果与三维矩阵乘三维向量加上一个代表位移的三维向量效果一致。

Scale

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 ) \bold{S}(s_x, s_y, s_z)=\begin{pmatrix}s_x&0&0&0\\0&s_y&0&0\\0&0&s_z&0\\0&0&0&1\end{pmatrix} S(sx,sy,sz)=sx0000sy0000sz00001

Translation

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 ) \bold{T}(t_x, t_y, t_z)=\begin{pmatrix}1&0&0&t_x\\0&1&0&t_y\\0&0&1&t_z\\0&0&0&1\end{pmatrix} T(tx,ty,tz)=100001000010txtytz1

Rotation

绕x,y,z轴旋转
在这里插入图片描述

R x ( α ) = ( 1 0 0 0 0 cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 ) \bold{R}_x(\alpha)=\begin{pmatrix}1&0&0&0\\0&\cos{\alpha}&-\sin{\alpha}&0\\0&\sin{\alpha}&\cos{\alpha}&0\\0&0&0&1\end{pmatrix} Rx(α)=10000cosαsinα00sinαcosα00001

绕y轴旋转的矩阵的 − sin ⁡ α -\sin{\alpha} sinα在左下角而不是右上角,这里貌似是y轴是 z ⃗ × x ⃗ \vec{z}\times\vec{x} z ×x 的出来的,和xyz这个顺序不一样(顺序一样的时候y应该由 x ⃗ × z ⃗ \vec{x}\times\vec{z} x ×z 得到)
R y ( α ) = ( cos ⁡ α 0 sin ⁡ α 0 0 1 0 0 − sin ⁡ α 0 cos ⁡ α 0 0 0 0 1 ) \bold{R}_y(\alpha)=\begin{pmatrix}\cos{\alpha}&0&\sin{\alpha}&0\\0&1&0&0\\-\sin{\alpha}&0&\cos{\alpha}&0\\0&0&0&1\end{pmatrix} Ry(α)=cosα0sinα00100sinα0cosα00001

R z ( α ) = ( cos ⁡ α − sin ⁡ α 0 0 sin ⁡ α cos ⁡ α 0 0 0 0 1 0 0 0 0 1 ) \bold{R}_z(\alpha)=\begin{pmatrix}\cos{\alpha}&-\sin{\alpha}&0&0\\\sin{\alpha}&\cos{\alpha}&0&0\\0&0&1&0\\0&0&0&1\end{pmatrix} Rz(α)=cosαsinα00sinαcosα0000100001

Rodrigues’ Rotation Formula

描述绕轴 n ⃗ \vec{n} n 旋转 α \alpha α(默认轴过原点,如果不过原点则先将其移动到原点,旋转后再移回原位)
R ( n , α ) = cos ⁡ ( α ) I + ( 1 − cos ⁡ ( α ) ) n n T + sin ⁡ ( α ) ( 0 − n z n y n z 0 − n x − n y n x 0 ) ⏟ N \bold{R}(\bold{n}, \alpha)=\cos{(\alpha)}\bold{I}+(1-\cos{(\alpha}))\bold{n}\bold{n}^T+\sin{(\alpha)}\underbrace{\begin{pmatrix}0&-n_z&n_y\\ n_z&0&-n_x\\-n_y&n_x&0\end{pmatrix}}_{\bold{N}} R(n,α)=cos(α)I+(1cos(α))nnT+sin(α)N 0nznynz0nxnynx0

证明(课程附带资料)如下:
在这里插入图片描述
在这里插入图片描述
描述 s ⃗ 绕 任 一 轴 a ⃗ \vec{s}绕任一轴\vec{a} s a 旋转
s ⃗ \vec{s} s a ⃗ \vec{a} a 上的投影 s / / ⃗ = ( a ⃗ ⋅ s ⃗ ) a ⃗ = a ⃗ a ⃗ T s ⃗ \vec{s_{//}}=(\vec{a}\cdot\vec{s})\vec{a}=\vec{a}\vec{a}^T\vec{s} s// =(a s )a =a a Ts ,其中 a ⃗ a ⃗ T \vec{a}\vec{a}^T a a T为张量积。

  • 证明张量积等于投影:
    ( a ⃗ ⋅ s ⃗ ) a ⃗ = ( a x s x + a y s y + a z s z ) ( a x a y a z ) = ( a x 2 a x a y a x a z a x a y a y 2 a y a z a x a z a y a z a z 2 ) ( s x s y s z ) = a a T s ⃗ (\vec{a}\cdot\vec{s})\vec{a}=(a_xs_x+a_ys_y+a_zs_z)\begin{pmatrix}a_x\\a_y\\a_z\end{pmatrix}=\begin{pmatrix}a_{x}^2&a_xa_y&a_xa_z\\a_xa_y&a_{y}^2&a_ya_z\\a_xa_z&a_ya_z&a_{z}^2\end{pmatrix}\begin{pmatrix}s_x\\s_y\\s_z\end{pmatrix}=aa^T\vec{s} (a s )a =(axsx+aysy+azsz)axayaz=ax2axayaxazaxayay2ayazaxazayazaz2sxsysz=aaTs

s ⃗ 分 解 为 s ⊥ ⃗ 和 s / / ⃗ , s ⊥ ⃗ = s ⃗ − s / / ⃗ \vec{s}分解为\vec{s_\perp}和\vec{s_{//}},\vec{s_{\perp}}=\vec{s}-\vec{s_{//}} s s s// ,s =s s//
b ⃗ = n o r m a l i z e ( s ⊥ ⃗ ) = s ⊥ ⃗ ∥ s ⊥ ⃗ ∥ , 显 然 b ⃗ 与 a ⃗ 互 相 垂 直 。 \vec{b}=normalize(\vec{s_{\perp}})=\frac{\vec{s_{\perp}}}{\|\vec{s\perp}\|},显然\vec{b}与\vec{a}互相垂直。 b =normalize(s )=s s b a

设向量 c ⃗ = a ⃗ × b ⃗ = a ⃗ × s ⊥ ⃗ ∥ s ⊥ ⃗ ∥ = a ⃗ × s ⃗ ∥ s ⊥ ∥ ⃗ ( s ⃗ 和 a ⃗ 平 行 的 部 分 叉 乘 结 果 为 0 ) \vec{c}=\vec{a}\times\vec{b}=\frac{\vec{a}\times\vec{s_{\perp}}}{\|\vec{s_{\perp}}\|}=\frac{\vec{a}\times\vec{s}}{\|\vec{s_{\perp}\|}}(\vec{s}和\vec{a}平行的部分叉乘结果为0) c =a ×b =s a ×s =s a ×s s a 0
s ⃗ 绕 a ⃗ 旋 转 旋 转 θ 角 的 结 果 s R o t ⃗ = s ⊥ R o t ⃗ + s / / ⃗ ( 因 为 s / / ⃗ 不 参 与 旋 转 ) \vec{s}绕\vec{a}旋转旋转\theta角的结果\vec{s^{Rot}}=\vec{s_{\perp}^{Rot}}+\vec{s_{//}}(因为\vec{s_{//}}不参与旋转) s a θsRot =sRot +s// (s// )
s ⊥ R o t ⃗ = ∥ s ⊥ ⃗ ∥ cos ⁡ θ b ⃗ + ∥ s ⊥ ⃗ ∥ sin ⁡ θ c ⃗ \vec{s_{\perp}^{Rot}}=\|\vec{s_{\perp}}\|\cos{\theta}\vec{b}+\|\vec{s_{\perp}}\|\sin{\theta}\vec{c} sRot =s cosθb +s sinθc
化简 s R o t ⃗ = s ⊥ R o t ⃗ + s / / ⃗ = cos ⁡ θ s ⊥ ⃗ + sin ⁡ θ a ⃗ × s ⃗ + s / / ⃗ , 将 叉 乘 写 成 矩 阵 形 式 = cos ⁡ θ ( s ⃗ − s / / ⃗ ) + s / / ⃗ + sin ⁡ θ ( 0 − a z a y a z 0 − a x − a y a x 0 ) s ⃗ = cos ⁡ θ s ⃗ + ( 1 − cos ⁡ θ ) s / / ⃗ + sin ⁡ θ ( 0 − a z a y a z 0 − a x − a y a x 0 ) s ⃗ = ( cos ⁡ θ I + ( 1 − cos ⁡ θ ) a a T + sin ⁡ θ ( 0 − a z a y a z 0 − a x − a y a x 0 ) ) s ⃗ \vec{s^{Rot}}=\vec{s_{\perp}^{Rot}}+\vec{s_{//}}\\= \cos{\theta}\vec{s_\perp}+\sin{\theta}\vec{a}\times\vec{s}+\vec{s_{//}},将叉乘写成矩阵形式\\= \cos{\theta}(\vec{s}-\vec{s_{//}})+\vec{s_{//}}+\sin{\theta}\begin{pmatrix}0&-a_z&a_y\\a_z&0&-a_x\\-a_y&a_x&0\end{pmatrix}\vec{s}\\ =\cos{\theta}\vec{s}+(1-\cos{\theta})\vec{s_{//}}+\sin{\theta}\begin{pmatrix}0&-a_z&a_y\\a_z&0&-a_x\\-a_y&a_x&0\end{pmatrix}\vec{s}\\ =(\cos{\theta}\bold{I}+(1-\cos{\theta})aa^T+\sin{\theta}\begin{pmatrix}0&-a_z&a_y\\a_z&0&-a_x\\-a_y&a_x&0\end{pmatrix})\vec{s} sRot =sRot +s// =cosθs +sinθa ×s +s// =cosθ(s s// )+s// +sinθ0azayaz0axayax0s =cosθs +(1cosθ)s// +sinθ0azayaz0axayax0s =(cosθI+(1cosθ)aaT+sinθ0azayaz0axayax0)s
证毕

Viewing Transformation

View/Camera Transformation

将物品渲染到窗口要经过三次变换
model:将物品按照世界坐标摆放
view:将物品从世界坐标转换为以摄像机为原点的坐标
projection:将物品从以摄像机为原点的坐标投影到窗口所在的平面

如何得到View Transformation

先定义摄像机:

  • Postition e ⃗ \vec{e} e
  • Look-at / gaze g ^ \hat{g} g^
  • Up direction t ^ \hat{t} t^(假定与look-at向量垂直)

然后将摄像机移动到原点,并将摄像机的三个轴与原点的三个轴对齐,则View Transformation可以分解成移动和旋转矩阵轮流作用。
M v i e w = R v i e w T v i e w M_{view}=R_{view}T_{view} Mview=RviewTview
在这里插入图片描述

移动到原点的矩阵很容易得到:
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}=\begin{pmatrix} 1&0&0&-x_e\\ 0&1&0&-y_e\\ 0&0&1&-z_e\\ 0&0&0&1\end{pmatrix} Tview=100001000010xeyeze1

但将三个轴对齐的矩阵就不那么直观。我们可以先写出将原点的三个轴旋转到和摄像机三个轴对齐的矩阵,然后再求逆矩阵获得。
将原点三个轴转到摄像机三个轴的矩阵很容易得到,即将三基向量变成摄像机的三个轴的基向量即可:
R v i e w − 1 = ( 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 ) R^{-1}_{view}=\begin{pmatrix} x_{\hat{g}\times\hat{t}} & x_t & x_{-g} & 0\\ y_{\hat{g}\times\hat{t}} & y_t & y_{-g} & 0\\ z_{\hat{g}\times\hat{t}} & z_t & z_{-g} & 0\\ 0 & 0 & 0 & 1\end{pmatrix} Rview1=xg^×t^yg^×t^zg^×t^0xtytzt0xgygzg00001

又因为旋转矩阵为正交矩阵,所以逆矩阵为转置矩阵:
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}=\begin{pmatrix} 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{pmatrix} Rview=xg^×t^xtxg0yg^×t^ytyg0zg^×t^ztzg00001

对世界坐标中的物体施展View Transformation,则可得到其在以相机为原点的坐标系中的坐标。

Project Transformation

投影矩阵分两种:

  • Orthographic project 正交投影
  • Perspective project 透视投影
    本质表现的区别就是透视投影会有近大远小的效果。
    在这里插入图片描述
Orthographic project

在空间中定义一个立方体( [l, r] x [b, t] x [f, n] ,这里f要比n小),将立方体转换为canonical (正则、规范、标准)” cube( [-1, 1]3)。 这个过程中物体会被拉伸
在这里插入图片描述
正交变换
M o r t h o = S o r t h o T 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}=S_{ortho}T_{ortho}=\begin{pmatrix} \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{pmatrix} \begin{pmatrix} 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{pmatrix} Mortho=SorthoTortho=rl20000tb20000nf2000011000010000102r+l2t+b2n+f1

Perspective project

可以先将远平面的物体压缩到一个和近平面等大的平面上(这个变换定义为 M p e r p − > o r t h o M_{perp->ortho} Mperp>ortho),再实施正交投影。
在这里插入图片描述
从侧方观察视锥,可得相似三角形关系:
在这里插入图片描述
同理可得 x ′ = n z x x^{'}=\frac{n}{z}x x=znx
在齐次坐标中:
( x ′ y ′ z ′ 1 )    ⟹    ( n x / z n t / z u n k o w n 1 ) = = ( n x n y s t i l l   u n k o w n z ) \begin{pmatrix}x^{'}\\y^{'}\\z^{'}\\1\end{pmatrix}\implies\begin{pmatrix}nx/z\\nt/z\\unkown\\1\end{pmatrix}==\begin{pmatrix}nx\\ny\\still\ unkown\\z\end{pmatrix} xyz1nx/znt/zunkown1==nxnystill unkownz
可得
M p e r p − > o r t h o ( x y z 1 ) = ( n x n y s t i l l   u n k o w n z ) M_{perp->ortho}\begin{pmatrix}x\\y\\z\\1\end{pmatrix}=\begin{pmatrix}nx\\ny\\still\ unkown\\z\end{pmatrix} Mperp>orthoxyz1=nxnystill unkownz

由此可得出矩阵的部分数值
M p e r p − > o r t h o = ( n 0 0 0 0 n 0 0 ? ? ? ? 0 0 1 0 ) M_{perp->ortho}=\begin{pmatrix} n&0&0&0\\ 0&n&0&0\\ ?&?&?&?\\ 0&0&1&0\end{pmatrix} Mperp>ortho=n0?00n?000?1000

然后考察两个关系:

  • 任何在近平面的点变换前后不变
  • 在远平面的点的z值在变换前后不变

写出两个关系可得:
近平面上(z=n):
M p e r p − > o r h t o ( x y n 1 ) = ( x y n 1 ) = = ( n x n y n 2 n ) M_{perp->orhto}\begin{pmatrix}x\\y\\n\\1\end{pmatrix}=\begin{pmatrix}x\\y\\n\\1\end{pmatrix}==\begin{pmatrix}nx\\ny\\n_2\\n\end{pmatrix} Mperp>orhtoxyn1=xyn1==nxnyn2n

由于z分量和xy无关,设:
M p e r p − > o r t h o = ( n 0 0 0 0 n 0 0 0 0 A B 0 0 1 0 ) M_{perp->ortho}=\begin{pmatrix} n&0&0&0\\ 0&n&0&0\\ 0&0&A&B\\ 0&0&1&0\end{pmatrix} Mperp>ortho=n0000n0000A100B0
其中 A n + B = n 2 An+B=n^2 An+B=n2

远平面(n=f),取点(0, 0, f, 1):
M p e r p − > o r h t o ( 0 0 f 1 ) = ( 0 0 f 1 ) = = ( 0 0 f 2 f ) M_{perp->orhto}\begin{pmatrix}0\\0\\f\\1\end{pmatrix}=\begin{pmatrix}0\\0\\f\\1\end{pmatrix}==\begin{pmatrix}0\\0\\f^2\\f\end{pmatrix} Mperp>orhto00f1=00f1==00f2f
得 到 A f + B = f 2 得到Af+B=f^2 Af+B=f2
结合两式解的
A = n + f B = − n f A=n+f \newline B=-nf A=n+fB=nf

至此,已经得到了 M p e r p − > o r t h o M_{perp->ortho} Mperp>ortho
而透视矩阵为
M p e r p = M o r h t o M p e r p − > o r h t o M_{perp}=M_{orhto}M_{perp->orhto} Mperp=MorhtoMperp>orhto

图片来源均为课程PPT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值