The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A 4x4 3D transformation matrix.
  18. @see Vector3D, Quaternion, AffineTransform
  19. @tags{OpenGL}
  20. */
  21. template <typename Type>
  22. class Matrix3D
  23. {
  24. public:
  25. /** Creates an identity matrix. */
  26. Matrix3D() noexcept
  27. {
  28. mat[0] = Type (1); mat[1] = 0; mat[2] = 0; mat[3] = 0;
  29. mat[4] = 0; mat[5] = Type (1); mat[6] = 0; mat[7] = 0;
  30. mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0;
  31. mat[12] = 0; mat[13] = 0; mat[14] = 0; mat[15] = Type (1);
  32. }
  33. /** Creates a copy of another matrix. */
  34. Matrix3D (const Matrix3D& other) noexcept
  35. {
  36. memcpy (mat, other.mat, sizeof (mat));
  37. }
  38. /** Copies another matrix. */
  39. Matrix3D& operator= (const Matrix3D& other) noexcept
  40. {
  41. memcpy (mat, other.mat, sizeof (mat));
  42. return *this;
  43. }
  44. /** Creates a matrix from its raw 4x4 values. */
  45. Matrix3D (Type m00, Type m10, Type m20, Type m30,
  46. Type m01, Type m11, Type m21, Type m31,
  47. Type m02, Type m12, Type m22, Type m32,
  48. Type m03, Type m13, Type m23, Type m33) noexcept
  49. {
  50. mat[0] = m00; mat[1] = m10; mat[2] = m20; mat[3] = m30;
  51. mat[4] = m01; mat[5] = m11; mat[6] = m21; mat[7] = m31;
  52. mat[8] = m02; mat[9] = m12; mat[10] = m22; mat[11] = m32;
  53. mat[12] = m03; mat[13] = m13; mat[14] = m23; mat[15] = m33;
  54. }
  55. /** Creates a matrix from an array of 16 raw values. */
  56. Matrix3D (const Type* values) noexcept
  57. {
  58. memcpy (mat, values, sizeof (mat));
  59. }
  60. /** Creates a matrix from a 2D affine transform. */
  61. Matrix3D (const AffineTransform& transform) noexcept
  62. {
  63. mat[0] = transform.mat00; mat[1] = transform.mat10; mat[2] = 0; mat[3] = 0;
  64. mat[4] = transform.mat01; mat[5] = transform.mat11; mat[6] = 0; mat[7] = 0;
  65. mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0;
  66. mat[12] = transform.mat02; mat[13] = transform.mat12; mat[14] = 0; mat[15] = Type (1);
  67. }
  68. /** Creates a matrix from a 3D vector translation. */
  69. Matrix3D (Vector3D<Type> vector) noexcept
  70. {
  71. mat[0] = Type (1); mat[1] = 0; mat[2] = 0; mat[3] = 0;
  72. mat[4] = 0; mat[5] = Type (1); mat[6] = 0; mat[7] = 0;
  73. mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0;
  74. mat[12] = vector.x; mat[13] = vector.y; mat[14] = vector.z; mat[15] = Type (1);
  75. }
  76. /** Returns a new matrix from the given frustum values. */
  77. static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type nearDistance, Type farDistance) noexcept
  78. {
  79. return { (Type (2) * nearDistance) / (right - left), 0, 0, 0,
  80. 0, (Type (2) * nearDistance) / (top - bottom), 0, 0,
  81. (right + left) / (right - left), (top + bottom) / (top - bottom), -(farDistance + nearDistance) / (farDistance - nearDistance), Type (-1),
  82. 0, 0, -(Type (2) * farDistance * nearDistance) / (farDistance - nearDistance), 0 };
  83. }
  84. /** Returns a matrix which will apply a rotation through the Y, X and Z angles specified by a vector. */
  85. static Matrix3D rotation (Vector3D<Type> eulerAngleRadians) noexcept
  86. {
  87. auto cx = std::cos (eulerAngleRadians.x), sx = std::sin (eulerAngleRadians.x),
  88. cy = std::cos (eulerAngleRadians.y), sy = std::sin (eulerAngleRadians.y),
  89. cz = std::cos (eulerAngleRadians.z), sz = std::sin (eulerAngleRadians.z);
  90. return { (cy * cz) + (sx * sy * sz), cx * sz, (cy * sx * sz) - (cz * sy), 0,
  91. (cz * sx * sy) - (cy * sz), cx * cz, (cy * cz * sx) + (sy * sz), 0,
  92. cx * sy, -sx, cx * cy, 0,
  93. 0, 0, 0, Type (1) };
  94. }
  95. /** Multiplies this matrix by another. */
  96. Matrix3D& operator*= (const Matrix3D& other) noexcept
  97. {
  98. return *this = *this * other;
  99. }
  100. /** Multiplies this matrix by another, and returns the result. */
  101. Matrix3D operator* (const Matrix3D& other) const noexcept
  102. {
  103. auto&& m2 = other.mat;
  104. return { mat[0] * m2[0] + mat[1] * m2[4] + mat[2] * m2[8] + mat[3] * m2[12],
  105. mat[0] * m2[1] + mat[1] * m2[5] + mat[2] * m2[9] + mat[3] * m2[13],
  106. mat[0] * m2[2] + mat[1] * m2[6] + mat[2] * m2[10] + mat[3] * m2[14],
  107. mat[0] * m2[3] + mat[1] * m2[7] + mat[2] * m2[11] + mat[3] * m2[15],
  108. mat[4] * m2[0] + mat[5] * m2[4] + mat[6] * m2[8] + mat[7] * m2[12],
  109. mat[4] * m2[1] + mat[5] * m2[5] + mat[6] * m2[9] + mat[7] * m2[13],
  110. mat[4] * m2[2] + mat[5] * m2[6] + mat[6] * m2[10] + mat[7] * m2[14],
  111. mat[4] * m2[3] + mat[5] * m2[7] + mat[6] * m2[11] + mat[7] * m2[15],
  112. mat[8] * m2[0] + mat[9] * m2[4] + mat[10] * m2[8] + mat[11] * m2[12],
  113. mat[8] * m2[1] + mat[9] * m2[5] + mat[10] * m2[9] + mat[11] * m2[13],
  114. mat[8] * m2[2] + mat[9] * m2[6] + mat[10] * m2[10] + mat[11] * m2[14],
  115. mat[8] * m2[3] + mat[9] * m2[7] + mat[10] * m2[11] + mat[11] * m2[15],
  116. mat[12] * m2[0] + mat[13] * m2[4] + mat[14] * m2[8] + mat[15] * m2[12],
  117. mat[12] * m2[1] + mat[13] * m2[5] + mat[14] * m2[9] + mat[15] * m2[13],
  118. mat[12] * m2[2] + mat[13] * m2[6] + mat[14] * m2[10] + mat[15] * m2[14],
  119. mat[12] * m2[3] + mat[13] * m2[7] + mat[14] * m2[11] + mat[15] * m2[15] };
  120. }
  121. /** The 4x4 matrix values. These are stored in the standard OpenGL order. */
  122. Type mat[16];
  123. };
  124. } // namespace juce