General Purpose library for Freestanding C++ and POSIX systems
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.

166 regels
2.4 KiB

  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/math/integer_math.hpp"
  4. #include "gp/math/q_math.hpp"
  5. #if !defined(NO_FP_MATH)
  6. # include "gp/math/fp_math.hpp"
  7. #endif
  8. namespace gp {
  9. template<typename T = gp_config::rendering::default_type>
  10. struct vec2_g {
  11. T x;
  12. T y;
  13. vec2_g(
  14. T _x,
  15. T _y
  16. )
  17. : x{_x}
  18. , y{_y}
  19. {}
  20. vec2_g operator/(vec2_g rhs) {
  21. return {
  22. x / rhs.x,
  23. y / rhs.y
  24. };
  25. }
  26. vec2_g operator+(vec2_g oth) {
  27. return {x+oth.x, y+oth.y};
  28. }
  29. vec2_g normalize() {
  30. T ilen = fast_isqrt(x*x+y*y);
  31. return {x*ilen, y*ilen};
  32. }
  33. };
  34. template<typename T = gp_config::rendering::default_type>
  35. struct vec3_g {
  36. T x;
  37. T y;
  38. T z;
  39. vec3_g(
  40. T _x,
  41. T _y,
  42. T _z
  43. )
  44. : x{_x}
  45. , y{_y}
  46. , z{_z}
  47. {}
  48. vec3_g(vec2_g<T> left, T right)
  49. : x{left.x}
  50. , y{left.y}
  51. , z{right}
  52. {}
  53. vec3_g(T left, vec2_g<T> right)
  54. : x{left}
  55. , y{right.x}
  56. , z{right.y}
  57. {}
  58. vec3_g operator/(vec3_g rhs) {
  59. return {
  60. x / rhs.x,
  61. y / rhs.y,
  62. z / rhs.z
  63. };
  64. }
  65. vec3_g operator+(vec3_g oth) {
  66. return {x+oth.x, y+oth.y, z+oth.z};
  67. }
  68. vec3_g normalize() {
  69. T ilen = fast_isqrt(x*x+y*y+z*z);
  70. return {x*ilen, y*ilen, z*ilen};
  71. }
  72. };
  73. template<typename T = gp_config::rendering::default_type>
  74. struct vec4_g {
  75. T x;
  76. T y;
  77. T z;
  78. T w;
  79. vec4_g(
  80. T _x,
  81. T _y,
  82. T _z,
  83. T _w
  84. )
  85. : x{_x}
  86. , y{_y}
  87. , z{_z}
  88. , w{_w}
  89. {}
  90. vec4_g(T left, vec3_g<> right)
  91. : x{left}
  92. , y{right.x}
  93. , z{right.y}
  94. , w{right.z}
  95. {}
  96. vec4_g(vec3_g<> left, T right)
  97. : x{left.x}
  98. , y{left.y}
  99. , z{left.z}
  100. , w{right}
  101. {}
  102. vec4_g operator/(vec4_g rhs) {
  103. return {
  104. x / rhs.x,
  105. y / rhs.y,
  106. z / rhs.z,
  107. w / rhs.w
  108. };
  109. }
  110. vec4_g operator+(vec4_g oth) {
  111. return {x+oth.x, y+oth.y, z+oth.z, w+oth.w};
  112. }
  113. vec4_g normalize() {
  114. T ilen = fast_isqrt(x*x+y*y+z*z+w*w);
  115. return {x*ilen, y*ilen, z*ilen, w*ilen};
  116. }
  117. };
  118. template<typename T>
  119. vec2_g<T> operator*(vec2_g<T> p, T v) {
  120. return {p.x*v, p.y*v};
  121. }
  122. template<typename T>
  123. vec3_g<T> operator*(vec3_g<T> p, T v) {
  124. return {p.x*v, p.y*v, p.z*v};
  125. }
  126. template<typename T>
  127. vec4_g<T> operator*(vec4_g<T> p, T v) {
  128. return {p.x*v, p.y*v, p.z*v, p.w*v};
  129. }
  130. template<typename T>
  131. vec2_g<T> operator*(T v, vec2_g<T> p) {
  132. return p*v;
  133. }
  134. template<typename T>
  135. vec3_g<T> operator*(T v, vec3_g<T> p) {
  136. return p*v;
  137. }
  138. template<typename T>
  139. vec4_g<T> operator*(T v, vec4_g<T> p) {
  140. return p*v;
  141. }
  142. }