简体   繁体   English

为什么要使用矩阵进行3D投影?

[英]Why use a Matrix for 3D Projection?

After searching up the calculations for a Projection Matrix (atleast in OpenGL), 在计算出投影矩阵后(至少在OpenGL中),

矩阵

Why bother using a Matrix when we have so many empty values? 当我们有这么多空值时,为什么还要使用矩阵呢? I count 9 entries marked as 0, and only 7 containing useful data. 我统计了9个标记为0的条目,只有7个包含有用的数据。 Why not just use a similar 1D array, and just store the data in a list-like shape? 为什么不只使用类似的一维数组,而只是将数据存储为列表状? Wouldn't this save memory and time creating functions which can manipulate matrices? 这样是否可以节省内存和创建可操纵矩阵的函数的时间? I'm sure that this entire argument can be used in other topics as-well, which makes me think, 我确信整个论点也可以在其他主题中使用,这让我觉得,

What is the specific reason for using Matrices in projecting 3D environments? 在投影3D环境中使用矩阵的具体原因是什么?

The projection of a 3D point (x,y,z) to the 2D image coordinates (X,Y) can be calculated as a vector-matrix multiplication in homogeneous coordinates: 可以将3D点(x,y,z)投影到2D图像坐标(X,Y)上,作为均质坐标中的向量矩阵乘法:

[ a_00  a_01  a_02  a_03 ]   [ x ]    [ X W ]
[ a_10  a_11  a_12  a_13 ] * [ y ] =  [ Y W ]
[ a_20  a_21  a_22  a_23 ]   [ z ]    [ Z W ]
[ a_30  a_31  a_32  a_33 ]   [ 1 ]    [  W  ]

with

[ X W ]   [ x a_00 + y a_01 + z a_02 + a_03 ]
[ Y W ]   [ x a_10 + y a_11 + z a_12 + a_13 ]
[ Z W ] = [ x a_20 + y a_21 + z a_22 + a_23 ]
[  W  ]   [ x a_30 + y a_31 + z a_32 + a_33 ]

And the pixel coordinates (X,Y) are obtained by dividing the first and second rows by the fourth row. 并且通过将第一行和第二行除以第四行来获得像素坐标(X,Y) This step is the conversion from homogeneous to cartesian coordinates. 此步骤是从齐次坐标到直角坐标的转换。

The third row of the OpenGL projection matrix is set up in a way that Z becomes the projected depth, which is such that z values between n and f (near and far planes) are mapped to -1...1 . OpenGL投影矩阵的第三行以Z成为投影深度的方式进行设置,即将nf (近平面和远平面)之间的z值映射到-1...1 It is the used for depth test/clipping. 用于深度测试/剪切。 Because the fourth row is [0 0 -1 0] , the conversion from homogeneous to cartesian coordinates corresponds to a division by -z , which results in the perspective transformation (with inverted depth). 因为第四行是[0 0 -1 0] ,所以从齐次坐标到直角坐标的转换对应于-z的除法,这将导致透视变换(深度反转)。

Any other way of expressing the projection would involve the same steps, namely the linear transformation, followed by the division by Z for the perspective foreshortening. 表示投影的任何其他方式都将涉及相同的步骤,即线性变换,然后用Z除以透视透视。 Matrices are the usual representation in linear algebra to for these operations. 矩阵是这些运算中线性代数的常用表示形式。

This is not specific for perspective projections, but many 3D transformatios can be expressed using a 4x4 matrix, including rotations, translations, scalings, shearings, reflections, perspective projection, orthogonal projection, and others. 这不是特定于透视投影的,但是可以使用4x4矩阵来表达许多3D变换,包括旋转,平移,缩放,剪切,反射,透视投影,正交投影等。

Multiple transformations that should be applied after one another can also be combined into a single 4x4 matrix by matrix multiplication. 还可以通过矩阵乘法将应依次应用的多个变换组合为单个4x4矩阵。 For example rotations around the X, Y and Z axis, or the MVP matrix. 例如,围绕X,Y和Z轴或MVP矩阵的旋转。 This is the model-view-projection matrix, which translates a 3D point in the local coordinate system of one object in the 3D scene, into its final pixel coordinate on the screen. 这是模型-视图-投影矩阵,该矩阵将3D场景中一个对象的局部坐标系中的3D点转换为其在屏幕上的最终像素坐标。 On these combined matrices all components can be non-zero. 在这些组合矩阵上,所有分量都可以为非零。

So the advantage is that a single operation, the vector-matrix multiplication is useable for all these cases, instead of several different operations. 因此,优点在于,单个运算,向量矩阵乘法可用于所有这些情况,而不是几个不同的运算。 It is performed in an efficient way on GPU hardware. 它在GPU硬件上以高效的方式执行。

It's not just about the single values, it's also about the mathematical properties of a matrix. 这不仅与单个值有关,还与矩阵的数学性质有关。 And the zeros are just as important as the nonzero values! 零和非零值一样重要! The very layout of the values has meaning! 值的布局很有意义!

Specifically the first three columns of a homogenous transformation matrix (like a 3D projection matrix) form the base vectors of local coordinate space, the 4th column defines a translation (which in case of a perspective projection moves the base away from the singularity point at the origin). 具体来说,同质转换矩阵的前三列(如3D投影矩阵)形成局部坐标空间的基向量,第4列定义了平移(如果是透视投影,则将基点移离基点处的奇点。起源)。

So in 3D space you have 3 values per position: You have to translate these three values into 3 values on your screen (the third value translates to a value that's used for depth comparison) and the 4th value (of the position and the destination) is used for perspective distortion. 因此,在3D空间中,每个位置有3个值:您必须将这三个值转换为屏幕上的3个值(第三个值转换为用于深度比较的值),第4个值(位置和目标位置)用于透视变形。 So for each of the 4 values in the original position you must know, how much it contributes to each of the 4 values in the output. 因此,对于原始位置的4个值中的每个值,您都必须知道,它对输出中4个值中的每个值有多少贡献。 If it doesn't contribute (and that's just as important) this is 0. So you need 4 · 4 = 16 values in total. 如果它没有贡献(同样重要),则为0。因此,您总共需要4·4 = 16个值。 Hence a 4×4 matrix. 因此是4×4矩阵。

It's probably quite rare that the projection matrix would get used as-is. 投影矩阵按原样使用可能非常罕见。 Typically, you're more likely to concatenate the projection matrix with the world and view matrices and multiply by the world-view-proj matrix all in one go. 通常,您更有可能一次将投影矩阵与世界连接起来并查看矩阵,然后乘以world-view-proj矩阵。

Also, GPUs are powerful and flexible, but if there's one thing they're best at doing, it's a series of multiply-adds on vectors (although newer hardware is just as efficient with scalar multiply-adds as vector multiply-adds). 此外,GPU强大而灵活,但是如果有一件他们最擅长的事情,那就是在向量上进行一系列的加法运算(尽管较新的硬件在进行标量乘法加法时与矢量乘积法一样有效)。 Matrix-vector multiplies are just a series of vector multiply-adds, and a more compact structure might be less efficient. 矩阵-矢量乘法只是一系列矢量的乘法加法,而更紧凑的结构可能效率较低。

That said, your point is not without merit, I am aware of one successful fixed-function based games console which had limited hardware registers for the projection matrix to take advantage of your exact point that most of the entries in the projection matrix are typically unused. 就是说,您的观点并非没有价值,我知道一个成功的基于固定功能的游戏机,其投影矩阵的硬件寄存器有限,以利用您的确切观点,即投影矩阵中的大多数条目通常未使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM