OpenGL绘图基本矩阵运算讲解

时间:2025-03-30 08:10:29

OpenGL绘图基本矩阵运算讲解

  • 一、平移矩阵
  • 二、缩放矩阵
  • 三、旋转矩阵
  • 四、投影矩阵
  • 五、剪切矩阵
  • 六、矩阵分解
  • 七、视图矩阵

一、平移矩阵

坐标平移 ( d x , d y , d z ) (dx,dy,dz) (dxdydz)
[ 1 0 0 d x 0 1 0 d y 0 0 1 d z 0 0 0 1 ] [100dx010dy001dz0001]

100001000010dxdydz1
例:
[ 1 0 0 d x 0 1 0 d y 0 0 1 d z 0 0 0 1 ] ⋅ [ x y z 1 ] = [ ( 1 ⋅ x ) + ( 0 ⋅ y ) + ( 0 ⋅ z ) + ( d x ⋅ 1 ) ( 0 ⋅ x ) + ( 1 ⋅ y ) + ( 0 ⋅ z ) + ( d y ⋅ 1 ) ( 0 ⋅ x ) + ( 0 ⋅ y ) + ( 1 ⋅ z ) + ( d z ⋅ 1 ) ( 0 ⋅ x ) + ( 0 ⋅ y ) + ( 0 ⋅ z ) + ( 1 ⋅ 1 ) ] = [ x + d x y + d y z + d z 1 ] [100dx010dy001dz0001]
\cdot [xyz1]
= [(1x)+(0y)+(0z)+(dx1)(0x)+(1y)+(0z)+(dy1)(0x)+(0y)+(1z)+(dz1)(0x)+(0y)+(0z)+(11)]
= [x+dxy+dyz+dz1]
100001000010dxdydz1xyz1=(1x)+(0y)+(0z)+(dx1)(0x)+(1y)+(0z)+(dy1)(0x)+(0y)+(1z)+(dz1)(0x)+(0y)+(0z)+(11)=x+dxy+dyz+dz1

二、缩放矩阵

缩放 ( s x , s y , s z ) (s_x,s_y,s_z) (sx,sy,sz)
[ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] [sx0000sy0000sz00001]

sx0000sy0000sz00001
例:
[ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] ⋅ [ x y z 1 ] = [ ( s x ⋅ x ) + ( 0 ⋅ y ) + ( 0 ⋅ z ) + ( 0 ⋅ 1 ) ( 0 ⋅ x ) + ( s y ⋅ y ) + ( 0 ⋅ z ) + ( 0 ⋅ 1 ) ( 0 ⋅ x ) + ( 0 ⋅ y ) + ( s z ⋅ z ) + ( 0 ⋅ 1 ) ( 0 ⋅ x ) + ( 0 ⋅ y ) + ( 0 ⋅ z ) + ( 1 ⋅ 1 ) ] = [ x ⋅ s x y ⋅ s y z ⋅ s z 1 ] [sx0000sy0000sz00001]
\cdot [xyz1]
= [(sxx)+(0y)+(0z)+(01)(0x)+(syy)+(0z)+(01)(0x)+(0y)+(szz)+(01)(0x)+(0y)+(0z)+(11)]
= [xsxysyzsz1]
sx0000sy0000sz00001xyz1=(sxx)+(0y)+(0z)+(01)(0x)+(syy)+(0z)+(01)(0x)+(0y)+(szz)+(01)(0x)+(0y)+(0z)+(11)=xsxysyzsz1

三、旋转矩阵

n ⃗ = ( n x , n y , n z ) \vec{n} = (n_x,n_y,n_z) n =(nx,ny,nz) 旋转 θ θ θ
[ n x 2 ( 1 − c o s θ ) + c o s θ n x n y ( 1 − c o s θ ) + n z s i n θ n z n x ( 1 − c o s θ ) − n y s i n θ 0 n x n y ( 1 − c o s θ ) − n z s i n θ n y 2 ( 1 − c o s θ ) + c o s θ n y n z ( 1 − c o s θ ) + n x s i n θ 0 n z n x ( 1 − c o s θ ) + n y s i n θ n y n z ( 1 − c o s θ ) − n x s i n θ n z 2 ( 1 − c o s θ ) + c o s θ 0 0 0 0 1 ] [n2x(1cosθ)+cosθnxny(1cosθ)+nzsinθnznx(1cosθ)nysinθ0nxny(1cosθ)nzsinθn2y(1cosθ)+cosθnynz(1cosθ)+nxsinθ0nznx(1cosθ)+nysinθnynz(1cosθ)nxsinθn2z(1cosθ)+cosθ00001]

nx2(1cosθ)+cosθnxny(1cosθ)nzsinθnznx(1cosθ)+nysinθ0nxny(1cosθ)+nzsinθny2(1cosθ)+cosθnynz(1cosθ)nxsinθ0nznx(1cosθ)nysinθnynz(1cosθ)+nxsinθnz2(1cosθ)+cosθ00001

四、投影矩阵

n n n近裁剪面
f f f 远裁剪面
l , r l,r lr近裁剪面左右边界
t , b t,b tb近裁剪面上下边界

透视矩阵:
其他参数转换方法:
r = t a n ( f o v y ) ∗ a s p e c t ∗ n r = tan(fovy)* aspect * n r=tan(fovy)aspectn
l = − r l = -r l=r
t = t a n ( f o v y ) ∗ n t = tan(fovy)* n t=tan(fovy)n
b = − t b = -t b=t

