Question

I'm working on a demo to convert a matrix (3x3) into a quaternion, however I'm stuck on something that will seem pretty simple for most, but my brain is shutting down as I've been working on this for hours. The code below compiles okay in my IDE of choice (VS2010 Express) using C++ and openGL, but I receive the following error - "The variable 'm11' is being used without being initialized.". This error appears for all the float variables in my matrix so when I try to run the code, it just bombs out. Can anyone help? Thank you

Code:

void matrixIntoQuaternions() {

            // Input matrix 3X3
            float m11,m12,m13;
            float m21,m22,m23;
            float m31,m32,m33;

            // Output quaternion
            float w,x,y,z;
            // Determine which of w,x,y, or z has the largest absolute value
            float fourWSquaredMinus1 = m11 + m22 + m33;
            float fourXSquaredMinus1 = m11 - m22 - m33;
            float fourYSquaredMinus1 = m22 - m11 - m33;
            float fourZSquaredMinus1 = m33 - m11 - m22;

            int biggestIndex = 0;
            float fourBiggestSquaredMinus1 = fourWSquaredMinus1;

            if(fourXSquaredMinus1 > fourBiggestSquaredMinus1) {
                fourBiggestSquaredMinus1 = fourXSquaredMinus1;
                biggestIndex = 1;
            }
            if (fourYSquaredMinus1 > fourBiggestSquaredMinus1) {
                fourBiggestSquaredMinus1 = fourYSquaredMinus1;
                biggestIndex = 2;
            }
            if (fourZSquaredMinus1 > fourBiggestSquaredMinus1) {
                fourBiggestSquaredMinus1 = fourZSquaredMinus1;
                biggestIndex = 3;
            }
            // Per form square root and division
            float biggestVal = sqrt (fourBiggestSquaredMinus1 + 1.0f ) * 0.5f;
            float mult = 0.25f / biggestVal;

            // Apply table to compute quaternion values
            switch (biggestIndex) {
                case 0:
                    w = biggestVal;
                    x = (m23 - m32) * mult;
                    y = (m31 - m13) * mult;
                    z = (m12 - m21) * mult;
                    break;
                case 1:
                    x = biggestVal;
                    w = (m23 - m32) * mult;
                    y = (m12 + m21) * mult;
                    z = (m31 + m13) * mult;
                    break;
                case 2:
                    y = biggestVal;
                    w = (m31 - m13) * mult;
                    x = (m12 + m21) * mult;
                    z = (m23 + m32) * mult;
                    break;
                case 3:
                    z = biggestVal;
                    w = (m12 - m21) * mult;
                    x = (m31 + m13) * mult;
                    y = (m23 + m32) * mult;
                    break;

                    quatX = x;
                    quatY = y;
                    quatZ = z;
                    quatW = w;

                    model = vec4(quatX,quatY,quatZ,quatW);
                }
            } 
Was it helpful?

Solution

Make your inputs inputs to your function, and dot not leave them undefined:

void matrixIntoQuaternions(
            float m11, float m12, float m13, // Make these function parameters!
            float m21, float m22, float m23,
            float m31, float m32, float m33
)
{
   // Rest of your code...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top