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.

mathfuncs.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************\
  2. | OpenGL 4 Example Code. |
  3. | Accompanies written series "Anton's OpenGL 4 Tutorials" |
  4. | Email: anton at antongerdelan dot net |
  5. | First version 27 Jan 2014 |
  6. | Copyright Dr Anton Gerdelan, Trinity College Dublin, Ireland. |
  7. | See individual libraries' separate legal notices |
  8. |******************************************************************************|
  9. | Commonly-used maths structures and functions |
  10. | Simple-as-possible. No disgusting templates. |
  11. | Structs vec3, mat4, versor. just hold arrays of floats called "v","m","q", |
  12. | respectively. So, for example, to get values from a mat4 do: my_mat.m |
  13. | A versor is the proper name for a unit quaternion. |
  14. | This is C++ because it's sort-of convenient to be able to use maths operators|
  15. \******************************************************************************/
  16. #pragma once
  17. // const used to convert degrees into radians
  18. #define TWO_PI 2.0 * M_PI
  19. #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
  20. #define ONE_RAD_IN_DEG 57.2957795
  21. struct vec2;
  22. struct vec3;
  23. struct vec4;
  24. struct versor;
  25. struct vec2 {
  26. vec2 ();
  27. vec2 (float x, float y);
  28. float v[2];
  29. };
  30. struct vec3 {
  31. vec3 ();
  32. // create from 3 scalars
  33. vec3 (float x, float y, float z);
  34. // create from vec2 and a scalar
  35. vec3 (const vec2& vv, float z);
  36. // create from truncated vec4
  37. vec3 (const vec4& vv);
  38. // add vector to vector
  39. vec3 operator+ (const vec3& rhs);
  40. // add scalar to vector
  41. vec3 operator+ (float rhs);
  42. // because user's expect this too
  43. vec3& operator+= (const vec3& rhs);
  44. // subtract vector from vector
  45. vec3 operator- (const vec3& rhs);
  46. // add vector to vector
  47. vec3 operator- (float rhs);
  48. // because users expect this too
  49. vec3& operator-= (const vec3& rhs);
  50. // multiply with scalar
  51. vec3 operator* (float rhs);
  52. // because users expect this too
  53. vec3& operator*= (float rhs);
  54. // divide vector by scalar
  55. vec3 operator/ (float rhs);
  56. // because users expect this too
  57. vec3& operator= (const vec3& rhs);
  58. // internal data
  59. float v[3];
  60. };
  61. struct vec4 {
  62. vec4 ();
  63. vec4 (float x, float y, float z, float w);
  64. vec4 (const vec2& vv, float z, float w);
  65. vec4 (const vec3& vv, float w);
  66. float v[4];
  67. };
  68. struct vec2u {
  69. unsigned v[2];
  70. };
  71. /* stored like this:
  72. 0 3 6
  73. 1 4 7
  74. 2 5 8 */
  75. struct mat3 {
  76. mat3 ();
  77. // note! this is entering components in ROW-major order
  78. mat3 (float a, float b, float c,
  79. float d, float e, float f,
  80. float g, float h, float i);
  81. float m[9];
  82. };
  83. /* stored like this:
  84. 0 4 8 12
  85. 1 5 9 13
  86. 2 6 10 14
  87. 3 7 11 15*/
  88. struct mat4 {
  89. mat4 ();
  90. // note! this is entering components in ROW-major order
  91. mat4 (float a, float b, float c, float d,
  92. float e, float f, float g, float h,
  93. float i, float j, float k, float l,
  94. float mm, float n, float o, float p);
  95. vec4 operator* (const vec4& rhs);
  96. mat4 operator* (const mat4& rhs);
  97. mat4& operator= (const mat4& rhs);
  98. float m[16];
  99. };
  100. struct versor {
  101. versor ();
  102. versor operator/ (float rhs);
  103. versor operator* (float rhs);
  104. versor operator* (const versor& rhs);
  105. versor operator+ (const versor& rhs);
  106. float q[4];
  107. };
  108. void print (const vec2& v);
  109. void print (const vec3& v);
  110. void print (const vec4& v);
  111. void print (const mat3& m);
  112. void print (const mat4& m);
  113. // floating point functions
  114. int signum(const float& f);
  115. float intbound(const float& s, const float& ds);
  116. float mod(const float& value, const float& modulus);
  117. // vector functions
  118. float length (const vec3& v);
  119. float length2 (const vec3& v);
  120. vec3 normalise (const vec3& v);
  121. float dot (const vec3& a, const vec3& b);
  122. vec3 cross (const vec3& a, const vec3& b);
  123. float get_squared_dist (vec3 from, vec3 to);
  124. float direction_to_heading (vec3 d);
  125. vec3 heading_to_direction (float degrees);
  126. // matrix functions
  127. mat3 zero_mat3 ();
  128. mat3 identity_mat3 ();
  129. mat4 zero_mat4 ();
  130. mat4 identity_mat4 ();
  131. float determinant (const mat4& mm);
  132. mat4 inverse (const mat4& mm);
  133. mat4 transpose (const mat4& mm);
  134. // affine functions
  135. mat4 translate (const mat4& m, const vec3& v);
  136. mat4 rotate_x_deg (const mat4& m, float deg);
  137. mat4 rotate_y_deg (const mat4& m, float deg);
  138. mat4 rotate_z_deg (const mat4& m, float deg);
  139. mat4 scale (const mat4& m, const vec3& v);
  140. // camera functions
  141. mat4 look_at (const vec3& cam_pos, vec3 targ_pos, const vec3& up);
  142. mat4 perspective (float fovy, float aspect, float near, float far);
  143. // quaternion functions
  144. versor quat_from_axis_rad (float radians, float x, float y, float z);
  145. versor quat_from_axis_deg (float degrees, float x, float y, float z);
  146. mat4 quat_to_mat4 (const versor& q);
  147. float dot (const versor& q, const versor& r);
  148. versor slerp (const versor& q, const versor& r);
  149. // stupid overloading wouldn't let me use const
  150. versor normalise (versor& q);
  151. void print (const versor& q);
  152. versor slerp (versor& q, versor& r, float t);