1 //-----------------------------------------------------------------------------
4 // Desc: Shortcut macros and functions for using DX objects
7 // Copyright (c) 1997-1998 Microsoft Corporation. All rights reserved
8 //-----------------------------------------------------------------------------
18 //-----------------------------------------------------------------------------
19 // Name: D3DMath_MatrixMultiply()
20 // Desc: Does the matrix operation: [Q] = [A] * [B].
21 //-----------------------------------------------------------------------------
22 VOID D3DMath_MatrixMultiply( D3DMATRIX& q, D3DMATRIX& a, D3DMATRIX& b )
24 FLOAT* pA = (FLOAT*)&a;
25 FLOAT* pB = (FLOAT*)&b;
28 ZeroMemory( pM, sizeof(D3DMATRIX) );
30 for( WORD i=0; i<4; i++ )
31 for( WORD j=0; j<4; j++ )
32 for( WORD k=0; k<4; k++ )
33 pM[4*i+j] += pA[4*k+j] * pB[4*i+k];
35 memcpy( &q, pM, sizeof(D3DMATRIX) );
41 //-----------------------------------------------------------------------------
42 // Name: D3DMath_MatrixInvert()
43 // Desc: Does the matrix operation: [Q] = inv[A]. Note: this function only
44 // works for matrices with [0 0 0 1] for the 4th column.
45 //-----------------------------------------------------------------------------
46 HRESULT D3DMath_MatrixInvert( D3DMATRIX& q, D3DMATRIX& a )
48 if( fabs(a._44 - 1.0f) > .001f)
50 if( fabs(a._14) > .001f || fabs(a._24) > .001f || fabs(a._34) > .001f )
53 FLOAT fDetInv = 1.0f / ( a._11 * ( a._22 * a._33 - a._23 * a._32 ) -
54 a._12 * ( a._21 * a._33 - a._23 * a._31 ) +
55 a._13 * ( a._21 * a._32 - a._22 * a._31 ) );
57 q._11 = fDetInv * ( a._22 * a._33 - a._23 * a._32 );
58 q._12 = -fDetInv * ( a._12 * a._33 - a._13 * a._32 );
59 q._13 = fDetInv * ( a._12 * a._23 - a._13 * a._22 );
62 q._21 = -fDetInv * ( a._21 * a._33 - a._23 * a._31 );
63 q._22 = fDetInv * ( a._11 * a._33 - a._13 * a._31 );
64 q._23 = -fDetInv * ( a._11 * a._23 - a._13 * a._21 );
67 q._31 = fDetInv * ( a._21 * a._32 - a._22 * a._31 );
68 q._32 = -fDetInv * ( a._11 * a._32 - a._12 * a._31 );
69 q._33 = fDetInv * ( a._11 * a._22 - a._12 * a._21 );
72 q._41 = -( a._41 * q._11 + a._42 * q._21 + a._43 * q._31 );
73 q._42 = -( a._41 * q._12 + a._42 * q._22 + a._43 * q._32 );
74 q._43 = -( a._41 * q._13 + a._42 * q._23 + a._43 * q._33 );
83 //-----------------------------------------------------------------------------
84 // Name: D3DMath_VectorMatrixMultiply()
85 // Desc: Multiplies a vector by a matrix
86 //-----------------------------------------------------------------------------
87 HRESULT D3DMath_VectorMatrixMultiply( D3DVECTOR& vDest, D3DVECTOR& vSrc,
90 FLOAT x = vSrc.x*mat._11 + vSrc.y*mat._21 + vSrc.z* mat._31 + mat._41;
91 FLOAT y = vSrc.x*mat._12 + vSrc.y*mat._22 + vSrc.z* mat._32 + mat._42;
92 FLOAT z = vSrc.x*mat._13 + vSrc.y*mat._23 + vSrc.z* mat._33 + mat._43;
93 FLOAT w = vSrc.x*mat._14 + vSrc.y*mat._24 + vSrc.z* mat._34 + mat._44;
95 if( fabs( w ) < g_EPSILON )
108 //-----------------------------------------------------------------------------
109 // Name: D3DMath_VertexMatrixMultiply()
110 // Desc: Multiplies a vertex by a matrix
111 //-----------------------------------------------------------------------------
112 HRESULT D3DMath_VertexMatrixMultiply( D3DVERTEX& vDest, D3DVERTEX& vSrc,
116 D3DVECTOR* pSrcVec = (D3DVECTOR*)&vSrc.x;
117 D3DVECTOR* pDestVec = (D3DVECTOR*)&vDest.x;
119 if( SUCCEEDED( hr = D3DMath_VectorMatrixMultiply( *pDestVec, *pSrcVec,
122 pSrcVec = (D3DVECTOR*)&vSrc.nx;
123 pDestVec = (D3DVECTOR*)&vDest.nx;
124 hr = D3DMath_VectorMatrixMultiply( *pDestVec, *pSrcVec, mat );
132 //-----------------------------------------------------------------------------
133 // Name: D3DMath_QuaternionFromRotation()
134 // Desc: Converts a normalized axis and angle to a unit quaternion.
135 //-----------------------------------------------------------------------------
136 VOID D3DMath_QuaternionFromRotation( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w,
137 D3DVECTOR& v, FLOAT fTheta )
139 x = (FLOAT)sin(fTheta/2) * v.x;
140 y = (FLOAT)sin(fTheta/2) * v.y;
141 z = (FLOAT)sin(fTheta/2) * v.z;
142 w = (FLOAT)cos(fTheta/2);
148 //-----------------------------------------------------------------------------
149 // Name: D3DMath_RotationFromQuaternion()
150 // Desc: Converts a normalized axis and angle to a unit quaternion.
151 //-----------------------------------------------------------------------------
152 VOID D3DMath_RotationFromQuaternion( D3DVECTOR& v, FLOAT& fTheta,
153 FLOAT x, FLOAT y, FLOAT z, FLOAT w )
156 fTheta = (FLOAT)( acos(w) * 2 );
157 v.x = (FLOAT)( x / sin(fTheta/2) );
158 v.y = (FLOAT)( y / sin(fTheta/2) );
159 v.z = (FLOAT)( z / sin(fTheta/2) );
165 //-----------------------------------------------------------------------------
166 // Name: D3DMath_QuaternionFromAngles()
167 // Desc: Converts euler angles to a unit quaternion.
168 //-----------------------------------------------------------------------------
169 VOID D3DMath_QuaternionFromAngles( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w,
170 FLOAT fYaw, FLOAT fPitch, FLOAT fRoll )
173 FLOAT fSinYaw = (FLOAT)sin(fYaw/2);
174 FLOAT fSinPitch = (FLOAT)sin(fPitch/2);
175 FLOAT fSinRoll = (FLOAT)sin(fRoll/2);
176 FLOAT fCosYaw = (FLOAT)cos(fYaw/2);
177 FLOAT fCosPitch = (FLOAT)cos(fPitch/2);
178 FLOAT fCosRoll = (FLOAT)cos(fRoll/2);
180 x = fSinRoll * fCosPitch * fCosYaw - fCosRoll * fSinPitch * fSinYaw;
181 y = fCosRoll * fSinPitch * fCosYaw + fSinRoll * fCosPitch * fSinYaw;
182 z = fCosRoll * fCosPitch * fSinYaw - fSinRoll * fSinPitch * fCosYaw;
183 w = fCosRoll * fCosPitch * fCosYaw + fSinRoll * fSinPitch * fSinYaw;
189 //-----------------------------------------------------------------------------
190 // Name: D3DMath_MatrixFromQuaternion()
191 // Desc: Converts a unit quaternion into a rotation matrix.
192 //-----------------------------------------------------------------------------
193 VOID D3DMath_MatrixFromQuaternion( D3DMATRIX& mat, FLOAT x, FLOAT y, FLOAT z,
196 FLOAT xx = x*x; FLOAT yy = y*y; FLOAT zz = z*z;
197 FLOAT xy = x*y; FLOAT xz = x*z; FLOAT yz = y*z;
198 FLOAT wx = w*x; FLOAT wy = w*y; FLOAT wz = w*z;
200 mat._11 = 1 - 2 * ( yy + zz );
201 mat._12 = 2 * ( xy - wz );
202 mat._13 = 2 * ( xz + wy );
204 mat._21 = 2 * ( xy + wz );
205 mat._22 = 1 - 2 * ( xx + zz );
206 mat._23 = 2 * ( yz - wx );
208 mat._31 = 2 * ( xz - wy );
209 mat._32 = 2 * ( yz + wx );
210 mat._33 = 1 - 2 * ( xx + yy );
212 mat._14 = mat._24 = mat._34 = 0.0f;
213 mat._41 = mat._42 = mat._43 = 0.0f;
220 //-----------------------------------------------------------------------------
221 // Name: D3DMath_QuaternionFromMatrix()
222 // Desc: Converts a rotation matrix into a unit quaternion.
223 //-----------------------------------------------------------------------------
224 VOID D3DMath_QuaternionFromMatrix( FLOAT& x, FLOAT& y, FLOAT& z, FLOAT& w,
227 if( mat._11 + mat._22 + mat._33 > 0.0f )
229 FLOAT s = (FLOAT)sqrt( mat._11 + mat._22 + mat._33 + mat._44 );
231 x = (mat._23-mat._32) / (2*s);
232 y = (mat._31-mat._13) / (2*s);
233 z = (mat._12-mat._21) / (2*s);
241 FLOAT xx = x*x; FLOAT yy = y*y; FLOAT zz = z*z;
242 FLOAT xy = x*y; FLOAT xz = x*z; FLOAT yz = y*z;
243 FLOAT wx = w*x; FLOAT wy = w*y; FLOAT wz = w*z;
245 mat._11 = 1 - 2 * ( yy + zz );
246 mat._12 = 2 * ( xy - wz );
247 mat._13 = 2 * ( xz + wy );
249 mat._21 = 2 * ( xy + wz );
250 mat._22 = 1 - 2 * ( xx + zz );
251 mat._23 = 2 * ( yz - wx );
253 mat._31 = 2 * ( xz - wy );
254 mat._32 = 2 * ( yz + wx );
255 mat._33 = 1 - 2 * ( xx + yy );
257 mat._14 = mat._24 = mat._34 = 0.0f;
258 mat._41 = mat._42 = mat._43 = 0.0f;
265 //-----------------------------------------------------------------------------
266 // Name: D3DMath_QuaternionMultiply()
267 // Desc: Mulitples two quaternions together as in {Q} = {A} * {B}.
268 //-----------------------------------------------------------------------------
269 VOID D3DMath_QuaternionMultiply( FLOAT& Qx, FLOAT& Qy, FLOAT& Qz, FLOAT& Qw,
270 FLOAT Ax, FLOAT Ay, FLOAT Az, FLOAT Aw,
271 FLOAT Bx, FLOAT By, FLOAT Bz, FLOAT Bw )
273 FLOAT Dx = Bw*Ax + Bx*Aw + By*Az + Bz*Ay;
274 FLOAT Dy = Bw*Ay + By*Aw + Bz*Ax + Bx*Az;
275 FLOAT Dz = Bw*Az + Bz*Aw + Bx*Ay + By*Ax;
276 FLOAT Dw = Bw*Aw + Bx*Ax + By*Ay + Bz*Az;
278 Qx = Dx; Qy = Dy; Qz = Dz; Qw = Dw;
284 //-----------------------------------------------------------------------------
285 // Name: D3DMath_SlerpQuaternions()
286 // Desc: Compute a quaternion which is the spherical linear interpolation
287 // between two other quaternions by dvFraction.
288 //-----------------------------------------------------------------------------
289 VOID D3DMath_QuaternionSlerp( FLOAT& Qx, FLOAT& Qy, FLOAT& Qz, FLOAT& Qw,
290 FLOAT Ax, FLOAT Ay, FLOAT Az, FLOAT Aw,
291 FLOAT Bx, FLOAT By, FLOAT Bz, FLOAT Bw,
297 // Compute dot product, aka cos(theta):
298 FLOAT fCosTheta = Ax*Bx + Ay*By + Az*Bz + Aw*Bw;
300 if( fCosTheta < 0.0f )
302 // Flip start quaternion
303 Ax = -Ax; Ay = -Ay; Ax = -Az; Aw = -Aw;
304 fCosTheta = -fCosTheta;
307 if( fCosTheta + 1.0f > 0.05f )
309 // If the quaternions are close, use linear interploation
310 if( 1.0f - fCosTheta < 0.05f )
312 fScale1 = 1.0f - fAlpha;
315 else // Otherwise, do spherical interpolation
317 FLOAT fTheta = (FLOAT)acos( fCosTheta );
318 FLOAT fSinTheta = (FLOAT)sin( fTheta );
320 fScale1 = (FLOAT)sin( fTheta * (1.0f-fAlpha) ) / fSinTheta;
321 fScale2 = (FLOAT)sin( fTheta * fAlpha ) / fSinTheta;
330 fScale1 = (FLOAT)sin( g_PI * (0.5f - fAlpha) );
331 fScale2 = (FLOAT)sin( g_PI * fAlpha );
334 Qx = fScale1 * Ax + fScale2 * Bx;
335 Qy = fScale1 * Ay + fScale2 * By;
336 Qz = fScale1 * Az + fScale2 * Bz;
337 Qw = fScale1 * Aw + fScale2 * Bw;