Calculate vector pointer from position and Euler angles

I have implemented an FPS style camera with a camera consisting of a position vector and Euler and yaw angles (x and y rotations). After setting up the projection matrix, I then translate to the camera coordinates by rotating and then translate to the reverse 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 have several viewports, each with its own camera, and from each camera I make the position of the other cameras (like a simple box). I would also like to draw a view vector for these cameras, but I don't know how to calculate the lookat vector from the Euler position and angles. I tried to multiply the original camera vector (0, 0, -1) by a matrix representing the camera rotation then adding the camera position to the transformed vector, but that doesn't work at all (most likely because I got 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();

      

I would like to draw a line segment from the camera in the direction of the view. I've looked all over the place for examples of this, but can't find anything.

Thank you very much!

+2


a source to share


1 answer


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

Basically, in order to draw a 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 brings the line out of the camera position 10 units in the direction of the view.

+4


a source







All Articles