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* D3DXColorLerp(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2, FLOAT s)
26 if ( !pout || !pc1 || !pc2 ) return NULL;
27 pout->r = (1-s) * (pc1->r) + s *(pc2->r);
28 pout->g = (1-s) * (pc1->g) + s *(pc2->g);
29 pout->b = (1-s) * (pc1->b) + s *(pc2->b);
30 pout->a = (1-s) * (pc1->a) + s *(pc2->a);
34 static inline D3DXCOLOR* D3DXColorModulate(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
36 if ( !pout || !pc1 || !pc2 ) return NULL;
37 pout->r = (pc1->r) * (pc2->r);
38 pout->g = (pc1->g) * (pc2->g);
39 pout->b = (pc1->b) * (pc2->b);
40 pout->a = (pc1->a) * (pc2->a);
44 static inline D3DXCOLOR* D3DXColorNegative(D3DXCOLOR *pout, CONST D3DXCOLOR *pc)
46 if ( !pout || !pc ) return NULL;
47 pout->r = 1.0f - pc->r;
48 pout->g = 1.0f - pc->g;
49 pout->b = 1.0f - pc->b;
54 /*_______________D3DXVECTOR2________________________*/
56 static inline D3DXVECTOR2* D3DXVec2Add(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
58 if ( !pout || !pv1 || !pv2) return NULL;
59 pout->x = pv1->x + pv2->x;
60 pout->y = pv1->y + pv2->y;
64 static inline FLOAT D3DXVec2CCW(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
66 if ( !pv1 || !pv2) return 0.0f;
67 return ( (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x) );
70 static inline FLOAT D3DXVec2Dot(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
72 if ( !pv1 || !pv2) return 0.0f;
73 return ( (pv1->x * pv2->x + pv1->y * pv2->y) );
76 static inline FLOAT D3DXVec2Length(CONST D3DXVECTOR2 *pv)
79 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
82 static inline FLOAT D3DXVec2LengthSq(CONST D3DXVECTOR2 *pv)
85 return( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
88 static inline D3DXVECTOR2* D3DXVec2Lerp(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, FLOAT s)
90 if ( !pout || !pv1 || !pv2) return NULL;
91 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
92 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
96 static inline D3DXVECTOR2* D3DXVec2Maximize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
98 if ( !pout || !pv1 || !pv2) return NULL;
99 pout->x = max(pv1->x , pv2->x);
100 pout->y = max(pv1->y , pv2->y);
104 static inline D3DXVECTOR2* D3DXVec2Minimize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
106 if ( !pout || !pv1 || !pv2) return NULL;
107 pout->x = min(pv1->x , pv2->x);
108 pout->y = min(pv1->y , pv2->y);
112 static inline D3DXVECTOR2* D3DXVec2Scale(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, FLOAT s)
114 if ( !pout || !pv) return NULL;
115 pout->x = s * (pv->x);
116 pout->y = s * (pv->y);
120 static inline D3DXVECTOR2* D3DXVec2Subtract(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
122 if ( !pout || !pv1 || !pv2) return NULL;
123 pout->x = pv1->x - pv2->x;
124 pout->y = pv1->y - pv2->y;
128 /*__________________D3DXVECTOR3_______________________*/
130 static inline D3DXVECTOR3* D3DXVec3Add(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
132 if ( !pout || !pv1 || !pv2) return NULL;
133 pout->x = pv1->x + pv2->x;
134 pout->y = pv1->y + pv2->y;
135 pout->z = pv1->z + pv2->z;
139 static inline D3DXVECTOR3* D3DXVec3Cross(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
141 if ( !pout || !pv1 || !pv2) return NULL;
142 pout->x = (pv1->y) * (pv2->z) - (pv1->z) * (pv2->y);
143 pout->y = (pv1->z) * (pv2->x) - (pv1->x) * (pv2->z);
144 pout->z = (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x);
148 static inline FLOAT D3DXVec3Dot(CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
150 if ( !pv1 || !pv2 ) return 0.0f;
151 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z);
154 static inline FLOAT D3DXVec3Length(CONST D3DXVECTOR3 *pv)
156 if (!pv) return 0.0f;
157 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) );
160 static inline FLOAT D3DXVec3LengthSq(CONST D3DXVECTOR3 *pv)
162 if (!pv) return 0.0f;
163 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z);
166 static inline D3DXVECTOR3* D3DXVec3Lerp(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, FLOAT s)
168 if ( !pout || !pv1 || !pv2) return NULL;
169 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
170 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
171 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
175 static inline D3DXVECTOR3* D3DXVec3Maximize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
177 if ( !pout || !pv1 || !pv2) return NULL;
178 pout->x = max(pv1->x , pv2->x);
179 pout->y = max(pv1->y , pv2->y);
180 pout->z = max(pv1->z , pv2->z);
184 static inline D3DXVECTOR3* D3DXVec3Minimize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
186 if ( !pout || !pv1 || !pv2) return NULL;
187 pout->x = min(pv1->x , pv2->x);
188 pout->y = min(pv1->y , pv2->y);
189 pout->z = min(pv1->z , pv2->z);
193 static inline D3DXVECTOR3* D3DXVec3Scale(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, FLOAT s)
195 if ( !pout || !pv) return NULL;
196 pout->x = s * (pv->x);
197 pout->y = s * (pv->y);
198 pout->z = s * (pv->z);
202 static inline D3DXVECTOR3* D3DXVec3Subtract(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
204 if ( !pout || !pv1 || !pv2) return NULL;
205 pout->x = pv1->x - pv2->x;
206 pout->y = pv1->y - pv2->y;
207 pout->z = pv1->z - pv2->z;
210 /*__________________D3DXVECTOR4_______________________*/
212 static inline D3DXVECTOR4* D3DXVec4Add(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
214 if ( !pout || !pv1 || !pv2) return NULL;
215 pout->x = pv1->x + pv2->x;
216 pout->y = pv1->y + pv2->y;
217 pout->z = pv1->z + pv2->z;
218 pout->w = pv1->w + pv2->w;
222 static inline FLOAT D3DXVec4Dot(CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
224 if (!pv1 || !pv2 ) return 0.0f;
225 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z) + (pv1->w) * (pv2->w);
228 static inline FLOAT D3DXVec4Length(CONST D3DXVECTOR4 *pv)
230 if (!pv) return 0.0f;
231 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w) );
234 static inline FLOAT D3DXVec4LengthSq(CONST D3DXVECTOR4 *pv)
236 if (!pv) return 0.0f;
237 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w);
240 static inline D3DXVECTOR4* D3DXVec4Lerp(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, FLOAT s)
242 if ( !pout || !pv1 || !pv2) return NULL;
243 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
244 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
245 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
246 pout->w = (1-s) * (pv1->w) + s * (pv2->w);
251 static inline D3DXVECTOR4* D3DXVec4Maximize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
253 if ( !pout || !pv1 || !pv2) return NULL;
254 pout->x = max(pv1->x , pv2->x);
255 pout->y = max(pv1->y , pv2->y);
256 pout->z = max(pv1->z , pv2->z);
257 pout->w = max(pv1->w , pv2->w);
261 static inline D3DXVECTOR4* D3DXVec4Minimize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
263 if ( !pout || !pv1 || !pv2) return NULL;
264 pout->x = min(pv1->x , pv2->x);
265 pout->y = min(pv1->y , pv2->y);
266 pout->z = min(pv1->z , pv2->z);
267 pout->w = min(pv1->w , pv2->w);
271 static inline D3DXVECTOR4* D3DXVec4Scale(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, FLOAT s)
273 if ( !pout || !pv) return NULL;
274 pout->x = s * (pv->x);
275 pout->y = s * (pv->y);
276 pout->z = s * (pv->z);
277 pout->w = s * (pv->w);
281 static inline D3DXVECTOR4* D3DXVec4Subtract(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
283 if ( !pout || !pv1 || !pv2) return NULL;
284 pout->x = pv1->x - pv2->x;
285 pout->y = pv1->y - pv2->y;
286 pout->z = pv1->z - pv2->z;
287 pout->w = pv1->w - pv2->w;
291 /*__________________D3DXQUATERNION____________________*/
293 static inline FLOAT D3DXPlaneDot(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
295 if ( !pp || !pv ) return 0.0f;
296 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) * (pv->w) );
299 static inline FLOAT D3DXPlaneDotCoord(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
301 if ( !pp || !pv ) return 0.0f;
302 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) );
305 static inline FLOAT D3DXPlaneDotNormal(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
307 if ( !pp || !pv ) return 0.0f;
308 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) );
311 /*__________________D3DXQUATERNION____________________*/
313 static inline D3DXQUATERNION* D3DXQuaternionConjugate(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
315 if ( !pout || !pq) return NULL;
323 static inline FLOAT D3DXQuaternionDot(CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
325 if ( !pq1 || !pq2 ) return 0.0f;
326 return (pq1->x) * (pq2->x) + (pq1->y) * (pq2->y) + (pq1->z) * (pq2->z) + (pq1->w) * (pq2->w);
329 static inline D3DXQUATERNION* D3DXQuaternionIdentity(D3DXQUATERNION *pout)
331 if ( !pout) return NULL;
339 static inline BOOL D3DXQuaternionIsIdentity(D3DXQUATERNION *pq)
341 if ( !pq) return FALSE;
342 return ( (pq->x == 0.0f) && (pq->y == 0.0f) && (pq->z == 0.0f) && (pq->w == 1.0f) );
345 static inline FLOAT D3DXQuaternionLength(CONST D3DXQUATERNION *pq)
347 if (!pq) return 0.0f;
348 return sqrt( (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w) );
351 static inline FLOAT D3DXQuaternionLengthSq(CONST D3DXQUATERNION *pq)
353 if (!pq) return 0.0f;
354 return (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w);