Jump to content
  • Advertisement
Sign in to follow this  
bah

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.

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 m
float inv[16]; // The inverse will go here

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


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
inversing matrix for what use can anyone give me an example for it ?

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!