3D 空間中的變換矩陣詳解
在 3D 計算機圖形學中,所有幾何變換都可以通過 4×4 齊次變換矩陣 來表示。以下詳細介紹各種變換矩陣及其原理。
核心變換矩陣
1. 單位矩陣(不變變換)
I=[1000010000100001] I = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} I=?1000?0100?0010?0001??
2. 平移矩陣(Translation)
將點 p=[x,y,z,1]T\mathbf{p} = [x, y, z, 1]^Tp=[x,y,z,1]T 沿向量 t=[tx,ty,tz]\mathbf{t} = [t_x, t_y, t_z]t=[tx?,ty?,tz?] 移動:
T=[100tx010ty001tz0001] T = \begin{bmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{bmatrix} T=?1000?0100?0010?tx?ty?tz?1??
變換后坐標:
p′=T?p=[x+tx,y+ty,z+tz,1]T p' = T \cdot p = [x+t_x, y+t_y, z+t_z, 1]^T p′=T?p=[x+tx?,y+ty?,z+tz?,1]T
T逆矩陣:將 (tx,ty,tz)(t_x,t_y,t_z)(tx?,ty?,tz?) 取負
3. 縮放矩陣(Scaling)
沿坐標軸縮放,縮放因子 (sx,sy,sz)(s_x, s_y, s_z)(sx?,sy?,sz?):
S=[sx0000sy0000sz00001] S = \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?000?0sy?00?00sz?0?0001??
特殊類型:
- 均勻縮放:sx=sy=szs_x = s_y = s_zsx?=sy?=sz?
- 鏡像:某個分量為負(如 sx=?1s_x = -1sx?=?1)
逆矩陣:使用 (1/sx,1/sy,1/sz)(1/s_x,1/s_y,1/s_z)(1/sx?,1/sy?,1/sz?)(假設因子不為0)。
4. 旋轉矩陣(Rotation)
a) 繞坐標軸旋轉
旋轉軸 | 矩陣 |
---|---|
X軸 | Rx(θ)=[10000cos?θ?sin?θ00sin?θcos?θ00001]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?(θ)=?1000?0cosθsinθ0?0?sinθcosθ0?0001?? |
Y軸 | Ry(θ)=[cos?θ0sin?θ00100?sin?θ0cos?θ00001]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θ0?sinθ0?0100?sinθ0cosθ0?0001?? |
Z軸 | Rz(θ)=[cos?θ?sin?θ00sin?θcos?θ0000100001]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θ00?0010?0001?? |
旋轉方向:右手坐標系中,逆時針為正方向(從旋轉軸正向觀察)
b) 繞任意軸旋轉(Rodrigues公式)
繞單位向量 u=[ux,uy,uz]\mathbf{u} = [u_x, u_y, u_z]u=[ux?,uy?,uz?] 旋轉角度 θ\thetaθ:
R(u,θ)=[cos?θ+ux2(1?cos?θ)uxuy(1?cos?θ)?uzsin?θuxuz(1?cos?θ)+uysin?θ0uyux(1?cos?θ)+uzsin?θcos?θ+uy2(1?cos?θ)uyuz(1?cos?θ)?uxsin?θ0uzux(1?cos?θ)?uysin?θuzuy(1?cos?θ)+uxsin?θcos?θ+uz2(1?cos?θ)00001] R(\mathbf{u}, \theta) = \begin{bmatrix} \cos\theta + u_x^2(1-\cos\theta) & u_xu_y(1-\cos\theta) - u_z\sin\theta & u_xu_z(1-\cos\theta) + u_y\sin\theta & 0 \\ u_yu_x(1-\cos\theta) + u_z\sin\theta & \cos\theta + u_y^2(1-\cos\theta) & u_yu_z(1-\cos\theta) - u_x\sin\theta & 0 \\ u_zu_x(1-\cos\theta) - u_y\sin\theta & u_zu_y(1-\cos\theta) + u_x\sin\theta & \cos\theta + u_z^2(1-\cos\theta) & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} R(u,θ)=?cosθ+ux2?(1?cosθ)uy?ux?(1?cosθ)+uz?sinθuz?ux?(1?cosθ)?uy?sinθ0?ux?uy?(1?cosθ)?uz?sinθcosθ+uy2?(1?cosθ)uz?uy?(1?cosθ)+ux?sinθ0?ux?uz?(1?cosθ)+uy?sinθuy?uz?(1?cosθ)?ux?sinθcosθ+uz2?(1?cosθ)0?0001??
5. 組合變換矩陣
變換矩陣可以通過乘法組合。注意矩陣乘法的順序非常重要(不滿足交換律)。通常,變換按以下順序應用:縮放 -> 旋轉 -> 平移,即:
M=T?R?S M = T \cdot R \cdot S M=T?R?S
但是,如果以不同的順序應用,結果會不同。
例如,先繞Y軸旋轉90度,再沿X軸平移3個單位,與先平移后旋轉的結果不同:
??先旋轉后平移??:點會先旋轉,然后沿世界坐標系的X軸平移。
??先平移后旋轉??:點先在世界坐標系中沿X軸平移,然后旋轉(此時點的位置已經改變,旋轉后位置不同)。
注意順序:
- 縮放 (S)
- 旋轉 ?
- 平移 (T)
這是因為矩陣乘法是 右乘結合:p′=T?(R?(S?p)) \mathbf{p}' = T \cdot (R \cdot (S \cdot \mathbf{p})) p′=T?(R?(S?p))
特殊變換類型
1. 剪切矩陣(Shear)
H=[1hxyhxz0hyx1hyz0hzxhzy100001] H = \begin{bmatrix} 1 & h_{xy} & h_{xz} & 0 \\ h_{yx} & 1 & h_{yz} & 0 \\ h_{zx} & h_{zy} & 1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} H=?1hyx?hzx?0?hxy?1hzy?0?hxz?hyz?10?0001??
2. 正交投影矩陣
向XY平面投影:
Portho=[1000010000000001]
P_{\text{ortho}} = \begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & 0 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
Portho?=?1000?0100?0000?0001??
變換矩陣在編程中的實現
使用GLM(OpenGL Mathematics)庫示例(兩種寫法)
- GLM函數疊加運算
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>// 初始化矩陣
glm::mat4 model = glm::mat4(1.0f); // 單位矩陣// 平移:沿X軸移動1.5單位
model = glm::translate(model, glm::vec3(1.5f, 0.0f, 0.0f));// 旋轉:繞Z軸旋轉90度
model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));// 縮放:Y軸放大2倍
model = glm::scale(model, glm::vec3(1.0f, 2.0f, 1.0f));
變換順序很重要:矩陣乘法從右到左應用變換,所以上面代碼的順序實際上是先縮放,再旋轉,最后平移。
在組合變換時,考慮使用逆序:先進行的變換在矩陣乘法中放在右邊(后乘),后進行的變換放在左邊(先乘)
2. 計算好各個變換矩陣后再相乘
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>int main() {// 單位矩陣glm::mat4 identity = glm::mat4(1.0f);// 平移變換:沿X軸移動2個單位glm::mat4 translation = glm::translate(glm::mat4(1.0), glm::vec3(2.0f, 0.0f, 0.0f));// 縮放變換:Y軸擴大3倍glm::mat4 scaling = glm::scale(glm::mat4(1.0), glm::vec3(1.0f, 3.0f, 1.0f));// 旋轉變換:繞Z軸旋轉45度glm::mat4 rotation = glm::rotate(glm::mat4(1.0), glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f));// 繞任意軸旋轉:繞(1,1,0)旋轉30度glm::vec3 axis = glm::normalize(glm::vec3(1.0f, 1.0f, 0.0f));glm::mat4 customRotation = glm::rotate(glm::mat4(1.0), glm::radians(30.0f), axis);// 組合變換:先縮放,再旋轉,最后平移glm::mat4 composite = translation * rotation * scaling;// 應用變換到點glm::vec4 point(1.0f, 1.0f, 0.0f, 1.0f);glm::vec4 transformedPoint = composite * point;return 0;
}
手動實現矩陣類
class Matrix4f {
public:float m[4][4];Matrix4f() { /* 初始化為單位矩陣 */ }static Matrix4f translate(float tx, float ty, float tz) {Matrix4f m;m.m[0][3] = tx;m.m[1][3] = ty;m.m[2][3] = tz;return m;}static Matrix4f scale(float sx, float sy, float sz) {Matrix4f m;m.m[0][0] = sx;m.m[1][1] = sy;m.m[2][2] = sz;return m;}static Matrix4f rotateX(float angle) {float rad = angle * M_PI/180.0;float c = cos(rad);float s = sin(rad);Matrix4f m;m.m[1][1] = c; m.m[1][2] = -s;m.m[2][1] = s; m.m[2][2] = c;return m;}Matrix4f operator*(const Matrix4f& right) const {// 矩陣乘法實現}Vec4f operator*(const Vec4f& v) const {// 矩陣向量乘法}
};
變換矩陣的性質與應用
-
齊次坐標
- 3D點:[x, y, z, 1]?
- 3D向量:[x, y, z, 0]?(防止被平移)
-
逆變換
- 平移逆矩陣:$ T^{-1}(t_x, t_y, t_z) = T(-t_x, -t_y, -t_z) $
- 縮放逆矩陣:$ S^{-1}(s_x, s_y, s_z) = S(1/s_x, 1/s_y, 1/s_z) $
- 旋轉逆矩陣:$ R^{-1} = R^T $(正交矩陣)
-
組合變換求逆
組合變換的逆矩陣可以通過矩陣求逆得到。如果變換矩陣是正交的(純旋轉,不含縮放或錯切),則轉置就是逆矩陣。如果包含平移和縮放,則需要計算逆矩陣:(T?R?S)?1=S?1?R?1?T?1 (T \cdot R \cdot S)^{-1} = S^{-1} \cdot R^{-1} \cdot T^{-1} (T?R?S)?1=S?1?R?1?T?1
其中:
T ?1 :將平移向量取負。
R ?1:旋轉矩陣的轉置(因為旋轉矩陣正交)。
S ?1:縮放因子的倒數(假設非零)。 -
在變換鏈中的位置
局部坐標 → 模型矩陣 → 世界坐標↓ 世界坐標 → 視圖矩陣 → 觀察坐標↓ 觀察坐標 → 投影矩陣 → 裁剪坐標
可視化理解變換順序
考慮一個點 P(0,1,0) 的變換過程:
正確的矩陣乘法順序:
Pfinal=[1003010000100001]?平移×[0?100100000100001]?旋轉×[1000020000100001]?縮放×[0101] P_{\text{final}} = \underset{\text{平移}}{\underbrace{
\begin{bmatrix}
1 & 0 & 0 & 3 \\
0 & 1 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
}}
\times
\underset{\text{旋轉}}{\underbrace{
\begin{bmatrix}
0 & -1 & 0 & 0 \\
1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
}}
\times
\underset{\text{縮放}}{\underbrace{
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 2 & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
}}
\times
\begin{bmatrix} 0 \\ 1 \\ 0 \\ 1 \end{bmatrix} Pfinal?=平移?1000?0100?0010?3001????×旋轉?0100??1000?0010?0001????×縮放?1000?0200?0010?0001????×?0101??
這些矩陣構成了 3D 圖形學的基礎,在 OpenGL、DirectX、Vulkan 等圖形 API 以及 Unity、Unreal 等游戲引擎中廣泛使用。掌握它們對于理解 3D 渲染管線至關重要。