home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / d3dvec.inl < prev    next >
Text File  |  1998-04-25  |  5KB  |  241 lines

  1.  
  2. /****************************************************************** 
  3.  *                                                                *
  4.  *   D3DVec.inl                                                   *
  5.  *                                                                *
  6.  *   Float-valued 3D vector class for Direct3D.                   *
  7.  *                                                                *
  8.  *   Copyright (c) 1996-1997 Microsoft Corp. All rights reserved. *
  9.  *                                                                *
  10.  ******************************************************************/
  11.  
  12. #include <math.h>
  13.  
  14. // =====================================
  15. // Constructors
  16. // =====================================
  17.  
  18. inline
  19. _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
  20. {
  21.     x = y = z = f;
  22. }
  23.  
  24. inline
  25. _D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
  26. {
  27.     x = _x; y = _y; z = _z;
  28. }
  29.  
  30. inline
  31. _D3DVECTOR::_D3DVECTOR(const D3DVALUE f[3])
  32. {
  33.     x = f[0]; y = f[1]; z = f[2];
  34. }
  35.  
  36. // =====================================
  37. // Access grants
  38. // =====================================
  39.  
  40. inline const D3DVALUE&
  41. _D3DVECTOR::operator[](int i) const
  42. {
  43.     return (&x)[i];
  44. }
  45.  
  46. inline D3DVALUE&
  47. _D3DVECTOR::operator[](int i)
  48. {
  49.     return (&x)[i];
  50. }
  51.  
  52.  
  53. // =====================================
  54. // Assignment operators
  55. // =====================================
  56.  
  57. inline _D3DVECTOR&
  58. _D3DVECTOR::operator += (const _D3DVECTOR& v)
  59. {
  60.    x += v.x;   y += v.y;   z += v.z;
  61.    return *this;
  62. }
  63.  
  64. inline _D3DVECTOR&
  65. _D3DVECTOR::operator -= (const _D3DVECTOR& v)
  66. {
  67.    x -= v.x;   y -= v.y;   z -= v.z;
  68.    return *this;
  69. }
  70.  
  71. inline _D3DVECTOR&
  72. _D3DVECTOR::operator *= (const _D3DVECTOR& v)
  73. {
  74.    x *= v.x;   y *= v.y;   z *= v.z;
  75.    return *this;
  76. }
  77.  
  78. inline _D3DVECTOR&
  79. _D3DVECTOR::operator /= (const _D3DVECTOR& v)
  80. {
  81.    x /= v.x;   y /= v.y;   z /= v.z;
  82.    return *this;
  83. }
  84.  
  85. inline _D3DVECTOR&
  86. _D3DVECTOR::operator *= (D3DVALUE s)
  87. {
  88.    x *= s;   y *= s;   z *= s;
  89.    return *this;
  90. }
  91.  
  92. inline _D3DVECTOR&
  93. _D3DVECTOR::operator /= (D3DVALUE s)
  94. {
  95.    x /= s;   y /= s;   z /= s;
  96.    return *this;
  97. }
  98.  
  99. inline _D3DVECTOR
  100. operator + (const _D3DVECTOR& v)
  101. {
  102.    return v;
  103. }
  104.  
  105. inline _D3DVECTOR
  106. operator - (const _D3DVECTOR& v)
  107. {
  108.    return _D3DVECTOR(-v.x, -v.y, -v.z);
  109. }
  110.  
  111. inline _D3DVECTOR
  112. operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  113. {
  114.    return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
  115. }
  116.  
  117. inline _D3DVECTOR
  118. operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  119. {
  120.    return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
  121. }
  122.  
  123. inline _D3DVECTOR
  124. operator * (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  125. {
  126.    return _D3DVECTOR(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z);
  127. }
  128.  
  129. inline _D3DVECTOR
  130. operator / (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  131. {
  132.    return _D3DVECTOR(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z);
  133. }
  134.  
  135. inline int
  136. operator < (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  137. {
  138.    return v1[0] < v2[0] && v1[1] < v2[1] && v1[2] < v2[2];
  139. }
  140.  
  141. inline int
  142. operator <= (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  143. {
  144.    return v1[0] <= v2[0] && v1[1] <= v2[1] && v1[2] <= v2[2];
  145. }
  146.  
  147. inline _D3DVECTOR
  148. operator * (const _D3DVECTOR& v, D3DVALUE s)
  149. {
  150.    return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
  151. }
  152.  
  153. inline _D3DVECTOR
  154. operator * (D3DVALUE s, const _D3DVECTOR& v)
  155. {
  156.    return _D3DVECTOR(s*v.x, s*v.y, s*v.z);
  157. }
  158.  
  159. inline _D3DVECTOR
  160. operator / (const _D3DVECTOR& v, D3DVALUE s)
  161. {
  162.    return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
  163. }
  164.  
  165. inline int
  166. operator == (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  167. {
  168.    return v1.x==v2.x && v1.y==v2.y && v1.z == v2.z;
  169. }
  170.  
  171. inline D3DVALUE
  172. Magnitude (const _D3DVECTOR& v)
  173. {
  174.    return (D3DVALUE) sqrt(SquareMagnitude(v));
  175. }
  176.  
  177. inline D3DVALUE
  178. SquareMagnitude (const _D3DVECTOR& v)
  179. {
  180.    return v.x*v.x + v.y*v.y + v.z*v.z;
  181. }
  182.  
  183. inline _D3DVECTOR
  184. Normalize (const _D3DVECTOR& v)
  185. {
  186.    return v / Magnitude(v);
  187. }
  188.  
  189. inline D3DVALUE
  190. Min (const _D3DVECTOR& v)
  191. {
  192.    D3DVALUE ret = v.x;
  193.    if (v.y < ret) ret = v.y;
  194.    if (v.z < ret) ret = v.z;
  195.    return ret;
  196. }
  197.  
  198. inline D3DVALUE
  199. Max (const _D3DVECTOR& v)
  200. {
  201.    D3DVALUE ret = v.x;
  202.    if (ret < v.y) ret = v.y;
  203.    if (ret < v.z) ret = v.z;
  204.    return ret;
  205. }
  206.  
  207. inline _D3DVECTOR
  208. Minimize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  209. {
  210.    return _D3DVECTOR( v1[0] < v2[0] ? v1[0] : v2[0],
  211.                    v1[1] < v2[1] ? v1[1] : v2[1],
  212.                    v1[2] < v2[2] ? v1[2] : v2[2]);
  213. }
  214.  
  215. inline _D3DVECTOR
  216. Maximize (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  217. {
  218.    return _D3DVECTOR( v1[0] > v2[0] ? v1[0] : v2[0],
  219.                    v1[1] > v2[1] ? v1[1] : v2[1],
  220.                    v1[2] > v2[2] ? v1[2] : v2[2]);
  221. }
  222.  
  223. inline D3DVALUE
  224. DotProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  225. {
  226.    return v1.x*v2.x + v1.y * v2.y + v1.z*v2.z;
  227. }
  228.  
  229. inline _D3DVECTOR
  230. CrossProduct (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  231. {
  232.     _D3DVECTOR result;
  233.  
  234.     result[0] = v1[1] * v2[2] - v1[2] * v2[1];
  235.     result[1] = v1[2] * v2[0] - v1[0] * v2[2];
  236.     result[2] = v1[0] * v2[1] - v1[1] * v2[0];
  237.  
  238.     return result;
  239. }
  240.  
  241.