1
0

affine.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /*
  8. Functions:
  9. CGLM_INLINE mat4s glms_translate(mat4s m, vec3s v);
  10. CGLM_INLINE mat4s glms_translate_x(mat4s m, float x);
  11. CGLM_INLINE mat4s glms_translate_y(mat4s m, float y);
  12. CGLM_INLINE mat4s glms_translate_z(mat4s m, float z);
  13. CGLM_INLINE mat4s glms_translate_make(vec3s v);
  14. CGLM_INLINE mat4s glms_scale_to(mat4s m, vec3s v);
  15. CGLM_INLINE mat4s glms_scale_make(vec3s v);
  16. CGLM_INLINE mat4s glms_scale(mat4s m, vec3s v);
  17. CGLM_INLINE mat4s glms_scale_uni(mat4s m, float s);
  18. CGLM_INLINE mat4s glms_rotate_x(mat4s m, float angle);
  19. CGLM_INLINE mat4s glms_rotate_y(mat4s m, float angle);
  20. CGLM_INLINE mat4s glms_rotate_z(mat4s m, float angle);
  21. CGLM_INLINE mat4s glms_rotate_make(float angle, vec3s axis);
  22. CGLM_INLINE mat4s glms_rotate(mat4s m, float angle, vec3s axis);
  23. CGLM_INLINE mat4s glms_rotate_at(mat4s m, vec3s pivot, float angle, vec3s axis);
  24. CGLM_INLINE mat4s glms_rotate_atm(vec3s pivot, float angle, vec3s axis);
  25. CGLM_INLINE mat4s glms_spin(mat4s m, float angle, vec3s axis);
  26. CGLM_INLINE vec3s glms_decompose_scalev(mat4s m);
  27. CGLM_INLINE bool glms_uniscaled(mat4s m);
  28. CGLM_INLINE void glms_decompose_rs(mat4s m, mat4s * r, vec3s * s);
  29. CGLM_INLINE void glms_decompose(mat4s m, vec4s t, mat4s * r, vec3s * s);
  30. */
  31. #ifndef cglms_affines_h
  32. #define cglms_affines_h
  33. #include "../common.h"
  34. #include "../types-struct.h"
  35. #include "../affine.h"
  36. #include "vec3.h"
  37. #include "vec4.h"
  38. #include "mat4.h"
  39. #include "affine-mat.h"
  40. /*!
  41. * @brief creates NEW translate transform matrix by v vector
  42. *
  43. * @param[in] v translate vector [x, y, z]
  44. * @returns affine transform
  45. */
  46. CGLM_INLINE
  47. mat4s
  48. glms_translate_make(vec3s v) {
  49. mat4s m;
  50. glm_translate_make(m.raw, v.raw);
  51. return m;
  52. }
  53. /*!
  54. * @brief creates NEW scale matrix by v vector
  55. *
  56. * @param[in] v scale vector [x, y, z]
  57. * @returns affine transform
  58. */
  59. CGLM_INLINE
  60. mat4s
  61. glms_scale_make(vec3s v) {
  62. mat4s m;
  63. glm_scale_make(m.raw, v.raw);
  64. return m;
  65. }
  66. /*!
  67. * @brief scales existing transform matrix by v vector
  68. * and stores result in same matrix
  69. *
  70. * @param[in] m affine transform
  71. * @param[in] v scale vector [x, y, z]
  72. * @returns affine transform
  73. */
  74. CGLM_INLINE
  75. mat4s
  76. glms_scale(mat4s m, vec3s v) {
  77. mat4s r;
  78. glm_scale_to(m.raw, v.raw, r.raw);
  79. return r;
  80. }
  81. /*!
  82. * @brief applies uniform scale to existing transform matrix v = [s, s, s]
  83. * and stores result in same matrix
  84. *
  85. * @param[in] m affine transform
  86. * @param[in] s scale factor
  87. * @returns affine transform
  88. */
  89. CGLM_INLINE
  90. mat4s
  91. glms_scale_uni(mat4s m, float s) {
  92. glm_scale_uni(m.raw, s);
  93. return m;
  94. }
  95. /*!
  96. * @brief creates NEW rotation matrix by angle and axis
  97. *
  98. * axis will be normalized so you don't need to normalize it
  99. *
  100. * @param[in] angle angle (radians)
  101. * @param[in] axis axis
  102. * @returns affine transform
  103. */
  104. CGLM_INLINE
  105. mat4s
  106. glms_rotate_make(float angle, vec3s axis) {
  107. mat4s m;
  108. glm_rotate_make(m.raw, angle, axis.raw);
  109. return m;
  110. }
  111. /*!
  112. * @brief creates NEW rotation matrix by angle and axis at given point
  113. *
  114. * this creates rotation matrix, it assumes you don't have a matrix
  115. *
  116. * this should work faster than glm_rotate_at because it reduces
  117. * one glm_translate.
  118. *
  119. * @param[in] pivot rotation center
  120. * @param[in] angle angle (radians)
  121. * @param[in] axis axis
  122. * @returns affine transform
  123. */
  124. CGLM_INLINE
  125. mat4s
  126. glms_rotate_atm(vec3s pivot, float angle, vec3s axis) {
  127. mat4s m;
  128. glm_rotate_atm(m.raw, pivot.raw, angle, axis.raw);
  129. return m;
  130. }
  131. /*!
  132. * @brief decompose scale vector
  133. *
  134. * @param[in] m affine transform
  135. * @returns scale vector (Sx, Sy, Sz)
  136. */
  137. CGLM_INLINE
  138. vec3s
  139. glms_decompose_scalev(mat4s m) {
  140. vec3s r;
  141. glm_decompose_scalev(m.raw, r.raw);
  142. return r;
  143. }
  144. /*!
  145. * @brief returns true if matrix is uniform scaled. This is helpful for
  146. * creating normal matrix.
  147. *
  148. * @param[in] m m
  149. *
  150. * @return boolean
  151. */
  152. CGLM_INLINE
  153. bool
  154. glms_uniscaled(mat4s m) {
  155. return glm_uniscaled(m.raw);
  156. }
  157. /*!
  158. * @brief decompose rotation matrix (mat4) and scale vector [Sx, Sy, Sz]
  159. * DON'T pass projected matrix here
  160. *
  161. * @param[in] m affine transform
  162. * @param[out] r rotation matrix
  163. * @param[out] s scale matrix
  164. */
  165. CGLM_INLINE
  166. void
  167. glms_decompose_rs(mat4s m, mat4s * __restrict r, vec3s * __restrict s) {
  168. glm_decompose_rs(m.raw, r->raw, s->raw);
  169. }
  170. /*!
  171. * @brief decompose affine transform, TODO: extract shear factors.
  172. * DON'T pass projected matrix here
  173. *
  174. * @param[in] m affine transform
  175. * @param[out] t translation vector
  176. * @param[out] r rotation matrix (mat4)
  177. * @param[out] s scaling vector [X, Y, Z]
  178. */
  179. CGLM_INLINE
  180. void
  181. glms_decompose(mat4s m, vec4s * __restrict t, mat4s * __restrict r, vec3s * __restrict s) {
  182. glm_decompose(m.raw, t->raw, r->raw, s->raw);
  183. }
  184. #include "affine-pre.h"
  185. #include "affine-post.h"
  186. #endif /* cglms_affines_h */