tags:

views:

275

answers:

1

I've implemented an FPS style camera, with the camera consisting of a position vector, and Euler angles pitch and yaw (x and y rotations). After setting up the projection matrix, I then translate to camera coordinates by rotating, then translating to the inverse of the camera position:

// Load projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set perspective
gluPerspective(m_fFOV, m_fWidth/m_fHeight, m_fNear, m_fFar);

// Load modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// Position camera
glRotatef(m_fRotateX, 1.0, 0.0, 0.0); 
glRotatef(m_fRotateY, 0.0, 1.0, 0.0); 
glTranslatef(-m_vPosition.x, -m_vPosition.y, -m_vPosition.z);

Now I've got a few viewports set up, each with its own camera, and from every camera I render the position of the other cameras (as a simple box). I'd like to also draw the view vector for these cameras, except I haven't a clue how to calculate the lookat vector from the position and Euler angles. I've tried to multiply the original camera vector (0, 0, -1) by a matrix representing the camera rotations then adding the camera position to the transformed vector, but that doesn't work at all (most probably because I'm way off base):

vector v1(0, 0, -1);
matrix m1 = matrix::IDENTITY;
m1.rotate(m_fRotateX, 0, 0);
m1.rotate(0, m_fRotateY, 0);

vector v2 = v1 * m1;
v2 = v2 + m_vPosition; // add camera position vector

glBegin(GL_LINES);
glVertex3fv(m_vPosition);
glVertex3fv(v2);
glEnd();

What I'd like is to draw a line segment from the camera towards the lookat direction. I've looked all over the place for examples of this, but can't seem to find anything.

Thanks a lot!

+3  A: 

I just figured it out. When I went back to add the answer, I saw that Ivan had just told me the same thing :)

Basically, to draw the camera vector, I do this:

glPushMatrix();

// Apply inverse camera transform
glTranslatef(m_vPosition.x, m_vPosition.y, m_vPosition.z);
glRotatef(-m_fRotateY, 0.0, 1.0, 0.0); 
glRotatef(-m_fRotateX, 1.0, 0.0, 0.0); 

// Then draw the vector representing the camera
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, -10);
glEnd();

glPopMatrix();

This draws a line from the camera position for 10 units in the lookat direction.

Jaap