Shashi Mishra
Shashi Mishra

Reputation: 115

how to get inverse of modelview matrix?

I transformed a vector by multiplying it with the Model View matrix.

glGetDoublev( GL_MODELVIEW_MATRIX, cam );
newX=cam[0]*tx+cam[1]*ty+cam[2]*tz+cam[3];
newY=cam[4]*tx+cam[5]*ty+cam[6]*tz+cam[7];
newZ=cam[8]*tx+cam[9]*ty+cam[10]*tz+cam[11];

How to do I get the inverse of model view matrix for the reverse transformation?

Upvotes: 0

Views: 3166

Answers (2)

datenwolf
datenwolf

Reputation: 162327

Well, eh, by inverting the matrix. It's a standard mathematical procedure you can find in literally every textbook on linear algebra. All matrix math libraries have a function for inverting a matrix. OpenGL is not a matrix math library, so it doesn't have a function for that. Look at libraries like GLM, Eigen or linmath.h

Upvotes: 1

Reto Koradi
Reto Koradi

Reputation: 54642

What you have in your posted code isn't the correct way of applying the MODELVIEW matrix. OpenGL uses column major order for matrices. Your calculation would need to look like this:

newX=cam[0]*tx+cam[4]*ty+cam[8]*tz+cam[12];
newY=cam[1]*tx+cam[5]*ty+cam[9]*tz+cam[13];
newZ=cam[2]*tx+cam[6]*ty+cam[10]*tz+cam[14];

For the inverted matrix, you can just use a generic matrix inversion algorithm, as @datenwolf already suggested. If you can make certain assumptions about the matrix, there are easier and more efficient methods. A typical case is that you know that your transformation matrix was composed of only rotations, translations, and scaling. In that case, you can pick the matrix apart into these components, invert each, and reassemble them into the inverted matrix. Since rotations, translations and scaling are all trivial to invert, that can be done with very simple math.

Upvotes: 2

Related Questions