• What is your GameDev Story?

Archived

This topic is now archived and is closed to further replies.

Invert a general 4x4 matrix

This topic is 6192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Can anyone point me to some code to invert a general 4x4 matrix? I need to fine the inverse of my projection matrix in openGL in order to implement frustum culling and portals. Thanks

Share on other sites
after doing a quick search on google....

he finds http://easyweb.easynet.co.uk/~mrmeanie/matrix/matrices.htm

you can also try searching "inverse matrix math+programming" on www.google.com

peace

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share on other sites
FYI,
To invert a 4x4 <b>transform matrix</b> (rotation sub-matrix in upper-left 3x3 area, translation vector in rightmost column or in the bottom row) just do the transpose of the rotation sub-matrix and negate the translation vector.

The 3x3 matrix is orthogonal (it represents a basis composed of orthogonal vectors) which means that its inverse is its transpose.

That''s why such a question is okay to ask here. Searching elsewhere will give you the okay, but pretty slow algorithms, especially concerning matrix inversions.

Share on other sites
Anonymous Poster,
take the following matrix:
0 1 0 1
1 0 0 2
0 0 1 3
0 0 0 1
Notice that the upper left 3x3 matrix is a (simple) rotation matrix.
Now let´s invert it with your method...
0 1 0 -1
1 0 0 -2
0 0 1 -3
0 0 0 1
And let´s multiply those two:
1 0 0 -1
0 1 0 1
0 0 1 0
0 0 0 1
Which for sure is _not_ the identity matrix, and thus your method is wrong.
And that:
0 1 0 -1
1 0 0 -2
0 0 1 -3
0 0 0 -1
is not the inverse either.

I once calculated a way to do matrix inversion if the upper left submatrix is a rotation-scaling matrix, but I do not have it here. If anyone wants to have it, I may look it up.

Share on other sites
inverse of a matrix like this where you have rotations and
translation together I reckon could go something like this:

r11 r12 r13 0r21 r22 r23 0   = V (for arguments sake)r31 r32 r33 0tx  ty  tz  1to find the inverse consider the fact that      r11 r12 r13 0   1  0  0  0v =  r21 r22 r23 0 x 0  1  0  0     r31 r32 r33 0   0  0  1  0     0   0   0   1   tx ty tz 1         write this as V = A * Btherefore inverse(V) = inverse(B)*inverse(A)inverse(B) = 1   0   0   0             0   1   0   0             0   0   1   0             -tx -ty -tz 1(as the inverse of a translation matrix is the negationof the translation part of the matrix)inverse(A) = r11 r21 r31 0             r12 r22 r32 0             r13 r23 r33 0             0   0   0   1(as the inverse of a rotation matrix is the transposeof the matrix)multiplying these 2 together gives the inverse:r11 r21 r31 0r12 r22 r32 0r13 r23 r33 0E   F   G   1where E = -tx*r11 + -ty*r12 + -tz*r13F = -tx*r21 + -ty*r22 + -tz*r23G = -tz*r31 + -ty*r32 + -tz*r33

This is just what I've been figuring out on paper the last
few minutes - a confirmation from someone that this is OK
would be cool though.

Thanks
Toby

Gobsmacked - by Toby Murray

(typo removed - thanks for pointing it out, had completely missed it)

Edited by - tobymurray on February 7, 2002 4:38:07 AM

Share on other sites
Yes, it is correct (I think).
And spliting up the matrix into two is a cool way to calculate it. I had much more problems calculating it, as I didn´t split the matrix into two. But I got the same result.
Btw, there is a typo in inverse(B), just in case someone is wondering...

Share on other sites
A general transform matrix would also have scaling and you could find the scaling matrix by the magnitude of the axes. That is all fine and dandy, but the question was about an OpenGL projection matrix. According to the doc on glFrustum the projection matrix is:

  |a 0 b 0||0 c d 0||0 0 e f||0 0 -1 0|//so the inverse would be|1/a 0 0 b/a|| 0 1/c 0 d/c|| 0 0 0 -1 || 0 0 1/f e/f|

Share on other sites
You can go ahead and do it the linear algrebra text book way and row reduce the matrix to identity and then use exactly the same sequence of elementary row operations to change the identity matrix into the inverse matrix, but thats not really code (nor is it fun to program) but it works.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 42
• Forum Statistics

• Total Topics
634131
• Total Posts
3015717
×