Browse Source

The Matrix3D::rotated() method wasn't doing what its comment said, so it has been renamed to Matrix3D::rotation() and made static, which correctly describes what it does. If you had code that was calling rotated(), you can safely replace it with rotation(), but should use the opportunity to check whether your code was doing what you thought it was!

tags/2021-05-28
jules 8 years ago
parent
commit
472c5616d4
3 changed files with 76 additions and 81 deletions
  1. +17
    -20
      examples/Demo/Source/Demos/OpenGLDemo.cpp
  2. +49
    -51
      modules/juce_opengl/geometry/juce_Matrix3D.h
  3. +10
    -10
      modules/juce_opengl/geometry/juce_Vector3D.h

+ 17
- 20
examples/Demo/Source/Demos/OpenGLDemo.cpp View File

@@ -340,9 +340,7 @@ struct OpenGLDemoClasses
DemoControlsOverlay (OpenGLDemo& d) DemoControlsOverlay (OpenGLDemo& d)
: demo (d), : demo (d),
vertexEditorComp (vertexDocument, nullptr), vertexEditorComp (vertexDocument, nullptr),
fragmentEditorComp (fragmentDocument, nullptr),
tabbedComp (TabbedButtonBar::TabsAtLeft),
showBackgroundToggle ("Draw 2D graphics in background")
fragmentEditorComp (fragmentDocument, nullptr)
{ {
addAndMakeVisible (statusLabel); addAndMakeVisible (statusLabel);
statusLabel.setJustificationType (Justification::topLeft); statusLabel.setJustificationType (Justification::topLeft);
@@ -575,13 +573,13 @@ struct OpenGLDemoClasses
CodeDocument vertexDocument, fragmentDocument; CodeDocument vertexDocument, fragmentDocument;
CodeEditorComponent vertexEditorComp, fragmentEditorComp; CodeEditorComponent vertexEditorComp, fragmentEditorComp;
TabbedComponent tabbedComp;
TabbedComponent tabbedComp { TabbedButtonBar::TabsAtLeft };
ComboBox presetBox, textureBox; ComboBox presetBox, textureBox;
Label presetLabel, textureLabel; Label presetLabel, textureLabel;
Slider speedSlider, sizeSlider; Slider speedSlider, sizeSlider;
ToggleButton showBackgroundToggle;
ToggleButton showBackgroundToggle { "Draw 2D graphics in background" };
OwnedArray<DemoTexture> textures; OwnedArray<DemoTexture> textures;
@@ -598,9 +596,6 @@ struct OpenGLDemoClasses
{ {
public: public:
OpenGLDemo() OpenGLDemo()
: doBackgroundDrawing (false),
scale (0.5f), rotationSpeed (0.0f), rotation (0.0f),
textureToUse (nullptr), lastTexture (nullptr)
{ {
if (MainAppWindow* mw = MainAppWindow::getMainAppWindow()) if (MainAppWindow* mw = MainAppWindow::getMainAppWindow())
mw->setRenderingEngine (0); mw->setRenderingEngine (0);
@@ -655,7 +650,7 @@ struct OpenGLDemoClasses
{ {
jassert (OpenGLHelpers::isContextActive()); jassert (OpenGLHelpers::isContextActive());
const float desktopScale = (float) openGLContext.getRenderingScale();
auto desktopScale = (float) openGLContext.getRenderingScale();
OpenGLHelpers::clear (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground, OpenGLHelpers::clear (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
Colours::lightblue)); Colours::lightblue));
@@ -718,17 +713,18 @@ struct OpenGLDemoClasses
Matrix3D<float> getProjectionMatrix() const Matrix3D<float> 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<float>::fromFrustum (-w, w, -h, h, 4.0f, 30.0f); return Matrix3D<float>::fromFrustum (-w, w, -h, h, 4.0f, 30.0f);
} }
Matrix3D<float> getViewMatrix() const Matrix3D<float> getViewMatrix() const
{ {
Matrix3D<float> viewMatrix = draggableOrientation.getRotationMatrix()
* Vector3D<float> (0.0f, 1.0f, -10.0f);
auto viewMatrix = draggableOrientation.getRotationMatrix()
* Vector3D<float> (0.0f, 1.0f, -10.0f);
Matrix3D<float> rotationMatrix = viewMatrix.rotated (Vector3D<float> (rotation, rotation, -0.3f));
auto rotationMatrix = Matrix3D<float>::rotation ({ rotation, rotation, -0.3f });
return rotationMatrix * viewMatrix; return rotationMatrix * viewMatrix;
} }
@@ -753,8 +749,8 @@ struct OpenGLDemoClasses
} }
Draggable3DOrientation draggableOrientation; Draggable3DOrientation draggableOrientation;
bool doBackgroundDrawing;
float scale, rotationSpeed;
bool doBackgroundDrawing = false;
float scale = 0.5f, rotationSpeed = 0;
BouncingNumber bouncingNumber; BouncingNumber bouncingNumber;
private: private:
@@ -802,7 +798,7 @@ struct OpenGLDemoClasses
ScopedPointer<DemoControlsOverlay> controlsOverlay; ScopedPointer<DemoControlsOverlay> controlsOverlay;
float rotation;
float rotation = 0;
ScopedPointer<OpenGLShaderProgram> shader; ScopedPointer<OpenGLShaderProgram> shader;
ScopedPointer<Shape> shape; ScopedPointer<Shape> shape;
@@ -810,7 +806,8 @@ struct OpenGLDemoClasses
ScopedPointer<Uniforms> uniforms; ScopedPointer<Uniforms> uniforms;
OpenGLTexture texture; OpenGLTexture texture;
DemoTexture* textureToUse, *lastTexture;
DemoTexture* textureToUse = nullptr;
DemoTexture* lastTexture = nullptr;
String newVertexShader, newFragmentShader, statusText; String newVertexShader, newFragmentShader, statusText;
@@ -852,8 +849,8 @@ struct OpenGLDemoClasses
triggerAsyncUpdate(); triggerAsyncUpdate();
newVertexShader = String();
newFragmentShader = String();
newVertexShader = {};
newFragmentShader = {};
} }
} }


