inverse matrix

Started by
5 comments, last by V-man 12 years, 8 months ago
Is there any function in glsl or opengl to get the inverse matrix?Now,I should use it,can anyone can help you?
Advertisement
Quote:Original post by jzpijiang
Is there any function in glsl or opengl to get the inverse matrix?Now,I should use it,can anyone can help you?
I don't know about GLSL, but there is no OpenGL function to invert a matrix (the OpenGL API doesn't include math functions, per se).

It should be easy enough to find references for such a function online though. Just google for '4x4 matrix inverse'.
I have a warning. If you naively google "matrix inverse", you will probably find the expression giving the elements of the inverse in terms of the cofactors. This is NOT how you calculate inverses in general, because of the number of operations required. I think that even for n=4 calculating the inverse in this way is slower than other methods (i.e. reducing the augmented matrix A|I to reduced row echelon form), but correct me if I'm wrong. Additionally, you should also be aware of questions of numerical stability of whatever method you use.
Quote:Original post by jonnyfish
I have a warning. If you naively google "matrix inverse", you will probably find the expression giving the elements of the inverse in terms of the cofactors. This is NOT how you calculate inverses in general, because of the number of operations required. I think that even for n=4 calculating the inverse in this way is slower than other methods (i.e. reducing the augmented matrix A|I to reduced row echelon form), but correct me if I'm wrong. Additionally, you should also be aware of questions of numerical stability of whatever method you use.
This has been discussed in a couple of threads in 'Math & Physics' recently. Not everyone agrees, but the general gist seems to be that for matrices of 4x4 and smaller, inversion by cofactors is generally preferred (or at least acceptable) because it's branchless (except possibly for checking for singularity) and because the types of matrices used in games and graphics are usually fairly well-behaved, numerically speaking.

It's also worth noting that unless you're inverting many, many matrices per frame, the difference in performance between, say, Cramer's Rule and Gauss-Jordan or LU, will probably not be noticable. So in short, any of these methods will probably be acceptable for most purposes.

And of course, any time matrix inversion comes up it's important to point out that most types of 'inversions' that come up in games and graphics (but not all) are special cases that can be optimized or even avoided entirely.
There're many tricks to do a simple and fast matrix inversion. Usally in CG you only need a few special kind of matrices:
- rotation
- translation
- scaling
- projection

To keep it simple, let's restrict to the most common matrices:
rotation(R) and tranlation(T)

In your 3D-world most object orientations are made up of one rotation and one tranlation:
O = T * R
In words:
Your object starts at position (0,0,0) looking along your z-axis.
First let your object look in the final direction (by rotating it), then move it to your final position(by translating it).

Now lets calculate the inverse:
inv(O) = inv(T*R)=inv(R)*inv(T)

Ok, as you can see you only need to calculate the inverse of R and T:
inv(R)=tranposition(R)
inv(T)=just negate the 'position' vector of your matrix

It is really simple like this. At last here's a practical approach to invert the orientation of your object if it has been build up of only rotations and translations:

1. given: O = orientation of your object = T * R
2. O got the following structure:
      ( R R R P)O =( R R R P)   ( R R R P)   ( 0 0 0 1)

,where R is the 3x3 rotation matrix and P is the position vector.

3. extract R and T
      ( R R R 0)R =( R R R 0)   ( R R R 0)   ( 0 0 0 1)

      ( 1 0 0 P)T =( 0 1 0 P)   ( 0 0 1 P)   ( 0 0 0 1)


4. invert R by calculating the transposition.
5. invert T by negating the position part of your T matrix:

           ( 1 0 0 -P)inv(T) =( 0 1 0 -P)        ( 0 0 1 -P)        ( 0 0 0  1)


6. multiply them
inv(O) = inv(R) * inv(T)

Conclusion:
This only works on special matrices, but in CG we commonly work with this special class of matrices. It is numerical stable, fast and easy. You can extend this approach by adding support for more matrices types, like scale matrices.

--
Ashaman

Is there any function in glsl or opengl to get the inverse matrix?Now,I should use it,can anyone can help you?



mat4 inverse(mat4)

check the manual pages: http://www.opengl.org/sdk/docs/manglsl/
SincerelyCasper B. Hansen
You can use my library if you want. glhlib
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement