PDA

View Full Version : Camera class problem



openujs
09-20-2006, 08:05 AM
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) {
case GLUT_KEY_LEFT:
pCamera->position+= Vector3( -10.0,0,0.0 );
break;
case GLUT_KEY_RIGHT :
pCamera->position += Vector3( 10.0,0,0.0 );
break;
case GLUT_KEY_UP :
pCamera->position += Vector3( 0,0,10.0 );
break;
case GLUT_KEY_DOWN:
pCamera->position += Vector3( 0,0,-10.0 );
break;
}
glutPostRedisplay();
}

k_szczech
09-20-2006, 08:35 AM
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.

openujs
09-20-2006, 10:36 AM
sorry...here is my projection setting

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
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 )?

k_szczech
09-20-2006, 11:35 PM
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.

openujs
09-21-2006, 06:06 AM
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

k_szczech
09-22-2006, 12:54 AM
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.

openujs
09-22-2006, 05:18 AM
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-

#DEFINE M_PI 3.14

//Working
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));

}

k_szczech
09-22-2006, 05:38 AM
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;