图形行与列的主要转换 [英] Graphics Row vs Column Major Transformations

查看:67
本文介绍了图形行与列的主要转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我很困惑.为什么大多数图形API(带有HLSL和OpenGL的DirectX)通常表示位置和法向矢量为行矢量的事物?这与数学课中出现的典型形式相反.两者在数学上都是正确的,我很好奇为什么图形专家选择了这种表示形式.让我解释一下...

向量与矩阵相乘的效率:

在我看来,行向量 x 和转换矩阵'A'被乘以 x * A ,从而导致"y = x * a"存在

  y [0] = x [0] * M [0] [0] + x [1] * M [1] [0] + x [2] * M [2] [0] +x [3] * M [3] [0];y [1] = x [0] * M [0] [1] + x [1] * M [1] [1] + x [2] * M [2] [1] + x [3] * M[3] [1];y [2] = x [0] * M [0] [2] + x [1] * M [1] [2] + x [2] * M [2] [2] + x [3] * M[3] [2];y [3] = x [0] * M [0] [3] + x [1] * M [1] [3] + x [2] * M [2] [3] + x [3] * M[3] [3]; 

请注意,要获取 y [0] ,必须读取矩阵的第一列.这不是比读取第一行效率低吗?矩阵通常以主要行"形式存储.由于内存中的位置,我将保证 A * x x * A 更有效率.这是错吗?

矩阵元素的计算

更复杂的是,创建转换矩阵的典型方法(例如,使用DirectX Math库)以以下形式填充矩阵

  |R00 R01 R02 0 ||R10 R11 R12 0 ||R20 R21 R22 0 ||T0 T1 T2 1 | 

其中, R 表示旋转/缩放项,而 T 表示平移项.此格式用于以 x * A = y 的形式进行乘法.为什么数学库不提供此矩阵的转置,从而可以在不手动转置数学库矩阵构造结果的情况下计算 A * x = y ?

摘要

由DirectX数学库创建的矩阵似乎建立的转换效率低于所述矩阵的转置.我在这个结论中是错误的还是有一个过度的理由?

解决方案

行大顺序允许从基本转换按从左到右的顺序创建仿射转换.例如,这种方式:

  M =比例*旋转*平移 

同意执行顺序.当然,相对于线性代数的定义,必须进行基本转换.在数学中使用的列优先顺序中,执行转换的顺序是相反的,并且违反直觉.两种方法的效率可能相同(取决于表在内存中的存储顺序).因此,在图形中使用列主矩阵只是线性代数标准的结果.

This has puzzled me for a long time. Why do most graphics APIs (DirectX with HLSL and OpenGL) usually represent things such that position and normal vectors are row vectors? This is opposite the typical form seem in a math class. Both are correct mathematically, I'm just curious about why graphics guys chose this representation. Let me explain...

Efficiency of the multiplication of a vector and matrix:

In my mind the row-vector x and transformation matrix 'A' are multiplied as x*A which leads to `y=x*a' being

y[0] = x[0]*M[0][0] + x[1]*M[1][0] + x[2]*M[2][0] + x[3]*M[3][0];
y[1] = x[0]*M[0][1] + x[1]*M[1][1] + x[2]*M[2][1] + x[3]*M[3][1];
y[2] = x[0]*M[0][2] + x[1]*M[1][2] + x[2]*M[2][2] + x[3]*M[3][2];
y[3] = x[0]*M[0][3] + x[1]*M[1][3] + x[2]*M[2][3] + x[3]*M[3][3];

Notice that to get y[0] the matrix's first column must be read. Isn't this less efficient than reading the first row instead? The matrix is typically stored "row-major." I will accert that A*x is more efficient than x*A because of positions in memory. Is this wrong?

Computation of the matrix elements

To further complicate, the typical way of creating the transformation matrix (for example using the DirectX Math library) populates the matrix in the form

| R00 R01 R02 0 |
| R10 R11 R12 0 |
| R20 R21 R22 0 |
| T0  T1  T2  1 |

where R denotes the rotation/scaling terms and T denotes the translation terms. This form is intended for multiplication in the form x*A = y. Why does the math library not provide the transpose of this matrix such that A*x = y can be computed without manually transposing the result of the math library matrix construction?

Summary

The matrix created by the directx math library seems to set up a less efficient transformation than the transpose of said matrix. Am I wrong in this conclusion or is there an overlying reason?

解决方案

Row-major order allows creating affine transformations in the left-to-right order from basic transformations. For example this way:

M = Scale * Rotation * Translation

Agreeing with order of execution. Of course basic transformations must be transposed vs. definitions from linear algebra. In column-major order used in math, the order in which transformations are executed is reversed and counterintuitive. The efficency of both approches could be identical (it depends on the order in which tables are stored in memory). Using column-major matrices in graphics is therefore only the result of linear algebra standard.

这篇关于图形行与列的主要转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