diff --git a/examples/Demo/Source/Demos/OpenGLDemo.cpp b/examples/Demo/Source/Demos/OpenGLDemo.cpp index a6fabbf974..96945736f3 100644 --- a/examples/Demo/Source/Demos/OpenGLDemo.cpp +++ b/examples/Demo/Source/Demos/OpenGLDemo.cpp @@ -340,9 +340,7 @@ struct OpenGLDemoClasses DemoControlsOverlay (OpenGLDemo& d) : demo (d), vertexEditorComp (vertexDocument, nullptr), - fragmentEditorComp (fragmentDocument, nullptr), - tabbedComp (TabbedButtonBar::TabsAtLeft), - showBackgroundToggle ("Draw 2D graphics in background") + fragmentEditorComp (fragmentDocument, nullptr) { addAndMakeVisible (statusLabel); statusLabel.setJustificationType (Justification::topLeft); @@ -575,13 +573,13 @@ struct OpenGLDemoClasses CodeDocument vertexDocument, fragmentDocument; CodeEditorComponent vertexEditorComp, fragmentEditorComp; - TabbedComponent tabbedComp; + TabbedComponent tabbedComp { TabbedButtonBar::TabsAtLeft }; ComboBox presetBox, textureBox; Label presetLabel, textureLabel; Slider speedSlider, sizeSlider; - ToggleButton showBackgroundToggle; + ToggleButton showBackgroundToggle { "Draw 2D graphics in background" }; OwnedArray textures; @@ -598,9 +596,6 @@ struct OpenGLDemoClasses { public: OpenGLDemo() - : doBackgroundDrawing (false), - scale (0.5f), rotationSpeed (0.0f), rotation (0.0f), - textureToUse (nullptr), lastTexture (nullptr) { if (MainAppWindow* mw = MainAppWindow::getMainAppWindow()) mw->setRenderingEngine (0); @@ -655,7 +650,7 @@ struct OpenGLDemoClasses { jassert (OpenGLHelpers::isContextActive()); - const float desktopScale = (float) openGLContext.getRenderingScale(); + auto desktopScale = (float) openGLContext.getRenderingScale(); OpenGLHelpers::clear (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground, Colours::lightblue)); @@ -718,17 +713,18 @@ struct OpenGLDemoClasses Matrix3D getProjectionMatrix() const { - float w = 1.0f / (scale + 0.1f); - float h = w * getLocalBounds().toFloat().getAspectRatio (false); + auto w = 1.0f / (scale + 0.1f); + auto h = w * getLocalBounds().toFloat().getAspectRatio (false); + return Matrix3D::fromFrustum (-w, w, -h, h, 4.0f, 30.0f); } Matrix3D getViewMatrix() const { - Matrix3D viewMatrix = draggableOrientation.getRotationMatrix() - * Vector3D (0.0f, 1.0f, -10.0f); + auto viewMatrix = draggableOrientation.getRotationMatrix() + * Vector3D (0.0f, 1.0f, -10.0f); - Matrix3D rotationMatrix = viewMatrix.rotated (Vector3D (rotation, rotation, -0.3f)); + auto rotationMatrix = Matrix3D::rotation ({ rotation, rotation, -0.3f }); return rotationMatrix * viewMatrix; } @@ -753,8 +749,8 @@ struct OpenGLDemoClasses } Draggable3DOrientation draggableOrientation; - bool doBackgroundDrawing; - float scale, rotationSpeed; + bool doBackgroundDrawing = false; + float scale = 0.5f, rotationSpeed = 0; BouncingNumber bouncingNumber; private: @@ -802,7 +798,7 @@ struct OpenGLDemoClasses ScopedPointer controlsOverlay; - float rotation; + float rotation = 0; ScopedPointer shader; ScopedPointer shape; @@ -810,7 +806,8 @@ struct OpenGLDemoClasses ScopedPointer uniforms; OpenGLTexture texture; - DemoTexture* textureToUse, *lastTexture; + DemoTexture* textureToUse = nullptr; + DemoTexture* lastTexture = nullptr; String newVertexShader, newFragmentShader, statusText; @@ -852,8 +849,8 @@ struct OpenGLDemoClasses triggerAsyncUpdate(); - newVertexShader = String(); - newFragmentShader = String(); + newVertexShader = {}; + newFragmentShader = {}; } } diff --git a/modules/juce_opengl/geometry/juce_Matrix3D.h b/modules/juce_opengl/geometry/juce_Matrix3D.h index 5bf7bdcf59..305c25f8c4 100644 --- a/modules/juce_opengl/geometry/juce_Matrix3D.h +++ b/modules/juce_opengl/geometry/juce_Matrix3D.h @@ -40,10 +40,10 @@ public: /** Creates an identity matrix. */ Matrix3D() noexcept { - mat[0] = (Type) 1; mat[1] = 0; mat[2] = 0; mat[3] = 0; - mat[4] = 0; mat[5] = (Type) 1; mat[6] = 0; mat[7] = 0; - mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0; - mat[12] = 0; mat[13] = 0; mat[14] = 0; mat[15] = (Type) 1; + mat[0] = Type (1); mat[1] = 0; mat[2] = 0; mat[3] = 0; + mat[4] = 0; mat[5] = Type (1); mat[6] = 0; mat[7] = 0; + mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0; + mat[12] = 0; mat[13] = 0; mat[14] = 0; mat[15] = Type (1); } /** Creates a copy of another matrix. */ @@ -60,10 +60,10 @@ public: } /** Creates a matrix from its raw 4x4 values. */ - Matrix3D (const Type& m00, const Type& m10, const Type& m20, const Type& m30, - const Type& m01, const Type& m11, const Type& m21, const Type& m31, - const Type& m02, const Type& m12, const Type& m22, const Type& m32, - const Type& m03, const Type& m13, const Type& m23, const Type& m33) noexcept + Matrix3D (Type m00, Type m10, Type m20, Type m30, + Type m01, Type m11, Type m21, Type m31, + Type m02, Type m12, Type m22, Type m32, + Type m03, Type m13, Type m23, Type m33) noexcept { mat[0] = m00; mat[1] = m10; mat[2] = m20; mat[3] = m30; mat[4] = m01; mat[5] = m11; mat[6] = m21; mat[7] = m31; @@ -82,26 +82,39 @@ public: { mat[0] = transform.mat00; mat[1] = transform.mat10; mat[2] = 0; mat[3] = 0; mat[4] = transform.mat01; mat[5] = transform.mat11; mat[6] = 0; mat[7] = 0; - mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0; - mat[12] = transform.mat02; mat[13] = transform.mat12; mat[14] = 0; mat[15] = (Type) 1; + mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0; + mat[12] = transform.mat02; mat[13] = transform.mat12; mat[14] = 0; mat[15] = Type (1); } /** Creates a matrix from a 3D vector translation. */ Matrix3D (Vector3D vector) noexcept { - mat[0] = (Type) 1; mat[1] = 0; mat[2] = 0; mat[3] = 0; - mat[4] = 0; mat[5] = (Type) 1; mat[6] = 0; mat[7] = 0; - mat[8] = 0; mat[9] = 0; mat[10] = (Type) 1; mat[11] = 0; - mat[12] = vector.x; mat[13] = vector.y; mat[14] = vector.z; mat[15] = (Type) 1; + mat[0] = Type (1); mat[1] = 0; mat[2] = 0; mat[3] = 0; + mat[4] = 0; mat[5] = Type (1); mat[6] = 0; mat[7] = 0; + mat[8] = 0; mat[9] = 0; mat[10] = Type (1); mat[11] = 0; + mat[12] = vector.x; mat[13] = vector.y; mat[14] = vector.z; mat[15] = Type (1); } /** Returns a new matrix from the given frustrum values. */ static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type nearDistance, Type farDistance) noexcept { - return Matrix3D ((2.0f * nearDistance) / (right - left), 0.0f, 0.0f, 0.0f, - 0.0f, (2.0f * nearDistance) / (top - bottom), 0.0f, 0.0f, - (right + left) / (right - left), (top + bottom) / (top - bottom), -(farDistance + nearDistance) / (farDistance - nearDistance), -1.0f, - 0.0f, 0.0f, -(2.0f * farDistance * nearDistance) / (farDistance - nearDistance), 0.0f); + return { (Type (2) * nearDistance) / (right - left), 0, 0, 0, + 0, (Type (2) * nearDistance) / (top - bottom), 0, 0, + (right + left) / (right - left), (top + bottom) / (top - bottom), -(farDistance + nearDistance) / (farDistance - nearDistance), Type (-1), + 0, 0, -(Type (2) * farDistance * nearDistance) / (farDistance - nearDistance), 0 }; + } + + /** Returns a matrix which will apply a rotation through the Y, X and Z angles specified by a vector. */ + static Matrix3D rotation (Vector3D eulerAngleRadians) noexcept + { + auto cx = std::cos (eulerAngleRadians.x), sx = std::sin (eulerAngleRadians.x), + cy = std::cos (eulerAngleRadians.y), sy = std::sin (eulerAngleRadians.y), + cz = std::cos (eulerAngleRadians.z), sz = std::sin (eulerAngleRadians.z); + + return { (cy * cz) + (sx * sy * sz), cx * sz, (cy * sx * sz) - (cz * sy), 0, + (cz * sx * sy) - (cy * sz), cx * cz, (cy * cz * sx) + (sy * sz), 0, + cx * sy, -sx, cx * cy, 0, + 0, 0, 0, Type (1) }; } /** Multiplies this matrix by another. */ @@ -113,39 +126,24 @@ public: /** Multiplies this matrix by another, and returns the result. */ Matrix3D operator* (const Matrix3D& other) const noexcept { - const Type* const m2 = other.mat; + auto&& m2 = other.mat; - return Matrix3D (mat[0] * m2[0] + mat[1] * m2[4] + mat[2] * m2[8] + mat[3] * m2[12], - mat[0] * m2[1] + mat[1] * m2[5] + mat[2] * m2[9] + mat[3] * m2[13], - mat[0] * m2[2] + mat[1] * m2[6] + mat[2] * m2[10] + mat[3] * m2[14], - mat[0] * m2[3] + mat[1] * m2[7] + mat[2] * m2[11] + mat[3] * m2[15], - mat[4] * m2[0] + mat[5] * m2[4] + mat[6] * m2[8] + mat[7] * m2[12], - mat[4] * m2[1] + mat[5] * m2[5] + mat[6] * m2[9] + mat[7] * m2[13], - mat[4] * m2[2] + mat[5] * m2[6] + mat[6] * m2[10] + mat[7] * m2[14], - mat[4] * m2[3] + mat[5] * m2[7] + mat[6] * m2[11] + mat[7] * m2[15], - mat[8] * m2[0] + mat[9] * m2[4] + mat[10] * m2[8] + mat[11] * m2[12], - mat[8] * m2[1] + mat[9] * m2[5] + mat[10] * m2[9] + mat[11] * m2[13], - mat[8] * m2[2] + mat[9] * m2[6] + mat[10] * m2[10] + mat[11] * m2[14], - mat[8] * m2[3] + mat[9] * m2[7] + mat[10] * m2[11] + mat[11] * m2[15], - mat[12] * m2[0] + mat[13] * m2[4] + mat[14] * m2[8] + mat[15] * m2[12], - mat[12] * m2[1] + mat[13] * m2[5] + mat[14] * m2[9] + mat[15] * m2[13], - mat[12] * m2[2] + mat[13] * m2[6] + mat[14] * m2[10] + mat[15] * m2[14], - mat[12] * m2[3] + mat[13] * m2[7] + mat[14] * m2[11] + mat[15] * m2[15]); - } - - /** Returns a copy of this matrix after rotation through the Y, X and then Z angles - specified by the vector. - */ - Matrix3D rotated (Vector3D eulerAngleRadians) const noexcept - { - const Type cx = std::cos (eulerAngleRadians.x), sx = std::sin (eulerAngleRadians.x), - cy = std::cos (eulerAngleRadians.y), sy = std::sin (eulerAngleRadians.y), - cz = std::cos (eulerAngleRadians.z), sz = std::sin (eulerAngleRadians.z); - - return Matrix3D ((cy * cz) + (sx * sy * sz), cx * sz, (cy * sx * sz) - (cz * sy), 0.0f, - (cz * sx * sy) - (cy * sz), cx * cz, (cy * cz * sx) + (sy * sz), 0.0f, - cx * sy, -sx, cx * cy, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f); + return { mat[0] * m2[0] + mat[1] * m2[4] + mat[2] * m2[8] + mat[3] * m2[12], + mat[0] * m2[1] + mat[1] * m2[5] + mat[2] * m2[9] + mat[3] * m2[13], + mat[0] * m2[2] + mat[1] * m2[6] + mat[2] * m2[10] + mat[3] * m2[14], + mat[0] * m2[3] + mat[1] * m2[7] + mat[2] * m2[11] + mat[3] * m2[15], + mat[4] * m2[0] + mat[5] * m2[4] + mat[6] * m2[8] + mat[7] * m2[12], + mat[4] * m2[1] + mat[5] * m2[5] + mat[6] * m2[9] + mat[7] * m2[13], + mat[4] * m2[2] + mat[5] * m2[6] + mat[6] * m2[10] + mat[7] * m2[14], + mat[4] * m2[3] + mat[5] * m2[7] + mat[6] * m2[11] + mat[7] * m2[15], + mat[8] * m2[0] + mat[9] * m2[4] + mat[10] * m2[8] + mat[11] * m2[12], + mat[8] * m2[1] + mat[9] * m2[5] + mat[10] * m2[9] + mat[11] * m2[13], + mat[8] * m2[2] + mat[9] * m2[6] + mat[10] * m2[10] + mat[11] * m2[14], + mat[8] * m2[3] + mat[9] * m2[7] + mat[10] * m2[11] + mat[11] * m2[15], + mat[12] * m2[0] + mat[13] * m2[4] + mat[14] * m2[8] + mat[15] * m2[12], + mat[12] * m2[1] + mat[13] * m2[5] + mat[14] * m2[9] + mat[15] * m2[13], + mat[12] * m2[2] + mat[13] * m2[6] + mat[14] * m2[10] + mat[15] * m2[14], + mat[12] * m2[3] + mat[13] * m2[7] + mat[14] * m2[11] + mat[15] * m2[15] }; } /** The 4x4 matrix values. These are stored in the standard OpenGL order. */ diff --git a/modules/juce_opengl/geometry/juce_Vector3D.h b/modules/juce_opengl/geometry/juce_Vector3D.h index ba292075f6..1c92c5552b 100644 --- a/modules/juce_opengl/geometry/juce_Vector3D.h +++ b/modules/juce_opengl/geometry/juce_Vector3D.h @@ -41,28 +41,28 @@ public: Vector3D& operator= (Vector3D other) noexcept { x = other.x; y = other.y; z = other.z; return *this; } /** Returns a vector that lies along the X axis. */ - static Vector3D xAxis() noexcept { return Vector3D ((Type) 1, 0, 0); } + static Vector3D xAxis() noexcept { return { (Type) 1, 0, 0 }; } /** Returns a vector that lies along the Y axis. */ - static Vector3D yAxis() noexcept { return Vector3D (0, (Type) 1, 0); } + static Vector3D yAxis() noexcept { return { 0, (Type) 1, 0 }; } /** Returns a vector that lies along the Z axis. */ - static Vector3D zAxis() noexcept { return Vector3D (0, 0, (Type) 1); } + static Vector3D zAxis() noexcept { return { 0, 0, (Type) 1 }; } Vector3D& operator+= (Vector3D other) noexcept { x += other.x; y += other.y; z += other.z; return *this; } Vector3D& operator-= (Vector3D other) noexcept { x -= other.x; y -= other.y; z -= other.z; return *this; } Vector3D& operator*= (Type scaleFactor) noexcept { x *= scaleFactor; y *= scaleFactor; z *= scaleFactor; return *this; } Vector3D& operator/= (Type scaleFactor) noexcept { x /= scaleFactor; y /= scaleFactor; z /= scaleFactor; return *this; } - Vector3D operator+ (Vector3D other) const noexcept { return Vector3D (x + other.x, y + other.y, z + other.z); } - Vector3D operator- (Vector3D other) const noexcept { return Vector3D (x - other.x, y - other.y, z - other.z); } - Vector3D operator* (Type scaleFactor) const noexcept { return Vector3D (x * scaleFactor, y * scaleFactor, z * scaleFactor); } - Vector3D operator/ (Type scaleFactor) const noexcept { return Vector3D (x / scaleFactor, y / scaleFactor, z / scaleFactor); } - Vector3D operator-() const noexcept { return Vector3D (-x, -y, -z); } + Vector3D operator+ (Vector3D other) const noexcept { return { x + other.x, y + other.y, z + other.z }; } + Vector3D operator- (Vector3D other) const noexcept { return { x - other.x, y - other.y, z - other.z }; } + Vector3D operator* (Type scaleFactor) const noexcept { return { x * scaleFactor, y * scaleFactor, z * scaleFactor }; } + Vector3D operator/ (Type scaleFactor) const noexcept { return { x / scaleFactor, y / scaleFactor, z / scaleFactor }; } + Vector3D operator-() const noexcept { return { -x, -y, -z }; } /** Returns the dot-product of these two vectors. */ Type operator* (Vector3D other) const noexcept { return x * other.x + y * other.y + z * other.z; } /** Returns the cross-product of these two vectors. */ - Vector3D operator^ (Vector3D other) const noexcept { return Vector3D (y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x); } + Vector3D operator^ (Vector3D other) const noexcept { return { y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x }; } Type length() const noexcept { return std::sqrt (lengthSquared()); } Type lengthSquared() const noexcept { return x * x + y * y + z * z; } @@ -72,7 +72,7 @@ public: /** Returns true if the vector is practically equal to the origin. */ bool lengthIsBelowEpsilon() const noexcept { - const Type epsilon (std::numeric_limits::epsilon()); + auto epsilon = std::numeric_limits::epsilon(); return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon); }