跳到主要内容

矩阵

快速计算向量位置,下文矩阵全是列主序四阶矩阵

  • 定义向量vec=(x,y,z,w)\overrightarrow{vec} = (x,y,z,w)
  • 定义矩阵 mat4=[m11m12m13m14m21m22m23m24m31m32m33m34m41m42m43m44] mat4 = \begin{bmatrix} m_{11} & m_{12} & m_{13} & m_{14} \\ m_{21} & m_{22} & m_{23} & m_{24} \\ m_{31} & m_{32} & m_{33} & m_{34} \\ m_{41} & m_{42} & m_{43} & m_{44} \\ \end{bmatrix}

四阶矩阵

向量与矩阵相乘

(x=xm11+ym21+zm31+wm41,y=xm12+ym22+zm32+wm42,z=xm13+ym23+zm33+wm43,w=xm14+ym24+zm34+wm44,)=mat4×vec\begin{pmatrix} x = x * m_{11} + y * m_{21} + z * m_{31} + w * m_{41} ,\\ y = x * m_{12} + y * m_{22} + z * m_{32} + w * m_{42} ,\\ z = x * m_{13} + y * m_{23} + z * m_{33} + w * m_{43} ,\\ w = x * m_{14} + y * m_{24} + z * m_{34} + w * m_{44} ,\\ \end{pmatrix} = mat4 \times \overrightarrow{vec} (x=xm11+ym12+zm13+wm14,y=xm22+ym22+zm23+wm24,z=xm33+ym32+zm33+wm34,w=xm44+ym42+zm43+wm44,)=vec×mat4\begin{pmatrix} x = x * m_{11} + y * m_{12} + z * m_{13} + w * m_{14} ,\\ y = x * m_{22} + y * m_{22} + z * m_{23} + w * m_{24} ,\\ z = x * m_{33} + y * m_{32} + z * m_{33} + w * m_{34} ,\\ w = x * m_{44} + y * m_{42} + z * m_{43} + w * m_{44} ,\\ \end{pmatrix} = \overrightarrow{vec} \times mat4

矩阵与矩阵相乘

