Convert Axis Angle to 4x4 homogeneous matrix?

Click For Summary
SUMMARY

The discussion focuses on converting an axis-angle representation to a 4x4 homogeneous matrix using OpenGL. The user shares code snippets for creating a quaternion from an axis-angle input and subsequently forming a 4x4 matrix. The key formulas for the matrix creation are derived from quaternion mathematics, specifically utilizing the components of the quaternion (m_x, m_y, m_z, m_w) to populate the matrix. The user successfully resolves their query after reviewing the provided resources.

PREREQUISITES
  • Understanding of quaternion mathematics and representation
  • Familiarity with OpenGL matrix operations
  • Knowledge of C++ programming language
  • Basic trigonometry, specifically sine and cosine functions
NEXT STEPS
  • Study "OpenGL Matrix Transformations" for deeper insights into matrix operations
  • Explore "Quaternion Mathematics" to understand the underlying principles of quaternion rotation
  • Learn about "GLM (OpenGL Mathematics)" for practical applications in graphics programming
  • Review "3D Graphics Programming" resources to enhance knowledge of transformations in 3D space
USEFUL FOR

Game developers, graphics programmers, and anyone working with 3D transformations in OpenGL will benefit from this discussion.

sarah22
Messages
26
Reaction score
0
I'm stuck on how did the author managed to convert the axis angle to a 4x4 homogenous matrix.

Here's the code below. The bolded ones are the one I'm struggling at right now.

void glQuaternion::CreateMatrix(GLfloat *pMatrix)
{
// Make sure the matrix has allocated memory to store the rotation data
if(!pMatrix) return;

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

// Now pMatrix[] is a 4x4 homogeneous matrix that can be applied to an OpenGL Matrix
}

Before he use the code above. He converted the axis angle to quaternion first which is the one below. I somehow managed to understand this one though. (http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm")

void glQuaternion::CreateFromAxisAngle(GLfloat x, GLfloat y, GLfloat z, GLfloat degrees)
{
// First we want to convert the degrees to radians
// since the angle is assumed to be in radians
GLfloat angle = GLfloat((degrees / 180.0f) * PI);

// Here we calculate the sin( theta / 2) once for optimization
GLfloat result = (GLfloat)sin( angle / 2.0f );

// calculate the w value by cos( theta / 2 )
m_w = (GLfloat)cos( angle / 2.0f );

// Calculate the x, y and z of the quaternion
m_x = GLfloat(x * result);
m_y = GLfloat(y * result);
m_z = GLfloat(z * result);
}

So my question is, how did he arrived to that formula (the bold one on the first code) ? If possible you can give me some links on how did he get that?

I'm looking all over the google but didn't get one. Maybe my tags were wrong.

Source for the code above: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class

Thank you
Sarah
 
Last edited by a moderator:
Physics news on Phys.org
nevermind. I got it.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
18K
Replies
1
Views
2K