# A question about gluLookat and projection matrix

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];
glGetFloatv(GL_PROJECTION_MATRIX ,tmp);
////////////////////////////
Thanks a lot.

Include

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)

gluLookAt defines a visualization matrix not a projection matrix.

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?

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
code

``````glLoadIdentity();
//this is why u need to invert it
glTranslatef(0,0,-5);
``````

Please correct me if i am wrong

Yes, you can extract the camera position.
I needed such a function myself and put it my library. You can download from
http://www.geocities.com/vmelkon/glhlibrary.html

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

Hi V-man
Will the method i suggested work ?
Do u use a similar method ?

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.

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.

Thanks everyone;
When I try to get GL_MODELVIEW_MATRIX parameters,I find that the matrix alway be like this:
1,0,0,0
0,1,0,0
0,0,1,0
0,0,0,1

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

Thank you V-man,I think your approach can help me to solve this problem.

include, you should do this: glMatrixMode(GL_MODELVIEW);