1 //-----------------------------------------------------------------------------
4 // Desc: Math functions and shortcuts for Direct3D programming.
7 // Copyright (C) 1997 Microsoft Corporation. All rights reserved
8 //-----------------------------------------------------------------------------
17 //-----------------------------------------------------------------------------
18 // Useful Math constants
19 //-----------------------------------------------------------------------------
20 const FLOAT g_PI = 3.14159265358979323846f; // Pi
21 const FLOAT g_2_PI = 6.28318530717958623200f; // 2 * Pi
22 const FLOAT g_PI_DIV_2 = 1.57079632679489655800f; // Pi / 2
23 const FLOAT g_PI_DIV_4 = 0.78539816339744827900f; // Pi / 4
24 const FLOAT g_INV_PI = 0.31830988618379069122f; // 1 / Pi
25 const FLOAT g_DEGTORAD = 0.01745329251994329547f; // Degrees to Radians
26 const FLOAT g_RADTODEG = 57.29577951308232286465f; // Radians to Degrees
27 const FLOAT g_HUGE = 1.0e+38f; // Huge number for FLOAT
28 const FLOAT g_EPSILON = 1.0e-5f; // Tolerance for FLOATs
33 //-----------------------------------------------------------------------------
34 // Fuzzy compares (within tolerance)
35 //-----------------------------------------------------------------------------
36 inline BOOL D3DMath_IsZero( FLOAT a, FLOAT fTol = g_EPSILON )
37 { return ( a <= 0.0f ) ? ( a >= -fTol ) : ( a <= fTol ); }
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 VOID D3DMath_MatrixMultiply( D3DMATRIX& q, D3DMATRIX& a, D3DMATRIX& b );
46 HRESULT D3DMath_MatrixInvert( D3DMATRIX& q, D3DMATRIX& a );
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 HRESULT D3DMath_VectorMatrixMultiply( D3DVECTOR& vDest, D3DVECTOR& vSrc,
56 HRESULT D3DMath_VertexMatrixMultiply( D3DVERTEX& vDest, D3DVERTEX& vSrc,
62 //-----------------------------------------------------------------------------
63 // Quaternion functions
64 //-----------------------------------------------------------------------------
65 VOID D3DMath_QuaternionFromRotation( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w,
66 D3DVECTOR& v, FLOAT fTheta );
67 VOID D3DMath_RotationFromQuaternion( D3DVECTOR& v, FLOAT& fTheta,
68 FLOAT x, FLOAT y, FLOAT z, FLOAT w );
69 VOID D3DMath_QuaternionFromAngles( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w,
70 FLOAT fYaw, FLOAT fPitch, FLOAT fRoll );
71 VOID D3DMath_MatrixFromQuaternion( D3DMATRIX& mat, FLOAT x, FLOAT y, FLOAT z,
73 VOID D3DMath_QuaternionFromMatrix( FLOAT &x, FLOAT &y, FLOAT &z, FLOAT &w,
75 VOID D3DMath_QuaternionMultiply( FLOAT& Qx, FLOAT& Qy, FLOAT& Qz, FLOAT& Qw,
76 FLOAT Ax, FLOAT Ay, FLOAT Az, FLOAT Aw,
77 FLOAT Bx, FLOAT By, FLOAT Bz, FLOAT Bw );
78 VOID D3DMath_QuaternionSlerp( FLOAT& Qx, FLOAT& Qy, FLOAT& Qz, FLOAT& Qw,
79 FLOAT Ax, FLOAT Ay, FLOAT Az, FLOAT Aw,
80 FLOAT Bx, FLOAT By, FLOAT Bz, FLOAT Bw,