matrix4.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "vec3.h"
  16. #include "vec4.h"
  17. #include "axisang.h"
  18. /* 4x4 Matrix */
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct matrix3;
  23. struct matrix4 {
  24. struct vec4 x, y, z, t;
  25. };
  26. static inline void matrix4_copy(struct matrix4 *dst, const struct matrix4 *m)
  27. {
  28. dst->x.m = m->x.m;
  29. dst->y.m = m->y.m;
  30. dst->z.m = m->z.m;
  31. dst->t.m = m->t.m;
  32. }
  33. static inline void matrix4_identity(struct matrix4 *dst)
  34. {
  35. vec4_zero(&dst->x);
  36. vec4_zero(&dst->y);
  37. vec4_zero(&dst->z);
  38. vec4_zero(&dst->t);
  39. dst->x.x = 1.0f;
  40. dst->y.y = 1.0f;
  41. dst->z.z = 1.0f;
  42. dst->t.w = 1.0f;
  43. }
  44. EXPORT void matrix4_from_matrix3(struct matrix4 *dst, const struct matrix3 *m);
  45. EXPORT void matrix4_from_quat(struct matrix4 *dst, const struct quat *q);
  46. EXPORT void matrix4_from_axisang(struct matrix4 *dst, const struct axisang *aa);
  47. EXPORT void matrix4_mul(struct matrix4 *dst, const struct matrix4 *m1, const struct matrix4 *m2);
  48. EXPORT float matrix4_determinant(const struct matrix4 *m);
  49. EXPORT void matrix4_translate3v(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v);
  50. EXPORT void matrix4_translate4v(struct matrix4 *dst, const struct matrix4 *m, const struct vec4 *v);
  51. EXPORT void matrix4_rotate(struct matrix4 *dst, const struct matrix4 *m, const struct quat *q);
  52. EXPORT void matrix4_rotate_aa(struct matrix4 *dst, const struct matrix4 *m, const struct axisang *aa);
  53. EXPORT void matrix4_scale(struct matrix4 *dst, const struct matrix4 *m, const struct vec3 *v);
  54. EXPORT bool matrix4_inv(struct matrix4 *dst, const struct matrix4 *m);
  55. EXPORT void matrix4_transpose(struct matrix4 *dst, const struct matrix4 *m);
  56. EXPORT void matrix4_translate3v_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m);
  57. EXPORT void matrix4_translate4v_i(struct matrix4 *dst, const struct vec4 *v, const struct matrix4 *m);
  58. EXPORT void matrix4_rotate_i(struct matrix4 *dst, const struct quat *q, const struct matrix4 *m);
  59. EXPORT void matrix4_rotate_aa_i(struct matrix4 *dst, const struct axisang *aa, const struct matrix4 *m);
  60. EXPORT void matrix4_scale_i(struct matrix4 *dst, const struct vec3 *v, const struct matrix4 *m);
  61. static inline void matrix4_translate3f(struct matrix4 *dst, const struct matrix4 *m, float x, float y, float z)
  62. {
  63. struct vec3 v;
  64. vec3_set(&v, x, y, z);
  65. matrix4_translate3v(dst, m, &v);
  66. }
  67. static inline void matrix4_rotate_aa4f(struct matrix4 *dst, const struct matrix4 *m, float x, float y, float z,
  68. float rot)
  69. {
  70. struct axisang aa;
  71. axisang_set(&aa, x, y, z, rot);
  72. matrix4_rotate_aa(dst, m, &aa);
  73. }
  74. static inline void matrix4_scale3f(struct matrix4 *dst, const struct matrix4 *m, float x, float y, float z)
  75. {
  76. struct vec3 v;
  77. vec3_set(&v, x, y, z);
  78. matrix4_scale(dst, m, &v);
  79. }
  80. #ifdef __cplusplus
  81. }
  82. #endif