+ 49
- 51
modules/juce_opengl/geometry/juce_Matrix3D.h View File

@@ -40,10 +40,10 @@ public:
/** Creates an identity matrix. */ /** Creates an identity matrix. */
Matrix3D() noexcept 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. */ /** Creates a copy of another matrix. */
@@ -60,10 +60,10 @@ public:
} }
/** Creates a matrix from its raw 4x4 values. */ /** 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[0] = m00; mat[1] = m10; mat[2] = m20; mat[3] = m30;
mat[4] = m01; mat[5] = m11; mat[6] = m21; mat[7] = m31; 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[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[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. */ /** Creates a matrix from a 3D vector translation. */
Matrix3D (Vector3D<Type> vector) noexcept Matrix3D (Vector3D<Type> 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. */ /** 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 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<Type> 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. */ /** Multiplies this matrix by another. */
@@ -113,39 +126,24 @@ public:
/** Multiplies this matrix by another, and returns the result. */ /** Multiplies this matrix by another, and returns the result. */
Matrix3D operator* (const Matrix3D& other) const noexcept Matrix3D operator* (const Matrix3D& other) const noexcept
{ {
const Type* const 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<Type> 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);
auto&& m2 = other.mat;
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. */ /** The 4x4 matrix values. These are stored in the standard OpenGL order. */


+ 10
- 10
modules/juce_opengl/geometry/juce_Vector3D.h View File

@@ -41,28 +41,28 @@ public:
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; }
/** Returns a vector that lies along the X axis. */ /** 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. */ /** 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. */ /** 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-= (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/= (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. */ /** Returns the dot-product of these two vectors. */
Type operator* (Vector3D other) const noexcept { return x * other.x + y * other.y + z * other.z; } Type operator* (Vector3D other) const noexcept { return x * other.x + y * other.y + z * other.z; }
/** Returns the cross-product of these two vectors. */ /** 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 length() const noexcept { return std::sqrt (lengthSquared()); }
Type lengthSquared() const noexcept { return x * x + y * y + z * z; } 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. */ /** Returns true if the vector is practically equal to the origin. */
bool lengthIsBelowEpsilon() const noexcept bool lengthIsBelowEpsilon() const noexcept
{ {
const Type epsilon (std::numeric_limits<Type>::epsilon());
auto epsilon = std::numeric_limits<Type>::epsilon();
return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon); return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
} }


Loading…
Cancel
Save