How to get camera position?

Started by
5 comments, last by sjf 17 years, 10 months ago
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!
Advertisement
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.
enum Bool { True, False, FileNotFound };
So then how can I track the camera position when the mouse is moved?
maintain the information in your own variables/code...
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.
Quote:Original post by Wudan
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.


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.
f@dzhttp://festini.device-zero.de
Thanks heaps everyone! Wudan, your snippet worked like a gem. A huge Thanks to you (++rating).

This topic is closed to new replies.

Advertisement