# How to get camera position?

This topic is 4419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do I determine the camera position when my scene is drawn from the following Point-of-view?: glLoadIdentity(); glTranslatef(0.0f, 0.0f, -20.0f); glRotatef(+g_mouse_z, 0.0f, 0.0f, 1.0f ); glRotatef(-g_mouse_y, 1.0f, 0.0f, 0.0f ); glRotatef(-g_mouse_x, 0.0f, 1.0f, 0.0f ); Th8ks peeps!

##### Share on other sites
It's the arguments to the first glTranslate, negated. In this case, the camera is at (0,0,20).

Note that, given a MODELVIEW matrix, it's impossible to tell where the camera is, and where the object is, as they've been multiplied together. However, in eye space (post-MODELVIEW), the camera is always at (0,0,0).

You might want to look at My simple camera tutorial.

##### Share on other sites
So then how can I track the camera position when the mouse is moved?

##### Share on other sites
maintain the information in your own variables/code...

##### Share on other sites
In my own code I keep track of it, but I get the info from OpenGL. I don't remember why, precisely, but it appears to work.

    GLfloat mdl[16];    float camera_org[3];    glGetDoublev(GL_MODELVIEW_MATRIX, mdl);    camera_org[0] = -(mdl[0] * mdl[12] + mdl[1] * mdl[13] + mdl[2] * mdl[14]);    camera_org[1] = -(mdl[4] * mdl[12] + mdl[5] * mdl[13] + mdl[6] * mdl[14]);    camera_org[2] = -(mdl[8] * mdl[12] + mdl[9] * mdl[13] + mdl[10] * mdl[14]);

It appears given the modelview matrix (which is an array of 16 floating point values), the origin is the negative of each axis component scaled by the matrices corresponding translation component. Or something. I don't pretend to completely understand it, but it seems to work for me.

##### Share on other sites
Quote:
 Original post by WudanIt appears given the modelview matrix (which is an array of 16 floating point values), the origin is the negative of each axis component scaled by the matrices corresponding translation component. Or something. I don't pretend to completely understand it, but it seems to work for me.

What you got there is the collapsed form of applying the negative translation matrix to the rotation matrix (which only works if you stay away from scaling). Or the other way round. Or the transposed (in this case the same as inversed) rotation matrix. It's just been too long. Plus I'd probably apply the inverse rotation and translation in the INVERSE order to set up the view.. if I wouldn't have packed all that stuff in a camera class long ago and never touched it again.

##### Share on other sites
Thanks heaps everyone! Wudan, your snippet worked like a gem. A huge Thanks to you (++rating).

1. 1
2. 2
Rutin
22
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631735
• Total Posts
3001941
×