affine-post.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. #ifndef cglm_affine_post_h
  8. #define cglm_affine_post_h
  9. /*
  10. Functions:
  11. CGLM_INLINE void glm_translated_to(mat4 m, vec3 v, mat4 dest);
  12. CGLM_INLINE void glm_translated(mat4 m, vec3 v);
  13. CGLM_INLINE void glm_translated_x(mat4 m, float to);
  14. CGLM_INLINE void glm_translated_y(mat4 m, float to);
  15. CGLM_INLINE void glm_translated_z(mat4 m, float to);
  16. CGLM_INLINE void glm_rotated_x(mat4 m, float angle, mat4 dest);
  17. CGLM_INLINE void glm_rotated_y(mat4 m, float angle, mat4 dest);
  18. CGLM_INLINE void glm_rotated_z(mat4 m, float angle, mat4 dest);
  19. CGLM_INLINE void glm_rotated(mat4 m, float angle, vec3 axis);
  20. CGLM_INLINE void glm_rotated_at(mat4 m, vec3 pivot, float angle, vec3 axis);
  21. CGLM_INLINE void glm_spinned(mat4 m, float angle, vec3 axis);
  22. */
  23. #include "common.h"
  24. #include "util.h"
  25. #include "vec3.h"
  26. #include "vec4.h"
  27. #include "mat4.h"
  28. #include "affine-mat.h"
  29. /*!
  30. * @brief translate existing transform matrix by v vector
  31. * and stores result in same matrix
  32. *
  33. * this is POST transform, applies to existing transform as last transfrom
  34. *
  35. * @param[in, out] m affine transfrom
  36. * @param[in] v translate vector [x, y, z]
  37. */
  38. CGLM_INLINE
  39. void
  40. glm_translated(mat4 m, vec3 v) {
  41. glm_vec3_add(m[3], v, m[3]);
  42. }
  43. /*!
  44. * @brief translate existing transform matrix by v vector
  45. * and store result in dest
  46. *
  47. * source matrix will remain same
  48. *
  49. * this is POST transform, applies to existing transform as last transfrom
  50. *
  51. * @param[in] m affine transfrom
  52. * @param[in] v translate vector [x, y, z]
  53. * @param[out] dest translated matrix
  54. */
  55. CGLM_INLINE
  56. void
  57. glm_translated_to(mat4 m, vec3 v, mat4 dest) {
  58. glm_mat4_copy(m, dest);
  59. glm_translated(dest, v);
  60. }
  61. /*!
  62. * @brief translate existing transform matrix by x factor
  63. *
  64. * this is POST transform, applies to existing transform as last transfrom
  65. *
  66. * @param[in, out] m affine transfrom
  67. * @param[in] x x factor
  68. */
  69. CGLM_INLINE
  70. void
  71. glm_translated_x(mat4 m, float x) {
  72. m[3][0] += x;
  73. }
  74. /*!
  75. * @brief translate existing transform matrix by y factor
  76. *
  77. * this is POST transform, applies to existing transform as last transfrom
  78. *
  79. * @param[in, out] m affine transfrom
  80. * @param[in] y y factor
  81. */
  82. CGLM_INLINE
  83. void
  84. glm_translated_y(mat4 m, float y) {
  85. m[3][1] += y;
  86. }
  87. /*!
  88. * @brief translate existing transform matrix by z factor
  89. *
  90. * this is POST transform, applies to existing transform as last transfrom
  91. *
  92. * @param[in, out] m affine transfrom
  93. * @param[in] z z factor
  94. */
  95. CGLM_INLINE
  96. void
  97. glm_translated_z(mat4 m, float z) {
  98. m[3][2] += z;
  99. }
  100. /*!
  101. * @brief rotate existing transform matrix around X axis by angle
  102. * and store result in dest
  103. *
  104. * this is POST transform, applies to existing transform as last transfrom
  105. *
  106. * @param[in] m affine transfrom
  107. * @param[in] angle angle (radians)
  108. * @param[out] dest rotated matrix
  109. */
  110. CGLM_INLINE
  111. void
  112. glm_rotated_x(mat4 m, float angle, mat4 dest) {
  113. CGLM_ALIGN_MAT mat4 t = GLM_MAT4_IDENTITY_INIT;
  114. float c, s;
  115. c = cosf(angle);
  116. s = sinf(angle);
  117. t[1][1] = c;
  118. t[1][2] = s;
  119. t[2][1] = -s;
  120. t[2][2] = c;
  121. glm_mul_rot(t, m, dest);
  122. }
  123. /*!
  124. * @brief rotate existing transform matrix around Y axis by angle
  125. * and store result in dest
  126. *
  127. * this is POST transform, applies to existing transform as last transfrom
  128. *
  129. * @param[in] m affine transfrom
  130. * @param[in] angle angle (radians)
  131. * @param[out] dest rotated matrix
  132. */
  133. CGLM_INLINE
  134. void
  135. glm_rotated_y(mat4 m, float angle, mat4 dest) {
  136. CGLM_ALIGN_MAT mat4 t = GLM_MAT4_IDENTITY_INIT;
  137. float c, s;
  138. c = cosf(angle);
  139. s = sinf(angle);
  140. t[0][0] = c;
  141. t[0][2] = -s;
  142. t[2][0] = s;
  143. t[2][2] = c;
  144. glm_mul_rot(t, m, dest);
  145. }
  146. /*!
  147. * @brief rotate existing transform matrix around Z axis by angle
  148. * and store result in dest
  149. *
  150. * this is POST transform, applies to existing transform as last transfrom
  151. *
  152. * @param[in] m affine transfrom
  153. * @param[in] angle angle (radians)
  154. * @param[out] dest rotated matrix
  155. */
  156. CGLM_INLINE
  157. void
  158. glm_rotated_z(mat4 m, float angle, mat4 dest) {
  159. CGLM_ALIGN_MAT mat4 t = GLM_MAT4_IDENTITY_INIT;
  160. float c, s;
  161. c = cosf(angle);
  162. s = sinf(angle);
  163. t[0][0] = c;
  164. t[0][1] = s;
  165. t[1][0] = -s;
  166. t[1][1] = c;
  167. glm_mul_rot(t, m, dest);
  168. }
  169. /*!
  170. * @brief rotate existing transform matrix around given axis by angle
  171. *
  172. * this is POST transform, applies to existing transform as last transfrom
  173. *
  174. * @param[in, out] m affine transfrom
  175. * @param[in] angle angle (radians)
  176. * @param[in] axis axis
  177. */
  178. CGLM_INLINE
  179. void
  180. glm_rotated(mat4 m, float angle, vec3 axis) {
  181. CGLM_ALIGN_MAT mat4 rot;
  182. glm_rotate_make(rot, angle, axis);
  183. glm_mul_rot(rot, m, m);
  184. }
  185. /*!
  186. * @brief rotate existing transform
  187. * around given axis by angle at given pivot point (rotation center)
  188. *
  189. * this is POST transform, applies to existing transform as last transfrom
  190. *
  191. * @param[in, out] m affine transfrom
  192. * @param[in] pivot rotation center
  193. * @param[in] angle angle (radians)
  194. * @param[in] axis axis
  195. */
  196. CGLM_INLINE
  197. void
  198. glm_rotated_at(mat4 m, vec3 pivot, float angle, vec3 axis) {
  199. CGLM_ALIGN(8) vec3 pivotInv;
  200. glm_vec3_negate_to(pivot, pivotInv);
  201. glm_translated(m, pivot);
  202. glm_rotated(m, angle, axis);
  203. glm_translated(m, pivotInv);
  204. }
  205. /*!
  206. * @brief rotate existing transform matrix around given axis by angle around self (doesn't affected by position)
  207. *
  208. * this is POST transform, applies to existing transform as last transfrom
  209. *
  210. * @param[in, out] m affine transfrom
  211. * @param[in] angle angle (radians)
  212. * @param[in] axis axis
  213. */
  214. CGLM_INLINE
  215. void
  216. glm_spinned(mat4 m, float angle, vec3 axis) {
  217. CGLM_ALIGN_MAT mat4 rot;
  218. glm_rotate_atm(rot, m[3], angle, axis);
  219. glm_mat4_mul(rot, m, m);
  220. }
  221. #endif /* cglm_affine_post_h */