2 * Copyright (C) 2007 David Adam
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef __D3DX8MATH_INL__
20 #define __D3DX8MATH_INL__
22 /*_______________D3DXCOLOR_____________________*/
24 static inline D3DXCOLOR* D3DXColorAdd(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
26 if ( !pout || !pc1 || !pc2 ) return NULL;
27 pout->r = (pc1->r) + (pc2->r);
28 pout->g = (pc1->g) + (pc2->g);
29 pout->b = (pc1->b) + (pc2->b);
30 pout->a = (pc1->a) + (pc2->a);
34 static inline D3DXCOLOR* D3DXColorLerp(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2, FLOAT s)
36 if ( !pout || !pc1 || !pc2 ) return NULL;
37 pout->r = (1-s) * (pc1->r) + s *(pc2->r);
38 pout->g = (1-s) * (pc1->g) + s *(pc2->g);
39 pout->b = (1-s) * (pc1->b) + s *(pc2->b);
40 pout->a = (1-s) * (pc1->a) + s *(pc2->a);
44 static inline D3DXCOLOR* D3DXColorModulate(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
46 if ( !pout || !pc1 || !pc2 ) return NULL;
47 pout->r = (pc1->r) * (pc2->r);
48 pout->g = (pc1->g) * (pc2->g);
49 pout->b = (pc1->b) * (pc2->b);
50 pout->a = (pc1->a) * (pc2->a);
54 static inline D3DXCOLOR* D3DXColorNegative(D3DXCOLOR *pout, CONST D3DXCOLOR *pc)
56 if ( !pout || !pc ) return NULL;
57 pout->r = 1.0f - pc->r;
58 pout->g = 1.0f - pc->g;
59 pout->b = 1.0f - pc->b;
64 static inline D3DXCOLOR* D3DXColorScale(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
66 if ( !pout || !pc ) return NULL;
74 static inline D3DXCOLOR* D3DXColorSubtract(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
76 if ( !pout || !pc1 || !pc2 ) return NULL;
77 pout->r = (pc1->r) - (pc2->r);
78 pout->g = (pc1->g) - (pc2->g);
79 pout->b = (pc1->b) - (pc2->b);
80 pout->a = (pc1->a) - (pc2->a);
84 /*_______________D3DXVECTOR2________________________*/
86 static inline D3DXVECTOR2* D3DXVec2Add(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
88 if ( !pout || !pv1 || !pv2) return NULL;
89 pout->x = pv1->x + pv2->x;
90 pout->y = pv1->y + pv2->y;
94 static inline FLOAT D3DXVec2CCW(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
96 if ( !pv1 || !pv2) return 0.0f;
97 return ( (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x) );
100 static inline FLOAT D3DXVec2Dot(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
102 if ( !pv1 || !pv2) return 0.0f;
103 return ( (pv1->x * pv2->x + pv1->y * pv2->y) );
106 static inline FLOAT D3DXVec2Length(CONST D3DXVECTOR2 *pv)
108 if (!pv) return 0.0f;
109 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
112 static inline FLOAT D3DXVec2LengthSq(CONST D3DXVECTOR2 *pv)
114 if (!pv) return 0.0f;
115 return( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
118 static inline D3DXVECTOR2* D3DXVec2Lerp(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, FLOAT s)
120 if ( !pout || !pv1 || !pv2) return NULL;
121 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
122 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
126 static inline D3DXVECTOR2* D3DXVec2Maximize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
128 if ( !pout || !pv1 || !pv2) return NULL;
129 pout->x = max(pv1->x , pv2->x);
130 pout->y = max(pv1->y , pv2->y);
134 static inline D3DXVECTOR2* D3DXVec2Minimize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
136 if ( !pout || !pv1 || !pv2) return NULL;
137 pout->x = min(pv1->x , pv2->x);
138 pout->y = min(pv1->y , pv2->y);
142 static inline D3DXVECTOR2* D3DXVec2Scale(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, FLOAT s)
144 if ( !pout || !pv) return NULL;
145 pout->x = s * (pv->x);
146 pout->y = s * (pv->y);
150 static inline D3DXVECTOR2* D3DXVec2Subtract(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
152 if ( !pout || !pv1 || !pv2) return NULL;
153 pout->x = pv1->x - pv2->x;
154 pout->y = pv1->y - pv2->y;
158 /*__________________D3DXVECTOR3_______________________*/
160 static inline D3DXVECTOR3* D3DXVec3Add(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
162 if ( !pout || !pv1 || !pv2) return NULL;
163 pout->x = pv1->x + pv2->x;
164 pout->y = pv1->y + pv2->y;
165 pout->z = pv1->z + pv2->z;
169 static inline D3DXVECTOR3* D3DXVec3Cross(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
171 if ( !pout || !pv1 || !pv2) return NULL;
172 pout->x = (pv1->y) * (pv2->z) - (pv1->z) * (pv2->y);
173 pout->y = (pv1->z) * (pv2->x) - (pv1->x) * (pv2->z);
174 pout->z = (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x);
178 static inline FLOAT D3DXVec3Dot(CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
180 if ( !pv1 || !pv2 ) return 0.0f;
181 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z);
184 static inline FLOAT D3DXVec3Length(CONST D3DXVECTOR3 *pv)
186 if (!pv) return 0.0f;
187 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) );
190 static inline FLOAT D3DXVec3LengthSq(CONST D3DXVECTOR3 *pv)
192 if (!pv) return 0.0f;
193 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z);
196 static inline D3DXVECTOR3* D3DXVec3Lerp(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, FLOAT s)
198 if ( !pout || !pv1 || !pv2) return NULL;
199 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
200 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
201 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
205 static inline D3DXVECTOR3* D3DXVec3Maximize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
207 if ( !pout || !pv1 || !pv2) return NULL;
208 pout->x = max(pv1->x , pv2->x);
209 pout->y = max(pv1->y , pv2->y);
210 pout->z = max(pv1->z , pv2->z);
214 static inline D3DXVECTOR3* D3DXVec3Minimize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
216 if ( !pout || !pv1 || !pv2) return NULL;
217 pout->x = min(pv1->x , pv2->x);
218 pout->y = min(pv1->y , pv2->y);
219 pout->z = min(pv1->z , pv2->z);
223 static inline D3DXVECTOR3* D3DXVec3Scale(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, FLOAT s)
225 if ( !pout || !pv) return NULL;
226 pout->x = s * (pv->x);
227 pout->y = s * (pv->y);
228 pout->z = s * (pv->z);
232 static inline D3DXVECTOR3* D3DXVec3Subtract(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
234 if ( !pout || !pv1 || !pv2) return NULL;
235 pout->x = pv1->x - pv2->x;
236 pout->y = pv1->y - pv2->y;
237 pout->z = pv1->z - pv2->z;
240 /*__________________D3DXVECTOR4_______________________*/
242 static inline D3DXVECTOR4* D3DXVec4Add(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
244 if ( !pout || !pv1 || !pv2) return NULL;
245 pout->x = pv1->x + pv2->x;
246 pout->y = pv1->y + pv2->y;
247 pout->z = pv1->z + pv2->z;
248 pout->w = pv1->w + pv2->w;
252 static inline FLOAT D3DXVec4Dot(CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
254 if (!pv1 || !pv2 ) return 0.0f;
255 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z) + (pv1->w) * (pv2->w);
258 static inline FLOAT D3DXVec4Length(CONST D3DXVECTOR4 *pv)
260 if (!pv) return 0.0f;
261 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w) );
264 static inline FLOAT D3DXVec4LengthSq(CONST D3DXVECTOR4 *pv)
266 if (!pv) return 0.0f;
267 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w);
270 static inline D3DXVECTOR4* D3DXVec4Lerp(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, FLOAT s)
272 if ( !pout || !pv1 || !pv2) return NULL;
273 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
274 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
275 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
276 pout->w = (1-s) * (pv1->w) + s * (pv2->w);
281 static inline D3DXVECTOR4* D3DXVec4Maximize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
283 if ( !pout || !pv1 || !pv2) return NULL;
284 pout->x = max(pv1->x , pv2->x);
285 pout->y = max(pv1->y , pv2->y);
286 pout->z = max(pv1->z , pv2->z);
287 pout->w = max(pv1->w , pv2->w);
291 static inline D3DXVECTOR4* D3DXVec4Minimize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
293 if ( !pout || !pv1 || !pv2) return NULL;
294 pout->x = min(pv1->x , pv2->x);
295 pout->y = min(pv1->y , pv2->y);
296 pout->z = min(pv1->z , pv2->z);
297 pout->w = min(pv1->w , pv2->w);
301 static inline D3DXVECTOR4* D3DXVec4Scale(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, FLOAT s)
303 if ( !pout || !pv) return NULL;
304 pout->x = s * (pv->x);
305 pout->y = s * (pv->y);
306 pout->z = s * (pv->z);
307 pout->w = s * (pv->w);
311 static inline D3DXVECTOR4* D3DXVec4Subtract(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
313 if ( !pout || !pv1 || !pv2) return NULL;
314 pout->x = pv1->x - pv2->x;
315 pout->y = pv1->y - pv2->y;
316 pout->z = pv1->z - pv2->z;
317 pout->w = pv1->w - pv2->w;
321 /*__________________D3DXMatrix____________________*/
322 #ifdef NONAMELESSUNION
323 # define D3DX_U(x) (x).u
325 # define D3DX_U(x) (x)
328 static inline D3DXMATRIX* D3DXMatrixIdentity(D3DXMATRIX *pout)
330 if ( !pout ) return NULL;
331 D3DX_U(*pout).m[0][1] = 0.0f;
332 D3DX_U(*pout).m[0][2] = 0.0f;
333 D3DX_U(*pout).m[0][3] = 0.0f;
334 D3DX_U(*pout).m[1][0] = 0.0f;
335 D3DX_U(*pout).m[1][2] = 0.0f;
336 D3DX_U(*pout).m[1][3] = 0.0f;
337 D3DX_U(*pout).m[2][0] = 0.0f;
338 D3DX_U(*pout).m[2][1] = 0.0f;
339 D3DX_U(*pout).m[2][3] = 0.0f;
340 D3DX_U(*pout).m[3][0] = 0.0f;
341 D3DX_U(*pout).m[3][1] = 0.0f;
342 D3DX_U(*pout).m[3][2] = 0.0f;
343 D3DX_U(*pout).m[0][0] = 1.0f;
344 D3DX_U(*pout).m[1][1] = 1.0f;
345 D3DX_U(*pout).m[2][2] = 1.0f;
346 D3DX_U(*pout).m[3][3] = 1.0f;
350 static inline BOOL D3DXMatrixIsIdentity(D3DXMATRIX *pm)
353 D3DXMATRIX testmatrix;
355 if ( !pm ) return FALSE;
356 D3DXMatrixIdentity(&testmatrix);
361 if ( D3DX_U(*pm).m[i][j] != D3DX_U(testmatrix).m[i][j] ) return FALSE;
368 /*__________________D3DXPLANE____________________*/
370 static inline FLOAT D3DXPlaneDot(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
372 if ( !pp || !pv ) return 0.0f;
373 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) * (pv->w) );
376 static inline FLOAT D3DXPlaneDotCoord(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
378 if ( !pp || !pv ) return 0.0f;
379 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) );
382 static inline FLOAT D3DXPlaneDotNormal(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
384 if ( !pp || !pv ) return 0.0f;
385 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) );
388 /*__________________D3DXQUATERNION____________________*/
390 static inline D3DXQUATERNION* D3DXQuaternionConjugate(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
392 if ( !pout || !pq) return NULL;
400 static inline FLOAT D3DXQuaternionDot(CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
402 if ( !pq1 || !pq2 ) return 0.0f;
403 return (pq1->x) * (pq2->x) + (pq1->y) * (pq2->y) + (pq1->z) * (pq2->z) + (pq1->w) * (pq2->w);
406 static inline D3DXQUATERNION* D3DXQuaternionIdentity(D3DXQUATERNION *pout)
408 if ( !pout) return NULL;
416 static inline BOOL D3DXQuaternionIsIdentity(D3DXQUATERNION *pq)
418 if ( !pq) return FALSE;
419 return ( (pq->x == 0.0f) && (pq->y == 0.0f) && (pq->z == 0.0f) && (pq->w == 1.0f) );
422 static inline FLOAT D3DXQuaternionLength(CONST D3DXQUATERNION *pq)
424 if (!pq) return 0.0f;
425 return sqrt( (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w) );
428 static inline FLOAT D3DXQuaternionLengthSq(CONST D3DXQUATERNION *pq)
430 if (!pq) return 0.0f;
431 return (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w);