d3dvec.inl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2000 Ove Kaaven
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #ifndef __WINE_D3DVEC_INL
  19. #define __WINE_D3DVEC_INL
  20. #include <math.h>
  21. /*** constructors ***/
  22. inline _D3DVECTOR::_D3DVECTOR(D3DVALUE f)
  23. {
  24. x = y = z = f;
  25. }
  26. inline _D3DVECTOR::_D3DVECTOR(D3DVALUE _x, D3DVALUE _y, D3DVALUE _z)
  27. {
  28. x = _x; y = _y; z = _z;
  29. }
  30. /*** assignment operators ***/
  31. inline _D3DVECTOR& _D3DVECTOR::operator += (const _D3DVECTOR& v)
  32. {
  33. x += v.x; y += v.y; z += v.z;
  34. return *this;
  35. }
  36. inline _D3DVECTOR& _D3DVECTOR::operator -= (const _D3DVECTOR& v)
  37. {
  38. x -= v.x; y -= v.y; z -= v.z;
  39. return *this;
  40. }
  41. inline _D3DVECTOR& _D3DVECTOR::operator *= (const _D3DVECTOR& v)
  42. {
  43. x *= v.x; y *= v.y; z *= v.z;
  44. return *this;
  45. }
  46. inline _D3DVECTOR& _D3DVECTOR::operator /= (const _D3DVECTOR& v)
  47. {
  48. x /= v.x; y /= v.y; z /= v.z;
  49. return *this;
  50. }
  51. inline _D3DVECTOR& _D3DVECTOR::operator *= (D3DVALUE s)
  52. {
  53. x *= s; y *= s; z *= s;
  54. return *this;
  55. }
  56. inline _D3DVECTOR& _D3DVECTOR::operator /= (D3DVALUE s)
  57. {
  58. x /= s; y /= s; z /= s;
  59. return *this;
  60. }
  61. /*** unary operators ***/
  62. inline _D3DVECTOR operator + (const _D3DVECTOR& v)
  63. {
  64. return v;
  65. }
  66. inline _D3DVECTOR operator - (const _D3DVECTOR& v)
  67. {
  68. return _D3DVECTOR(-v.x, -v.y, -v.z);
  69. }
  70. /*** binary operators ***/
  71. inline _D3DVECTOR operator + (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  72. {
  73. return _D3DVECTOR(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z);
  74. }
  75. inline _D3DVECTOR operator - (const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  76. {
  77. return _D3DVECTOR(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
  78. }
  79. inline _D3DVECTOR operator * (const _D3DVECTOR& v, D3DVALUE s)
  80. {
  81. return _D3DVECTOR(v.x*s, v.y*s, v.z*s);
  82. }
  83. inline _D3DVECTOR operator * (D3DVALUE s, const _D3DVECTOR& v)
  84. {
  85. return _D3DVECTOR(v.x*s, v.y*s, v.z*s);
  86. }
  87. inline _D3DVECTOR operator / (const _D3DVECTOR& v, D3DVALUE s)
  88. {
  89. return _D3DVECTOR(v.x/s, v.y/s, v.z/s);
  90. }
  91. inline D3DVALUE SquareMagnitude(const _D3DVECTOR& v)
  92. {
  93. return v.x*v.x + v.y*v.y + v.z*v.z; /* DotProduct(v, v) */
  94. }
  95. inline D3DVALUE Magnitude(const _D3DVECTOR& v)
  96. {
  97. return sqrt(SquareMagnitude(v));
  98. }
  99. inline _D3DVECTOR Normalize(const _D3DVECTOR& v)
  100. {
  101. return v / Magnitude(v);
  102. }
  103. inline D3DVALUE DotProduct(const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  104. {
  105. return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
  106. }
  107. inline _D3DVECTOR CrossProduct(const _D3DVECTOR& v1, const _D3DVECTOR& v2)
  108. {
  109. _D3DVECTOR res;
  110. /* this is a left-handed cross product, right? */
  111. res.x = v1.y * v2.z - v1.z * v2.y;
  112. res.y = v1.z * v2.x - v1.x * v2.z;
  113. res.z = v1.x * v2.y - v1.y * v2.x;
  114. return res;
  115. }
  116. #endif