void TestMatrixMultiply() { Matrix44 matrix1; const float m1data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; matrix1.InitFrom( m1data ); Matrix44 matrix2; const float m2data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; matrix2.InitFrom( m2data ); Matrix44 result; Matrix44::Multiply( matrix1, matrix2, result ); Matrix44 expectedResult; const float exData[] = { 90, 100, 110, 120, 202, 228, 254, 280, 314, 356, 398, 440, 426, 484, 542, 600 }; expectedResult.InitFrom( exData ); for (int i = 0; i < 16; ++i) { if (std::abs( result.m[ i ] - expectedResult.m[ i ] ) > 0.0001f) { std::cerr << "Matrix multiply failed!" << std::endl; } } }
void TestMatrixInverse() { Matrix44 mat; mat.MakeProjection( 45, 4.0f / 3.0f, 1, 200 ); Matrix44 result; Matrix44::Invert( mat, result ); Matrix44 expectedResult; const float exData[] = { 0.552285f, -0, -0, -0, -0, 0.414214f, -0, -0, -0, -0, -0, -0.4975f, -0, -0, -1, 0.5025f }; expectedResult.InitFrom( exData ); for (int i = 0; i < 16; ++i) { if (std::abs( result.m[ i ] - expectedResult.m[ i ] ) > 0.0001f) { std::cerr << "Matrix inverse failed!" << std::endl; } } }
void Matrix44::Transpose( Matrix44& out ) const { float tmp[ 16 ]; tmp[ 0 ] = m[ 0 ]; tmp[ 1 ] = m[ 4 ]; tmp[ 2 ] = m[ 8 ]; tmp[ 3 ] = m[ 12 ]; tmp[ 4 ] = m[ 1 ]; tmp[ 5 ] = m[ 5 ]; tmp[ 6 ] = m[ 9 ]; tmp[ 7 ] = m[ 13 ]; tmp[ 8 ] = m[ 2 ]; tmp[ 9 ] = m[ 6 ]; tmp[ 10 ] = m[ 10 ]; tmp[ 11 ] = m[ 14 ]; tmp[ 12 ] = m[ 3 ]; tmp[ 13 ] = m[ 7 ]; tmp[ 14 ] = m[ 11 ]; tmp[ 15 ] = m[ 15 ]; out.InitFrom( tmp ); #if DEBUG ae3d::CheckNaN( out ); #endif }
void Matrix44::Invert( const Matrix44& matrix, Matrix44& out ) { float invTrans[ 16 ]; InverseTranspose( matrix.m, invTrans ); Matrix44 iTrans; iTrans.InitFrom( invTrans ); iTrans.Transpose( out ); #if DEBUG ae3d::CheckNaN( out ); #endif }
void Matrix44::Multiply( const Matrix44& a, const Matrix44& b, Matrix44& out ) { float tmp[ 16 ]; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { tmp[ i * 4 + j ] = a.m[ i * 4 + 0 ] * b.m[ 0 * 4 + j ] + a.m[ i * 4 + 1 ] * b.m[ 1 * 4 + j ] + a.m[ i * 4 + 2 ] * b.m[ 2 * 4 + j ] + a.m[ i * 4 + 3 ] * b.m[ 3 * 4 + j ]; } } out.InitFrom( tmp ); #if DEBUG ae3d::CheckNaN( out ); #endif }