[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

Reversing gluLookAt()

Hi Guys,

I am struggling a little bit with transformation matricies. I am trying to find a solution that will let me derive values for "eye position" "look at point" "up vector" from a transformation matrix. I know that gluLookAt will let me convert the other way, but I cant find a way to go back!

Many Thanks,

  • 4
1 Solution
I don't believe that you can reverse all of that info.

gluLookAt performs the following operation to produce the transformation matrix:

     | centerX - eyeX |
f = | centerY - eyeY |
     | centerZ - eyeZ |

f = f / ||f||
UP' = UP / ||UP||
u = (f x UP) x f

The transformation matrix, M:

       |  s[0]   s[1]   s[2]   0  |
M = |  u[0]   u[1]   u[2]   0  |
       | -f[0]  -f[1]   -f[2]    0  |
       |    0      0        0      1  |

As you can see, the only data regarding the center and eye positions are lost, as it's the *difference* between these values that is used in the matrix....

Futhermore, the 'difference' values are then normalised, so you probably wouldn't even be able to retrieve that much, for sure.
You can reverse the Modelview matrix in order to extract the required camera information.

Firstly, retrieve the Modelview matrix:

  GLfloat viewMatrix[16] ;
  glGetFloatv( GL_MODELVIEW_MATRIX, viewMatrix ) ;

Then from this, you can produce the right and up vector components:

  // assuming that you have a vector class called vector3:
  vector3 right( viewMatrix[0], viewMatrix[4], viewMatrix[8] ) ;
  vector3 up   ( viewMatrix[1], viewMatrix[5], viewMatrix[9] ) ;

And also the eye position:

  vector3 eye ( -viewMatrix[3], -viewMatrix[6], -viewMatrix[9] ) ;

So long as the Right and Up vectors are normalized (which OpenGL does automatically), then the Look At vector is simply the Cross Product between the Right and Up vectors, defined as:

   vector3 at  ( right.y*up.z - right.x*up.y, right.z*up.x - right.x*up.z, right.x*up.y - right.y*up.x ) ;

So by mapping a transformation to the modelview matrix, you can deduce these properties.
Sorry, this line:

  vector3 eye ( -viewMatrix[3], -viewMatrix[6], -viewMatrix[9] ) ;

Should be:

  vector3 eye ( -viewMatrix[3], -viewMatrix[7], -viewMatrix[11] ) ;

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now