Skip to main content
gamedev.net
🔒 Locked

Matrix Operations

Started by mooserman352 3 replies 1k views
Original Post
mooserman352
mooserman352
Anybody want to point me to some code that will compute the inverse of a opengl matrix. I'm kind of too lazy and pressed for time. Thanks.
Ademan555
Ademan555
Well, as long as its an orthogonal matrix (it should be unless you use scaling) then its inverse IS its transpose.

hope that helps
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Zakwayda
Zakwayda
My matrix code uses 2d indexing, so I did a search and replace to convert to 1d column-major, and came up with this:

template <class T> bool Matrix4<T>::InvertSelf(T epsilon){    T mi_23_23 = m[10] * m[15] - m[14] * m[11];    T mi_23_13 = m[6]  * m[15] - m[14] * m[7];    T mi_23_12 = m[6]  * m[11] - m[10] * m[7];    T mi_23_03 = m[2]  * m[15] - m[14] * m[3];    T mi_23_02 = m[2]  * m[11] - m[10] * m[3];    T mi_23_01 = m[2]  * m[7]  - m[6]  * m[3];	    T mi_123_123 = m[5] * mi_23_23 - m[9] * mi_23_13 + m[13] * mi_23_12;    T mi_123_023 = m[1] * mi_23_23 - m[9] * mi_23_03 + m[13] * mi_23_02;    T mi_123_013 = m[1] * mi_23_13 - m[5] * mi_23_03 + m[13] * mi_23_01;    T mi_123_012 = m[1] * mi_23_12 - m[5] * mi_23_02 + m[9]  * mi_23_01;    T det = m[0] * mi_123_123 - m[4] * mi_123_023 + m[8] * mi_123_013 - m[12] * mi_123_012;	    if (Math::Fabs(det) < epsilon)        return false;	    T invdet = (T)1.0 / det;    T mi_13_23 = m[9] * m[15] - m[13] * m[11];    T mi_13_13 = m[5] * m[15] - m[13] * m[7];    T mi_13_12 = m[5] * m[11] - m[9]  * m[7];    T mi_13_03 = m[1] * m[15] - m[13] * m[3];    T mi_13_02 = m[1] * m[11] - m[9]  * m[3];    T mi_13_01 = m[1] * m[7]  - m[5]  * m[3];	    T mi_12_23 = m[9] * m[14] - m[13] * m[10];    T mi_12_13 = m[5] * m[14] - m[13] * m[6];    T mi_12_12 = m[5] * m[10] - m[9]  * m[6];    T mi_12_03 = m[1] * m[14] - m[13] * m[2];    T mi_12_02 = m[1] * m[10] - m[9]  * m[2];    T mi_12_01 = m[1] * m[6]  - m[5]  * m[2];	    T mi_023_123 = m[4] * mi_23_23 - m[8] * mi_23_13 + m[12] * mi_23_12;    T mi_023_023 = m[0] * mi_23_23 - m[8] * mi_23_03 + m[12] * mi_23_02;    T mi_023_013 = m[0] * mi_23_13 - m[4] * mi_23_03 + m[12] * mi_23_01;    T mi_023_012 = m[0] * mi_23_12 - m[4] * mi_23_02 + m[8]  * mi_23_01;	    T mi_013_123 = m[4] * mi_13_23 - m[8] * mi_13_13 + m[12] * mi_13_12;    T mi_013_023 = m[0] * mi_13_23 - m[8] * mi_13_03 + m[12] * mi_13_02;    T mi_013_013 = m[0] * mi_13_13 - m[4] * mi_13_03 + m[12] * mi_13_01;    T mi_013_012 = m[0] * mi_13_12 - m[4] * mi_13_02 + m[8]  * mi_13_01;	    T mi_012_123 = m[4] * mi_12_23 - m[8] * mi_12_13 + m[12] * mi_12_12;    T mi_012_023 = m[0] * mi_12_23 - m[8] * mi_12_03 + m[12] * mi_12_02;    T mi_012_013 = m[0] * mi_12_13 - m[4] * mi_12_03 + m[12] * mi_12_01;    T mi_012_012 = m[0] * mi_12_12 - m[4] * mi_12_02 + m[8]  * mi_12_01;    m[0]  =  mi_123_123 * invdet;    m[1]  = -mi_123_023 * invdet;    m[2]  =  mi_123_013 * invdet;    m[3]  = -mi_123_012 * invdet;	    m[4]  = -mi_023_123 * invdet;    m[5]  =  mi_023_023 * invdet;    m[6]  = -mi_023_013 * invdet;    m[7]  =  mi_023_012 * invdet;	    m[8]  =  mi_013_123 * invdet;    m[9]  = -mi_013_023 * invdet;    m[10] =  mi_013_013 * invdet;    m[11] = -mi_013_012 * invdet;	    m[12] = -mi_012_123 * invdet;    m[13] =  mi_012_023 * invdet;    m[14] = -mi_012_013 * invdet;    m[15] =  mi_012_012 * invdet;    return true;}

I'm 99% sure I got all the searching and replacing right...

A couple of caveats. First of all, this can optimized considerably based on a priori knowledge about what sort of transformation the matrix represents. Do you just need to invert a simple rotation-translation matrix? Or are you using scaling? How about projection? Depending on your needs, the code could be made much simpler and faster.

Secondly, the code I posted is not the fastest way to compute the inverse. I've seen code that is faster, but I don't yet understand the math behind it and so haven't implemented it. So be aware that this is not the fastest implementation. However, I've seen a lot of code online that uses the same method I'm using here.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.