Advertisement

NeHe Camera Class

Started by November 23, 2004 12:07 PM
-1 comments, last by skyfire360 20 years ago
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?
I do real things with imaginary numbers

This topic is closed to new replies.

Advertisement