vec3.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "vec3.h"
  15. #include "vec4.h"
  16. #include "quat.h"
  17. #include "axisang.h"
  18. #include "plane.h"
  19. #include "matrix3.h"
  20. #include "math-extra.h"
  21. void vec3_from_vec4(struct vec3 *dst, const struct vec4 *v)
  22. {
  23. dst->m = v->m;
  24. dst->w = 0.0f;
  25. }
  26. float vec3_plane_dist(const struct vec3 *v, const struct plane *p)
  27. {
  28. return vec3_dot(v, &p->dir) - p->dist;
  29. }
  30. void vec3_rotate(struct vec3 *dst, const struct vec3 *v, const struct matrix3 *m)
  31. {
  32. struct vec3 temp;
  33. vec3_copy(&temp, v);
  34. dst->x = vec3_dot(&temp, &m->x);
  35. dst->y = vec3_dot(&temp, &m->y);
  36. dst->z = vec3_dot(&temp, &m->z);
  37. dst->w = 0.0f;
  38. }
  39. void vec3_transform(struct vec3 *dst, const struct vec3 *v, const struct matrix4 *m)
  40. {
  41. struct vec4 v4;
  42. vec4_from_vec3(&v4, v);
  43. vec4_transform(&v4, &v4, m);
  44. vec3_from_vec4(dst, &v4);
  45. }
  46. void vec3_transform3x4(struct vec3 *dst, const struct vec3 *v, const struct matrix3 *m)
  47. {
  48. struct vec3 temp;
  49. vec3_sub(&temp, v, &m->t);
  50. dst->x = vec3_dot(&temp, &m->x);
  51. dst->y = vec3_dot(&temp, &m->y);
  52. dst->z = vec3_dot(&temp, &m->z);
  53. dst->w = 0.0f;
  54. }
  55. void vec3_mirror(struct vec3 *dst, const struct vec3 *v, const struct plane *p)
  56. {
  57. struct vec3 temp;
  58. vec3_mulf(&temp, &p->dir, vec3_plane_dist(v, p) * 2.0f);
  59. vec3_sub(dst, v, &temp);
  60. }
  61. void vec3_mirrorv(struct vec3 *dst, const struct vec3 *v, const struct vec3 *vec)
  62. {
  63. struct vec3 temp;
  64. vec3_mulf(&temp, vec, vec3_dot(v, vec) * 2.0f);
  65. vec3_sub(dst, v, &temp);
  66. }
  67. void vec3_rand(struct vec3 *dst, int positive_only)
  68. {
  69. dst->x = rand_float(positive_only);
  70. dst->y = rand_float(positive_only);
  71. dst->z = rand_float(positive_only);
  72. dst->w = 0.0f;
  73. }