Calculate lookat vector from position and Euler angles
Posted
by Jaap
on Stack Overflow
See other posts from Stack Overflow
or by Jaap
Published on 2010-05-03T20:09:30Z
Indexed on
2010/05/03
21:58 UTC
Read the original article
Hit count: 296
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!
© Stack Overflow or respective owner