行主序矩阵与列主序矩阵
首先要了解的是,对于连续内存数据:
\[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}
\]
行主序矩阵是这样解释数据的:
\[M=
\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}
\]
而列主序矩阵是这样解释数据的:
\[M=
\begin{bmatrix}
m_{11} & m_{21} & m_{31} & m_{41} \\
m_{12} & m_{22} & m_{32} & m_{42} \\
m_{13} & m_{23} & m_{33} & m_{43} \\
m_{14} & m_{24} & m_{34} & m_{44}
\end{bmatrix}
\]
DirectX的数学使用的是row major matrix,但是HLSL packs matrices in a column major order
虽然HLSL使用column major order进行packs.但是实际它读取matrices是安装row major order的。
this is the matrix we are passing from our app, which is in row major ordering:
\[\begin{bmatrix}
1 & 2 & 3 & 4 \\
5 & 6 & 7 & 8 \\
9 & 10 & 11 & 12 \\
13 & 14 & 15 & 16
\end{bmatrix}
\]
this is how HLSL is storing the matrix:
\[\begin{bmatrix}
1 & 5 & 9 & 13 \\
2 & 6 & 10 & 14 \\
3 & 7 & 11 & 15 \\
4 & 8 & 12 & 16
\end{bmatrix}
\]
HLSL code:
\[\text{output.pos} = \text{mul}(\text{input.pos},\text{wvpMat});
\]
HLSL assembly:
0: dp4 r0.x, v0.xyzw, cb0[0].xyzw // r0.x <- output.pos.x
1: dp4 r0.y, v0.xyzw, cb0[1].xyzw // r0.y <- output.pos.y
2: dp4 r0.z, v0.xyzw, cb0[2].xyzw // r0.z <- output.pos.z
3: dp4 r0.w, v0.xyzw, cb0[3].xyzw // r0.w <- output.pos.w
cb0[0] is now this in HLSL (this was a column in our app, but is now a row in HLSL, which makes the multiplication easier):
\[\begin{bmatrix}1 & 5 & 9 & 13\end{bmatrix}
\]
World/View/Project Space
View Space
\[\begin{bmatrix}
\mathtt{right}.x & \mathtt{up}.x & \mathtt{forward}.x & \mathtt{position}.x \\
\mathtt{right}.y & \mathtt{up}.y & \mathtt{forward}.y & \mathtt{position}.y \\
\mathtt{right}.z & \mathtt{up}.z & \mathtt{forward}.z & \mathtt{position}.z \\
0 & 0 & 0 & 1
\end{bmatrix}
\]
The right, up and forward vector are normalized vectors . The describe the camera's right direction in the virtual world, the up direction, and the forward direction . The position vector is an x,y,z coordinate describing the position of the camera in the virtual world.
Moving From Space to Space
So just to recap, to get a 3d model from object (a.k.a. local) space to projection space, we multiply each vertex by world, then view, then projection. It will look like this:
finalvertex.pos = vertex.pos * worldMatrix * viewMatrix * projectionMatrix;
参考:
!(Transform )[https://www.braynzarsoft.net/viewtutorial/q16390-transformations-and-world-view-projection-space-matrices]