How to get the camera's position in 3d space?

I need to get the cameras position in x,y and z coordinates. How can I get this information? I don’t find
any method for this.

I am programming a 3d space game. It is possible to move around in the 3d space with the arrow keys. glTranslatef and glRotatef calls moves the camera when the user wants to move. So the camera 's x,y and z coordinates is changed when the user presses the arrow keys.

By definition in OpenGL, the camera is always in (0,0,0).
You then change the modelview matrix to “move the world” around the camera.

So you have to keep track yourself of where in your virtual world your camera is.

What you need is a better way to handle your movement so it isn’t so complicated to keep track of where you are.

In most 3D programming, you make sure to build the modelview matrix from scratch each frame (starting with a call to glLoadIdententiy() ), using the stored position and heading of the camera. A common way is to have 3 vectors, position, up, and direction defining your camera position, and calculate your matrix from that. (basically doing a change of basis in linear algebra)

It gets a slightly trickier to move it around, you have to work with vectors instead of just adds.
But its easier in the long run. It quickly gets confusing if you try to save a matrix between frames, and for not much gain.

Another tip is the gluLookAt function from the gl-utilities.
You can find those here: … tutilities

Can also recommend the PoverVR SDKs matrix classes for all your 3d linear algebra needs.
Find those under the “implementation” tab on the above link.

Note: I say “from scratch” but usually you have parts of it cached of course, but the point is to not let it accumulate over frames, but rebuild it from identity every frame.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.