# Nehe's Quaternion camera question

This topic is 3092 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone here tell me where did he get his formula? Did he make it or is it the standard way to convert for OpenGL use. http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class http://www.opengl.org/sdk/docs/man/xhtml/glMultMatrix.xml
// First row
pMatrix[ 0] = 1.0f - 2.0f * ( m_y * m_y + m_z * m_z );
pMatrix[ 1] = 2.0f * (m_x * m_y + m_z * m_w);
pMatrix[ 2] = 2.0f * (m_x * m_z - m_y * m_w);
pMatrix[ 3] = 0.0f;

// Second row
pMatrix[ 4] = 2.0f * ( m_x * m_y - m_z * m_w );
pMatrix[ 5] = 1.0f - 2.0f * ( m_x * m_x + m_z * m_z );
pMatrix[ 6] = 2.0f * (m_z * m_y + m_x * m_w );
pMatrix[ 7] = 0.0f;

// Third row
pMatrix[ 8] = 2.0f * ( m_x * m_z + m_y * m_w );
pMatrix[ 9] = 2.0f * ( m_y * m_z - m_x * m_w );
pMatrix[10] = 1.0f - 2.0f * ( m_x * m_x + m_y * m_y );
pMatrix[11] = 0.0f;

// Fourth row
pMatrix[12] = 0;
pMatrix[13] = 0;
pMatrix[14] = 0;
pMatrix[15] = 1.0f;



##### Share on other sites
That's the standard method for building a matrix from a quaternion - you'll find it in pretty much any math library, and in any reference on the subject.

The algorithm itself isn't specific to OpenGL or any other API, but for a given implementation you do have to make sure that the code matches the conventions of the API being used (e.g. row- or column-vector notation, row- or column-major matrix layout, etc.).

##### Share on other sites
Now, I got it. Thank you jyk. I found it here. http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

I have another question. Does including it on my project will really make my ass pain defending it? What if they ask me on how did I derive that formula? Is there any book or site that will help me how the formula went out.

##### Share on other sites
Quote:
 I have another question. Does including it on my project will really make my ass pain defending it? What if they ask me on how did I derive that formula? Is there any book or site that will help me how the formula went out.
Are you asking if it's ok to use the formula in your projects without getting explicit permission from anyone? If so, the answer is yes, it's fine: it's common knowledge, and can be found in literally hundreds (maybe even thousands) of math libraries and game code bases. I'm sure there's nothing for you to worry about.

Deriving it oneself can still be a good exercise though. There's more than one way to derive the formula, but here's a fairly straightforward method. Start with the formula for rotating a vector v by a quaternion q (using standard quaternion multiplication order):
v' = q * v * conjugate(q);
Note that for the purpose of this formula, the vector v must be represented as a quaternion (the x, y, and z elements of v are assigned to the x, y, and z elements of the quaternion, and the 'w' element of the quaternion is typically given a value of zero).

Now, work through the multiplications longhand until you have a value for each element of v'. If you then express the result in the form of a matrix-vector multiplication, you will have the matrix that you posted earlier.

[Edit: A couple of places you can find derivations are in the book '3D Math Primer', and in the articles on quaternions in one of the first two Game Programming Gems books.]

• 36
• 15
• 9
• 23
• 10