matrix4.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  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,
  47. const struct axisang *aa);
  48. EXPORT void matrix4_mul(struct matrix4 *dst, const struct matrix4 *m1,
  49. const struct matrix4 *m2);
  50. EXPORT float matrix4_determinant(const struct matrix4 *m);
  51. EXPORT void matrix4_translate3v(struct matrix4 *dst, const struct matrix4 *m,
  52. const struct vec3 *v);
  53. EXPORT void matrix4_translate4v(struct matrix4 *dst, const struct matrix4 *m,
  54. const struct vec4 *v);
  55. EXPORT void matrix4_rotate(struct matrix4 *dst, const struct matrix4 *m,
  56. const struct quat *q);
  57. EXPORT void matrix4_rotate_aa(struct matrix4 *dst, const struct matrix4 *m,
  58. const struct axisang *aa);
  59. EXPORT void matrix4_scale(struct matrix4 *dst, const struct matrix4 *m,
  60. const struct vec3 *v);
  61. EXPORT bool matrix4_inv(struct matrix4 *dst, const struct matrix4 *m);
  62. EXPORT void matrix4_transpose(struct matrix4 *dst, const struct matrix4 *m);
  63. EXPORT void matrix4_translate3v_i(struct matrix4 *dst, const struct vec3 *v,
  64. const struct matrix4 *m);
  65. EXPORT void matrix4_translate4v_i(struct matrix4 *dst, const struct vec4 *v,
  66. const struct matrix4 *m);
  67. EXPORT void matrix4_rotate_i(struct matrix4 *dst, const struct quat *q,
  68. const struct matrix4 *m);
  69. EXPORT void matrix4_rotate_aa_i(struct matrix4 *dst, const struct axisang *aa,
  70. const struct matrix4 *m);
  71. EXPORT void matrix4_scale_i(struct matrix4 *dst, const struct vec3 *v,
  72. const struct matrix4 *m);
  73. static inline void matrix4_translate3f(struct matrix4 *dst,
  74. const struct matrix4 *m, float x, float y, float z)
  75. {
  76. struct vec3 v;
  77. vec3_set(&v, x, y, z);
  78. matrix4_translate3v(dst, m, &v);
  79. }
  80. static inline void matrix4_rotate_aa4f(struct matrix4 *dst,
  81. const struct matrix4 *m, float x, float y, float z, float rot)
  82. {
  83. struct axisang aa;
  84. axisang_set(&aa, x, y, z, rot);
  85. matrix4_rotate_aa(dst, m, &aa);
  86. }
  87. static inline void matrix4_scale3f(struct matrix4 *dst,
  88. const struct matrix4 *m, float x, float y, float z)
  89. {
  90. struct vec3 v;
  91. vec3_set(&v, x, y, z);
  92. matrix4_scale(dst, m, &v);
  93. }
  94. #ifdef __cplusplus
  95. }
  96. #endif