Jump to content
  • Advertisement

Archived

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

bakery2k1

Invert a general 4x4 matrix

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 0
r21 r22 r23 0 = V (for arguments sake)
r31 r32 r33 0
tx ty tz 1

to find the inverse consider the fact that


r11 r12 r13 0 1 0 0 0
v = 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 * B


therefore 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 negation
of 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 transpose
of the matrix)

multiplying these 2 together gives the inverse:


r11 r21 r31 0
r12 r22 r32 0
r13 r23 r33 0
E F G 1


where
E = -tx*r11 + -ty*r12 + -tz*r13
F = -tx*r21 + -ty*r22 + -tz*r23
G = -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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!