• 9
• 9
• 11
• 12
• 9

# 1st person camera without gluLookAt

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

## Recommended Posts

I'm trying to program a first person camera without gluLookAt. Currently, i'm using the pilotView method given in the red book: void pilotView() { glRotated(pitch, 0.0, 1.0, 0.0); glRotated(heading, 1.0, 0.0, 0.0); glTranslated(-camx, -camy, -camz); } My keyboard function is this: void myGlutKeyboardCallback(unsigned char key, int x, int y) { if(key =='a') { camx--; } if(key == 's') { camz++; } if(key == 'd') { camx++; } if(key == 'w') { camz--; } if(key =='r') { camx = 0.0, camy = 10, camz = 50; } if(key == 27) { exit(0); } glutPostRedisplay(); } The problem is that if i'm not facing down the z=0 axis, the translate command obviously doesn't work properly since it has no knowledge or the direction the camera is facing. Can someone help me out?

##### Share on other sites
You are increasing/decreasing the position always in parallel to the _global_ space axes. But what you want to do (presumbly since you speak from gluLookAt) is increasing/decreasing it in camera local axes meaning.

Hence you could do 1 of 3 equivalent things:

(1) After computing the rotation matrix of the camera, extract the appropriate basis vector from it, scale it by the amount of motion, and add it to the position vector (defined in global space). Render as usual.

You probably know that the rotation matrix could be understood as a collection of the "side", "up", and "depth" or "look-at" vectors. So it is possible to get the 3 local principal direction expressed in the super-ordinated co-ordinate system from it (e.g. the local axes expressed in the global space).

(2) Use a local motion vector like [+/-1, 0, 0] for left/right, transform it with the camera's transformation matrix, and add the result to the camera's position vector (defined in global space). Render as usual.

(3) Use a local motion vector like [+/-1, 0, 0] for left/right and add it to the camera's position vector (defined in local space). Render after transforming it with the camera's transformation matrix.

Although these methods will consider the camera's orientation, they doesn't prevent you from diving into the ground or flying into the sky (just "pilotView"). A much different method is to be used to actually move on a ground.

##### Share on other sites
Ok, 11 views and no replies. Have i not explained this correctly or something? :S

##### Share on other sites
Thank you haegarr. However, its the extracting the view vector that i'm having trouble with.... i don't know how to do it! I'm useless at maths...

##### Share on other sites
Build a matrix from the both rotation angles you have, namely "pitch" and "heading". For this purpose you need a 3x3 matrix, but working with a 4x4 homogeneous matrix is also okay. Have you a library for that stuff at hand?

If the matrix is build as usual for OpenGL, it is a 4x4 matrix stored as a 1-dimensional array like here (please notice the indexing):
m[00] m[04] m[08] m[12]m[01] m[05] m[09] m[13]m[02] m[06] m[10] m[14]m[03] m[07] m[11] m[15]

In this scheme, m[00] to [m02] denote the 3 components of the "right" vector, m[04] to m[06] the "up" vector, and m[08] to m[10] the "towards" vector (notice that using "look-at" implies the negative of the "towards" vector, since OpenGL looks along the _negative_ z axis). If you are using a 3x3 matrix only then it is the same as the upper left 3x3 sub-matrix in the above scheme.

Furthur help could be given only if you tell us about the matrix math implementation you are using (or are about to use).