Camera in OpenGL
Ok, I am simulating a camera in OpenGL by using a class to store the camera''s x, y, z, xang, yang, and zang. My question is this: To ''move'' my camera do I just translate and rotate according to my camera values before I draw my scene, and if so, do I translate the camera first then rotate it or vice versa? I tried rotating first:
glRotatef(camera.xang, 1.0f, 0.0f, 0.0f);
glRotatef(camera.yang, 0.0f, 1.0f, 0.0f);
glRotatef(camera.zang, 0.0f, 0.0f, 1.0f);
glTranslatef(camera.x, camera.y, camera.z);
But when I ''move'' the camera forward by incrementing the camera.z value it moves along the original z axis--not forward unless I am facing straight down. When I translate first:
glTranslatef(camera.x, camera.y, camera.z);
glRotatef(camera.xang, 1.0f, 0.0f, 0.0f);
glRotatef(camera.yang, 0.0f, 1.0f, 0.0f);
glRotatef(camera.zang, 0.0f, 0.0f, 1.0f);
The camera moves forward correctly but instead of the scene rotating aroudn the camera, it rotates around the origin, which is not where I am. If this does not make sense I am sorry, but this is driving me nuts!
Consider using gluLookAt() instead of computing your own transformations. In that function you specify the camera position, the coordinate that the camera is looking at and in what direction the cameras up vector is pointing.
If you must do your own transformations, you need to transform the world coordinates to your camera coordinates and not the other way around.
glTranslatef(-camera.x, -camera.y, -camera.z);
glRotatef(-camera.xang, 1, 0, 0);
glRotatef(-camera.yang, 0, 1, 0);
glRotatef(-camera.zang, 0, 0, 1);
And remember to always apply these transformation to the GL_MODELVIEW matrix, not GL_PROJECTION.
If you want to move the camera forward, you must first compute the direction the camera is facing. And then add this vector to your camera position. Another reason for using the gluLookAt scheme.
Compute the view direction by multiplying a vector (0,0,-1) with the inverse of above matrix. I believe OpenGL uses a right handed coordinate system where the z axis points out from your screen hence you will use (0,0,-1) to move into the screen.
Hope this helps =)
If you must do your own transformations, you need to transform the world coordinates to your camera coordinates and not the other way around.
glTranslatef(-camera.x, -camera.y, -camera.z);
glRotatef(-camera.xang, 1, 0, 0);
glRotatef(-camera.yang, 0, 1, 0);
glRotatef(-camera.zang, 0, 0, 1);
And remember to always apply these transformation to the GL_MODELVIEW matrix, not GL_PROJECTION.
If you want to move the camera forward, you must first compute the direction the camera is facing. And then add this vector to your camera position. Another reason for using the gluLookAt scheme.
Compute the view direction by multiplying a vector (0,0,-1) with the inverse of above matrix. I believe OpenGL uses a right handed coordinate system where the z axis points out from your screen hence you will use (0,0,-1) to move into the screen.
Hope this helps =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement