PDA

View Full Version : camera rotation



javalurnin
12-21-2009, 11:56 AM
How can I do the following:

Z = roll by a small angle around camera viewing axis (z roll in the opposite direction) = roll around the n axis in the uvn coord system

Y = yaw by a small angle around the camera y axis. i.e., the V axis in the uvn coord system. (y ywa in the opposite direction)

I want to do these rotations using my existing camera code...

extern Object *objs[];
extern int obj_count;

Camera::Camera()
{
eye = Vector3(0.0, 1.75, 15.0);
lookat = Vector3(0.0, 0.0, 0.0);
viewangle = 0.0;
}

void
Camera::orientMe()
{
lookat.c[0] = sin(viewangle);
lookat.c[2] = -cos(viewangle);
glLoadIdentity();
gluLookAt(eye.c[0], eye.c[1], eye.c[2],
eye.c[0] + lookat.c[0],
eye.c[1] + lookat.c[1],
eye.c[2] + lookat.c[2],
0.0, 1.0, 0.0);
}

void
Camera::moveMe(int dir)
{
double x = eye.c[0];
double z = eye.c[2];
eye.c[0] = eye.c[0] + dir*(lookat.c[0])*0.05;
eye.c[2] = eye.c[2] + dir*(lookat.c[2])*0.05;

for (int i=0; i<obj_count; i++)
{
if (objs[i]->inside(eye))
{
eye.c[0] = x;
eye.c[2] = z;
break;
}
}

glLoadIdentity();
gluLookAt(eye.c[0], eye.c[1], eye.c[2],
eye.c[0] + lookat.c[0],
eye.c[1] + lookat.c[1],
eye.c[2] + lookat.c[2],
0.0, 1.0, 0.0);
}

void
Camera::step(int dir)
{
double ex = eye.c[0];
double ez = eye.c[2];
double lx = lookat.c[0];
double lz = lookat.c[2];

if (eye.c[2] < lookat.c[2])
dir = -dir;

if (lookat.c[0] < 0)
eye.c[2] = eye.c[2] - dir*0.1;
else if (lookat.c[0] > 0)
eye.c[2] = eye.c[2] + dir*0.1;

eye.c[0] = eye.c[0] + dir*0.1;

lookat.c[0] = lookat.c[0] + dir*0.1;
lookat.c[2] = lookat.c[2] + dir*0.1;

for (int i=0; i<obj_count; i++)
{
if (objs[i]->inside(eye))
{
eye.c[0] = ex;
eye.c[2] = ez;
lookat.c[0] = lx;
lookat.c[2] = lz;
break;
}
}

glLoadIdentity();
gluLookAt(eye.c[0], eye.c[1], eye.c[2],
eye.c[0] + lookat.c[0],
eye.c[1] + lookat.c[1],
eye.c[2] + lookat.c[2],
0.0, 1.0, 0.0);

}

void
Camera::pitch(double dir)
{
lookat.c[1] += dir*0.075;
}