• Advertisement
Sign in to follow this  

glRotate, glTranslate, rotations...

This topic is 3386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Guys, I really need someone to explain this to me like I am an idiot. I need my camera to rotate on it's x-axis and y-axis, or pitch and yaw, respectively. I have a camera class that remembers the position, view vectors, and pitch/yaw angles. When I want to move forward or backward, I apply a translation: mPos is the the camera location vector, mView is the point we are looking at, and vector is the view vector (mView-mPos) mPos.x = mPos.x + vector.x * speed; mView.x = mView.x + vector.x * speed; mPos.z = mPos.z + vector.z * speed; mView.z = mView.z + vector.z * speed; mPos.y = mPos.y + vector.y * speed; mView.y = mView.y + vector.y * speed; For a rotation, I incrememt the pitch/yaw and apply one of the following rotation matrices, depending on the axis of rotation: mView.y = (float)(mPos.y + cos(angle)*vector.y - sin(angle)*vector.z); mView.z = (float)(mPos.z + sin(angle)*vector.y + cos(angle)*vector.z); mView.z = (float)(mPos.z + sin(angle)*vector.x + cos(angle)*vector.z); mView.x = (float)(mPos.x + cos(angle)*vector.x - sin(angle)*vector.z); In my displayFunc callback, I apply the following transformations: glRotatef(c.pitch,1,0,0); glRotatef(c.yaw,0,1,0); glTranslatef(-c.mPos.x,-c.mPos.y,-c.mPos.z); So, basically, when I rotate, I rotate in the right direction, but the camera moves in tiny circles in the desired direction. Then, depending on where I am in the rotation of the "tiny circle" I am, the "forward/backward" direction of the camera will be wrong. I think my calculations of the view vectors positions is messed up. My math is not great, so please help in any way you can. I hope I have given sufficient info. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Maybe this will help:

class Camera						// manage camera position
{
private:


public:
vector3d mPos;
vector3d mView;
vector3d mUp;

float pitch;
float yaw;

Camera() { pitch = 0; yaw = 0; }
void position(float pos_x, float pos_y, float pos_z,
float view_x, float view_y, float view_z,
float up_x, float up_y, float up_z)
{
mPos = vector3d(pos_x, pos_y, pos_z ); // set position
mView = vector3d(view_x, view_y, view_z); // set view
mUp = vector3d(up_x, up_y, up_z ); // set the up vector
}


void move(float speed)
{
vector3d vector = mView - mPos; // Get the view vector

// apply translation matrix

mPos.x = mPos.x + vector.x * speed;
mView.x = mView.x + vector.x * speed;
mPos.z = mPos.z + vector.z * speed;
mView.z = mView.z + vector.z * speed;
mPos.y = mPos.y + vector.y * speed;
mView.y = mView.y + vector.y * speed;


}

void rotate(float angle, float x, float y, float z)
{
vector3d vector = mView - mPos;

if (x == 1)
{
if (pitch < 360)
pitch += angle;
else
pitch = angle;

mView.y = (float)(mPos.y + cos(angle)*vector.y - sin(angle)*vector.z);
mView.z = (float)(mPos.z + sin(angle)*vector.y + cos(angle)*vector.z);
}
else if ( y == 1)
{
if (yaw < 360)
yaw += angle;
else
yaw = angle;

mView.z = (float)(mPos.z + sin(angle)*vector.x + cos(angle)*vector.z);
mView.x = (float)(mPos.x + cos(angle)*vector.x - sin(angle)*vector.z);



}

return;
}



...and here is my display callback:


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// position camera
glPushMatrix();
//gluLookAt(p.c.mPos.x,p.c.mPos.y, p.c.mPos.z,p.c.mView.x,p.c.mView.y, p.c.mView.z, p.c.mUp.x, p.c.mUp.y, p.c.mUp.z);



glRotatef(p.c.pitch,1,0,0);
glRotatef(p.c.yaw,0,1,0);
glTranslatef(-p.c.mPos.x,-p.c.mPos.y,-p.c.mPos.z);


// draw plane
glPushMatrix();
glLoadIdentity();
p.render();
glPopMatrix();

// draw world and apply camera transformation
w.render();
glPopMatrix();

glFlush();
glutSwapBuffers();
return;
}

Share this post


Link to post
Share on other sites
If you are using eye/aim/up vectors then use gluLookAt.

Study this demo.
http://www.morrowland.com/apron/tutorials/gl/gl_camera_4.zip

Share this post


Link to post
Share on other sites
I was using gluLookAt, but I couldnt get the X-axis rotation to work properly. That's why I changed it.

This tutorial is not helping me. I must be missing something. It does not illustrate rotation about the x-axis and then movement along the direction vector created by that rotation.

By the way, this is for a flight sim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement