tester
tester

Reputation: 425

OpenGL camera rotation using gluLookAt

I am trying to use gluLookAt to implement an FPS style camera in OpenGL fixed function pipeline. The mouse should rotate the camera in any given direction.

I store the position of the camera:

float xP;
float yP;
float zP;

I store the look at coordinates:

float xL;
float yL;
float zL;

The up vector is always set to (0,1,0)

I use this camera as follows: gluLookAt(xP,yP,zP, xL,yL,zL, 0,1,0);

I want my camera to be able to be able to move along the yaw and pitch, but not roll.

After every frame, I reset the coordinates of the mouse to the middle of the screen. From this I am able to get a change in both x and y.

How can I convert the change in x and y after each frame to appropriately change the lookat coordinates (xL, yL, zL) to rotate the camera?

Upvotes: 1

Views: 967

Answers (1)

Dorota Kadłubowska
Dorota Kadłubowska

Reputation: 675

Start with a set of vectors:

fwd = (0, 0, -1);
rht = (1, 0, 0);
up = (0, 1, 0);

Given that Your x and y, taken from the mouse positions You mentioned, are small enough You can take them directly as yaw and pitch rotations respectively. With yaw value rotate the rht and fwd vectors over the up vector, than rotate fwd vactor over the rht with pitch value. This way You'll have a new forward direction for Your camera (fwd vactor) from which You can derive a new look-at point (L = P + fwd in Your case).

You have to remember to restrict pitch rotation not to have fwd and up vectors parallel at some point. You can prevent that by recreating the up vector every time You do pitch rotation - simply do a cross product between rht and fwd vactors. A side-note here though - this way up will not always be (0,1,0).

Upvotes: 2

Related Questions