开发者

2D Matrix to 3D Matrix

开发者 https://www.devze.com 2023-03-26 05:37 出处:网络
I have 2D transformatio开发者_如何学编程ns stored in a plain ol\' 3x3 Matrix. How can I re-format that one to a Matrix I can shove over to OpenGL in order to transform orthogonal shapes, polygons and

I have 2D transformatio开发者_如何学编程ns stored in a plain ol' 3x3 Matrix. 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.

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?)


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. 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

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.

Say this is your original matrix:

xx xy tx

yx yy ty

 0  0  1

This matrix is missing the Z transformation. Inserting identity means: Leave Z as is, and don't mix with the rest. So ·z = z· = 0, except zz = 1. This gives you the following matrix

       ↓

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. Be aware that OpenGL numbers the matrix in column major order i.e, the indices in the array go like this (hexadecimal digits)

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

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.

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. 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. Already for 4×4 matrices determinant inversion is slower than every other method. 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.


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 x-y plane even though it is now in three-space.

0

精彩评论

暂无评论...
验证码 换一张
取 消