[ma11ma12ma13ma14ma21ma22ma23ma24ma31ma32ma33ma34ma41ma42ma43ma44]×[mb11mb12mb13mb14mb21mb22mb23mb24mb31mb32mb33mb34mb41mb42mb43mb44]\begin{bmatrix} m_{a11} & m_{a12} & m_{a13} & m_{a14} \\ m_{a21} & m_{a22} & m_{a23} & m_{a24} \\ m_{a31} & m_{a32} & m_{a33} & m_{a34} \\ m_{a41} & m_{a42} & m_{a43} & m_{a44} \\ \end{bmatrix} \times \begin{bmatrix} m_{b11} & m_{b12} & m_{b13} & m_{b14} \\ m_{b21} & m_{b22} & m_{b23} & m_{b24} \\ m_{b31} & m_{b32} & m_{b33} & m_{b34} \\ m_{b41} & m_{b42} & m_{b43} & m_{b44} \\ \end{bmatrix}
  • mc11=ma11×mb11+ma21×mb12+ma31×mb13+ma41×mb14m_{c11} = m_{a11} \times m_{b11} + m_{a21} \times m_{b12} + m_{a31} \times m_{b13} + m_{a41} \times m_{b14}
  • mc12=ma11×mb21+ma21×mb22+ma31×mb23+ma41×mb24m_{c12} = m_{a11} \times m_{b21} + m_{a21} \times m_{b22} + m_{a31} \times m_{b23} + m_{a41} \times m_{b24}
  • mc13=ma11×mb31+ma21×mb32+ma31×mb33+ma41×mb34m_{c13} = m_{a11} \times m_{b31} + m_{a21} \times m_{b32} + m_{a31} \times m_{b33} + m_{a41} \times m_{b34}
  • mc14=ma12×mb41+ma22×mb42+ma32×mb43+ma42×mb44m_{c14} = m_{a12} \times m_{b41} + m_{a22} \times m_{b42} + m_{a32} \times m_{b43} + m_{a42} \times m_{b44}
  • mc21=ma12×mb11+ma22×mb12+ma32×mb13+ma42×mb14m_{c21} = m_{a12} \times m_{b11} + m_{a22} \times m_{b12} + m_{a32} \times m_{b13} + m_{a42} \times m_{b14}
  • mc22=ma12×mb21+ma22×mb22+ma32×mb23+ma42×mb24m_{c22} = m_{a12} \times m_{b21} + m_{a22} \times m_{b22} + m_{a32} \times m_{b23} + m_{a42} \times m_{b24}
  • mc23=ma12×mb31+ma22×mb32+ma32×mb33+ma42×mb34m_{c23} = m_{a12} \times m_{b31} + m_{a22} \times m_{b32} + m_{a32} \times m_{b33} + m_{a42} \times m_{b34}
  • mc24=ma12×mb41+ma22×mb42+ma32×mb43+ma42×mb44m_{c24} = m_{a12} \times m_{b41} + m_{a22} \times m_{b42} + m_{a32} \times m_{b43} + m_{a42} \times m_{b44}
  • mc31=ma13×mb11+ma23×mb12+ma33×mb13+ma43×mb14m_{c31} = m_{a13} \times m_{b11} + m_{a23} \times m_{b12} + m_{a33} \times m_{b13} + m_{a43} \times m_{b14}
  • mc32=ma13×mb21+ma23×mb22+ma33×mb23+ma43×mb24m_{c32} = m_{a13} \times m_{b21} + m_{a23} \times m_{b22} + m_{a33} \times m_{b23} + m_{a43} \times m_{b24}
  • mc33=ma13×mb31+ma23×mb32+ma33×mb33+ma43×mb34m_{c33} = m_{a13} \times m_{b31} + m_{a23} \times m_{b32} + m_{a33} \times m_{b33} + m_{a43} \times m_{b34}
  • mc34=ma13×mb41+ma23×mb42+ma33×mb43+ma43×mb44m_{c34} = m_{a13} \times m_{b41} + m_{a23} \times m_{b42} + m_{a33} \times m_{b43} + m_{a43} \times m_{b44}
  • mc41=ma14×mb11+ma24×mb12+ma34×mb13+ma44×mb14m_{c41} = m_{a14} \times m_{b11} + m_{a24} \times m_{b12} + m_{a34} \times m_{b13} + m_{a44} \times m_{b14}
  • mc42=ma14×mb21+ma24×mb22+ma34×mb23+ma44×mb24m_{c42} = m_{a14} \times m_{b21} + m_{a24} \times m_{b22} + m_{a34} \times m_{b23} + m_{a44} \times m_{b24}
  • mc43=ma14×mb31+ma24×mb32+ma34×mb33+ma44×mb34m_{c43} = m_{a14} \times m_{b31} + m_{a24} \times m_{b32} + m_{a34} \times m_{b33} + m_{a44} \times m_{b34}
  • mc44=ma14×mb41+ma24×mb42+ma34×mb43+ma44×mb44m_{c44} = m_{a14} \times m_{b41} + m_{a24} \times m_{b42} + m_{a34} \times m_{b43} + m_{a44} \times m_{b44}

模型矩阵

位移

(x+x1,y+y1,z+z1,w,)=[100001000010x1y1z11]×vec\begin{pmatrix} x + x_1 , \\ y + y_1 , \\ z + z_1 , \\ w , \\ \end{pmatrix} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ x_1 & y_1 & z_1 & 1 \\ \end{bmatrix} \times \overrightarrow{vec}

缩放

(x×x1,y×y1,z×z1,w,)=[x10000y10000z100001]×vec\begin{pmatrix} x \times x_1 , \\ y \times y_1 , \\ z \times z_1 , \\ w , \\ \end{pmatrix} = \begin{bmatrix} x_1 & 0 & 0 & 0 \\ 0 & y_1 & 0 & 0 \\ 0 & 0 & z_1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \times \overrightarrow{vec}

旋转

旋转

