Beispiel #1
0
        public static void RotationMatrix(ref Matrix matrix, out Quaternion result)
        {
            //float scale = matrix.M11 + matrix.M22 + matrix.M33;

            //if( scale > 0.0f )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(scale + 1.0f) ) );

            //	result.W = sqrt * 0.5f;
            //	sqrt = 0.5f / sqrt;

            //	result.X = (matrix.M23 - matrix.M32) * sqrt;
            //	result.Y = (matrix.M31 - matrix.M13) * sqrt;
            //	result.Z = (matrix.M12 - matrix.M21) * sqrt;
            //	return;
            //}

            //if( (matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33) )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M11 - matrix.M22 - matrix.M33) ) );
            //	float half = 0.5f / sqrt;

            //	result.X = 0.5f * sqrt;
            //	result.Y = (matrix.M12 + matrix.M21) * half;
            //	result.Z = (matrix.M13 + matrix.M31) * half;
            //	result.W = (matrix.M23 - matrix.M32) * half;
            //	return;
            //}

            //if( matrix.M22 > matrix.M33 )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M22 - matrix.M11 - matrix.M33) ) );
            //	float half = 0.5f / sqrt;

            //	result.X = (matrix.M21 + matrix.M12) * half;
            //	result.Y = 0.5f * sqrt;
            //	result.Z = (matrix.M32 + matrix.M23) * half;
            //	result.W = (matrix.M31 - matrix.M13) * half;
            //	return;
            //}

            //float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M33 - matrix.M11 - matrix.M22) ) );
            //float half = 0.5f / sqrt;

            //result.X = (matrix.M31 + matrix.M13) * half;
            //result.Y = (matrix.M32 + matrix.M23) * half;
            //result.Z = 0.5f * sqrt;
            //result.W = (matrix.M12 - matrix.M21) * half;

            unsafe
            {
                fixed(Quaternion *pin_result = &result)
                {
                    fixed(Matrix *pin_matrix = &matrix)
                    {
                        IDllImportApi.D3DXQuaternionRotationMatrix(pin_result, pin_matrix);
                    }
                }
            }
        }
Beispiel #2
0
        public static Quaternion RotationMatrix(Matrix matrix)
        {
            Quaternion result;

            //float scale = matrix.M11 + matrix.M22 + matrix.M33;

            //if( scale > 0.0f )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(scale + 1.0f) ) );

            //	result.W = sqrt * 0.5f;
            //	sqrt = 0.5f / sqrt;

            //	result.X = (matrix.M23 - matrix.M32) * sqrt;
            //	result.Y = (matrix.M31 - matrix.M13) * sqrt;
            //	result.Z = (matrix.M12 - matrix.M21) * sqrt;

            //	return result;
            //}

            //if( (matrix.M11 >= matrix.M22) && (matrix.M11 >= matrix.M33) )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M11 - matrix.M22 - matrix.M33) ) );
            //	float half = 0.5f / sqrt;

            //	result.X = 0.5f * sqrt;
            //	result.Y = (matrix.M12 + matrix.M21) * half;
            //	result.Z = (matrix.M13 + matrix.M31) * half;
            //	result.W = (matrix.M23 - matrix.M32) * half;

            //	return result;
            //}

            //if( matrix.M22 > matrix.M33 )
            //{
            //	float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M22 - matrix.M11 - matrix.M33) ) );
            //	float half = 0.5f / sqrt;

            //	result.X = (matrix.M21 + matrix.M12) * half;
            //	result.Y = 0.5f * sqrt;
            //	result.Z = (matrix.M32 + matrix.M23) * half;
            //	result.W = (matrix.M31 - matrix.M13) * half;

            //	return result;
            //}

            //float sqrt = (float)( Math.Sqrt( (double)(1.0f + matrix.M33 - matrix.M11 - matrix.M22) ) );
            //float half = 0.5f / sqrt;

            //result.X = (matrix.M31 + matrix.M13) * half;
            //result.Y = (matrix.M32 + matrix.M23) * half;
            //result.Z = 0.5f * sqrt;
            //result.W = (matrix.M12 - matrix.M21) * half;

            //Quaternion result2;
            unsafe
            {
                IDllImportApi.D3DXQuaternionRotationMatrix((Quaternion *)&result, (Matrix *)&matrix);
            }

            return(result);
        }