Sign in to follow this  
skyfire360

NeHe Camera Class

Recommended Posts

I'm working on a quaternion camera of my own and would like to use the approach that NeHe used in his camera class. I understand what quaternions are, what they are used for, and how they store their information. However, I only understand what actually is going on in the SetPerspective function, and not why it happens. Here's NeHe's SetPerspective funcion:
void glCamera::SetPrespective()
{
	GLfloat Matrix[16];
	glQuaternion q;

	// Make the Quaternions that will represent our rotations
	m_qPitch.CreateFromAxisAngle(1.0f, 0.0f, 0.0f, m_PitchDegrees);
	m_qHeading.CreateFromAxisAngle(0.0f, 1.0f, 0.0f, m_HeadingDegrees);
	
	// Combine the pitch and heading rotations and store the results in q
	q = m_qPitch * m_qHeading;
	q.CreateMatrix(Matrix);

	// Let OpenGL set our new prespective on the world!
	glMultMatrixf(Matrix);
	
	// Create a matrix from the pitch Quaternion and get the j vector 
	// for our direction.
	m_qPitch.CreateMatrix(Matrix);
	m_DirectionVector.j = Matrix[9];

	// Combine the heading and pitch rotations and make a matrix to get
	// the i and j vectors for our direction.
	q = m_qHeading * m_qPitch;
	q.CreateMatrix(Matrix);
	m_DirectionVector.i = Matrix[8];
	m_DirectionVector.k = Matrix[10];

	// 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);
}

For instance, why do you multiply pitch then heading to get the world-to-camera matrix then multiply the heading by the pitch to get the up and forward direction vectors? Are the two matricies that result inverses of each other? What do these two matricies that result from 'q' represent?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this