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.

155 lines
6.8KB

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