矩阵内容:
[ 2 n r − l 0 r + l r − l 0 0 2 n t − b t + b t − b 0 0 0 − ( f + n ) f − n − 2 f n f − n 0 0 − 1 0 ] [2nrl0r+lrl002ntbt+btb000(f+n)fn2fnfn0010]

rl2n0000tb2n00rlr+ltbt+bfn(f+n)100fn2fn0

正交矩阵:

[ 2 n r − l 0 0 − r + l r − l 0 2 t − b 0 − t + b t − b 0 0 − 2 f − n − f + n f − n 0 0 0 1 ] [2nrl00r+lrl02tb0t+btb002fnf+nfn0001]

rl2n0000tb20000fn20rlr+ltbt+bfnf+n1

正交矩阵2D
f = 1 f=1 f=1
n = − 1 n=-1 n=1
[ 2 n r − l 0 0 − r + l r − l 0 2 t − b 0 − t + b t − b 0 0 − 1 0 0 0 0 1 ] [2nrl00r+lrl02tb0t+btb00100001]

rl2n0000tb2000010rlr+ltbt+b01

五、剪切矩阵

h n m h_{nm} hnm 由n轴向m轴剪切值
剪切矩阵:
[ 1 h y x h z x 0 h x y 1 h z y 0 h x z h y z 1 0 0 0 0 1 ] [1hyxhzx0hxy1hzy0hxzhyz100001]

1hxyhxz0hyx1hyz0hzxhzy100001

六、矩阵分解

Model矩阵 M = T ∗ R ∗ S M = T * R * S M=TRS
其中 T为平移矩阵,R为旋转矩阵,S为缩放矩阵
记 R ′ [ R 11 R 12 R 13 0 R 21 R 22 R 23 0 R 31 R 32 R 33 0 0 0 0 1 ] = [ n x 2 ( 1 − c o s θ ) + c o s θ n x n y ( 1 − c o s θ ) + n z s i n θ n z n x ( 1 − c o s θ ) − n y s i n θ 0 n x n y ( 1 − c o s θ ) − n z s i n θ n y 2 ( 1 − c o s θ ) + c o s θ n y n z ( 1 − c o s θ ) + n x s i n θ 0 n z n x ( 1 − c o s θ ) + n y s i n θ n y n z ( 1 − c o s θ ) − n x s i n θ n z 2 ( 1 − c o s θ ) + c o s θ 0 0 0 0 1 ] 记R' [R11R12R130R21R22R230R31R32R3300001]

= [n2x(1cosθ)+cosθnxny(1cosθ)+nzsinθnznx(1cosθ)nysinθ0nxny(1cosθ)nzsinθn2y(1cosθ)+cosθnynz(1cosθ)+nxsinθ0nznx(1cosθ)+nysinθnynz(1cosθ)nxsinθn2z(1cosθ)+cosθ00001]
RR11R21R310R12R22R320R13R23R3300001=nx2(1cosθ)+cosθnxny(1cosθ)nzsinθnznx(1cosθ)+nysinθ0nxny(1cosθ)+nzsinθny2(1cosθ)+cosθnynz(1cosθ)nxsinθ0nznx(1cosθ)nysinθnynz(1cosθ)+nxsinθnz2(1cosθ)+cosθ00001
则 [ 1 0 0 d x 0 1 0 d y 0 0 1 d z 0 0 0 1 ] ⋅ [ R 11 R 12 R 13 0 R 21 R 22 R 23 0 R 31 R 32 R 33 0 0 0 0 1 ] ⋅ [ s x 0 0 0 0 s y 0 0 0 0 s z 0 0 0 0 1 ] = 则 [100dx010dy001dz0001]
\cdot [R11R12R130R21R22R230R31R32R3300001]
\cdot [sx0000sy0000sz00001]
=
100001000010dxdydz1R11R21R310R12R22R320R13R23R3300001sx0000sy0000sz00001=

[ s x ∗ R 11 s x ∗ R 12 s x ∗ R 13 d x s y ∗ R 21 s y ∗ R 22 s y ∗ R 23 d y s z ∗ R 31 s z ∗ R 32 s z ∗ R 33 d z 0 0 0 1 ] [sxR11sxR12sxR13dxsyR21syR22syR23dyszR31szR32szR33dz0001]
sxR11syR21szR310sxR12syR22szR320sxR13syR23szR330dxdydz1

七、视图矩阵

N ⃗ = E y e ⃗ − A t ⃗ \vec{N} = \vec{Eye} - \vec{At} N =Eye At
N ⃗ = N ⃗ ∣ N ⃗ ∣ \vec{N} = \frac{\vec{N}}{|\vec{N}|} N =N N

U ⃗ = U p ⃗ × N ⃗ \vec{U} = \vec{Up} \times\vec{N} U =Up ×N
U ⃗ = U ⃗ ∣ U ⃗ ∣ \vec{U} = \frac{\vec{U}}{|\vec{U}|} U =U U

V ⃗ = N ⃗ × U ⃗ \vec{V} = \vec{N}\times \vec{U} V =N ×U
V ⃗ = V ⃗ ∣ V ⃗ ∣ \vec{V} = \frac{\vec{V}}{|\vec{V}|} V =V V
视图矩阵
[ U x U y U z − N ⃗ × E y e ⃗ V x V y V z − V ⃗ × E y e ⃗ N x N y N z − U ⃗ × E y e ⃗ 0 0 0 1 ] [UxUyUzN×EyeVxVyVzV×EyeNxNyNzU×Eye0001]

UxVxNx0UyVyNy0UzVzNz0N ×Eye V ×Eye U ×Eye 1