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.

1809 lines
34 KiB

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