Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1757 lines
34 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: d3dx8math.inl
  6. // Content: D3DX math inline functions
  7. //
  8. //////////////////////////////////////////////////////////////////////////////
  9. #ifndef __D3DX8MATH_INL__
  10. #define __D3DX8MATH_INL__
  11. //===========================================================================
  12. //
  13. // Inline Class Methods
  14. //
  15. //===========================================================================
  16. #ifdef __cplusplus
  17. //--------------------------
  18. // 2D Vector
  19. //--------------------------
  20. D3DXINLINE
  21. D3DXVECTOR2::D3DXVECTOR2( CONST FLOAT *pf )
  22. {
  23. #ifdef D3DX_DEBUG
  24. if(!pf)
  25. return;
  26. #endif
  27. x = pf[0];
  28. y = pf[1];
  29. }
  30. D3DXINLINE
  31. D3DXVECTOR2::D3DXVECTOR2( FLOAT fx, FLOAT fy )
  32. {
  33. x = fx;
  34. y = fy;
  35. }
  36. // casting
  37. D3DXINLINE
  38. D3DXVECTOR2::operator FLOAT* ()
  39. {
  40. return (FLOAT *) &x;
  41. }
  42. D3DXINLINE
  43. D3DXVECTOR2::operator CONST FLOAT* () const
  44. {
  45. return (CONST FLOAT *) &x;
  46. }
  47. // assignment operators
  48. D3DXINLINE D3DXVECTOR2&
  49. D3DXVECTOR2::operator += ( CONST D3DXVECTOR2& v )
  50. {
  51. x += v.x;
  52. y += v.y;
  53. return *this;
  54. }
  55. D3DXINLINE D3DXVECTOR2&
  56. D3DXVECTOR2::operator -= ( CONST D3DXVECTOR2& v )
  57. {
  58. x -= v.x;
  59. y -= v.y;
  60. return *this;
  61. }
  62. D3DXINLINE D3DXVECTOR2&
  63. D3DXVECTOR2::operator *= ( FLOAT f )
  64. {
  65. x *= f;
  66. y *= f;
  67. return *this;
  68. }
  69. D3DXINLINE D3DXVECTOR2&
  70. D3DXVECTOR2::operator /= ( FLOAT f )
  71. {
  72. FLOAT fInv = 1.0f / f;
  73. x *= fInv;
  74. y *= fInv;
  75. return *this;
  76. }
  77. // unary operators
  78. D3DXINLINE D3DXVECTOR2
  79. D3DXVECTOR2::operator + () const
  80. {
  81. return *this;
  82. }
  83. D3DXINLINE D3DXVECTOR2
  84. D3DXVECTOR2::operator - () const
  85. {
  86. return D3DXVECTOR2(-x, -y);
  87. }
  88. // binary operators
  89. D3DXINLINE D3DXVECTOR2
  90. D3DXVECTOR2::operator + ( CONST D3DXVECTOR2& v ) const
  91. {
  92. return D3DXVECTOR2(x + v.x, y + v.y);
  93. }
  94. D3DXINLINE D3DXVECTOR2
  95. D3DXVECTOR2::operator - ( CONST D3DXVECTOR2& v ) const
  96. {
  97. return D3DXVECTOR2(x - v.x, y - v.y);
  98. }
  99. D3DXINLINE D3DXVECTOR2
  100. D3DXVECTOR2::operator * ( FLOAT f ) const
  101. {
  102. return D3DXVECTOR2(x * f, y * f);
  103. }
  104. D3DXINLINE D3DXVECTOR2
  105. D3DXVECTOR2::operator / ( FLOAT f ) const
  106. {
  107. FLOAT fInv = 1.0f / f;
  108. return D3DXVECTOR2(x * fInv, y * fInv);
  109. }
  110. D3DXINLINE D3DXVECTOR2
  111. operator * ( FLOAT f, CONST D3DXVECTOR2& v )
  112. {
  113. return D3DXVECTOR2(f * v.x, f * v.y);
  114. }
  115. D3DXINLINE BOOL
  116. D3DXVECTOR2::operator == ( CONST D3DXVECTOR2& v ) const
  117. {
  118. return x == v.x && y == v.y;
  119. }
  120. D3DXINLINE BOOL
  121. D3DXVECTOR2::operator != ( CONST D3DXVECTOR2& v ) const
  122. {
  123. return x != v.x || y != v.y;
  124. }
  125. //--------------------------
  126. // 3D Vector
  127. //--------------------------
  128. D3DXINLINE
  129. D3DXVECTOR3::D3DXVECTOR3( CONST FLOAT *pf )
  130. {
  131. #ifdef D3DX_DEBUG
  132. if(!pf)
  133. return;
  134. #endif
  135. x = pf[0];
  136. y = pf[1];
  137. z = pf[2];
  138. }
  139. D3DXINLINE
  140. D3DXVECTOR3::D3DXVECTOR3( CONST D3DVECTOR& v )
  141. {
  142. x = v.x;
  143. y = v.y;
  144. z = v.z;
  145. }
  146. D3DXINLINE
  147. D3DXVECTOR3::D3DXVECTOR3( FLOAT fx, FLOAT fy, FLOAT fz )
  148. {
  149. x = fx;
  150. y = fy;
  151. z = fz;
  152. }
  153. // casting
  154. D3DXINLINE
  155. D3DXVECTOR3::operator FLOAT* ()
  156. {
  157. return (FLOAT *) &x;
  158. }
  159. D3DXINLINE
  160. D3DXVECTOR3::operator CONST FLOAT* () const
  161. {
  162. return (CONST FLOAT *) &x;
  163. }
  164. // assignment operators
  165. D3DXINLINE D3DXVECTOR3&
  166. D3DXVECTOR3::operator += ( CONST D3DXVECTOR3& v )
  167. {
  168. x += v.x;
  169. y += v.y;
  170. z += v.z;
  171. return *this;
  172. }
  173. D3DXINLINE D3DXVECTOR3&
  174. D3DXVECTOR3::operator -= ( CONST D3DXVECTOR3& v )
  175. {
  176. x -= v.x;
  177. y -= v.y;
  178. z -= v.z;
  179. return *this;
  180. }
  181. D3DXINLINE D3DXVECTOR3&
  182. D3DXVECTOR3::operator *= ( FLOAT f )
  183. {
  184. x *= f;
  185. y *= f;
  186. z *= f;
  187. return *this;
  188. }
  189. D3DXINLINE D3DXVECTOR3&
  190. D3DXVECTOR3::operator /= ( FLOAT f )
  191. {
  192. FLOAT fInv = 1.0f / f;
  193. x *= fInv;
  194. y *= fInv;
  195. z *= fInv;
  196. return *this;
  197. }
  198. // unary operators
  199. D3DXINLINE D3DXVECTOR3
  200. D3DXVECTOR3::operator + () const
  201. {
  202. return *this;
  203. }
  204. D3DXINLINE D3DXVECTOR3
  205. D3DXVECTOR3::operator - () const
  206. {
  207. return D3DXVECTOR3(-x, -y, -z);
  208. }
  209. // binary operators
  210. D3DXINLINE D3DXVECTOR3
  211. D3DXVECTOR3::operator + ( CONST D3DXVECTOR3& v ) const
  212. {
  213. return D3DXVECTOR3(x + v.x, y + v.y, z + v.z);
  214. }
  215. D3DXINLINE D3DXVECTOR3
  216. D3DXVECTOR3::operator - ( CONST D3DXVECTOR3& v ) const
  217. {
  218. return D3DXVECTOR3(x - v.x, y - v.y, z - v.z);
  219. }
  220. D3DXINLINE D3DXVECTOR3
  221. D3DXVECTOR3::operator * ( FLOAT f ) const
  222. {
  223. return D3DXVECTOR3(x * f, y * f, z * f);
  224. }
  225. D3DXINLINE D3DXVECTOR3
  226. D3DXVECTOR3::operator / ( FLOAT f ) const
  227. {
  228. FLOAT fInv = 1.0f / f;
  229. return D3DXVECTOR3(x * fInv, y * fInv, z * fInv);
  230. }
  231. D3DXINLINE D3DXVECTOR3
  232. operator * ( FLOAT f, CONST struct D3DXVECTOR3& v )
  233. {
  234. return D3DXVECTOR3(f * v.x, f * v.y, f * v.z);
  235. }
  236. D3DXINLINE BOOL
  237. D3DXVECTOR3::operator == ( CONST D3DXVECTOR3& v ) const
  238. {
  239. return x == v.x && y == v.y && z == v.z;
  240. }
  241. D3DXINLINE BOOL
  242. D3DXVECTOR3::operator != ( CONST D3DXVECTOR3& v ) const
  243. {
  244. return x != v.x || y != v.y || z != v.z;
  245. }
  246. //--------------------------
  247. // 4D Vector
  248. //--------------------------
  249. D3DXINLINE
  250. D3DXVECTOR4::D3DXVECTOR4( CONST FLOAT *pf )
  251. {
  252. #ifdef D3DX_DEBUG
  253. if(!pf)
  254. return;
  255. #endif
  256. x = pf[0];
  257. y = pf[1];
  258. z = pf[2];
  259. w = pf[3];
  260. }
  261. D3DXINLINE
  262. D3DXVECTOR4::D3DXVECTOR4( FLOAT fx, FLOAT fy, FLOAT fz, FLOAT fw )
  263. {
  264. x = fx;
  265. y = fy;
  266. z = fz;
  267. w = fw;
  268. }
  269. // casting
  270. D3DXINLINE
  271. D3DXVECTOR4::operator FLOAT* ()
  272. {
  273. return (FLOAT *) &x;
  274. }
  275. D3DXINLINE
  276. D3DXVECTOR4::operator CONST FLOAT* () const
  277. {
  278. return (CONST FLOAT *) &x;
  279. }
  280. // assignment operators
  281. D3DXINLINE D3DXVECTOR4&
  282. D3DXVECTOR4::operator += ( CONST D3DXVECTOR4& v )
  283. {
  284. x += v.x;
  285. y += v.y;
  286. z += v.z;
  287. w += v.w;
  288. return *this;
  289. }
  290. D3DXINLINE D3DXVECTOR4&
  291. D3DXVECTOR4::operator -= ( CONST D3DXVECTOR4& v )
  292. {
  293. x -= v.x;
  294. y -= v.y;
  295. z -= v.z;
  296. w -= v.w;
  297. return *this;
  298. }
  299. D3DXINLINE D3DXVECTOR4&
  300. D3DXVECTOR4::operator *= ( FLOAT f )
  301. {
  302. x *= f;
  303. y *= f;
  304. z *= f;
  305. w *= f;
  306. return *this;
  307. }
  308. D3DXINLINE D3DXVECTOR4&
  309. D3DXVECTOR4::operator /= ( FLOAT f )
  310. {
  311. FLOAT fInv = 1.0f / f;
  312. x *= fInv;
  313. y *= fInv;
  314. z *= fInv;
  315. w *= fInv;
  316. return *this;
  317. }
  318. // unary operators
  319. D3DXINLINE D3DXVECTOR4
  320. D3DXVECTOR4::operator + () const
  321. {
  322. return *this;
  323. }
  324. D3DXINLINE D3DXVECTOR4
  325. D3DXVECTOR4::operator - () const
  326. {
  327. return D3DXVECTOR4(-x, -y, -z, -w);
  328. }
  329. // binary operators
  330. D3DXINLINE D3DXVECTOR4
  331. D3DXVECTOR4::operator + ( CONST D3DXVECTOR4& v ) const
  332. {
  333. return D3DXVECTOR4(x + v.x, y + v.y, z + v.z, w + v.w);
  334. }
  335. D3DXINLINE D3DXVECTOR4
  336. D3DXVECTOR4::operator - ( CONST D3DXVECTOR4& v ) const
  337. {
  338. return D3DXVECTOR4(x - v.x, y - v.y, z - v.z, w - v.w);
  339. }
  340. D3DXINLINE D3DXVECTOR4
  341. D3DXVECTOR4::operator * ( FLOAT f ) const
  342. {
  343. return D3DXVECTOR4(x * f, y * f, z * f, w * f);
  344. }
  345. D3DXINLINE D3DXVECTOR4
  346. D3DXVECTOR4::operator / ( FLOAT f ) const
  347. {
  348. FLOAT fInv = 1.0f / f;
  349. return D3DXVECTOR4(x * fInv, y * fInv, z * fInv, w * fInv);
  350. }
  351. D3DXINLINE D3DXVECTOR4
  352. operator * ( FLOAT f, CONST D3DXVECTOR4& v )
  353. {
  354. return D3DXVECTOR4(f * v.x, f * v.y, f * v.z, f * v.w);
  355. }
  356. D3DXINLINE BOOL
  357. D3DXVECTOR4::operator == ( CONST D3DXVECTOR4& v ) const
  358. {
  359. return x == v.x && y == v.y && z == v.z && w == v.w;
  360. }
  361. D3DXINLINE BOOL
  362. D3DXVECTOR4::operator != ( CONST D3DXVECTOR4& v ) const
  363. {
  364. return x != v.x || y != v.y || z != v.z || w != v.w;
  365. }
  366. //--------------------------
  367. // Matrix
  368. //--------------------------
  369. D3DXINLINE
  370. D3DXMATRIX::D3DXMATRIX( CONST FLOAT* pf )
  371. {
  372. #ifdef D3DX_DEBUG
  373. if(!pf)
  374. return;
  375. #endif
  376. memcpy(&_11, pf, sizeof(D3DXMATRIX));
  377. }
  378. D3DXINLINE
  379. D3DXMATRIX::D3DXMATRIX( CONST D3DMATRIX& mat )
  380. {
  381. memcpy(&_11, &mat, sizeof(D3DXMATRIX));
  382. }
  383. D3DXINLINE
  384. D3DXMATRIX::D3DXMATRIX( FLOAT f11, FLOAT f12, FLOAT f13, FLOAT f14,
  385. FLOAT f21, FLOAT f22, FLOAT f23, FLOAT f24,
  386. FLOAT f31, FLOAT f32, FLOAT f33, FLOAT f34,
  387. FLOAT f41, FLOAT f42, FLOAT f43, FLOAT f44 )
  388. {
  389. _11 = f11; _12 = f12; _13 = f13; _14 = f14;
  390. _21 = f21; _22 = f22; _23 = f23; _24 = f24;
  391. _31 = f31; _32 = f32; _33 = f33; _34 = f34;
  392. _41 = f41; _42 = f42; _43 = f43; _44 = f44;
  393. }
  394. // access grants
  395. D3DXINLINE FLOAT&
  396. D3DXMATRIX::operator () ( UINT iRow, UINT iCol )
  397. {
  398. return m[iRow][iCol];
  399. }
  400. D3DXINLINE FLOAT
  401. D3DXMATRIX::operator () ( UINT iRow, UINT iCol ) const
  402. {
  403. return m[iRow][iCol];
  404. }
  405. // casting operators
  406. D3DXINLINE
  407. D3DXMATRIX::operator FLOAT* ()
  408. {
  409. return (FLOAT *) &_11;
  410. }
  411. D3DXINLINE
  412. D3DXMATRIX::operator CONST FLOAT* () const
  413. {
  414. return (CONST FLOAT *) &_11;
  415. }
  416. // assignment operators
  417. D3DXINLINE D3DXMATRIX&
  418. D3DXMATRIX::operator *= ( CONST D3DXMATRIX& mat )
  419. {
  420. D3DXMatrixMultiply(this, this, &mat);
  421. return *this;
  422. }
  423. D3DXINLINE D3DXMATRIX&
  424. D3DXMATRIX::operator += ( CONST D3DXMATRIX& mat )
  425. {
  426. _11 += mat._11; _12 += mat._12; _13 += mat._13; _14 += mat._14;
  427. _21 += mat._21; _22 += mat._22; _23 += mat._23; _24 += mat._24;
  428. _31 += mat._31; _32 += mat._32; _33 += mat._33; _34 += mat._34;
  429. _41 += mat._41; _42 += mat._42; _43 += mat._43; _44 += mat._44;
  430. return *this;
  431. }
  432. D3DXINLINE D3DXMATRIX&
  433. D3DXMATRIX::operator -= ( CONST D3DXMATRIX& mat )
  434. {
  435. _11 -= mat._11; _12 -= mat._12; _13 -= mat._13; _14 -= mat._14;
  436. _21 -= mat._21; _22 -= mat._22; _23 -= mat._23; _24 -= mat._24;
  437. _31 -= mat._31; _32 -= mat._32; _33 -= mat._33; _34 -= mat._34;
  438. _41 -= mat._41; _42 -= mat._42; _43 -= mat._43; _44 -= mat._44;
  439. return *this;
  440. }
  441. D3DXINLINE D3DXMATRIX&
  442. D3DXMATRIX::operator *= ( FLOAT f )
  443. {
  444. _11 *= f; _12 *= f; _13 *= f; _14 *= f;
  445. _21 *= f; _22 *= f; _23 *= f; _24 *= f;
  446. _31 *= f; _32 *= f; _33 *= f; _34 *= f;
  447. _41 *= f; _42 *= f; _43 *= f; _44 *= f;
  448. return *this;
  449. }
  450. D3DXINLINE D3DXMATRIX&
  451. D3DXMATRIX::operator /= ( FLOAT f )
  452. {
  453. FLOAT fInv = 1.0f / f;
  454. _11 *= fInv; _12 *= fInv; _13 *= fInv; _14 *= fInv;
  455. _21 *= fInv; _22 *= fInv; _23 *= fInv; _24 *= fInv;
  456. _31 *= fInv; _32 *= fInv; _33 *= fInv; _34 *= fInv;
  457. _41 *= fInv; _42 *= fInv; _43 *= fInv; _44 *= fInv;
  458. return *this;
  459. }
  460. // unary operators
  461. D3DXINLINE D3DXMATRIX
  462. D3DXMATRIX::operator + () const
  463. {
  464. return *this;
  465. }
  466. D3DXINLINE D3DXMATRIX
  467. D3DXMATRIX::operator - () const
  468. {
  469. return D3DXMATRIX(-_11, -_12, -_13, -_14,
  470. -_21, -_22, -_23, -_24,
  471. -_31, -_32, -_33, -_34,
  472. -_41, -_42, -_43, -_44);
  473. }
  474. // binary operators
  475. D3DXINLINE D3DXMATRIX
  476. D3DXMATRIX::operator * ( CONST D3DXMATRIX& mat ) const
  477. {
  478. D3DXMATRIX matT;
  479. D3DXMatrixMultiply(&matT, this, &mat);
  480. return matT;
  481. }
  482. D3DXINLINE D3DXMATRIX
  483. D3DXMATRIX::operator + ( CONST D3DXMATRIX& mat ) const
  484. {
  485. return D3DXMATRIX(_11 + mat._11, _12 + mat._12, _13 + mat._13, _14 + mat._14,
  486. _21 + mat._21, _22 + mat._22, _23 + mat._23, _24 + mat._24,
  487. _31 + mat._31, _32 + mat._32, _33 + mat._33, _34 + mat._34,
  488. _41 + mat._41, _42 + mat._42, _43 + mat._43, _44 + mat._44);
  489. }
  490. D3DXINLINE D3DXMATRIX
  491. D3DXMATRIX::operator - ( CONST D3DXMATRIX& mat ) const
  492. {
  493. return D3DXMATRIX(_11 - mat._11, _12 - mat._12, _13 - mat._13, _14 - mat._14,
  494. _21 - mat._21, _22 - mat._22, _23 - mat._23, _24 - mat._24,
  495. _31 - mat._31, _32 - mat._32, _33 - mat._33, _34 - mat._34,
  496. _41 - mat._41, _42 - mat._42, _43 - mat._43, _44 - mat._44);
  497. }
  498. D3DXINLINE D3DXMATRIX
  499. D3DXMATRIX::operator * ( FLOAT f ) const
  500. {
  501. return D3DXMATRIX(_11 * f, _12 * f, _13 * f, _14 * f,
  502. _21 * f, _22 * f, _23 * f, _24 * f,
  503. _31 * f, _32 * f, _33 * f, _34 * f,
  504. _41 * f, _42 * f, _43 * f, _44 * f);
  505. }
  506. D3DXINLINE D3DXMATRIX
  507. D3DXMATRIX::operator / ( FLOAT f ) const
  508. {
  509. FLOAT fInv = 1.0f / f;
  510. return D3DXMATRIX(_11 * fInv, _12 * fInv, _13 * fInv, _14 * fInv,
  511. _21 * fInv, _22 * fInv, _23 * fInv, _24 * fInv,
  512. _31 * fInv, _32 * fInv, _33 * fInv, _34 * fInv,
  513. _41 * fInv, _42 * fInv, _43 * fInv, _44 * fInv);
  514. }
  515. D3DXINLINE D3DXMATRIX
  516. operator * ( FLOAT f, CONST D3DXMATRIX& mat )
  517. {
  518. return D3DXMATRIX(f * mat._11, f * mat._12, f * mat._13, f * mat._14,
  519. f * mat._21, f * mat._22, f * mat._23, f * mat._24,
  520. f * mat._31, f * mat._32, f * mat._33, f * mat._34,
  521. f * mat._41, f * mat._42, f * mat._43, f * mat._44);
  522. }
  523. D3DXINLINE BOOL
  524. D3DXMATRIX::operator == ( CONST D3DXMATRIX& mat ) const
  525. {
  526. return 0 == memcmp(this, &mat, sizeof(D3DXMATRIX));
  527. }
  528. D3DXINLINE BOOL
  529. D3DXMATRIX::operator != ( CONST D3DXMATRIX& mat ) const
  530. {
  531. return 0 != memcmp(this, &mat, sizeof(D3DXMATRIX));
  532. }
  533. //--------------------------
  534. // Quaternion
  535. //--------------------------
  536. D3DXINLINE
  537. D3DXQUATERNION::D3DXQUATERNION( CONST FLOAT* pf )
  538. {
  539. #ifdef D3DX_DEBUG
  540. if(!pf)
  541. return;
  542. #endif
  543. x = pf[0];
  544. y = pf[1];
  545. z = pf[2];
  546. w = pf[3];
  547. }
  548. D3DXINLINE
  549. D3DXQUATERNION::D3DXQUATERNION( FLOAT fx, FLOAT fy, FLOAT fz, FLOAT fw )
  550. {
  551. x = fx;
  552. y = fy;
  553. z = fz;
  554. w = fw;
  555. }
  556. // casting
  557. D3DXINLINE
  558. D3DXQUATERNION::operator FLOAT* ()
  559. {
  560. return (FLOAT *) &x;
  561. }
  562. D3DXINLINE
  563. D3DXQUATERNION::operator CONST FLOAT* () const
  564. {
  565. return (CONST FLOAT *) &x;
  566. }
  567. // assignment operators
  568. D3DXINLINE D3DXQUATERNION&
  569. D3DXQUATERNION::operator += ( CONST D3DXQUATERNION& q )
  570. {
  571. x += q.x;
  572. y += q.y;
  573. z += q.z;
  574. w += q.w;
  575. return *this;
  576. }
  577. D3DXINLINE D3DXQUATERNION&
  578. D3DXQUATERNION::operator -= ( CONST D3DXQUATERNION& q )
  579. {
  580. x -= q.x;
  581. y -= q.y;
  582. z -= q.z;
  583. w -= q.w;
  584. return *this;
  585. }
  586. D3DXINLINE D3DXQUATERNION&
  587. D3DXQUATERNION::operator *= ( CONST D3DXQUATERNION& q )
  588. {
  589. D3DXQuaternionMultiply(this, this, &q);
  590. return *this;
  591. }
  592. D3DXINLINE D3DXQUATERNION&
  593. D3DXQUATERNION::operator *= ( FLOAT f )
  594. {
  595. x *= f;
  596. y *= f;
  597. z *= f;
  598. w *= f;
  599. return *this;
  600. }
  601. D3DXINLINE D3DXQUATERNION&
  602. D3DXQUATERNION::operator /= ( FLOAT f )
  603. {
  604. FLOAT fInv = 1.0f / f;
  605. x *= fInv;
  606. y *= fInv;
  607. z *= fInv;
  608. w *= fInv;
  609. return *this;
  610. }
  611. // unary operators
  612. D3DXINLINE D3DXQUATERNION
  613. D3DXQUATERNION::operator + () const
  614. {
  615. return *this;
  616. }
  617. D3DXINLINE D3DXQUATERNION
  618. D3DXQUATERNION::operator - () const
  619. {
  620. return D3DXQUATERNION(-x, -y, -z, -w);
  621. }
  622. // binary operators
  623. D3DXINLINE D3DXQUATERNION
  624. D3DXQUATERNION::operator + ( CONST D3DXQUATERNION& q ) const
  625. {
  626. return D3DXQUATERNION(x + q.x, y + q.y, z + q.z, w + q.w);
  627. }
  628. D3DXINLINE D3DXQUATERNION
  629. D3DXQUATERNION::operator - ( CONST D3DXQUATERNION& q ) const
  630. {
  631. return D3DXQUATERNION(x - q.x, y - q.y, z - q.z, w - q.w);
  632. }
  633. D3DXINLINE D3DXQUATERNION
  634. D3DXQUATERNION::operator * ( CONST D3DXQUATERNION& q ) const
  635. {
  636. D3DXQUATERNION qT;
  637. D3DXQuaternionMultiply(&qT, this, &q);
  638. return qT;
  639. }
  640. D3DXINLINE D3DXQUATERNION
  641. D3DXQUATERNION::operator * ( FLOAT f ) const
  642. {
  643. return D3DXQUATERNION(x * f, y * f, z * f, w * f);
  644. }
  645. D3DXINLINE D3DXQUATERNION
  646. D3DXQUATERNION::operator / ( FLOAT f ) const
  647. {
  648. FLOAT fInv = 1.0f / f;
  649. return D3DXQUATERNION(x * fInv, y * fInv, z * fInv, w * fInv);
  650. }
  651. D3DXINLINE D3DXQUATERNION
  652. operator * (FLOAT f, CONST D3DXQUATERNION& q )
  653. {
  654. return D3DXQUATERNION(f * q.x, f * q.y, f * q.z, f * q.w);
  655. }
  656. D3DXINLINE BOOL
  657. D3DXQUATERNION::operator == ( CONST D3DXQUATERNION& q ) const
  658. {
  659. return x == q.x && y == q.y && z == q.z && w == q.w;
  660. }
  661. D3DXINLINE BOOL
  662. D3DXQUATERNION::operator != ( CONST D3DXQUATERNION& q ) const
  663. {
  664. return x != q.x || y != q.y || z != q.z || w != q.w;
  665. }
  666. //--------------------------
  667. // Plane
  668. //--------------------------
  669. D3DXINLINE
  670. D3DXPLANE::D3DXPLANE( CONST FLOAT* pf )
  671. {
  672. #ifdef D3DX_DEBUG
  673. if(!pf)
  674. return;
  675. #endif
  676. a = pf[0];
  677. b = pf[1];
  678. c = pf[2];
  679. d = pf[3];
  680. }
  681. D3DXINLINE
  682. D3DXPLANE::D3DXPLANE( FLOAT fa, FLOAT fb, FLOAT fc, FLOAT fd )
  683. {
  684. a = fa;
  685. b = fb;
  686. c = fc;
  687. d = fd;
  688. }
  689. // casting
  690. D3DXINLINE
  691. D3DXPLANE::operator FLOAT* ()
  692. {
  693. return (FLOAT *) &a;
  694. }
  695. D3DXINLINE
  696. D3DXPLANE::operator CONST FLOAT* () const
  697. {
  698. return (CONST FLOAT *) &a;
  699. }
  700. // unary operators
  701. D3DXINLINE D3DXPLANE
  702. D3DXPLANE::operator + () const
  703. {
  704. return *this;
  705. }
  706. D3DXINLINE D3DXPLANE
  707. D3DXPLANE::operator - () const
  708. {
  709. return D3DXPLANE(-a, -b, -c, -d);
  710. }
  711. // binary operators
  712. D3DXINLINE BOOL
  713. D3DXPLANE::operator == ( CONST D3DXPLANE& p ) const
  714. {
  715. return a == p.a && b == p.b && c == p.c && d == p.d;
  716. }
  717. D3DXINLINE BOOL
  718. D3DXPLANE::operator != ( CONST D3DXPLANE& p ) const
  719. {
  720. return a != p.a || b != p.b || c != p.c || d != p.d;
  721. }
  722. //--------------------------
  723. // Color
  724. //--------------------------
  725. D3DXINLINE
  726. D3DXCOLOR::D3DXCOLOR( DWORD dw )
  727. {
  728. CONST FLOAT f = 1.0f / 255.0f;
  729. r = f * (FLOAT) (unsigned char) (dw >> 16);
  730. g = f * (FLOAT) (unsigned char) (dw >> 8);
  731. b = f * (FLOAT) (unsigned char) (dw >> 0);
  732. a = f * (FLOAT) (unsigned char) (dw >> 24);
  733. }
  734. D3DXINLINE
  735. D3DXCOLOR::D3DXCOLOR( CONST FLOAT* pf )
  736. {
  737. #ifdef D3DX_DEBUG
  738. if(!pf)
  739. return;
  740. #endif
  741. r = pf[0];
  742. g = pf[1];
  743. b = pf[2];
  744. a = pf[3];
  745. }
  746. D3DXINLINE
  747. D3DXCOLOR::D3DXCOLOR( CONST D3DCOLORVALUE& c )
  748. {
  749. r = c.r;
  750. g = c.g;
  751. b = c.b;
  752. a = c.a;
  753. }
  754. D3DXINLINE
  755. D3DXCOLOR::D3DXCOLOR( FLOAT fr, FLOAT fg, FLOAT fb, FLOAT fa )
  756. {
  757. r = fr;
  758. g = fg;
  759. b = fb;
  760. a = fa;
  761. }
  762. // casting
  763. D3DXINLINE
  764. D3DXCOLOR::operator DWORD () const
  765. {
  766. DWORD dwR = r >= 1.0f ? 0xff : r <= 0.0f ? 0x00 : (DWORD) (r * 255.0f + 0.5f);
  767. DWORD dwG = g >= 1.0f ? 0xff : g <= 0.0f ? 0x00 : (DWORD) (g * 255.0f + 0.5f);
  768. DWORD dwB = b >= 1.0f ? 0xff : b <= 0.0f ? 0x00 : (DWORD) (b * 255.0f + 0.5f);
  769. DWORD dwA = a >= 1.0f ? 0xff : a <= 0.0f ? 0x00 : (DWORD) (a * 255.0f + 0.5f);
  770. return (dwA << 24) | (dwR << 16) | (dwG << 8) | dwB;
  771. }
  772. D3DXINLINE
  773. D3DXCOLOR::operator FLOAT * ()
  774. {
  775. return (FLOAT *) &r;
  776. }
  777. D3DXINLINE
  778. D3DXCOLOR::operator CONST FLOAT * () const
  779. {
  780. return (CONST FLOAT *) &r;
  781. }
  782. D3DXINLINE
  783. D3DXCOLOR::operator D3DCOLORVALUE * ()
  784. {
  785. return (D3DCOLORVALUE *) &r;
  786. }
  787. D3DXINLINE
  788. D3DXCOLOR::operator CONST D3DCOLORVALUE * () const
  789. {
  790. return (CONST D3DCOLORVALUE *) &r;
  791. }
  792. D3DXINLINE
  793. D3DXCOLOR::operator D3DCOLORVALUE& ()
  794. {
  795. return *((D3DCOLORVALUE *) &r);
  796. }
  797. D3DXINLINE
  798. D3DXCOLOR::operator CONST D3DCOLORVALUE& () const
  799. {
  800. return *((CONST D3DCOLORVALUE *) &r);
  801. }
  802. // assignment operators
  803. D3DXINLINE D3DXCOLOR&
  804. D3DXCOLOR::operator += ( CONST D3DXCOLOR& c )
  805. {
  806. r += c.r;
  807. g += c.g;
  808. b += c.b;
  809. a += c.a;
  810. return *this;
  811. }
  812. D3DXINLINE D3DXCOLOR&
  813. D3DXCOLOR::operator -= ( CONST D3DXCOLOR& c )
  814. {
  815. r -= c.r;
  816. g -= c.g;
  817. b -= c.b;
  818. a -= c.a;
  819. return *this;
  820. }
  821. D3DXINLINE D3DXCOLOR&
  822. D3DXCOLOR::operator *= ( FLOAT f )
  823. {
  824. r *= f;
  825. g *= f;
  826. b *= f;
  827. a *= f;
  828. return *this;
  829. }
  830. D3DXINLINE D3DXCOLOR&
  831. D3DXCOLOR::operator /= ( FLOAT f )
  832. {
  833. FLOAT fInv = 1.0f / f;
  834. r *= fInv;
  835. g *= fInv;
  836. b *= fInv;
  837. a *= fInv;
  838. return *this;
  839. }
  840. // unary operators
  841. D3DXINLINE D3DXCOLOR
  842. D3DXCOLOR::operator + () const
  843. {
  844. return *this;
  845. }
  846. D3DXINLINE D3DXCOLOR
  847. D3DXCOLOR::operator - () const
  848. {
  849. return D3DXCOLOR(-r, -g, -b, -a);
  850. }
  851. // binary operators
  852. D3DXINLINE D3DXCOLOR
  853. D3DXCOLOR::operator + ( CONST D3DXCOLOR& c ) const
  854. {
  855. return D3DXCOLOR(r + c.r, g + c.g, b + c.b, a + c.a);
  856. }
  857. D3DXINLINE D3DXCOLOR
  858. D3DXCOLOR::operator - ( CONST D3DXCOLOR& c ) const
  859. {
  860. return D3DXCOLOR(r - c.r, g - c.g, b - c.b, a - c.a);
  861. }
  862. D3DXINLINE D3DXCOLOR
  863. D3DXCOLOR::operator * ( FLOAT f ) const
  864. {
  865. return D3DXCOLOR(r * f, g * f, b * f, a * f);
  866. }
  867. D3DXINLINE D3DXCOLOR
  868. D3DXCOLOR::operator / ( FLOAT f ) const
  869. {
  870. FLOAT fInv = 1.0f / f;
  871. return D3DXCOLOR(r * fInv, g * fInv, b * fInv, a * fInv);
  872. }
  873. D3DXINLINE D3DXCOLOR
  874. operator * (FLOAT f, CONST D3DXCOLOR& c )
  875. {
  876. return D3DXCOLOR(f * c.r, f * c.g, f * c.b, f * c.a);
  877. }
  878. D3DXINLINE BOOL
  879. D3DXCOLOR::operator == ( CONST D3DXCOLOR& c ) const
  880. {
  881. return r == c.r && g == c.g && b == c.b && a == c.a;
  882. }
  883. D3DXINLINE BOOL
  884. D3DXCOLOR::operator != ( CONST D3DXCOLOR& c ) const
  885. {
  886. return r != c.r || g != c.g || b != c.b || a != c.a;
  887. }
  888. #endif //__cplusplus
  889. //===========================================================================
  890. //
  891. // Inline functions
  892. //
  893. //===========================================================================
  894. //--------------------------
  895. // 2D Vector
  896. //--------------------------
  897. D3DXINLINE FLOAT D3DXVec2Length
  898. ( CONST D3DXVECTOR2 *pV )
  899. {
  900. #ifdef D3DX_DEBUG
  901. if(!pV)
  902. return 0.0f;
  903. #endif
  904. #ifdef __cplusplus
  905. return sqrtf(pV->x * pV->x + pV->y * pV->y);
  906. #else
  907. return (FLOAT) sqrt(pV->x * pV->x + pV->y * pV->y);
  908. #endif
  909. }
  910. D3DXINLINE FLOAT D3DXVec2LengthSq
  911. ( CONST D3DXVECTOR2 *pV )
  912. {
  913. #ifdef D3DX_DEBUG
  914. if(!pV)
  915. return 0.0f;
  916. #endif
  917. return pV->x * pV->x + pV->y * pV->y;
  918. }
  919. D3DXINLINE FLOAT D3DXVec2Dot
  920. ( CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  921. {
  922. #ifdef D3DX_DEBUG
  923. if(!pV1 || !pV2)
  924. return 0.0f;
  925. #endif
  926. return pV1->x * pV2->x + pV1->y * pV2->y;
  927. }
  928. D3DXINLINE FLOAT D3DXVec2CCW
  929. ( CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  930. {
  931. #ifdef D3DX_DEBUG
  932. if(!pV1 || !pV2)
  933. return 0.0f;
  934. #endif
  935. return pV1->x * pV2->y - pV1->y * pV2->x;
  936. }
  937. D3DXINLINE D3DXVECTOR2* D3DXVec2Add
  938. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  939. {
  940. #ifdef D3DX_DEBUG
  941. if(!pOut || !pV1 || !pV2)
  942. return NULL;
  943. #endif
  944. pOut->x = pV1->x + pV2->x;
  945. pOut->y = pV1->y + pV2->y;
  946. return pOut;
  947. }
  948. D3DXINLINE D3DXVECTOR2* D3DXVec2Subtract
  949. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  950. {
  951. #ifdef D3DX_DEBUG
  952. if(!pOut || !pV1 || !pV2)
  953. return NULL;
  954. #endif
  955. pOut->x = pV1->x - pV2->x;
  956. pOut->y = pV1->y - pV2->y;
  957. return pOut;
  958. }
  959. D3DXINLINE D3DXVECTOR2* D3DXVec2Minimize
  960. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  961. {
  962. #ifdef D3DX_DEBUG
  963. if(!pOut || !pV1 || !pV2)
  964. return NULL;
  965. #endif
  966. pOut->x = pV1->x < pV2->x ? pV1->x : pV2->x;
  967. pOut->y = pV1->y < pV2->y ? pV1->y : pV2->y;
  968. return pOut;
  969. }
  970. D3DXINLINE D3DXVECTOR2* D3DXVec2Maximize
  971. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2 )
  972. {
  973. #ifdef D3DX_DEBUG
  974. if(!pOut || !pV1 || !pV2)
  975. return NULL;
  976. #endif
  977. pOut->x = pV1->x > pV2->x ? pV1->x : pV2->x;
  978. pOut->y = pV1->y > pV2->y ? pV1->y : pV2->y;
  979. return pOut;
  980. }
  981. D3DXINLINE D3DXVECTOR2* D3DXVec2Scale
  982. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV, FLOAT s )
  983. {
  984. #ifdef D3DX_DEBUG
  985. if(!pOut || !pV)
  986. return NULL;
  987. #endif
  988. pOut->x = pV->x * s;
  989. pOut->y = pV->y * s;
  990. return pOut;
  991. }
  992. D3DXINLINE D3DXVECTOR2* D3DXVec2Lerp
  993. ( D3DXVECTOR2 *pOut, CONST D3DXVECTOR2 *pV1, CONST D3DXVECTOR2 *pV2,
  994. FLOAT s )
  995. {
  996. #ifdef D3DX_DEBUG
  997. if(!pOut || !pV1 || !pV2)
  998. return NULL;
  999. #endif
  1000. pOut->x = pV1->x + s * (pV2->x - pV1->x);
  1001. pOut->y = pV1->y + s * (pV2->y - pV1->y);
  1002. return pOut;
  1003. }
  1004. //--------------------------
  1005. // 3D Vector
  1006. //--------------------------
  1007. D3DXINLINE FLOAT D3DXVec3Length
  1008. ( CONST D3DXVECTOR3 *pV )
  1009. {
  1010. #ifdef D3DX_DEBUG
  1011. if(!pV)
  1012. return 0.0f;
  1013. #endif
  1014. #ifdef __cplusplus
  1015. return sqrtf(pV->x * pV->x + pV->y * pV->y + pV->z * pV->z);
  1016. #else
  1017. return (FLOAT) sqrt(pV->x * pV->x + pV->y * pV->y + pV->z * pV->z);
  1018. #endif
  1019. }
  1020. D3DXINLINE FLOAT D3DXVec3LengthSq
  1021. ( CONST D3DXVECTOR3 *pV )
  1022. {
  1023. #ifdef D3DX_DEBUG
  1024. if(!pV)
  1025. return 0.0f;
  1026. #endif
  1027. return pV->x * pV->x + pV->y * pV->y + pV->z * pV->z;
  1028. }
  1029. D3DXINLINE FLOAT D3DXVec3Dot
  1030. ( CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1031. {
  1032. #ifdef D3DX_DEBUG
  1033. if(!pV1 || !pV2)
  1034. return 0.0f;
  1035. #endif
  1036. return pV1->x * pV2->x + pV1->y * pV2->y + pV1->z * pV2->z;
  1037. }
  1038. D3DXINLINE D3DXVECTOR3* D3DXVec3Cross
  1039. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1040. {
  1041. D3DXVECTOR3 v;
  1042. #ifdef D3DX_DEBUG
  1043. if(!pOut || !pV1 || !pV2)
  1044. return NULL;
  1045. #endif
  1046. v.x = pV1->y * pV2->z - pV1->z * pV2->y;
  1047. v.y = pV1->z * pV2->x - pV1->x * pV2->z;
  1048. v.z = pV1->x * pV2->y - pV1->y * pV2->x;
  1049. *pOut = v;
  1050. return pOut;
  1051. }
  1052. D3DXINLINE D3DXVECTOR3* D3DXVec3Add
  1053. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1054. {
  1055. #ifdef D3DX_DEBUG
  1056. if(!pOut || !pV1 || !pV2)
  1057. return NULL;
  1058. #endif
  1059. pOut->x = pV1->x + pV2->x;
  1060. pOut->y = pV1->y + pV2->y;
  1061. pOut->z = pV1->z + pV2->z;
  1062. return pOut;
  1063. }
  1064. D3DXINLINE D3DXVECTOR3* D3DXVec3Subtract
  1065. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1066. {
  1067. #ifdef D3DX_DEBUG
  1068. if(!pOut || !pV1 || !pV2)
  1069. return NULL;
  1070. #endif
  1071. pOut->x = pV1->x - pV2->x;
  1072. pOut->y = pV1->y - pV2->y;
  1073. pOut->z = pV1->z - pV2->z;
  1074. return pOut;
  1075. }
  1076. D3DXINLINE D3DXVECTOR3* D3DXVec3Minimize
  1077. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1078. {
  1079. #ifdef D3DX_DEBUG
  1080. if(!pOut || !pV1 || !pV2)
  1081. return NULL;
  1082. #endif
  1083. pOut->x = pV1->x < pV2->x ? pV1->x : pV2->x;
  1084. pOut->y = pV1->y < pV2->y ? pV1->y : pV2->y;
  1085. pOut->z = pV1->z < pV2->z ? pV1->z : pV2->z;
  1086. return pOut;
  1087. }
  1088. D3DXINLINE D3DXVECTOR3* D3DXVec3Maximize
  1089. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2 )
  1090. {
  1091. #ifdef D3DX_DEBUG
  1092. if(!pOut || !pV1 || !pV2)
  1093. return NULL;
  1094. #endif
  1095. pOut->x = pV1->x > pV2->x ? pV1->x : pV2->x;
  1096. pOut->y = pV1->y > pV2->y ? pV1->y : pV2->y;
  1097. pOut->z = pV1->z > pV2->z ? pV1->z : pV2->z;
  1098. return pOut;
  1099. }
  1100. D3DXINLINE D3DXVECTOR3* D3DXVec3Scale
  1101. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV, FLOAT s)
  1102. {
  1103. #ifdef D3DX_DEBUG
  1104. if(!pOut || !pV)
  1105. return NULL;
  1106. #endif
  1107. pOut->x = pV->x * s;
  1108. pOut->y = pV->y * s;
  1109. pOut->z = pV->z * s;
  1110. return pOut;
  1111. }
  1112. D3DXINLINE D3DXVECTOR3* D3DXVec3Lerp
  1113. ( D3DXVECTOR3 *pOut, CONST D3DXVECTOR3 *pV1, CONST D3DXVECTOR3 *pV2,
  1114. FLOAT s )
  1115. {
  1116. #ifdef D3DX_DEBUG
  1117. if(!pOut || !pV1 || !pV2)
  1118. return NULL;
  1119. #endif
  1120. pOut->x = pV1->x + s * (pV2->x - pV1->x);
  1121. pOut->y = pV1->y + s * (pV2->y - pV1->y);
  1122. pOut->z = pV1->z + s * (pV2->z - pV1->z);
  1123. return pOut;
  1124. }
  1125. //--------------------------
  1126. // 4D Vector
  1127. //--------------------------
  1128. D3DXINLINE FLOAT D3DXVec4Length
  1129. ( CONST D3DXVECTOR4 *pV )
  1130. {
  1131. #ifdef D3DX_DEBUG
  1132. if(!pV)
  1133. return 0.0f;
  1134. #endif
  1135. #ifdef __cplusplus
  1136. return sqrtf(pV->x * pV->x + pV->y * pV->y + pV->z * pV->z + pV->w * pV->w);
  1137. #else
  1138. return (FLOAT) sqrt(pV->x * pV->x + pV->y * pV->y + pV->z * pV->z + pV->w * pV->w);
  1139. #endif
  1140. }
  1141. D3DXINLINE FLOAT D3DXVec4LengthSq
  1142. ( CONST D3DXVECTOR4 *pV )
  1143. {
  1144. #ifdef D3DX_DEBUG
  1145. if(!pV)
  1146. return 0.0f;
  1147. #endif
  1148. return pV->x * pV->x + pV->y * pV->y + pV->z * pV->z + pV->w * pV->w;
  1149. }
  1150. D3DXINLINE FLOAT D3DXVec4Dot
  1151. ( CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2 )
  1152. {
  1153. #ifdef D3DX_DEBUG
  1154. if(!pV1 || !pV2)
  1155. return 0.0f;
  1156. #endif
  1157. return pV1->x * pV2->x + pV1->y * pV2->y + pV1->z * pV2->z + pV1->w * pV2->w;
  1158. }
  1159. D3DXINLINE D3DXVECTOR4* D3DXVec4Add
  1160. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2)
  1161. {
  1162. #ifdef D3DX_DEBUG
  1163. if(!pOut || !pV1 || !pV2)
  1164. return NULL;
  1165. #endif
  1166. pOut->x = pV1->x + pV2->x;
  1167. pOut->y = pV1->y + pV2->y;
  1168. pOut->z = pV1->z + pV2->z;
  1169. pOut->w = pV1->w + pV2->w;
  1170. return pOut;
  1171. }
  1172. D3DXINLINE D3DXVECTOR4* D3DXVec4Subtract
  1173. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2)
  1174. {
  1175. #ifdef D3DX_DEBUG
  1176. if(!pOut || !pV1 || !pV2)
  1177. return NULL;
  1178. #endif
  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;
  1183. return pOut;
  1184. }
  1185. D3DXINLINE D3DXVECTOR4* D3DXVec4Minimize
  1186. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2)
  1187. {
  1188. #ifdef D3DX_DEBUG
  1189. if(!pOut || !pV1 || !pV2)
  1190. return NULL;
  1191. #endif
  1192. pOut->x = pV1->x < pV2->x ? pV1->x : pV2->x;
  1193. pOut->y = pV1->y < pV2->y ? pV1->y : pV2->y;
  1194. pOut->z = pV1->z < pV2->z ? pV1->z : pV2->z;
  1195. pOut->w = pV1->w < pV2->w ? pV1->w : pV2->w;
  1196. return pOut;
  1197. }
  1198. D3DXINLINE D3DXVECTOR4* D3DXVec4Maximize
  1199. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2)
  1200. {
  1201. #ifdef D3DX_DEBUG
  1202. if(!pOut || !pV1 || !pV2)
  1203. return NULL;
  1204. #endif
  1205. pOut->x = pV1->x > pV2->x ? pV1->x : pV2->x;
  1206. pOut->y = pV1->y > pV2->y ? pV1->y : pV2->y;
  1207. pOut->z = pV1->z > pV2->z ? pV1->z : pV2->z;
  1208. pOut->w = pV1->w > pV2->w ? pV1->w : pV2->w;
  1209. return pOut;
  1210. }
  1211. D3DXINLINE D3DXVECTOR4* D3DXVec4Scale
  1212. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV, FLOAT s)
  1213. {
  1214. #ifdef D3DX_DEBUG
  1215. if(!pOut || !pV)
  1216. return NULL;
  1217. #endif
  1218. pOut->x = pV->x * s;
  1219. pOut->y = pV->y * s;
  1220. pOut->z = pV->z * s;
  1221. pOut->w = pV->w * s;
  1222. return pOut;
  1223. }
  1224. D3DXINLINE D3DXVECTOR4* D3DXVec4Lerp
  1225. ( D3DXVECTOR4 *pOut, CONST D3DXVECTOR4 *pV1, CONST D3DXVECTOR4 *pV2,
  1226. FLOAT s )
  1227. {
  1228. #ifdef D3DX_DEBUG
  1229. if(!pOut || !pV1 || !pV2)
  1230. return NULL;
  1231. #endif
  1232. pOut->x = pV1->x + s * (pV2->x - pV1->x);
  1233. pOut->y = pV1->y + s * (pV2->y - pV1->y);
  1234. pOut->z = pV1->z + s * (pV2->z - pV1->z);
  1235. pOut->w = pV1->w + s * (pV2->w - pV1->w);
  1236. return pOut;
  1237. }
  1238. //--------------------------
  1239. // 4D Matrix
  1240. //--------------------------
  1241. D3DXINLINE D3DXMATRIX* D3DXMatrixIdentity
  1242. ( D3DXMATRIX *pOut )
  1243. {
  1244. #ifdef D3DX_DEBUG
  1245. if(!pOut)
  1246. return NULL;
  1247. #endif
  1248. pOut->m[0][1] = pOut->m[0][2] = pOut->m[0][3] =
  1249. pOut->m[1][0] = pOut->m[1][2] = pOut->m[1][3] =
  1250. pOut->m[2][0] = pOut->m[2][1] = pOut->m[2][3] =
  1251. pOut->m[3][0] = pOut->m[3][1] = pOut->m[3][2] = 0.0f;
  1252. pOut->m[0][0] = pOut->m[1][1] = pOut->m[2][2] = pOut->m[3][3] = 1.0f;
  1253. return pOut;
  1254. }
  1255. D3DXINLINE BOOL D3DXMatrixIsIdentity
  1256. ( CONST D3DXMATRIX *pM )
  1257. {
  1258. #ifdef D3DX_DEBUG
  1259. if(!pM)
  1260. return FALSE;
  1261. #endif
  1262. return pM->m[0][0] == 1.0f && pM->m[0][1] == 0.0f && pM->m[0][2] == 0.0f && pM->m[0][3] == 0.0f &&
  1263. pM->m[1][0] == 0.0f && pM->m[1][1] == 1.0f && pM->m[1][2] == 0.0f && pM->m[1][3] == 0.0f &&
  1264. pM->m[2][0] == 0.0f && pM->m[2][1] == 0.0f && pM->m[2][2] == 1.0f && pM->m[2][3] == 0.0f &&
  1265. pM->m[3][0] == 0.0f && pM->m[3][1] == 0.0f && pM->m[3][2] == 0.0f && pM->m[3][3] == 1.0f;
  1266. }
  1267. //--------------------------
  1268. // Quaternion
  1269. //--------------------------
  1270. D3DXINLINE FLOAT D3DXQuaternionLength
  1271. ( CONST D3DXQUATERNION *pQ )
  1272. {
  1273. #ifdef D3DX_DEBUG
  1274. if(!pQ)
  1275. return 0.0f;
  1276. #endif
  1277. #ifdef __cplusplus
  1278. return sqrtf(pQ->x * pQ->x + pQ->y * pQ->y + pQ->z * pQ->z + pQ->w * pQ->w);
  1279. #else
  1280. return (FLOAT) sqrt(pQ->x * pQ->x + pQ->y * pQ->y + pQ->z * pQ->z + pQ->w * pQ->w);
  1281. #endif
  1282. }
  1283. D3DXINLINE FLOAT D3DXQuaternionLengthSq
  1284. ( CONST D3DXQUATERNION *pQ )
  1285. {
  1286. #ifdef D3DX_DEBUG
  1287. if(!pQ)
  1288. return 0.0f;
  1289. #endif
  1290. return pQ->x * pQ->x + pQ->y * pQ->y + pQ->z * pQ->z + pQ->w * pQ->w;
  1291. }
  1292. D3DXINLINE FLOAT D3DXQuaternionDot
  1293. ( CONST D3DXQUATERNION *pQ1, CONST D3DXQUATERNION *pQ2 )
  1294. {
  1295. #ifdef D3DX_DEBUG
  1296. if(!pQ1 || !pQ2)
  1297. return 0.0f;
  1298. #endif
  1299. return pQ1->x * pQ2->x + pQ1->y * pQ2->y + pQ1->z * pQ2->z + pQ1->w * pQ2->w;
  1300. }
  1301. D3DXINLINE D3DXQUATERNION* D3DXQuaternionIdentity
  1302. ( D3DXQUATERNION *pOut )
  1303. {
  1304. #ifdef D3DX_DEBUG
  1305. if(!pOut)
  1306. return NULL;
  1307. #endif
  1308. pOut->x = pOut->y = pOut->z = 0.0f;
  1309. pOut->w = 1.0f;
  1310. return pOut;
  1311. }
  1312. D3DXINLINE BOOL D3DXQuaternionIsIdentity
  1313. ( CONST D3DXQUATERNION *pQ )
  1314. {
  1315. #ifdef D3DX_DEBUG
  1316. if(!pQ)
  1317. return FALSE;
  1318. #endif
  1319. return pQ->x == 0.0f && pQ->y == 0.0f && pQ->z == 0.0f && pQ->w == 1.0f;
  1320. }
  1321. D3DXINLINE D3DXQUATERNION* D3DXQuaternionConjugate
  1322. ( D3DXQUATERNION *pOut, CONST D3DXQUATERNION *pQ )
  1323. {
  1324. #ifdef D3DX_DEBUG
  1325. if(!pOut || !pQ)
  1326. return NULL;
  1327. #endif
  1328. pOut->x = -pQ->x;
  1329. pOut->y = -pQ->y;
  1330. pOut->z = -pQ->z;
  1331. pOut->w = pQ->w;
  1332. return pOut;
  1333. }
  1334. //--------------------------
  1335. // Plane
  1336. //--------------------------
  1337. D3DXINLINE FLOAT D3DXPlaneDot
  1338. ( CONST D3DXPLANE *pP, CONST D3DXVECTOR4 *pV)
  1339. {
  1340. #ifdef D3DX_DEBUG
  1341. if(!pP || !pV)
  1342. return 0.0f;
  1343. #endif
  1344. return pP->a * pV->x + pP->b * pV->y + pP->c * pV->z + pP->d * pV->w;
  1345. }
  1346. D3DXINLINE FLOAT D3DXPlaneDotCoord
  1347. ( CONST D3DXPLANE *pP, CONST D3DXVECTOR3 *pV)
  1348. {
  1349. #ifdef D3DX_DEBUG
  1350. if(!pP || !pV)
  1351. return 0.0f;
  1352. #endif
  1353. return pP->a * pV->x + pP->b * pV->y + pP->c * pV->z + pP->d;
  1354. }
  1355. D3DXINLINE FLOAT D3DXPlaneDotNormal
  1356. ( CONST D3DXPLANE *pP, CONST D3DXVECTOR3 *pV)
  1357. {
  1358. #ifdef D3DX_DEBUG
  1359. if(!pP || !pV)
  1360. return 0.0f;
  1361. #endif
  1362. return pP->a * pV->x + pP->b * pV->y + pP->c * pV->z;
  1363. }
  1364. //--------------------------
  1365. // Color
  1366. //--------------------------
  1367. D3DXINLINE D3DXCOLOR* D3DXColorNegative
  1368. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC)
  1369. {
  1370. #ifdef D3DX_DEBUG
  1371. if(!pOut || !pC)
  1372. return NULL;
  1373. #endif
  1374. pOut->r = 1.0f - pC->r;
  1375. pOut->g = 1.0f - pC->g;
  1376. pOut->b = 1.0f - pC->b;
  1377. pOut->a = pC->a;
  1378. return pOut;
  1379. }
  1380. D3DXINLINE D3DXCOLOR* D3DXColorAdd
  1381. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC1, CONST D3DXCOLOR *pC2)
  1382. {
  1383. #ifdef D3DX_DEBUG
  1384. if(!pOut || !pC1 || !pC2)
  1385. return NULL;
  1386. #endif
  1387. pOut->r = pC1->r + pC2->r;
  1388. pOut->g = pC1->g + pC2->g;
  1389. pOut->b = pC1->b + pC2->b;
  1390. pOut->a = pC1->a + pC2->a;
  1391. return pOut;
  1392. }
  1393. D3DXINLINE D3DXCOLOR* D3DXColorSubtract
  1394. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC1, CONST D3DXCOLOR *pC2)
  1395. {
  1396. #ifdef D3DX_DEBUG
  1397. if(!pOut || !pC1 || !pC2)
  1398. return NULL;
  1399. #endif
  1400. pOut->r = pC1->r - pC2->r;
  1401. pOut->g = pC1->g - pC2->g;
  1402. pOut->b = pC1->b - pC2->b;
  1403. pOut->a = pC1->a - pC2->a;
  1404. return pOut;
  1405. }
  1406. D3DXINLINE D3DXCOLOR* D3DXColorScale
  1407. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC, FLOAT s)
  1408. {
  1409. #ifdef D3DX_DEBUG
  1410. if(!pOut || !pC)
  1411. return NULL;
  1412. #endif
  1413. pOut->r = pC->r * s;
  1414. pOut->g = pC->g * s;
  1415. pOut->b = pC->b * s;
  1416. pOut->a = pC->a * s;
  1417. return pOut;
  1418. }
  1419. D3DXINLINE D3DXCOLOR* D3DXColorModulate
  1420. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC1, CONST D3DXCOLOR *pC2)
  1421. {
  1422. #ifdef D3DX_DEBUG
  1423. if(!pOut || !pC1 || !pC2)
  1424. return NULL;
  1425. #endif
  1426. pOut->r = pC1->r * pC2->r;
  1427. pOut->g = pC1->g * pC2->g;
  1428. pOut->b = pC1->b * pC2->b;
  1429. pOut->a = pC1->a * pC2->a;
  1430. return pOut;
  1431. }
  1432. D3DXINLINE D3DXCOLOR* D3DXColorLerp
  1433. (D3DXCOLOR *pOut, CONST D3DXCOLOR *pC1, CONST D3DXCOLOR *pC2, FLOAT s)
  1434. {
  1435. #ifdef D3DX_DEBUG
  1436. if(!pOut || !pC1 || !pC2)
  1437. return NULL;
  1438. #endif
  1439. pOut->r = pC1->r + s * (pC2->r - pC1->r);
  1440. pOut->g = pC1->g + s * (pC2->g - pC1->g);
  1441. pOut->b = pC1->b + s * (pC2->b - pC1->b);
  1442. pOut->a = pC1->a + s * (pC2->a - pC1->a);
  1443. return pOut;
  1444. }
  1445. #endif // __D3DX8MATH_INL__