Matrix4x4.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibGfx/Matrix.h>
  8. #include <LibGfx/Vector3.h>
  9. #include <LibGfx/Vector4.h>
  10. #include <math.h>
  11. namespace Gfx {
  12. template<typename T>
  13. using Matrix4x4 = Matrix<4, T>;
  14. template<typename T>
  15. constexpr static Vector4<T> operator*(const Matrix4x4<T>& m, const Vector4<T>& v)
  16. {
  17. auto const& elements = m.elements();
  18. return Vector4<T>(
  19. v.x() * elements[0][0] + v.y() * elements[0][1] + v.z() * elements[0][2] + v.w() * elements[0][3],
  20. v.x() * elements[1][0] + v.y() * elements[1][1] + v.z() * elements[1][2] + v.w() * elements[1][3],
  21. v.x() * elements[2][0] + v.y() * elements[2][1] + v.z() * elements[2][2] + v.w() * elements[2][3],
  22. v.x() * elements[3][0] + v.y() * elements[3][1] + v.z() * elements[3][2] + v.w() * elements[3][3]);
  23. }
  24. template<typename T>
  25. constexpr static Vector3<T> transform_point(const Matrix4x4<T>& m, const Vector3<T>& p)
  26. {
  27. auto const& elements = m.elements();
  28. return Vector3<T>(
  29. p.x() * elements[0][0] + p.y() * elements[0][1] + p.z() * elements[0][2] + elements[0][3],
  30. p.x() * elements[1][0] + p.y() * elements[1][1] + p.z() * elements[1][2] + elements[1][3],
  31. p.x() * elements[2][0] + p.y() * elements[2][1] + p.z() * elements[2][2] + elements[2][3]);
  32. }
  33. template<typename T>
  34. constexpr static Vector3<T> transform_direction(const Matrix4x4<T>& m, const Vector3<T>& d)
  35. {
  36. auto const& elements = m.elements();
  37. return Vector3<T>(
  38. d.x() * elements[0][0] + d.y() * elements[0][1] + d.z() * elements[0][2],
  39. d.x() * elements[1][0] + d.y() * elements[1][1] + d.z() * elements[1][2],
  40. d.x() * elements[2][0] + d.y() * elements[2][1] + d.z() * elements[2][2]);
  41. }
  42. template<typename T>
  43. constexpr static Matrix4x4<T> translation_matrix(const Vector3<T>& p)
  44. {
  45. return Matrix4x4<T>(
  46. 1, 0, 0, p.x(),
  47. 0, 1, 0, p.y(),
  48. 0, 0, 1, p.z(),
  49. 0, 0, 0, 1);
  50. }
  51. template<typename T>
  52. constexpr static Matrix4x4<T> scale_matrix(const Vector3<T>& s)
  53. {
  54. return Matrix4x4<T>(
  55. s.x(), 0, 0, 0,
  56. 0, s.y(), 0, 0,
  57. 0, 0, s.z(), 0,
  58. 0, 0, 0, 1);
  59. }
  60. template<typename T>
  61. constexpr static Matrix4x4<T> rotation_matrix(const Vector3<T>& axis, T angle)
  62. {
  63. T c = cos(angle);
  64. T s = sin(angle);
  65. T t = 1 - c;
  66. T x = axis.x();
  67. T y = axis.y();
  68. T z = axis.z();
  69. return Matrix4x4<T>(
  70. t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
  71. t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
  72. t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
  73. 0, 0, 0, 1);
  74. }
  75. typedef Matrix4x4<float> FloatMatrix4x4;
  76. typedef Matrix4x4<double> DoubleMatrix4x4;
  77. }
  78. using Gfx::DoubleMatrix4x4;
  79. using Gfx::FloatMatrix4x4;
  80. using Gfx::Matrix4x4;