视图矩阵

通过目标点,视点,上方向,三个点计算视图矩阵。x,y 方向不变,z 方向相反。

  • 目标点:target=(tx,ty,tz,tw)\overrightarrow{target} = (tx,ty,tz,tw)
  • 视点:eye=(ex,ey,ez,ew)\overrightarrow{eye} = (ex,ey,ez,ew)
  • 上方向:up=(ux,uy,uz,uw)\overrightarrow{up} = (ux,uy,uz,uw)
  1. 以目标点为起点,视点为终点计算d=eyetarget=(dx,dy,dz,dw)\overrightarrow{d} = \overrightarrow{eye} - \overrightarrow{target} = (dx,dy,dz,dw)
  2. d\overrightarrow{d}归一化d=1/d\overrightarrow{d} *= 1 / |\overrightarrow{d}|
  3. 利用d\overrightarrow{d}与上方向叉乘求出a=d×up=(ax,ay,az,aw)\overrightarrow{a} = \overrightarrow{d} \times \overrightarrow{up} = (ax,ay,az,aw)
  4. a\overrightarrow{a}归一化a=1/a\overrightarrow{a} *= 1 / |\overrightarrow{a}|
  5. 利用d\overrightarrow{d}与上方向叉乘求出b=d×a=(bx,by,bz,bw)\overrightarrow{b} = \overrightarrow{d} \times \overrightarrow{a} = (bx,by,bz,bw)
  6. 利用d\overrightarrow{d}取反求出c=d\overrightarrow{c} = -\overrightarrow{d}
  7. (a,b,c)(\overrightarrow{a},\overrightarrow{b},\overrightarrow{c})即为视图矩阵中的(x,y,z)(\overrightarrow{x},\overrightarrow{y},\overrightarrow{z})

正交投影矩阵

在裁剪空间内,物体大小不会因为距离远近而改变,通过上下边界,左右边界,近平面,远平面计算正交投影矩阵。

  • 上边界:top
  • 下边界:bottom
  • 左边界:left
  • 右边界:right
  • 近平面:near
  • 远平面:far
  1. 计算裁剪空间的三轴方向长度与相机世界的三轴方向上的 1 个单位比例关系。
    • 裁剪空间(用 1 表示) / 相机世界(用 2 表示,默认为 1)
    • w1=w1w2=2rightleft w_1 = \frac{w_1}{w_2} = \frac{2}{right - left}
    • h1=h1h2=2topbottom h_1 = \frac{h_1}{h_2} = \frac{2}{top - bottom}
    • p1=p1p2=2farnear p_1 = \frac{p_1}{p_2} = \frac{2}{far - near}
  2. 通过比例关系求出矩阵与裁剪空间的位移量
    • x=right+left2×2rightleft=right+leftrightleftx = \frac{right + left}{2} \times \frac{2}{right - left} = \frac{right + left}{right - left}
    • y=top+bottom2×2topbottom=top+bottomtopbottomy = \frac{top + bottom}{2} \times \frac{2}{top - bottom} = \frac{top + bottom}{top - bottom}
    • z=far+near2×2farnear=far+nearfarnearz = \frac{far + near}{2} \times \frac{2}{far - near} = \frac{far + near}{far - near}
  • orthographicProjection=[2rightleft00002topbottom00002farnear0right+leftrightlefttop+bottomtopbottomfar+nearfarnear1]= orthographicProjection = \begin{bmatrix} \frac{2}{right - left} & 0 & 0 & 0 \\ 0 & \frac{2}{top - bottom} & 0 & 0 \\ 0 & 0 & \frac{2}{far - near} & 0 \\ \frac{right + left}{right - left} & \frac{top + bottom}{top - bottom} & \frac{far + near}{far - near} & 1 \\ \end{bmatrix} =

透视投影矩阵

