View Full Version : A question about gluLookat and projection matrix

04-23-2006, 02:09 AM
I want to get the gluLookAt parameters from glGetFloatv(GL_PROJECTION_MATRIX ,tmp).
I can get this value,but how can I calculate these parameters to the position of camera,target and up vector?
code like this:
float tmp[16];
Thanks a lot.


04-23-2006, 02:59 AM
normally the projection will not contain such camera information other than backplane/frontplane and fov/ortho size

transforms should be stored in modelview, as one typcially puts local transforms in the modelview as well, normally you cannot find out where the camera really is (unless the objects are not locally transformed).

however best is you familiarize yourself with matrices and what glulookat actually does to a matrix. (sample code is on the web, and probably on this forum as well, if you search)

04-23-2006, 03:44 AM
gluLookAt defines a visualization matrix not a projection matrix.

04-23-2006, 03:58 AM
Thank you,CrazyButcher and jide.
if I use glGetFloatv(GL_MODELVIEW_MATRIX ,tmp),and the objects are not locally transformed before,may I find the camera position exactlly?

04-23-2006, 09:21 AM
u would have to get the MODELVIEW matrix invert it and multiply the coordinate vector <0,0,0,1> to get the trnasformed position of the camera. For exaple , to place the camera at (0,0,5 ) u use the followinf

//this is why u need to invert it
glTranslatef(0,0,-5);Please correct me if i am wrong

04-23-2006, 10:59 AM
Yes, you can extract the camera position.
I needed such a function myself and put it my library. You can download from

It's called glhExtractLookAtFromMatrixf2

may I find the camera position exactlly?No, center3D will not be exactly as the orginal you send to gluLookAt, or in my libs case, glhLookAtf2

04-23-2006, 12:52 PM
Hi V-man
Will the method i suggested work ?
Do u use a similar method ?

04-24-2006, 02:11 AM
The simplest way is to just use the last column of the modelview matrix and negate it (the last column always contains the vector from the camera to the origin).

This is the same as the method proposed by gl_CodER, but with a lot less computation. It's actually not neccesary to do a full matrix invert, because you only need the last column of the inverted matrix.

04-24-2006, 10:53 AM
Originally posted by gl_CodER:
Hi V-man
Will the method i suggested work ?
Do u use a similar method ? The OP wants to get all the parameters for gluLookAt "position of camera,target and up".

The code in glhExtractLookAtFromMatrixf2 is something like 200 lines. I've forgotten why but I think it's because I take different branches depending on what is found in the matrix.
It basically tries to do the reverse of gluLookAt.
It's not a full matrix invert.

04-24-2006, 12:24 PM
Thanks everyone;
When I try to get GL_MODELVIEW_MATRIX parameters,I find that the matrix alway be like this:

Code is like this:
gluLookAt(CameraX,CameraY,CameraZ,TargetX,TargetY, TargetZ,0.0f,0.0f,1.0f);
float tmp[16];
I am sorry for my poor English,and thank you very much for your help, :)

04-24-2006, 12:32 PM
Thank you V-man,I think your approach can help me to solve this problem.

04-24-2006, 12:47 PM
include, you should do this: glMatrixMode(GL_MODELVIEW);
before you call gluLookAt.

BTW, once you've got your inverse modelview matrix:-
[0][1][2] is the X axis, so that's the vector going off to your right.
[4][5][6] is the Y axis, so that's the up vector.
[8][9][10] is the Z axis, so that's the 'view' vector, sometimes called the view direction.
[12][13][14] is the camera translation, sometimes called the position.