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.

153 lines
7.1KB

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