Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

254 lignes
6.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. //
  11. // The Matrix classes
  12. // ------------------
  13. //
  14. #if !defined __DH_MATRIX_H__
  15. #define __DH_MATRIX_H__
  16. #include <cmath>
  17. #define VECTOR_OP(elems, op) \
  18. template<typename U> \
  19. inline Vec##elems<T> operator op(Vec##elems<U> const &val) const \
  20. { \
  21. Vec##elems<T> ret; \
  22. for (int n = 0; n < elems; n++) \
  23. ret[n] = (*this)[n] op val[n]; \
  24. return ret; \
  25. }
  26. #define BOOL_OP(elems, op, ret) \
  27. inline bool operator op(Vec##elems<T> const &val) const \
  28. { \
  29. for (int n = 0; n < elems; n++) \
  30. if ((*this)[n] != val[n]) \
  31. return ret; \
  32. return !ret; \
  33. }
  34. #define SCALAR_OP(elems, op) \
  35. inline Vec##elems<T> operator op(T const &val) const \
  36. { \
  37. Vec##elems<T> ret; \
  38. for (int n = 0; n < elems; n++) \
  39. ret[n] = (*this)[n] op val; \
  40. return ret; \
  41. }
  42. #define CAST_OP(elems, dest) \
  43. inline operator Vec##dest<T>() const \
  44. { \
  45. Vec##dest<T> ret; \
  46. for (int n = 0; n < elems && n < dest; n++) \
  47. ret[n] = (*this)[n]; \
  48. return ret; \
  49. }
  50. #define OPERATORS(elems) \
  51. inline T& operator[](int n) { return *(&x + n); } \
  52. inline T const& operator[](int n) const { return *(&x + n); } \
  53. \
  54. VECTOR_OP(elems, -) \
  55. VECTOR_OP(elems, +) \
  56. VECTOR_OP(elems, *) \
  57. VECTOR_OP(elems, /) \
  58. \
  59. BOOL_OP(elems, ==, false) \
  60. BOOL_OP(elems, !=, true) \
  61. \
  62. SCALAR_OP(elems, -) \
  63. SCALAR_OP(elems, +) \
  64. SCALAR_OP(elems, *) \
  65. SCALAR_OP(elems, /) \
  66. \
  67. CAST_OP(elems, 2) \
  68. CAST_OP(elems, 3) \
  69. CAST_OP(elems, 4) \
  70. \
  71. template<typename U> \
  72. inline operator Vec##elems<U>() const \
  73. { \
  74. Vec##elems<U> ret; \
  75. for (int n = 0; n < elems; n++) \
  76. ret[n] = static_cast<U>((*this)[n]); \
  77. return ret; \
  78. } \
  79. \
  80. inline T sqlen() const \
  81. { \
  82. T acc = 0; \
  83. for (int n = 0; n < elems; n++) \
  84. acc += (*this)[n] * (*this)[n]; \
  85. return acc; \
  86. } \
  87. \
  88. inline float len() const \
  89. { \
  90. return sqrtf((float)sqlen()); \
  91. }
  92. template <typename T> struct Vec2;
  93. template <typename T> struct Vec3;
  94. template <typename T> struct Vec4;
  95. template <typename T> struct Vec2
  96. {
  97. inline Vec2() { }
  98. inline Vec2(T val) { x = y = val; }
  99. inline Vec2(T _x, T _y) { x = _x; y = _y; }
  100. OPERATORS(2)
  101. union { T x; T a; T i; };
  102. union { T y; T b; T j; };
  103. };
  104. typedef Vec2<float> float2;
  105. typedef Vec2<int> int2;
  106. template <typename T> struct Vec3
  107. {
  108. inline Vec3() { }
  109. inline Vec3(T val) { x = y = z = val; }
  110. inline Vec3(T _x, T _y, T _z) { x = _x; y = _y; z = _z; }
  111. OPERATORS(3)
  112. union { T x; T a; T i; };
  113. union { T y; T b; T j; };
  114. union { T z; T c; T k; };
  115. };
  116. typedef Vec3<float> float3;
  117. typedef Vec3<int> int3;
  118. template <typename T> struct Vec4
  119. {
  120. inline Vec4() { }
  121. inline Vec4(T val) { x = y = z = w = val; }
  122. inline Vec4(T _x, T _y, T _z, T _w) { x = _x; y = _y; z = _z; w = _w; }
  123. OPERATORS(4)
  124. union { T x; T a; T i; };
  125. union { T y; T b; T j; };
  126. union { T z; T c; T k; };
  127. union { T w; T d; T l; };
  128. };
  129. typedef Vec4<float> float4;
  130. typedef Vec4<int> int4;
  131. #define SCALAR_GLOBAL(elems, op, U) \
  132. template<typename T> \
  133. static inline Vec##elems<U> operator op(U const &val, \
  134. Vec##elems<T> const &that) \
  135. { \
  136. Vec##elems<U> ret; \
  137. for (int n = 0; n < elems; n++) \
  138. ret[n] = val op that[n]; \
  139. return ret; \
  140. }
  141. #define SCALAR_GLOBAL2(elems, op) \
  142. SCALAR_GLOBAL(elems, op, int) \
  143. SCALAR_GLOBAL(elems, op, float)
  144. #define GLOBALS(elems) \
  145. SCALAR_GLOBAL2(elems, -) \
  146. SCALAR_GLOBAL2(elems, +) \
  147. SCALAR_GLOBAL2(elems, *) \
  148. SCALAR_GLOBAL2(elems, /)
  149. GLOBALS(2)
  150. GLOBALS(3)
  151. GLOBALS(4)
  152. template <typename T> struct Mat4
  153. {
  154. inline Mat4() { }
  155. inline Mat4(T val)
  156. {
  157. for (int j = 0; j < 4; j++)
  158. for (int i = 0; i < 4; i++)
  159. v[i][j] = (i == j) ? val : 0;
  160. }
  161. inline Mat4(Vec4<T> v0, Vec4<T> v1, Vec4<T> v2, Vec4<T> v3)
  162. {
  163. v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3;
  164. }
  165. inline Vec4<T>& operator[](int n) { return v[n]; }
  166. inline Vec4<T> const& operator[](int n) const { return v[n]; }
  167. T det() const;
  168. Mat4<T> invert() const;
  169. static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
  170. static Mat4<T> perspective(T theta, T width, T height, T near, T far);
  171. inline Mat4<T> operator +(Mat4<T> const val) const
  172. {
  173. Mat4<T> ret;
  174. for (int j = 0; j < 4; j++)
  175. for (int i = 0; i < 4; i++)
  176. ret[i][j] = v[i][j] + val[i][j];
  177. return ret;
  178. }
  179. inline Mat4<T> operator -(Mat4<T> const val) const
  180. {
  181. Mat4<T> ret;
  182. for (int j = 0; j < 4; j++)
  183. for (int i = 0; i < 4; i++)
  184. ret[i][j] = v[i][j] - val[i][j];
  185. return ret;
  186. }
  187. inline Mat4<T> operator *(Mat4<T> const val) const
  188. {
  189. Mat4<T> ret;
  190. for (int j = 0; j < 4; j++)
  191. for (int i = 0; i < 4; i++)
  192. {
  193. T tmp = 0;
  194. for (int k = 0; k < 4; k++)
  195. tmp += v[k][j] * val[i][k];
  196. ret[i][j] = tmp;
  197. }
  198. return ret;
  199. }
  200. inline Vec4<T> operator *(Vec4<T> const val) const
  201. {
  202. Vec4<T> ret;
  203. for (int j = 0; j < 4; j++)
  204. {
  205. T tmp = 0;
  206. for (int i = 0; i < 4; i++)
  207. tmp += v[i][j] * val[i];
  208. ret[j] = tmp;
  209. }
  210. return ret;
  211. }
  212. Vec4<T> v[4];
  213. };
  214. typedef Mat4<float> float4x4;
  215. typedef Mat4<int> int4x4;
  216. #endif // __DH_MATRIX_H__