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.

159 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MATRIX3D_H_INCLUDED
  18. #define JUCE_MATRIX3D_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A 4x4 transformation matrix.
  22. @see AffineTransform
  23. */
  24. template <typename Type>
  25. class Matrix3D
  26. {
  27. public:
  28. Matrix3D() noexcept
  29. {
  30. mat[0] = (Type) 1; mat[1] = 0; mat[2] = 0; mat[3] = 0;
  31. mat[4] = 0; mat[5] = (Type) 1; mat[6] = 0; mat[7] = 0;
  32. mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0;
  33. mat[12] = 0; mat[13] = 0; mat[14] = 0; mat[15] = (Type) 1;
  34. }
  35. Matrix3D (const Matrix3D& other) noexcept
  36. {
  37. memcpy (mat, other.mat, sizeof (mat));
  38. }
  39. Matrix3D (const AffineTransform& transform) noexcept
  40. {
  41. mat[0] = transform.mat00; mat[1] = transform.mat10; mat[2] = 0; mat[3] = 0;
  42. mat[4] = transform.mat01; mat[5] = transform.mat11; mat[6] = 0; mat[7] = 0;
  43. mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0;
  44. mat[12] = transform.mat02; mat[13] = transform.mat12; mat[14] = 0; mat[15] = (Type) 1;
  45. }
  46. Matrix3D (const Type* values) noexcept
  47. {
  48. memcpy (mat, values, sizeof (mat));
  49. }
  50. Matrix3D (const Type& m00, const Type& m10, const Type& m20, const Type& m30,
  51. const Type& m01, const Type& m11, const Type& m21, const Type& m31,
  52. const Type& m02, const Type& m12, const Type& m22, const Type& m32,
  53. const Type& m03, const Type& m13, const Type& m23, const 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. Matrix3D (Vector3D<Type> vector) noexcept
  61. {
  62. mat[0] = (Type) 1; mat[1] = 0; mat[2] = 0; mat[3] = 0;
  63. mat[4] = 0; mat[5] = (Type) 1; mat[6] = 0; mat[7] = 0;
  64. mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0;
  65. mat[12] = vector.x; mat[13] = vector.y; mat[14] = vector.z; mat[15] = (Type) 1;
  66. }
  67. Matrix3D& operator= (const Matrix3D& other) noexcept
  68. {
  69. memcpy (mat, other.mat, sizeof (mat));
  70. return *this;
  71. }
  72. /** Returns a new matrix from the given frustrum values. */
  73. static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type near, Type far) noexcept
  74. {
  75. return Matrix3D ((2.0f * near) / (right - left), 0.0f, 0.0f, 0.0f,
  76. 0.0f, (2.0f * near) / (top - bottom), 0.0f, 0.0f,
  77. (right + left) / (right - left), (top + bottom) / (top - bottom), -(far + near) / (far - near), -1.0f,
  78. 0.0f, 0.0f, -(2.0f * far * near) / (far - near), 0.0f);
  79. }
  80. /** Multiplies this matrix by another. */
  81. Matrix3D& operator*= (const Matrix3D& other) noexcept
  82. {
  83. return *this = *this * other;
  84. }
  85. /** Multiplies this matrix by another, and returns the result. */
  86. Matrix3D operator* (const Matrix3D& other) const noexcept
  87. {
  88. const Type* const m2 = other.mat;
  89. return Matrix3D (mat[0] * m2[0] + mat[4] * m2[1] + mat[8] * m2[2] + mat[12] * m2[3],
  90. mat[1] * m2[0] + mat[5] * m2[1] + mat[9] * m2[2] + mat[13] * m2[3],
  91. mat[2] * m2[0] + mat[6] * m2[1] + mat[10] * m2[2] + mat[14] * m2[3],
  92. mat[3] * m2[0] + mat[7] * m2[1] + mat[11] * m2[2] + mat[15] * m2[3],
  93. mat[0] * m2[4] + mat[4] * m2[5] + mat[8] * m2[6] + mat[12] * m2[7],
  94. mat[1] * m2[4] + mat[5] * m2[5] + mat[9] * m2[6] + mat[13] * m2[7],
  95. mat[2] * m2[4] + mat[6] * m2[5] + mat[10] * m2[6] + mat[14] * m2[7],
  96. mat[3] * m2[4] + mat[7] * m2[5] + mat[11] * m2[6] + mat[15] * m2[7],
  97. mat[0] * m2[8] + mat[4] * m2[9] + mat[8] * m2[10] + mat[12] * m2[11],
  98. mat[1] * m2[8] + mat[5] * m2[9] + mat[9] * m2[10] + mat[13] * m2[11],
  99. mat[2] * m2[8] + mat[6] * m2[9] + mat[10] * m2[10] + mat[14] * m2[11],
  100. mat[3] * m2[8] + mat[7] * m2[9] + mat[11] * m2[10] + mat[15] * m2[11],
  101. mat[0] * m2[12] + mat[4] * m2[13] + mat[8] * m2[14] + mat[12] * m2[15],
  102. mat[1] * m2[12] + mat[5] * m2[13] + mat[9] * m2[14] + mat[13] * m2[15],
  103. mat[2] * m2[12] + mat[6] * m2[13] + mat[10] * m2[14] + mat[14] * m2[15],
  104. mat[3] * m2[12] + mat[7] * m2[13] + mat[11] * m2[14] + mat[15] * m2[15]);
  105. }
  106. /** Returns a copy of this matrix after rotation through the Y, X and then Z angles
  107. specified by the vector.
  108. */
  109. Matrix3D rotated (Vector3D<Type> eulerAngleRadians) const noexcept
  110. {
  111. const Type cx = std::cos (eulerAngleRadians.x);
  112. const Type sx = std::sin (eulerAngleRadians.x);
  113. const Type cy = std::cos (eulerAngleRadians.y);
  114. const Type sy = std::sin (eulerAngleRadians.y);
  115. const Type cz = std::cos (eulerAngleRadians.z);
  116. const Type sz = std::sin (eulerAngleRadians.z);
  117. return Matrix3D ((cy * cz) + (sx * sy * sz), cx * sz, (cy * sx * sz) - (cz * sy), 0.0f,
  118. (cz * sx * sy) - (cy * sz), cx * cz, (cy * cz * sx) + (sy * sz), 0.0f,
  119. cx * sy, -sx, cx * cy, 0.0f,
  120. 0.0f, 0.0f, 0.0f, 1.0f);
  121. }
  122. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  123. /** Multiplies the active OpenGL context's matrix by this one. */
  124. void applyToOpenGL() const noexcept
  125. {
  126. OpenGLHelpers::applyMatrix (mat);
  127. }
  128. #endif
  129. /** The 4x4 matrix values. These are stored in the standard OpenGL order. */
  130. Type mat[16];
  131. };
  132. #endif // JUCE_MATRIX3D_H_INCLUDED