Link to home
Start Free TrialLog in
Avatar of chrishughes
chrishughes

asked on

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,

Chris
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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] ) ;