2 * Copyright (C) 2007 David Adam
3 * Copyright (C) 2007 Tony Wasserka
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #ifndef __D3DX9MATH_INL__
21 #define __D3DX9MATH_INL__
23 /* constructors & operators */
26 inline D3DXVECTOR2::D3DXVECTOR2()
30 inline D3DXVECTOR2::D3DXVECTOR2(CONST FLOAT *pf)
37 inline D3DXVECTOR2::D3DXVECTOR2(FLOAT fx, FLOAT fy)
43 inline D3DXVECTOR2::operator FLOAT* ()
48 inline D3DXVECTOR2::operator CONST FLOAT* () const
50 return (CONST FLOAT*)&x;
53 inline D3DXVECTOR2& D3DXVECTOR2::operator += (CONST D3DXVECTOR2& v)
60 inline D3DXVECTOR2& D3DXVECTOR2::operator -= (CONST D3DXVECTOR2& v)
67 inline D3DXVECTOR2& D3DXVECTOR2::operator *= (FLOAT f)
74 inline D3DXVECTOR2& D3DXVECTOR2::operator /= (FLOAT f)
81 inline D3DXVECTOR2 D3DXVECTOR2::operator + () const
86 inline D3DXVECTOR2 D3DXVECTOR2::operator - () const
88 return D3DXVECTOR2(-x, -y);
91 inline D3DXVECTOR2 D3DXVECTOR2::operator + (CONST D3DXVECTOR2& v) const
93 return D3DXVECTOR2(x + v.x, y + v.y);
96 inline D3DXVECTOR2 D3DXVECTOR2::operator - (CONST D3DXVECTOR2& v) const
98 return D3DXVECTOR2(x - v.x, y - v.y);
101 inline D3DXVECTOR2 D3DXVECTOR2::operator * (FLOAT f) const
103 return D3DXVECTOR2(x * f, y * f);
106 inline D3DXVECTOR2 D3DXVECTOR2::operator / (FLOAT f) const
108 return D3DXVECTOR2(x / f, y / f);
111 inline D3DXVECTOR2 operator * (FLOAT f, CONST D3DXVECTOR2& v)
113 return D3DXVECTOR2(f * v.x, f * v.y);
116 inline BOOL D3DXVECTOR2::operator == (CONST D3DXVECTOR2& v) const
118 return x == v.x && y == v.y;
121 inline BOOL D3DXVECTOR2::operator != (CONST D3DXVECTOR2& v) const
123 return x != v.x || y != v.y;
126 inline D3DXVECTOR3::D3DXVECTOR3()
130 inline D3DXVECTOR3::D3DXVECTOR3(CONST FLOAT *pf)
138 inline D3DXVECTOR3::D3DXVECTOR3(CONST D3DVECTOR& v)
145 inline D3DXVECTOR3::D3DXVECTOR3(FLOAT fx, FLOAT fy, FLOAT fz)
152 inline D3DXVECTOR3::operator FLOAT* ()
157 inline D3DXVECTOR3::operator CONST FLOAT* () const
159 return (CONST FLOAT*)&x;
162 inline D3DXVECTOR3& D3DXVECTOR3::operator += (CONST D3DXVECTOR3& v)
170 inline D3DXVECTOR3& D3DXVECTOR3::operator -= (CONST D3DXVECTOR3& v)
178 inline D3DXVECTOR3& D3DXVECTOR3::operator *= (FLOAT f)
186 inline D3DXVECTOR3& D3DXVECTOR3::operator /= (FLOAT f)
194 inline D3DXVECTOR3 D3DXVECTOR3::operator + () const
199 inline D3DXVECTOR3 D3DXVECTOR3::operator - () const
201 return D3DXVECTOR3(-x, -y, -z);
204 inline D3DXVECTOR3 D3DXVECTOR3::operator + (CONST D3DXVECTOR3& v) const
206 return D3DXVECTOR3(x + v.x, y + v.y, z + v.z);
209 inline D3DXVECTOR3 D3DXVECTOR3::operator - (CONST D3DXVECTOR3& v) const
211 return D3DXVECTOR3(x - v.x, y - v.y, z - v.z);
214 inline D3DXVECTOR3 D3DXVECTOR3::operator * (FLOAT f) const
216 return D3DXVECTOR3(x * f, y * f, z * f);
219 inline D3DXVECTOR3 D3DXVECTOR3::operator / (FLOAT f) const
221 return D3DXVECTOR3(x / f, y / f, z / f);
224 inline D3DXVECTOR3 operator * (FLOAT f, CONST D3DXVECTOR3& v)
226 return D3DXVECTOR3(f * v.x, f * v.y, f * v.z);
229 inline BOOL D3DXVECTOR3::operator == (CONST D3DXVECTOR3& v) const
231 return x == v.x && y == v.y && z == v.z;
234 inline BOOL D3DXVECTOR3::operator != (CONST D3DXVECTOR3& v) const
236 return x != v.x || y != v.y || z != v.z;
239 inline D3DXVECTOR4::D3DXVECTOR4()
243 inline D3DXVECTOR4::D3DXVECTOR4(CONST FLOAT *pf)
252 inline D3DXVECTOR4::D3DXVECTOR4(FLOAT fx, FLOAT fy, FLOAT fz, FLOAT fw)
260 inline D3DXVECTOR4::operator FLOAT* ()
265 inline D3DXVECTOR4::operator CONST FLOAT* () const
267 return (CONST FLOAT*)&x;
270 inline D3DXVECTOR4& D3DXVECTOR4::operator += (CONST D3DXVECTOR4& v)
279 inline D3DXVECTOR4& D3DXVECTOR4::operator -= (CONST D3DXVECTOR4& v)
288 inline D3DXVECTOR4& D3DXVECTOR4::operator *= (FLOAT f)
297 inline D3DXVECTOR4& D3DXVECTOR4::operator /= (FLOAT f)
306 inline D3DXVECTOR4 D3DXVECTOR4::operator + () const
311 inline D3DXVECTOR4 D3DXVECTOR4::operator - () const
313 return D3DXVECTOR4(-x, -y, -z, -w);
316 inline D3DXVECTOR4 D3DXVECTOR4::operator + (CONST D3DXVECTOR4& v) const
318 return D3DXVECTOR4(x + v.x, y + v.y, z + v.z, w + v.w);
321 inline D3DXVECTOR4 D3DXVECTOR4::operator - (CONST D3DXVECTOR4& v) const
323 return D3DXVECTOR4(x - v.x, y - v.y, z - v.z, w - v.w);
326 inline D3DXVECTOR4 D3DXVECTOR4::operator * (FLOAT f) const
328 return D3DXVECTOR4(x * f, y * f, z * f, w * f);
331 inline D3DXVECTOR4 D3DXVECTOR4::operator / (FLOAT f) const
333 return D3DXVECTOR4(x / f, y / f, z / f, w / f);
336 inline D3DXVECTOR4 operator * (FLOAT f, CONST D3DXVECTOR4& v)
338 return D3DXVECTOR4(f * v.x, f * v.y, f * v.z, f * v.w);
341 inline BOOL D3DXVECTOR4::operator == (CONST D3DXVECTOR4& v) const
343 return x == v.x && y == v.y && z == v.z && w == v.w;
346 inline BOOL D3DXVECTOR4::operator != (CONST D3DXVECTOR4& v) const
348 return x != v.x || y != v.y || z != v.z || w != v.w;
351 inline D3DXMATRIX::D3DXMATRIX()
355 inline D3DXMATRIX::D3DXMATRIX(CONST FLOAT *pf)
358 memcpy(&_11, pf, sizeof(D3DXMATRIX));
361 inline D3DXMATRIX::D3DXMATRIX(CONST D3DMATRIX& mat)
363 memcpy(&_11, &mat, sizeof(D3DXMATRIX));
366 inline D3DXMATRIX::D3DXMATRIX(FLOAT f11, FLOAT f12, FLOAT f13, FLOAT f14,
367 FLOAT f21, FLOAT f22, FLOAT f23, FLOAT f24,
368 FLOAT f31, FLOAT f32, FLOAT f33, FLOAT f34,
369 FLOAT f41, FLOAT f42, FLOAT f43, FLOAT f44)
371 _11 = f11; _12 = f12; _13 = f13; _14 = f14;
372 _21 = f21; _22 = f22; _23 = f23; _24 = f24;
373 _31 = f31; _32 = f32; _33 = f33; _34 = f34;
374 _41 = f41; _42 = f42; _43 = f43; _44 = f44;
377 inline FLOAT& D3DXMATRIX::operator () (UINT row, UINT col)
382 inline FLOAT D3DXMATRIX::operator () (UINT row, UINT col) const
387 inline D3DXMATRIX::operator FLOAT* ()
392 inline D3DXMATRIX::operator CONST FLOAT* () const
394 return (CONST FLOAT*)&_11;
397 inline D3DXMATRIX& D3DXMATRIX::operator *= (CONST D3DXMATRIX& mat)
399 D3DXMatrixMultiply(this, this, &mat);
403 inline D3DXMATRIX& D3DXMATRIX::operator += (CONST D3DXMATRIX& mat)
405 _11 += mat._11; _12 += mat._12; _13 += mat._13; _14 += mat._14;
406 _21 += mat._21; _22 += mat._22; _23 += mat._23; _24 += mat._24;
407 _31 += mat._31; _32 += mat._32; _33 += mat._33; _34 += mat._34;
408 _41 += mat._41; _42 += mat._42; _43 += mat._43; _44 += mat._44;
412 inline D3DXMATRIX& D3DXMATRIX::operator -= (CONST D3DXMATRIX& mat)
414 _11 -= mat._11; _12 -= mat._12; _13 -= mat._13; _14 -= mat._14;
415 _21 -= mat._21; _22 -= mat._22; _23 -= mat._23; _24 -= mat._24;
416 _31 -= mat._31; _32 -= mat._32; _33 -= mat._33; _34 -= mat._34;
417 _41 -= mat._41; _42 -= mat._42; _43 -= mat._43; _44 -= mat._44;
421 inline D3DXMATRIX& D3DXMATRIX::operator *= (FLOAT f)
423 _11 *= f; _12 *= f; _13 *= f; _14 *= f;
424 _21 *= f; _22 *= f; _23 *= f; _24 *= f;
425 _31 *= f; _32 *= f; _33 *= f; _34 *= f;
426 _41 *= f; _42 *= f; _43 *= f; _44 *= f;
430 inline D3DXMATRIX& D3DXMATRIX::operator /= (FLOAT f)
432 FLOAT inv = 1.0f / f;
433 _11 *= inv; _12 *= inv; _13 *= inv; _14 *= inv;
434 _21 *= inv; _22 *= inv; _23 *= inv; _24 *= inv;
435 _31 *= inv; _32 *= inv; _33 *= inv; _34 *= inv;
436 _41 *= inv; _42 *= inv; _43 *= inv; _44 *= inv;
440 inline D3DXMATRIX D3DXMATRIX::operator + () const
445 inline D3DXMATRIX D3DXMATRIX::operator - () const
447 return D3DXMATRIX(-_11, -_12, -_13, -_14,
448 -_21, -_22, -_23, -_24,
449 -_31, -_32, -_33, -_34,
450 -_41, -_42, -_43, -_44);
453 inline D3DXMATRIX D3DXMATRIX::operator * (CONST D3DXMATRIX& mat) const
456 D3DXMatrixMultiply(&buf, this, &mat);
460 inline D3DXMATRIX D3DXMATRIX::operator + (CONST D3DXMATRIX& mat) const
462 return D3DXMATRIX(_11 + mat._11, _12 + mat._12, _13 + mat._13, _14 + mat._14,
463 _21 + mat._21, _22 + mat._22, _23 + mat._23, _24 + mat._24,
464 _31 + mat._31, _32 + mat._32, _33 + mat._33, _34 + mat._34,
465 _41 + mat._41, _42 + mat._42, _43 + mat._43, _44 + mat._44);
468 inline D3DXMATRIX D3DXMATRIX::operator - (CONST D3DXMATRIX& mat) const
470 return D3DXMATRIX(_11 - mat._11, _12 - mat._12, _13 - mat._13, _14 - mat._14,
471 _21 - mat._21, _22 - mat._22, _23 - mat._23, _24 - mat._24,
472 _31 - mat._31, _32 - mat._32, _33 - mat._33, _34 - mat._34,
473 _41 - mat._41, _42 - mat._42, _43 - mat._43, _44 - mat._44);
476 inline D3DXMATRIX D3DXMATRIX::operator * (FLOAT f) const
478 return D3DXMATRIX(_11 * f, _12 * f, _13 * f, _14 * f,
479 _21 * f, _22 * f, _23 * f, _24 * f,
480 _31 * f, _32 * f, _33 * f, _34 * f,
481 _41 * f, _42 * f, _43 * f, _44 * f);
484 inline D3DXMATRIX D3DXMATRIX::operator / (FLOAT f) const
486 FLOAT inv = 1.0f / f;
487 return D3DXMATRIX(_11 * inv, _12 * inv, _13 * inv, _14 * inv,
488 _21 * inv, _22 * inv, _23 * inv, _24 * inv,
489 _31 * inv, _32 * inv, _33 * inv, _34 * inv,
490 _41 * inv, _42 * inv, _43 * inv, _44 * inv);
493 inline D3DXMATRIX operator * (FLOAT f, CONST D3DXMATRIX& mat)
495 return D3DXMATRIX(f * mat._11, f * mat._12, f * mat._13, f * mat._14,
496 f * mat._21, f * mat._22, f * mat._23, f * mat._24,
497 f * mat._31, f * mat._32, f * mat._33, f * mat._34,
498 f * mat._41, f * mat._42, f * mat._43, f * mat._44);
501 inline BOOL D3DXMATRIX::operator == (CONST D3DXMATRIX& mat) const
503 return (memcmp(this, &mat, sizeof(D3DXMATRIX)) == 0);
506 inline BOOL D3DXMATRIX::operator != (CONST D3DXMATRIX& mat) const
508 return (memcmp(this, &mat, sizeof(D3DXMATRIX)) != 0);
511 inline D3DXQUATERNION::D3DXQUATERNION()
515 inline D3DXQUATERNION::D3DXQUATERNION(CONST FLOAT *pf)
524 inline D3DXQUATERNION::D3DXQUATERNION(FLOAT fx, FLOAT fy, FLOAT fz, FLOAT fw)
532 inline D3DXQUATERNION::operator FLOAT* ()
537 inline D3DXQUATERNION::operator CONST FLOAT* () const
539 return (CONST FLOAT*)&x;
542 inline D3DXQUATERNION& D3DXQUATERNION::operator += (CONST D3DXQUATERNION& quat)
551 inline D3DXQUATERNION& D3DXQUATERNION::operator -= (CONST D3DXQUATERNION& quat)
560 /* TODO: uncomment this when D3DXQuaternionMultiply has been implemented
561 inline D3DXQUATERNION& D3DXQUATERNION::operator *= (CONST D3DXQUATERNION& quat)
563 D3DXQuaternionMultiply(this, this, &quat);
568 inline D3DXQUATERNION& D3DXQUATERNION::operator *= (FLOAT f)
577 inline D3DXQUATERNION& D3DXQUATERNION::operator /= (FLOAT f)
579 FLOAT inv = 1.0f / f;
587 inline D3DXQUATERNION D3DXQUATERNION::operator + () const
592 inline D3DXQUATERNION D3DXQUATERNION::operator - () const
594 return D3DXQUATERNION(-x, -y, -z, -w);
597 inline D3DXQUATERNION D3DXQUATERNION::operator + (CONST D3DXQUATERNION& quat) const
599 return D3DXQUATERNION(x + quat.x, y + quat.y, z + quat.z, w + quat.w);
602 inline D3DXQUATERNION D3DXQUATERNION::operator - (CONST D3DXQUATERNION& quat) const
604 return D3DXQUATERNION(x - quat.x, y - quat.y, z - quat.z, w - quat.w);
607 /* TODO: uncomment this when D3DXQuaternionMultiply has been implemented
608 inline D3DXQUATERNION D3DXQUATERNION::operator * (CONST D3DXQUATERNION& quat) const
611 D3DXQuaternionMultiply(&buf, this, &quat);
616 inline D3DXQUATERNION D3DXQUATERNION::operator * (FLOAT f) const
618 return D3DXQUATERNION(x * f, y * f, z * f, w * f);
621 inline D3DXQUATERNION D3DXQUATERNION::operator / (FLOAT f) const
623 FLOAT inv = 1.0f / f;
624 return D3DXQUATERNION(x * inv, y * inv, z * inv, w * inv);
627 inline D3DXQUATERNION operator * (FLOAT f, CONST D3DXQUATERNION& quat)
629 return D3DXQUATERNION(f * quat.x, f * quat.y, f * quat.z, f * quat.w);
632 inline BOOL D3DXQUATERNION::operator == (CONST D3DXQUATERNION& quat) const
634 return x == quat.x && y == quat.y && z == quat.z && w == quat.w;
637 inline BOOL D3DXQUATERNION::operator != (CONST D3DXQUATERNION& quat) const
639 return x != quat.x || y != quat.y || z != quat.z || w != quat.w;
642 inline D3DXPLANE::D3DXPLANE()
646 inline D3DXPLANE::D3DXPLANE(CONST FLOAT *pf)
655 inline D3DXPLANE::D3DXPLANE(FLOAT fa, FLOAT fb, FLOAT fc, FLOAT fd)
663 inline D3DXPLANE::operator FLOAT* ()
668 inline D3DXPLANE::operator CONST FLOAT* () const
670 return (CONST FLOAT*)&a;
673 inline D3DXPLANE D3DXPLANE::operator + () const
678 inline D3DXPLANE D3DXPLANE::operator - () const
680 return D3DXPLANE(-a, -b, -c, -d);
683 inline BOOL D3DXPLANE::operator == (CONST D3DXPLANE& pl) const
685 return a == pl.a && b == pl.b && c == pl.c && d == pl.d;
688 inline BOOL D3DXPLANE::operator != (CONST D3DXPLANE& pl) const
690 return a != pl.a || b != pl.b || c != pl.c || d != pl.d;
693 inline D3DXCOLOR::D3DXCOLOR()
697 inline D3DXCOLOR::D3DXCOLOR(DWORD col)
699 CONST FLOAT f = 1.0f / 255.0f;
700 r = f * (FLOAT)(unsigned char)(col >> 16);
701 g = f * (FLOAT)(unsigned char)(col >> 8);
702 b = f * (FLOAT)(unsigned char)col;
703 a = f * (FLOAT)(unsigned char)(col >> 24);
706 inline D3DXCOLOR::D3DXCOLOR(CONST FLOAT *pf)
715 inline D3DXCOLOR::D3DXCOLOR(CONST D3DCOLORVALUE& col)
723 inline D3DXCOLOR::D3DXCOLOR(FLOAT fr, FLOAT fg, FLOAT fb, FLOAT fa)
731 inline D3DXCOLOR::operator DWORD () const
733 DWORD _r = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (DWORD)(r * 255.0f + 0.5f);
734 DWORD _g = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (DWORD)(g * 255.0f + 0.5f);
735 DWORD _b = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (DWORD)(b * 255.0f + 0.5f);
736 DWORD _a = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (DWORD)(a * 255.0f + 0.5f);
738 return (_a << 24) | (_r << 16) | (_g << 8) | _b;
741 inline D3DXCOLOR::operator FLOAT * ()
746 inline D3DXCOLOR::operator CONST FLOAT * () const
748 return (CONST FLOAT*)&r;
751 inline D3DXCOLOR::operator D3DCOLORVALUE * ()
753 return (D3DCOLORVALUE*)&r;
756 inline D3DXCOLOR::operator CONST D3DCOLORVALUE * () const
758 return (CONST D3DCOLORVALUE*)&r;
761 inline D3DXCOLOR::operator D3DCOLORVALUE& ()
763 return *((D3DCOLORVALUE*)&r);
766 inline D3DXCOLOR::operator CONST D3DCOLORVALUE& () const
768 return *((CONST D3DCOLORVALUE*)&r);
771 inline D3DXCOLOR& D3DXCOLOR::operator += (CONST D3DXCOLOR& col)
780 inline D3DXCOLOR& D3DXCOLOR::operator -= (CONST D3DXCOLOR& col)
789 inline D3DXCOLOR& D3DXCOLOR::operator *= (FLOAT f)
798 inline D3DXCOLOR& D3DXCOLOR::operator /= (FLOAT f)
800 FLOAT inv = 1.0f / f;
808 inline D3DXCOLOR D3DXCOLOR::operator + () const
813 inline D3DXCOLOR D3DXCOLOR::operator - () const
815 return D3DXCOLOR(-r, -g, -b, -a);
818 inline D3DXCOLOR D3DXCOLOR::operator + (CONST D3DXCOLOR& col) const
820 return D3DXCOLOR(r + col.r, g + col.g, b + col.b, a + col.a);
823 inline D3DXCOLOR D3DXCOLOR::operator - (CONST D3DXCOLOR& col) const
825 return D3DXCOLOR(r - col.r, g - col.g, b - col.b, a - col.a);
828 inline D3DXCOLOR D3DXCOLOR::operator * (FLOAT f) const
830 return D3DXCOLOR(r * f, g * f, b * f, a * f);
833 inline D3DXCOLOR D3DXCOLOR::operator / (FLOAT f) const
835 FLOAT inv = 1.0f / f;
836 return D3DXCOLOR(r * inv, g * inv, b * inv, a * inv);
839 inline D3DXCOLOR operator * (FLOAT f, CONST D3DXCOLOR& col)
841 return D3DXCOLOR(f * col.r, f * col.g, f * col.b, f * col.a);
844 inline BOOL D3DXCOLOR::operator == (CONST D3DXCOLOR& col) const
846 return r == col.r && g == col.g && b == col.b && a == col.a;
849 inline BOOL D3DXCOLOR::operator != (CONST D3DXCOLOR& col) const
851 return r != col.r || g != col.g || b != col.b || a != col.a;
854 inline D3DXFLOAT16::D3DXFLOAT16()
858 inline D3DXFLOAT16::D3DXFLOAT16(FLOAT f)
860 D3DXFloat32To16Array(this, &f, 1);
863 inline D3DXFLOAT16::D3DXFLOAT16(CONST D3DXFLOAT16 &f)
868 inline D3DXFLOAT16::operator FLOAT ()
871 D3DXFloat16To32Array(&f, this, 1);
875 inline BOOL D3DXFLOAT16::operator == (CONST D3DXFLOAT16 &f) const
877 return value == f.value;
880 inline BOOL D3DXFLOAT16::operator != (CONST D3DXFLOAT16 &f) const
882 return value != f.value;
885 #endif /* __cplusplus */
887 /*_______________D3DXCOLOR_____________________*/
889 static inline D3DXCOLOR* D3DXColorAdd(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
891 if ( !pout || !pc1 || !pc2 ) return NULL;
892 pout->r = (pc1->r) + (pc2->r);
893 pout->g = (pc1->g) + (pc2->g);
894 pout->b = (pc1->b) + (pc2->b);
895 pout->a = (pc1->a) + (pc2->a);
899 static inline D3DXCOLOR* D3DXColorLerp(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2, FLOAT s)
901 if ( !pout || !pc1 || !pc2 ) return NULL;
902 pout->r = (1-s) * (pc1->r) + s *(pc2->r);
903 pout->g = (1-s) * (pc1->g) + s *(pc2->g);
904 pout->b = (1-s) * (pc1->b) + s *(pc2->b);
905 pout->a = (1-s) * (pc1->a) + s *(pc2->a);
909 static inline D3DXCOLOR* D3DXColorModulate(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
911 if ( !pout || !pc1 || !pc2 ) return NULL;
912 pout->r = (pc1->r) * (pc2->r);
913 pout->g = (pc1->g) * (pc2->g);
914 pout->b = (pc1->b) * (pc2->b);
915 pout->a = (pc1->a) * (pc2->a);
919 static inline D3DXCOLOR* D3DXColorNegative(D3DXCOLOR *pout, CONST D3DXCOLOR *pc)
921 if ( !pout || !pc ) return NULL;
922 pout->r = 1.0f - pc->r;
923 pout->g = 1.0f - pc->g;
924 pout->b = 1.0f - pc->b;
929 static inline D3DXCOLOR* D3DXColorScale(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
931 if ( !pout || !pc ) return NULL;
932 pout->r = s* (pc->r);
933 pout->g = s* (pc->g);
934 pout->b = s* (pc->b);
935 pout->a = s* (pc->a);
939 static inline D3DXCOLOR* D3DXColorSubtract(D3DXCOLOR *pout, CONST D3DXCOLOR *pc1, CONST D3DXCOLOR *pc2)
941 if ( !pout || !pc1 || !pc2 ) return NULL;
942 pout->r = (pc1->r) - (pc2->r);
943 pout->g = (pc1->g) - (pc2->g);
944 pout->b = (pc1->b) - (pc2->b);
945 pout->a = (pc1->a) - (pc2->a);
949 /*_______________D3DXVECTOR2________________________*/
951 static inline D3DXVECTOR2* D3DXVec2Add(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
953 if ( !pout || !pv1 || !pv2) return NULL;
954 pout->x = pv1->x + pv2->x;
955 pout->y = pv1->y + pv2->y;
959 static inline FLOAT D3DXVec2CCW(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
961 if ( !pv1 || !pv2) return 0.0f;
962 return ( (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x) );
965 static inline FLOAT D3DXVec2Dot(CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
967 if ( !pv1 || !pv2) return 0.0f;
968 return ( (pv1->x * pv2->x + pv1->y * pv2->y) );
971 static inline FLOAT D3DXVec2Length(CONST D3DXVECTOR2 *pv)
973 if (!pv) return 0.0f;
974 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
977 static inline FLOAT D3DXVec2LengthSq(CONST D3DXVECTOR2 *pv)
979 if (!pv) return 0.0f;
980 return( (pv->x) * (pv->x) + (pv->y) * (pv->y) );
983 static inline D3DXVECTOR2* D3DXVec2Lerp(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, FLOAT s)
985 if ( !pout || !pv1 || !pv2) return NULL;
986 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
987 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
991 static inline D3DXVECTOR2* D3DXVec2Maximize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
993 if ( !pout || !pv1 || !pv2) return NULL;
994 pout->x = pv1->x > pv2->x ? pv1->x : pv2->x;
995 pout->y = pv1->y > pv2->y ? pv1->y : pv2->y;
999 static inline D3DXVECTOR2* D3DXVec2Minimize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
1001 if ( !pout || !pv1 || !pv2) return NULL;
1002 pout->x = pv1->x < pv2->x ? pv1->x : pv2->x;
1003 pout->y = pv1->y < pv2->y ? pv1->y : pv2->y;
1007 static inline D3DXVECTOR2* D3DXVec2Scale(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, FLOAT s)
1009 if ( !pout || !pv) return NULL;
1010 pout->x = s * (pv->x);
1011 pout->y = s * (pv->y);
1015 static inline D3DXVECTOR2* D3DXVec2Subtract(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2)
1017 if ( !pout || !pv1 || !pv2) return NULL;
1018 pout->x = pv1->x - pv2->x;
1019 pout->y = pv1->y - pv2->y;
1023 /*__________________D3DXVECTOR3_______________________*/
1025 static inline D3DXVECTOR3* D3DXVec3Add(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1027 if ( !pout || !pv1 || !pv2) return NULL;
1028 pout->x = pv1->x + pv2->x;
1029 pout->y = pv1->y + pv2->y;
1030 pout->z = pv1->z + pv2->z;
1034 static inline D3DXVECTOR3* D3DXVec3Cross(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1036 if ( !pout || !pv1 || !pv2) return NULL;
1037 pout->x = (pv1->y) * (pv2->z) - (pv1->z) * (pv2->y);
1038 pout->y = (pv1->z) * (pv2->x) - (pv1->x) * (pv2->z);
1039 pout->z = (pv1->x) * (pv2->y) - (pv1->y) * (pv2->x);
1043 static inline FLOAT D3DXVec3Dot(CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1045 if ( !pv1 || !pv2 ) return 0.0f;
1046 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z);
1049 static inline FLOAT D3DXVec3Length(CONST D3DXVECTOR3 *pv)
1051 if (!pv) return 0.0f;
1052 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) );
1055 static inline FLOAT D3DXVec3LengthSq(CONST D3DXVECTOR3 *pv)
1057 if (!pv) return 0.0f;
1058 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z);
1061 static inline D3DXVECTOR3* D3DXVec3Lerp(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, FLOAT s)
1063 if ( !pout || !pv1 || !pv2) return NULL;
1064 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
1065 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
1066 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
1070 static inline D3DXVECTOR3* D3DXVec3Maximize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1072 if ( !pout || !pv1 || !pv2) return NULL;
1073 pout->x = pv1->x > pv2->x ? pv1->x : pv2->x;
1074 pout->y = pv1->y > pv2->y ? pv1->y : pv2->y;
1075 pout->z = pv1->z > pv2->z ? pv1->z : pv2->z;
1079 static inline D3DXVECTOR3* D3DXVec3Minimize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1081 if ( !pout || !pv1 || !pv2) return NULL;
1082 pout->x = pv1->x < pv2->x ? pv1->x : pv2->x;
1083 pout->y = pv1->y < pv2->y ? pv1->y : pv2->y;
1084 pout->z = pv1->z < pv2->z ? pv1->z : pv2->z;
1088 static inline D3DXVECTOR3* D3DXVec3Scale(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, FLOAT s)
1090 if ( !pout || !pv) return NULL;
1091 pout->x = s * (pv->x);
1092 pout->y = s * (pv->y);
1093 pout->z = s * (pv->z);
1097 static inline D3DXVECTOR3* D3DXVec3Subtract(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1099 if ( !pout || !pv1 || !pv2) return NULL;
1100 pout->x = pv1->x - pv2->x;
1101 pout->y = pv1->y - pv2->y;
1102 pout->z = pv1->z - pv2->z;
1105 /*__________________D3DXVECTOR4_______________________*/
1107 static inline D3DXVECTOR4* D3DXVec4Add(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
1109 if ( !pout || !pv1 || !pv2) return NULL;
1110 pout->x = pv1->x + pv2->x;
1111 pout->y = pv1->y + pv2->y;
1112 pout->z = pv1->z + pv2->z;
1113 pout->w = pv1->w + pv2->w;
1117 static inline FLOAT D3DXVec4Dot(CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
1119 if (!pv1 || !pv2 ) return 0.0f;
1120 return (pv1->x) * (pv2->x) + (pv1->y) * (pv2->y) + (pv1->z) * (pv2->z) + (pv1->w) * (pv2->w);
1123 static inline FLOAT D3DXVec4Length(CONST D3DXVECTOR4 *pv)
1125 if (!pv) return 0.0f;
1126 return sqrt( (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w) );
1129 static inline FLOAT D3DXVec4LengthSq(CONST D3DXVECTOR4 *pv)
1131 if (!pv) return 0.0f;
1132 return (pv->x) * (pv->x) + (pv->y) * (pv->y) + (pv->z) * (pv->z) + (pv->w) * (pv->w);
1135 static inline D3DXVECTOR4* D3DXVec4Lerp(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, FLOAT s)
1137 if ( !pout || !pv1 || !pv2) return NULL;
1138 pout->x = (1-s) * (pv1->x) + s * (pv2->x);
1139 pout->y = (1-s) * (pv1->y) + s * (pv2->y);
1140 pout->z = (1-s) * (pv1->z) + s * (pv2->z);
1141 pout->w = (1-s) * (pv1->w) + s * (pv2->w);
1146 static inline D3DXVECTOR4* D3DXVec4Maximize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
1148 if ( !pout || !pv1 || !pv2) return NULL;
1149 pout->x = pv1->x > pv2->x ? pv1->x : pv2->x;
1150 pout->y = pv1->y > pv2->y ? pv1->y : pv2->y;
1151 pout->z = pv1->z > pv2->z ? pv1->z : pv2->z;
1152 pout->w = pv1->w > pv2->w ? pv1->w : pv2->w;
1156 static inline D3DXVECTOR4* D3DXVec4Minimize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
1158 if ( !pout || !pv1 || !pv2) return NULL;
1159 pout->x = pv1->x < pv2->x ? pv1->x : pv2->x;
1160 pout->y = pv1->y < pv2->y ? pv1->y : pv2->y;
1161 pout->z = pv1->z < pv2->z ? pv1->z : pv2->z;
1162 pout->w = pv1->w < pv2->w ? pv1->w : pv2->w;
1166 static inline D3DXVECTOR4* D3DXVec4Scale(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, FLOAT s)
1168 if ( !pout || !pv) return NULL;
1169 pout->x = s * (pv->x);
1170 pout->y = s * (pv->y);
1171 pout->z = s * (pv->z);
1172 pout->w = s * (pv->w);
1176 static inline D3DXVECTOR4* D3DXVec4Subtract(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2)
1178 if ( !pout || !pv1 || !pv2) return NULL;
1179 pout->x = pv1->x - pv2->x;
1180 pout->y = pv1->y - pv2->y;
1181 pout->z = pv1->z - pv2->z;
1182 pout->w = pv1->w - pv2->w;
1186 /*__________________D3DXMatrix____________________*/
1187 #ifdef NONAMELESSUNION
1188 # define D3DX_U(x) (x).u
1190 # define D3DX_U(x) (x)
1193 static inline D3DXMATRIX* D3DXMatrixIdentity(D3DXMATRIX *pout)
1195 if ( !pout ) return NULL;
1196 D3DX_U(*pout).m[0][1] = 0.0f;
1197 D3DX_U(*pout).m[0][2] = 0.0f;
1198 D3DX_U(*pout).m[0][3] = 0.0f;
1199 D3DX_U(*pout).m[1][0] = 0.0f;
1200 D3DX_U(*pout).m[1][2] = 0.0f;
1201 D3DX_U(*pout).m[1][3] = 0.0f;
1202 D3DX_U(*pout).m[2][0] = 0.0f;
1203 D3DX_U(*pout).m[2][1] = 0.0f;
1204 D3DX_U(*pout).m[2][3] = 0.0f;
1205 D3DX_U(*pout).m[3][0] = 0.0f;
1206 D3DX_U(*pout).m[3][1] = 0.0f;
1207 D3DX_U(*pout).m[3][2] = 0.0f;
1208 D3DX_U(*pout).m[0][0] = 1.0f;
1209 D3DX_U(*pout).m[1][1] = 1.0f;
1210 D3DX_U(*pout).m[2][2] = 1.0f;
1211 D3DX_U(*pout).m[3][3] = 1.0f;
1215 static inline BOOL D3DXMatrixIsIdentity(D3DXMATRIX *pm)
1218 D3DXMATRIX testmatrix;
1220 if ( !pm ) return FALSE;
1221 D3DXMatrixIdentity(&testmatrix);
1226 if ( D3DX_U(*pm).m[i][j] != D3DX_U(testmatrix).m[i][j] ) return FALSE;
1233 /*__________________D3DXPLANE____________________*/
1235 static inline FLOAT D3DXPlaneDot(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
1237 if ( !pp || !pv ) return 0.0f;
1238 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) * (pv->w) );
1241 static inline FLOAT D3DXPlaneDotCoord(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
1243 if ( !pp || !pv ) return 0.0f;
1244 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) + (pp->d) );
1247 static inline FLOAT D3DXPlaneDotNormal(CONST D3DXPLANE *pp, CONST D3DXVECTOR4 *pv)
1249 if ( !pp || !pv ) return 0.0f;
1250 return ( (pp->a) * (pv->x) + (pp->b) * (pv->y) + (pp->c) * (pv->z) );
1253 /*__________________D3DXQUATERNION____________________*/
1255 static inline D3DXQUATERNION* D3DXQuaternionConjugate(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1257 if ( !pout || !pq) return NULL;
1265 static inline FLOAT D3DXQuaternionDot(CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1267 if ( !pq1 || !pq2 ) return 0.0f;
1268 return (pq1->x) * (pq2->x) + (pq1->y) * (pq2->y) + (pq1->z) * (pq2->z) + (pq1->w) * (pq2->w);
1271 static inline D3DXQUATERNION* D3DXQuaternionIdentity(D3DXQUATERNION *pout)
1273 if ( !pout) return NULL;
1281 static inline BOOL D3DXQuaternionIsIdentity(D3DXQUATERNION *pq)
1283 if ( !pq) return FALSE;
1284 return ( (pq->x == 0.0f) && (pq->y == 0.0f) && (pq->z == 0.0f) && (pq->w == 1.0f) );
1287 static inline FLOAT D3DXQuaternionLength(CONST D3DXQUATERNION *pq)
1289 if (!pq) return 0.0f;
1290 return sqrt( (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w) );
1293 static inline FLOAT D3DXQuaternionLengthSq(CONST D3DXQUATERNION *pq)
1295 if (!pq) return 0.0f;
1296 return (pq->x) * (pq->x) + (pq->y) * (pq->y) + (pq->z) * (pq->z) + (pq->w) * (pq->w);