在裁剪空间内,物体大小会因为距离远近而改变,通过上下边界,左右边界,近平面,远平面计算正交投影矩阵。

  • fov:摄像机视锥体垂直视野角度
  • aspect:摄像机视锥体宽高比
  • near:摄像机近裁剪面到视点的距离
  • far:摄像机远裁剪面到视点的距离
  1. 相机可视区域为四棱台形状,转化为长方体形状的投影盒子
    1. topn=tan(fov2)\frac{top}{n} = \tan(\frac{fov}{2})
    2. top=tan(fov2)×ntop = \tan(\frac{fov}{2}) \times n
    3. bottom=topbottom = -top
    4. righttop=aspect\frac{right}{top} = aspect
    5. right=top×aspeceright = top \times aspece
    6. left=rightleft = -right
    7. p1=(x1,y1,z1,w1) p_1 = ( x_1 , y_1 , z_1 , w_1),则p1p_1在投影盒子里的投影(x 与 y 发生偏移,z 位置不变)为:
    8. x2x1=nz1\frac{x_2}{x_1} = \frac{-n}{z_1}
    9. x2=nz1×x1x_2 = \frac{-n}{z_1} \times x_1
    10. 同理可得 y2=nz1×y1y_2 =\frac{-n}{z_1} \times y_1
    11. p2=(nz1×x1,nz1×y1,z1,w1) p_2 = ( \frac{-n}{z_1} \times x_1 , \frac{-n}{z_1} \times y_1 , z_1 , w_1)
  2. 将投影盒子与裁剪空间进行映射
    1. 设裁剪空间中p3=(x3,y3,z3,w3)p_3 = ( x_3 , y_3 , z_3 , w_3 )与投影盒子中的p2p_2对应
    2. p4=p3×z1p_4 = p_3 \times -z_1
    3. w3=w1w_3 = w_1
    4. x3(1)1(1)=nz1×x1leftrightleft\frac{ x_3 - (-1) }{ 1 - (-1) } = \frac{ \frac{-n}{z_1} \times x_1 - left }{ right - left }
    5. x4=z1×x3=2×nrightleft×x1+right+leftrightleft×z1 x_4 = -z_1 \times x_3 = \frac{ 2 \times n}{ right - left } \times x_1 + \frac{ right + left }{ right - left } \times z_1
    6. 同理可得x4=z1×x3=2×ntopbottom×y1+top+bottomtopbottom×z1 x_4 = -z_1 \times x_3 = \frac{ 2 \times n}{ top - bottom } \times y_1 + \frac{ top + bottom }{ top - bottom } \times z_1
    7. 为了方便矩阵运算,逆向求z4z_4的结果 projectionMatrix=[2×nrightleft00002×ntopbottom0000k1right+leftrightlefttop+bottomtopbottomb1] projectionMatrix = \begin{bmatrix} \frac{ 2 \times n}{ right - left } & 0 & 0 & 0 \\ 0 & \frac{ 2 \times n}{ top - bottom } & 0 & 0 \\ 0 & 0 & k & -1 \\ \frac{ right + left }{ right - left } & \frac{ top + bottom }{ top - bottom } & b & 1 \\ \end{bmatrix}
    8. z4=z1×z3=k×z1+bz_4 = -z_1 \times z_3 = k \times z_1 + b
      • z1=nz_1 = -n时,z3=1z_3 = -1
      • z1=fz_1 = -f时,z3=1z_3 = 1
      • 求出z4=far+nearfarnear×z12×near×farfarnear z_4 = -\frac{far + near}{far-near} \times z_1 - \frac{2 \times near \times far}{far - near}
  3. 最后求出 projectionMatrix=[2×nrightleft00002×ntopbottom0000far+nearfarnear1right+leftrightlefttop+bottomtopbottom2×near×farfarnear1] projectionMatrix = \begin{bmatrix} \frac{ 2 \times n}{ right - left } & 0 & 0 & 0 \\ 0 & \frac{ 2 \times n}{ top - bottom } & 0 & 0 \\ 0 & 0 & - \frac{ far + near }{ far-near } & -1 \\ \frac{ right + left }{ right - left } & \frac{ top + bottom }{ top - bottom } & - \frac{ 2 \times near \times far }{ far - near } & 1 \\ \end{bmatrix}