00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "q_shared.h"
00023
00024 mat3_t mat3_default( idVec3_t( 1, 0, 0 ), idVec3_t( 0, 1, 0 ), idVec3_t( 0, 0, 1 ) );
00025
00026 void toMatrix( quat_t const &src, mat3_t &dst ) {
00027 float wx, wy, wz;
00028 float xx, yy, yz;
00029 float xy, xz, zz;
00030 float x2, y2, z2;
00031
00032 x2 = src.x + src.x;
00033 y2 = src.y + src.y;
00034 z2 = src.z + src.z;
00035
00036 xx = src.x * x2;
00037 xy = src.x * y2;
00038 xz = src.x * z2;
00039
00040 yy = src.y * y2;
00041 yz = src.y * z2;
00042 zz = src.z * z2;
00043
00044 wx = src.w * x2;
00045 wy = src.w * y2;
00046 wz = src.w * z2;
00047
00048 dst[ 0 ][ 0 ] = 1.0f - ( yy + zz );
00049 dst[ 0 ][ 1 ] = xy - wz;
00050 dst[ 0 ][ 2 ] = xz + wy;
00051
00052 dst[ 1 ][ 0 ] = xy + wz;
00053 dst[ 1 ][ 1 ] = 1.0f - ( xx + zz );
00054 dst[ 1 ][ 2 ] = yz - wx;
00055
00056 dst[ 2 ][ 0 ] = xz - wy;
00057 dst[ 2 ][ 1 ] = yz + wx;
00058 dst[ 2 ][ 2 ] = 1.0f - ( xx + yy );
00059 }
00060
00061 void toMatrix( angles_t const &src, mat3_t &dst ) {
00062 float angle;
00063 static float sr, sp, sy, cr, cp, cy;
00064
00065 angle = src.yaw * ( M_PI * 2.0f / 360.0f );
00066 sy = sin( angle );
00067 cy = cos( angle );
00068
00069 angle = src.pitch * ( M_PI * 2.0f / 360.0f );
00070 sp = sin( angle );
00071 cp = cos( angle );
00072
00073 angle = src.roll * ( M_PI * 2.0f / 360.0f );
00074 sr = sin( angle );
00075 cr = cos( angle );
00076
00077 dst[ 0 ].set( cp * cy, cp * sy, -sp );
00078 dst[ 1 ].set( sr * sp * cy + cr * -sy, sr * sp * sy + cr * cy, sr * cp );
00079 dst[ 2 ].set( cr * sp * cy + -sr * -sy, cr * sp * sy + -sr * cy, cr * cp );
00080 }
00081
00082 void toMatrix( idVec3_t const &src, mat3_t &dst ) {
00083 angles_t sup = src;
00084 toMatrix(sup, dst);
00085 }
00086
00087 void mat3_t::ProjectVector( const idVec3_t &src, idVec3_t &dst ) const {
00088 dst.x = src * mat[ 0 ];
00089 dst.y = src * mat[ 1 ];
00090 dst.z = src * mat[ 2 ];
00091 }
00092
00093 void mat3_t::UnprojectVector( const idVec3_t &src, idVec3_t &dst ) const {
00094 dst = mat[ 0 ] * src.x + mat[ 1 ] * src.y + mat[ 2 ] * src.z;
00095 }
00096
00097 void mat3_t::Transpose( mat3_t &matrix ) {
00098 int i;
00099 int j;
00100
00101 for( i = 0; i < 3; i++ ) {
00102 for( j = 0; j < 3; j++ ) {
00103 matrix[ i ][ j ] = mat[ j ][ i ];
00104 }
00105 }
00106 }
00107
00108 void mat3_t::Transpose( void ) {
00109 float temp;
00110 int i;
00111 int j;
00112
00113 for( i = 0; i < 3; i++ ) {
00114 for( j = i + 1; j < 3; j++ ) {
00115 temp = mat[ i ][ j ];
00116 mat[ i ][ j ] = mat[ j ][ i ];
00117 mat[ j ][ i ] = temp;
00118 }
00119 }
00120 }
00121
00122 mat3_t mat3_t::Inverse( void ) const {
00123 mat3_t inv( *this );
00124
00125 inv.Transpose();
00126
00127 return inv;
00128 }
00129
00130 void mat3_t::Clear( void ) {
00131 mat[0].set( 1, 0, 0 );
00132 mat[1].set( 0, 1, 0 );
00133 mat[2].set( 0, 0, 1 );
00134 }