Inverting 4x4 martrix (Opengl)

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

Recommended Posts

Guys, i have a 4x4 matrix declared as float matrix[16]; I need to invert it, can someone please help me with this?

Share on other sites
Ok i've found something that works perfectly.

void matrixIdentity(float m[16]){    m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;    m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;    m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;    m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;}int matrixInvert(float src[16], float inverse[16]){    float t;    int i, j, k, swap;    float tmp[4][4];    matrixIdentity(inverse);    for (i = 0; i < 4; i++) {	for (j = 0; j < 4; j++) {	    tmp[j] = src[i*4+j];	}    }    for (i = 0; i < 4; i++) {        /* look for largest element in column. */        swap = i;        for (j = i + 1; j < 4; j++) {            if (fabs(tmp[j]) > fabs(tmp)) {                swap = j;            }        }        if (swap != i) {            /* swap rows. */            for (k = 0; k < 4; k++) {                t = tmp[k];                tmp[k] = tmp[swap][k];                tmp[swap][k] = t;                t = inverse[i*4+k];                inverse[i*4+k] = inverse[swap*4+k];                inverse[swap*4+k] = t;            }        }        if (tmp == 0) {            /* no non-zero pivot.  the matrix is singular, which	       shouldn't happen.  This means the user gave us a bad	       matrix. */            return 0;        }        t = tmp;        for (k = 0; k < 4; k++) {            tmp[k] /= t;            inverse[i*4+k] /= t;        }        for (j = 0; j < 4; j++) {            if (j != i) {                t = tmp[j];                for (k = 0; k < 4; k++) {                    tmp[j][k] -= tmp[k]*t;                    inverse[j*4+k] -= inverse[i*4+k]*t;                }            }        }    }    return 1;}

Post can be closed.

Share on other sites
Quote:
 i have a 4x4 matrix declared asfloat matrix[16];I need to invert it, can someone please help me with this?
There are many references and examples available online that show how to invert a 4x4 matrix. A Google search for '4x4 matrix inverse' returns quite a few hits (in fact, '4x4 matrix inverse' is one of Google's autocompletions when you type '4x4 mat' :).

IMX, the most commonly used method for inverting small matrices in a games or graphics context is to apply Cramer's rule, but you'll sometimes see other methods used as well (e.g. Gaussian elimination).

Regarding your specific example, you'll need to find sample code that works with a 1-d array, adapt code that uses 2-d array indexing to work with your 1-d array (note that it's easy to introduce errors when doing this), or use or write a matrix class or wrapper that allows 2-d indexing.

It's also worth noting that there are some types of transforms that can be treated as 'special cases' as far as inversion is concerned. I'm assuming though that it's a general inverse function that you're looking for.

[Edit: Looks like you found it :) Just FYI though, on these forums posts generally aren't 'closed' when the OP finds a solution (remember, the threads are for everyone, not just the original poster).]

Share on other sites
xynapse, what you have posted in LU-factorization.

Now, just an important thought: the two most common 4x4 matrices types you have in GL are:

Projection matrices
Change of Co-ordinate matrices

Chances are you are finding the inverse of the latter, and in that case LU-factorization is over kill uaully, as the matrices are the vast, vast majority of the time of the from:

| x |
| R y |
| z |
| 0 0 0 1 |

where the vast majority of the time R is an orthogonal matrix, i.e its transpose is its inverse.

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 12
• 10
• Forum Statistics

• Total Topics
632578
• Total Posts
3007158

×