PDA

lgc_ustc
01-04-2004, 07:02 PM
Hi,all,

I have recently made a camera class for my project. Although all the code has been written and it works perfectly, I still have some questions. The key code section in the class is from http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class, and it looks like:

void glCamera::SetPrespective()
{
GLfloat Matrix[16];

glRotatef(m_PitchDegrees, 1.0f, 0.0f, 0.0f);

glGetFloatv(GL_MODELVIEW_MATRIX, Matrix);

m_DirectionVector.i = Matrix[8];
m_DirectionVector.k = Matrix[10];

glRotatef(m_PitchDegrees, 1.0f, 0.0f, 0.0f);

glGetFloatv(GL_MODELVIEW_MATRIX, Matrix);
m_DirectionVector.j = Matrix[9];

// Scale the direction by our speed.
m_DirectionVector *= m_ForwardVelocity;

// Increment our position by the vector
m_Position.x += m_DirectionVector.i;
m_Position.y += m_DirectionVector.j;
m_Position.z += m_DirectionVector.k;

// Translate to our new position.
glTranslatef(-m_Position.x, -m_Position.y, m_Position.z);
}

This code sets up the model/view matrix using the camera's heading and pitch degrees. It works perfectly. What I do not get is the way it gets the direction vector from the model/view matrix using glGetFloat. Can anybody explain this a little bit or introduce some theoretic materials? Can I get the up vector in the similar way? Thanks a lot. http://www.opengl.org/discussion_boards/ubb/smile.gif

mogumbo
01-07-2004, 08:19 AM
The upper-left 3x3 submatrix inside a 4x4 transformation matrix is really just 3 vectors. In the identity matrix you have this 3x3 submatrix:

1 0 0
0 1 0
0 0 1

which is really 3 vectors, one pointing straight down the x-axis, one pointing down the y-axis, and another pointing down the z-axis. If you apply rotations to this matrix, you end up with vectors that point in other directions.

Your code fragment is just using the z-axis component as the direction vector (components 8, 9 and 10). So the up vector would be the y-axis (components 4, 5, and 6).

lgc_ustc
01-07-2004, 10:48 PM