Camera class problem

I am trying to build a simple camera class, I am incrementing camera x pos on RIGHT key pressed and decrementing on LEFT key pressed. Same thing with UP and DOWN Keys.
when I move the camera, the object ( teapot) seems to be rotating left and right, however it is also zooming close and far from the camera. Not sure what is going on.
Following is my code.

position = Vector3(0.0,0.0,500.0);
lookAt = Vector3( 0.0, 0.0, 0.0 );
up = Vector3( 0.0, 1.0, 0.0 );

void keyPressed( key )
switch(key) {
pCamera->position+= Vector3( -10.0,0,0.0 );
pCamera->position += Vector3( 10.0,0,0.0 );
case GLUT_KEY_UP :
pCamera->position += Vector3( 0,0,10.0 );
pCamera->position += Vector3( 0,0,-10.0 );

You forgot to show us the part of code where you set up your projection and modelview matrices.
I assume you use gluLookAt, right?
In this case the reason is simple - camera moves from one position to another but always looks at the same point. You can solve this by setting the lookAt vector to position+forward every time you change the position of camera.
forward would be a vector: (0.0, 0.0, 1.0)
By rotating this forward vector you rotate the camera and by changing it’s length you can do simple zooming.

sorry…here is my projection setting

glMatrixMode( GL_PROJECTION );
gluPerspective( 50.0f, (GLfloat)width/(GLfloat)height,0.5f,1000.0f);
glMatrixMode( GL_MODELVIEW );

gluLookAt( pCamera->position.m_x, pCamera->position.m_y, pCamera->position.m_z,
	       pCamera->lookAt.m_x, pCamera->lookAt.m_y, pCamera->lookAt.m_z ,
		   0.0f, 1.0f, 0.0f

but question is…camera is not going forward when itis moving right and right. so why lookat = position + forward? and why forward is constant ( 0.0, 0.0, 1.0 )?

If it would be position = position + forward, then it would be strange, but it’s lookat = position + forward.
And as I said - forward vector is not constant - you can use it to rotate / zoom.
Please read my previous post again.

And another thing - you forgot to place glLoadIdentity before gluLookAt.

i still cant catch ur concept of lookat=position + forward for every camera move.

lets say i have object sitting in origin (0,0,0) and camera is at ( 0,0, 100). if i move camera forward ie forward vector( 0,0,-10), new position of the camera will be (0,0,90), so now you are saying position of the object ( lookAt) should be=positon+forward (0,0,90) + 0,0,-10 = 0,0,80. that makes object sitting right in front of you. Does that make sense?

Also if i move the camera to the right( 10,0,0 ), position of the camera will be (10,0,100), that means position of object( lookat) = position + forward( 10,0,100) + 10,0,0 = 20,0,100, that is way off for the object. please clarify. thanks

From your first post:

when I move the camera, the object ( teapot) seems to be rotating left and right, however it is also zooming close and far from the camera. Not sure what is going on.
Your lookAt vector is always (0, 0, 0) and this is where your teapot is, right?
So when you move the camera it will always rotate towards the teapot so you will not see teapot moving on the screen - instead you will see it rotating (you just look at it from different locations). You also se zooming, because when your camera moves, the distance to teapot changes.

I finally managed to make moveCamera() working, but the problem with rotateCamera(). in my rotateCamera(), I am moving camera left and right in a circle around the object, using polar coordinated concept. here is my code-


void CNextCamera::moveCamera( float speed )
position += Vector3( 0,0,speed );
lookAt += Vector3( 0,0, speed );


//Not working???WHATS WRONG!!!

void CNextCamera::rotateCamera( float speed )
//Using polar coordinates theory
float radius = position.m_z - lookAt.m_z;
position.m_x = position.m_x + (float)(radius* sin( speed * M_PI/180));
position.m_z = position.m_z + (float)(radius* cos( speed * M_PI/180));


CNextCamera::rotateCamera - this one is all messed up.
Do you want to rotate camera around lookAt point (some sort of 3rd person camera) or around position (1st person camera)?
Depending on what you want you should rotate position around lookAt or lookAt around position.
So, a pseudocode (V is a vector):

V = position - lookAt;
rotate(V, angle);
position = lookat + V;

Function rotate just rotates given vector around point (0,0,0) by given angle. Rotation around Z axis would look like this:

Vnew.x = Vold.x * cos(angle) + Vold.y * sin(angle);
Vnew.y = Vold.y * cos(angle) - Vold.x * sin(angle);
Vnew.z = Vold.z;