0

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?

Shashi Mishra
  • 105
  • 2
  • 9

2 Answers2

2

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.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • +1 for noting that, for MV matrices, the inverse is usually an order of magnitude simpler to calculate than in the general case. –  Feb 07 '16 at 20:57
1

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

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • It didn't work in the sense that you didn't get the correct inverted matrix? Or you didn't get the desired result when using it for something? If you wrote an inversion algorithm, or got one from somewhere, I would always test it standalone. Feed it a few example matrices, and verify that it produces the correct inverse by checking that the product of the two results in the identity matrix. – Reto Koradi Apr 20 '14 at 21:19
  • 1
    @ShashiMishra: You might be thrown off by the way OpenGL (and most other graphics libraries) order their matrices: They're usually in column major order, i.e. first the elements from the 1st column, then the elements from the second and so on. However most people naively assume row major order, which transposes everything and reverses order of operations. – datenwolf Apr 20 '14 at 21:38