Search code examples
matrixrotationglm-mathfbx

Set matrix rotation in glm::mat4


I'm trying to port some code from Autodesk FBX SDK to use glm instead. The Autodesk SDK has a class called FbxAMatrix (FBX affine matrix class - similiar to glm::mat4) which provides the funtion:

void SetR(const FbxVector4& pR); /* Set matrix's Euler rotation. X, Y and Z rotation values expressed as a vector. The rotation transform is constructed in rotation order XYZ. */

This function is supposed to set the rotation - while glm::rotate() will just rotate the matrix. In my understanding, these are two different operations - and if I run both functions with same inputs, the result is different too. The same problem happens, if I try to use glm::eulerAngleXYZ() instead.

IdentityMatrix:

{ 1, 0, 0, 0 }
{ 0, 1, 0, 0 }
{ 0, 0, 1, 0 }
{ 0, 0, 0, 1 }

RotationVector:

{ 15 }
{ 0 }
{ 0 }

FbxAMatrix::IdentityMatrix.SetR(RotationVector):

 { 1, 0, 0, 0 }
 { 0, 0.96592582628906831, 0.25881904510252074, 0 } 
 { 0, 0.25881904510252074, 0.96592582628906831, 0 } 
 { 0, 0, 0, 1 }

glm::rotate(IdentityMatrix, 1.0f, RotationVector):

 { 1, 0, 0, 0 }
 { 0, 0.540302277, 0.841470957, 0 }
 { 0, 0.841470957, 0.540302277, 0 }
 { 0, 0, 0, 1 }

glm::eulerAngleXYZ(RotationVector):

 { 1, 0, 0, 0 }
 { 0, 0.759687901, 0.650287867, 0 }
 { 0, 0.650287867, 0.759687901, 0 }
 { 0, 0, 0, 1 }

So my question is: How can I set the rotation of an matrix with glm (to get smiliar results to FbxAMatrix.SetR(RotationVector))? If there is no available function, I would like to understand the difference between rotate the identity matrix and set the rotation of the identity matrix.


Solution

  • glm::eulerAngleXYZ does exactly what you need. The problem is that your autodesk function works with degrees, and glm with radians. You can see that non-unit numbers in FbxAMatrix::SetR section are sine and cosine of 15 degrees, and numbers in glm::eulerAngleXYZ are sine and cosine of 15. So, to have a needed equivalent you'd use:

    glm::eulerAngleXYZ(
      x / 180 * PI,
      y / 180 * PI,
      z / 180 * PI
    );
    

    in your case being x=15 and y=z=0.