简体   繁体   English

二维矩阵到三维矩阵

[英]2D Matrix to 3D Matrix

I have 2D transformations stored in a plain ol' 3x3 Matrix. 我有2D转换存储在普通的'3x3矩阵中。 How can I re-format that one to a Matrix I can shove over to OpenGL in order to transform orthogonal shapes, polygons and suchlike. 我怎样才能将那个格式重新格式化为矩阵,我可以将其转移到OpenGL上,以便转换正交形状,多边形等。

How do I have to put the values so the transformations are preserved? 我如何设置值以保持转换?

(On an unrelated note, is there a fast way to invert a 3x3 Matrix?) (在一个不相关的说明中,是否有一种快速的方法来反转3x3矩阵?)

Some explanation about transformation matrices: All the columns, except the last one, describe the orientation of a new coordinate system in the base of the current coordinate system. 关于变换矩阵的一些解释:除了最后一列之外,所有列都描述了当前坐标系基础中新坐标系的方向。 So the first column is the X vector of the new coordinate system, as seen from the current, the second is the new Y vector and the 3rd is the new Z. So far this only covers the rotation. 所以第一列是新坐标系的X向量,从当前看,第二列是新的Y向量,第三列是新的Z.到目前为止,这只涵盖了旋转。 The last column is used for the relative offset. 最后一列用于相对偏移量。 The last row and the bottom most right value are used for the homogenous transformations. 最后一行和最右下一些值用于同质变换。 It's best to leave the last row 0, ..., 0, 1 最好留下最后一行0,...,0,1

In your case you're missing the Z values, so we just insert a identity transform there, so that incoming values are left as they are. 在你的情况下,你错过了Z值,所以我们只是在那里插入一个身份变换,以便传入的值保持不变。

Say this is your original matrix: 说这是你的原始矩阵:

xx xy tx

yx yy ty

 0  0  1

This matrix is missing the Z transformation. 该矩阵缺少Z变换。 Inserting identity means: Leave Z as is, and don't mix with the rest. 插入标识意味着:保持Z原样,不要与其余部分混合。 So ·z = z· = 0, except zz = 1. This gives you the following matrix 所以·z = z·= 0,除了zz = 1.这给出了以下矩阵

       ↓

xx xy  0 tx

yx yy  0 ty

 0  0  1  0  ←

 0  0  0  1

You can apply that onto the current OpenGL matrix stack with glMultMatrix if OpenGL version is below 3 core profile. 如果OpenGL版本低于3核心配置文件,您可以使用glMultMatrix将其应用于当前的OpenGL矩阵堆栈。 Be aware that OpenGL numbers the matrix in column major order ie, the indices in the array go like this (hexadecimal digits) 请注意,OpenGL按列主要顺序对矩阵进行编号,即数组中的索引如下所示(十六进制数字)

0 4 8 c
1 5 9 d
2 6 a e
3 7 b f

This contrary to the usual C notation which is 这与通常的C表示法相反

0 1 2 3
4 5 6 7
8 9 a b
c d e f

With OpenGL-3 core and later you've to do matrix management and manipulation yourself, anyway. 使用OpenGL-3核心,无论如何你都要自己进行矩阵管理和操作。

EDIT for second part of question 编辑第二部分问题

If by inverting one means finding the matrix M^-1 for a given matrix M, so that M^1 * M = M * M^1 = 1 . 如果通过反转一个意味着找到给定矩阵M的矩阵M ^ -1,则M ^ 1 * M = M * M ^ 1 = 1 For 3×3 matrices the determinant inversion method requires less operations than Gauss-Jordan elemination and is thus the most efficient way to do it. 对于3×3矩阵,行列式反演方法比Gauss-Jordan选择需要更少的操作,因此是最有效的方法。 Already for 4×4 matrices determinant inversion is slower than every other method. 对于4×4矩阵,行列式反演已经比其他方法慢。 http://www.sosmath.com/matrix/inverse/inverse.html http://www.sosmath.com/matrix/inverse/inverse.html

If you know that your matrix is orthonormal, then you may just transpose the upper left part except bottom row and rightmost column, and negate the sign of the rightmost column, except the very bottom right element. 如果你知道你的矩阵是正交的,那么你可以只调换除了底行和最右列之外的左上部分,并否定最右边列的符号,除了最右下的元素。 This exploits the fact that for orthonormal matrices M^-1 = M^T. 这利用了对于正交矩阵M ^ -1 = M ^ T的事实。

Just add the fourth row and column. 只需添加第四行和第二列。 For example given 举个例子

2 3 3
3 2 4
0 0 1

Create the following 创建以下内容

2 3 3 0
3 2 4 0
0 0 1 0
0 0 0 1

The transformation still occurs on the xy plane even though it is now in three-space. 转换仍然发生在xy平面上,即使它现在处于三维空间。

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

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