# OpenGL Requesting the inverse of a Xform matrix from OpenGL

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

## Recommended Posts

glGet( GL_MODELVIEW, arrayOfFloats ); gives you the modelview matrix, how can you retrieve its inverse?

##### Share on other sites
No way, inverse it yourself!

Most of the time, GL_MODELVIEW is homogenous, so there is fast algorithm.

##### Share on other sites
As you've probably guessed, OpenGL doesn't offer any built-in functionality for getting the inverse matrix (that I know of).

The general inverse of a 4x4 matrix takes a little work, but if your transformations are limited to rotation, uniform scale, and translation, the inverse can be found at much less expense.

I'll take a shot at writing the code off the top of my head, but I make no guarantees about the correctness of the following:

float m[16];   // Use glGetFloat() to load the modelview matrix into mfloat inv[16]; // The inverse will go hereinv[0] = m[0];inv[1] = m[4];inv[2] = m[8];inv[4] = m[1];inv[5] = m[5];inv[6] = m[9];inv[8] = m[2];inv[9] = m[6];inv[10] = m[10];inv[12] = inv[0]*-m[12]+inv[4]*-m[13]+inv[8]*-m[14];inv[13] = inv[1]*-m[12]+inv[5]*-m[13]+inv[9]*-m[14];inv[14] = inv[2]*-m[12]+inv[6]*-m[13]+inv[10]*-m[14];inv[3] = 0.0f;inv[7] = 0.0f;inv[11] = 0.0f;inv[15] = 1.0f;

I easily may have screwed that up somewhere. But the general idea is that if your matrix is T*R, where R is rotation matrix and T is a translation matrix, then the inverse is R^-1*T^-1. R^-1 is simply the transpose of R, and T can be inverted by simply negating the translation. So for this particular subset of transformations, the inverse can be computed much more cheaply than in the general case.

##### Share on other sites
Thanks for the replies. I already have code in place to calculate the inverse of a compound transformation.

##### Share on other sites
inversing matrix for what use can anyone give me an example for it ?

##### Share on other sites
You inverse a matrix to transform the normals in a scene, but I can't remember why I wanted it..

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633334
• Total Posts
3011410
• ### Who's Online (See full list)

There are no registered users currently online

×