Search code examples
c#linear-algebraopentk

Find rotation matrix in OpenGL


Take this with a grain of salt, I have practically no knowledge of linear algebra. That said, my problem lies within a rotation matrix that I have multiplied to my current matrix several times. Essentially, I'm trying to be able to gain the original matrix before transformation( correct terminology?). So in theory, I should be able to have "undo"ed the rotation matrix. I have heard of transposing the matrix that has had the rotation matrix applied to however I don't even know how I would obtain that. Every method I have theorized on how to do this ultimately has resulted in having to know what the original matrix prior to the rotation matrix being applied to it was ( or some way to find out ). Is there a way to do this in OpenGL and/or mathematically speaking?

Thanks in advance. Best Regards.


Solution

  • After having spoken to Mike some more, I have determined that he has underlying problems that first need to addressed before attempting to tackle this. It appears as though he is using deprecated OpenGL (glPush/Pop), and does not have access to the matrices in question. I have instructed him to learn modern opengl, and then come back to this topic at a later date.

    However, I have found out that you can indeed extract a rotation from a RTS Transformation matrix. In OpenTK, which he is using, this is done by using ExtractRotation() of a Matrix4, which gives you a quarternion. You then use the ToEulerAngles() function of the Quaternion to get a Vector3 that symbolizes the rotation in radians. The last step is then to convert those radians to degrees, using something like this:

            /// <summary>
            /// Converts radians to degrees.
            /// </summary>
            /// <param name="radians">The angle in radians.</param>
            public static float ToDegrees(float radians)
            {
                return radians * 57.29578f;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="radians"></param>
            /// <returns></returns>
            public static Vector3 ToDegrees(Vector3 radians)
            {
                return new Vector3(ToDegrees(radians.X),ToDegrees(radians.Y), ToDegrees(radians.Z));
            }
    

    And this will get you the rotation of the xy and z components. It should be noted one final time that this only works for RTS Transformations. And as a final opinion, none of this should be used in performance sensitive code; instead, I'd suggest storing the rotation somewhere so you can quickly reference it instead of attempting to extract it, and if needed be, sync and build the transformation matrix on a layer beneath it.