migrating to the latest JUCE version
This commit is contained in:
@ -1,159 +1,159 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Stores a 3D orientation, which can be rotated by dragging with the mouse.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class Draggable3DOrientation
|
||||
{
|
||||
public:
|
||||
using VectorType = Vector3D<float>;
|
||||
using QuaternionType = Quaternion<float>;
|
||||
|
||||
/** Creates a Draggable3DOrientation, initially set up to be aligned along the X axis. */
|
||||
Draggable3DOrientation (float objectRadius = 0.5f) noexcept
|
||||
: radius (jmax (0.1f, objectRadius)),
|
||||
quaternion (VectorType::xAxis(), 0)
|
||||
{
|
||||
}
|
||||
|
||||
/** Creates a Draggable3DOrientation from a user-supplied quaternion. */
|
||||
Draggable3DOrientation (const Quaternion<float>& quaternionToUse,
|
||||
float objectRadius = 0.5f) noexcept
|
||||
: radius (jmax (0.1f, objectRadius)),
|
||||
quaternion (quaternionToUse)
|
||||
{
|
||||
}
|
||||
|
||||
/** Resets the orientation, specifying the axis to align it along. */
|
||||
void reset (const VectorType& axis) noexcept
|
||||
{
|
||||
quaternion = QuaternionType (axis, 0);
|
||||
}
|
||||
|
||||
/** Sets the viewport area within which mouse-drag positions will occur.
|
||||
You'll need to set this rectangle before calling mouseDown. The centre of the
|
||||
rectangle is assumed to be the centre of the object that will be rotated, and
|
||||
the size of the rectangle will be used to scale the object radius - see setRadius().
|
||||
*/
|
||||
void setViewport (Rectangle<int> newArea) noexcept
|
||||
{
|
||||
area = newArea;
|
||||
}
|
||||
|
||||
/** Sets the size of the rotated object, as a proportion of the viewport's size.
|
||||
@see setViewport
|
||||
*/
|
||||
void setRadius (float newRadius) noexcept
|
||||
{
|
||||
radius = jmax (0.1f, newRadius);
|
||||
}
|
||||
|
||||
/** Begins a mouse-drag operation.
|
||||
You must call this before any calls to mouseDrag(). The position that is supplied
|
||||
will be treated as being relative to the centre of the rectangle passed to setViewport().
|
||||
*/
|
||||
template <typename Type>
|
||||
void mouseDown (Point<Type> mousePos) noexcept
|
||||
{
|
||||
lastMouse = mousePosToProportion (mousePos.toFloat());
|
||||
}
|
||||
|
||||
/** Continues a mouse-drag operation.
|
||||
After calling mouseDown() to begin a drag sequence, you can call this method
|
||||
to continue it.
|
||||
*/
|
||||
template <typename Type>
|
||||
void mouseDrag (Point<Type> mousePos) noexcept
|
||||
{
|
||||
auto oldPos = projectOnSphere (lastMouse);
|
||||
lastMouse = mousePosToProportion (mousePos.toFloat());
|
||||
auto newPos = projectOnSphere (lastMouse);
|
||||
|
||||
quaternion *= rotationFromMove (oldPos, newPos);
|
||||
}
|
||||
|
||||
/** Returns the matrix that should be used to apply the current orientation.
|
||||
@see applyToOpenGLMatrix
|
||||
*/
|
||||
Matrix3D<float> getRotationMatrix() const noexcept
|
||||
{
|
||||
return quaternion.getRotationMatrix();
|
||||
}
|
||||
|
||||
/** Provides direct access to the quaternion. */
|
||||
QuaternionType& getQuaternion() noexcept
|
||||
{
|
||||
return quaternion;
|
||||
}
|
||||
|
||||
private:
|
||||
Rectangle<int> area;
|
||||
float radius;
|
||||
QuaternionType quaternion;
|
||||
Point<float> lastMouse;
|
||||
|
||||
Point<float> mousePosToProportion (Point<float> mousePos) const noexcept
|
||||
{
|
||||
auto scale = jmin (area.getWidth(), area.getHeight()) / 2;
|
||||
|
||||
// You must call setViewport() to give this object a valid window size before
|
||||
// calling any of the mouse input methods!
|
||||
jassert (scale > 0);
|
||||
|
||||
return { (mousePos.x - (float) area.getCentreX()) / (float) scale,
|
||||
((float) area.getCentreY() - mousePos.y) / (float) scale };
|
||||
}
|
||||
|
||||
VectorType projectOnSphere (Point<float> pos) const noexcept
|
||||
{
|
||||
auto radiusSquared = radius * radius;
|
||||
auto xySquared = pos.x * pos.x + pos.y * pos.y;
|
||||
|
||||
return { pos.x, pos.y,
|
||||
xySquared < radiusSquared * 0.5f ? std::sqrt (radiusSquared - xySquared)
|
||||
: (radiusSquared / (2.0f * std::sqrt (xySquared))) };
|
||||
}
|
||||
|
||||
QuaternionType rotationFromMove (const VectorType& from, const VectorType& to) const noexcept
|
||||
{
|
||||
auto rotationAxis = (to ^ from);
|
||||
|
||||
if (rotationAxis.lengthIsBelowEpsilon())
|
||||
rotationAxis = VectorType::xAxis();
|
||||
|
||||
auto d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
|
||||
|
||||
return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Stores a 3D orientation, which can be rotated by dragging with the mouse.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class Draggable3DOrientation
|
||||
{
|
||||
public:
|
||||
using VectorType = Vector3D<float>;
|
||||
using QuaternionType = Quaternion<float>;
|
||||
|
||||
/** Creates a Draggable3DOrientation, initially set up to be aligned along the X axis. */
|
||||
Draggable3DOrientation (float objectRadius = 0.5f) noexcept
|
||||
: radius (jmax (0.1f, objectRadius)),
|
||||
quaternion (VectorType::xAxis(), 0)
|
||||
{
|
||||
}
|
||||
|
||||
/** Creates a Draggable3DOrientation from a user-supplied quaternion. */
|
||||
Draggable3DOrientation (const Quaternion<float>& quaternionToUse,
|
||||
float objectRadius = 0.5f) noexcept
|
||||
: radius (jmax (0.1f, objectRadius)),
|
||||
quaternion (quaternionToUse)
|
||||
{
|
||||
}
|
||||
|
||||
/** Resets the orientation, specifying the axis to align it along. */
|
||||
void reset (const VectorType& axis) noexcept
|
||||
{
|
||||
quaternion = QuaternionType (axis, 0);
|
||||
}
|
||||
|
||||
/** Sets the viewport area within which mouse-drag positions will occur.
|
||||
You'll need to set this rectangle before calling mouseDown. The centre of the
|
||||
rectangle is assumed to be the centre of the object that will be rotated, and
|
||||
the size of the rectangle will be used to scale the object radius - see setRadius().
|
||||
*/
|
||||
void setViewport (Rectangle<int> newArea) noexcept
|
||||
{
|
||||
area = newArea;
|
||||
}
|
||||
|
||||
/** Sets the size of the rotated object, as a proportion of the viewport's size.
|
||||
@see setViewport
|
||||
*/
|
||||
void setRadius (float newRadius) noexcept
|
||||
{
|
||||
radius = jmax (0.1f, newRadius);
|
||||
}
|
||||
|
||||
/** Begins a mouse-drag operation.
|
||||
You must call this before any calls to mouseDrag(). The position that is supplied
|
||||
will be treated as being relative to the centre of the rectangle passed to setViewport().
|
||||
*/
|
||||
template <typename Type>
|
||||
void mouseDown (Point<Type> mousePos) noexcept
|
||||
{
|
||||
lastMouse = mousePosToProportion (mousePos.toFloat());
|
||||
}
|
||||
|
||||
/** Continues a mouse-drag operation.
|
||||
After calling mouseDown() to begin a drag sequence, you can call this method
|
||||
to continue it.
|
||||
*/
|
||||
template <typename Type>
|
||||
void mouseDrag (Point<Type> mousePos) noexcept
|
||||
{
|
||||
auto oldPos = projectOnSphere (lastMouse);
|
||||
lastMouse = mousePosToProportion (mousePos.toFloat());
|
||||
auto newPos = projectOnSphere (lastMouse);
|
||||
|
||||
quaternion *= rotationFromMove (oldPos, newPos);
|
||||
}
|
||||
|
||||
/** Returns the matrix that should be used to apply the current orientation.
|
||||
@see applyToOpenGLMatrix
|
||||
*/
|
||||
Matrix3D<float> getRotationMatrix() const noexcept
|
||||
{
|
||||
return quaternion.getRotationMatrix();
|
||||
}
|
||||
|
||||
/** Provides direct access to the quaternion. */
|
||||
QuaternionType& getQuaternion() noexcept
|
||||
{
|
||||
return quaternion;
|
||||
}
|
||||
|
||||
private:
|
||||
Rectangle<int> area;
|
||||
float radius;
|
||||
QuaternionType quaternion;
|
||||
Point<float> lastMouse;
|
||||
|
||||
Point<float> mousePosToProportion (Point<float> mousePos) const noexcept
|
||||
{
|
||||
auto scale = jmin (area.getWidth(), area.getHeight()) / 2;
|
||||
|
||||
// You must call setViewport() to give this object a valid window size before
|
||||
// calling any of the mouse input methods!
|
||||
jassert (scale > 0);
|
||||
|
||||
return { (mousePos.x - (float) area.getCentreX()) / (float) scale,
|
||||
((float) area.getCentreY() - mousePos.y) / (float) scale };
|
||||
}
|
||||
|
||||
VectorType projectOnSphere (Point<float> pos) const noexcept
|
||||
{
|
||||
auto radiusSquared = radius * radius;
|
||||
auto xySquared = pos.x * pos.x + pos.y * pos.y;
|
||||
|
||||
return { pos.x, pos.y,
|
||||
xySquared < radiusSquared * 0.5f ? std::sqrt (radiusSquared - xySquared)
|
||||
: (radiusSquared / (2.0f * std::sqrt (xySquared))) };
|
||||
}
|
||||
|
||||
QuaternionType rotationFromMove (const VectorType& from, const VectorType& to) const noexcept
|
||||
{
|
||||
auto rotationAxis = (to ^ from);
|
||||
|
||||
if (rotationAxis.lengthIsBelowEpsilon())
|
||||
rotationAxis = VectorType::xAxis();
|
||||
|
||||
auto d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
|
||||
|
||||
return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,154 +1,154 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A 4x4 3D transformation matrix.
|
||||
|
||||
@see Vector3D, Quaternion, AffineTransform
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Matrix3D
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** Creates a copy of another matrix. */
|
||||
Matrix3D (const Matrix3D& other) noexcept
|
||||
{
|
||||
memcpy (mat, other.mat, sizeof (mat));
|
||||
}
|
||||
|
||||
/** Copies another matrix. */
|
||||
Matrix3D& operator= (const Matrix3D& other) noexcept
|
||||
{
|
||||
memcpy (mat, other.mat, sizeof (mat));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Creates a matrix from its raw 4x4 values. */
|
||||
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;
|
||||
mat[8] = m02; mat[9] = m12; mat[10] = m22; mat[11] = m32;
|
||||
mat[12] = m03; mat[13] = m13; mat[14] = m23; mat[15] = m33;
|
||||
}
|
||||
|
||||
/** Creates a matrix from an array of 16 raw values. */
|
||||
Matrix3D (const Type* values) noexcept
|
||||
{
|
||||
memcpy (mat, values, sizeof (mat));
|
||||
}
|
||||
|
||||
/** Creates a matrix from a 2D affine transform. */
|
||||
Matrix3D (const AffineTransform& transform) noexcept
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** Creates a matrix from a 3D vector translation. */
|
||||
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);
|
||||
}
|
||||
|
||||
/** Returns a new matrix from the given frustum values. */
|
||||
static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type nearDistance, Type farDistance) noexcept
|
||||
{
|
||||
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. */
|
||||
Matrix3D& operator*= (const Matrix3D& other) noexcept
|
||||
{
|
||||
return *this = *this * other;
|
||||
}
|
||||
|
||||
/** Multiplies this matrix by another, and returns the result. */
|
||||
Matrix3D operator* (const Matrix3D& other) const noexcept
|
||||
{
|
||||
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. */
|
||||
Type mat[16];
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A 4x4 3D transformation matrix.
|
||||
|
||||
@see Vector3D, Quaternion, AffineTransform
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Matrix3D
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** Creates a copy of another matrix. */
|
||||
Matrix3D (const Matrix3D& other) noexcept
|
||||
{
|
||||
memcpy (mat, other.mat, sizeof (mat));
|
||||
}
|
||||
|
||||
/** Copies another matrix. */
|
||||
Matrix3D& operator= (const Matrix3D& other) noexcept
|
||||
{
|
||||
memcpy (mat, other.mat, sizeof (mat));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** Creates a matrix from its raw 4x4 values. */
|
||||
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;
|
||||
mat[8] = m02; mat[9] = m12; mat[10] = m22; mat[11] = m32;
|
||||
mat[12] = m03; mat[13] = m13; mat[14] = m23; mat[15] = m33;
|
||||
}
|
||||
|
||||
/** Creates a matrix from an array of 16 raw values. */
|
||||
Matrix3D (const Type* values) noexcept
|
||||
{
|
||||
memcpy (mat, values, sizeof (mat));
|
||||
}
|
||||
|
||||
/** Creates a matrix from a 2D affine transform. */
|
||||
Matrix3D (const AffineTransform& transform) noexcept
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/** Creates a matrix from a 3D vector translation. */
|
||||
static Matrix3D fromTranslation (Vector3D<Type> vector) noexcept
|
||||
{
|
||||
return { Type (1), 0, 0, 0,
|
||||
0, Type (1), 0, 0,
|
||||
0, 0, Type (1), 0,
|
||||
vector.x, vector.y, vector.z, Type (1) };
|
||||
}
|
||||
|
||||
/** Returns a new matrix from the given frustum values. */
|
||||
static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type nearDistance, Type farDistance) noexcept
|
||||
{
|
||||
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. */
|
||||
Matrix3D& operator*= (const Matrix3D& other) noexcept
|
||||
{
|
||||
return *this = *this * other;
|
||||
}
|
||||
|
||||
/** Multiplies this matrix by another, and returns the result. */
|
||||
Matrix3D operator* (const Matrix3D& other) const noexcept
|
||||
{
|
||||
auto&& m2 = other.mat;
|
||||
|
||||
return { mat[0] * m2[0] + mat[4] * m2[1] + mat[8] * m2[2] + mat[12] * m2[3],
|
||||
mat[1] * m2[0] + mat[5] * m2[1] + mat[9] * m2[2] + mat[13] * m2[3],
|
||||
mat[2] * m2[0] + mat[6] * m2[1] + mat[10] * m2[2] + mat[14] * m2[3],
|
||||
mat[3] * m2[0] + mat[7] * m2[1] + mat[11] * m2[2] + mat[15] * m2[3],
|
||||
mat[0] * m2[4] + mat[4] * m2[5] + mat[8] * m2[6] + mat[12] * m2[7],
|
||||
mat[1] * m2[4] + mat[5] * m2[5] + mat[9] * m2[6] + mat[13] * m2[7],
|
||||
mat[2] * m2[4] + mat[6] * m2[5] + mat[10] * m2[6] + mat[14] * m2[7],
|
||||
mat[3] * m2[4] + mat[7] * m2[5] + mat[11] * m2[6] + mat[15] * m2[7],
|
||||
mat[0] * m2[8] + mat[4] * m2[9] + mat[8] * m2[10] + mat[12] * m2[11],
|
||||
mat[1] * m2[8] + mat[5] * m2[9] + mat[9] * m2[10] + mat[13] * m2[11],
|
||||
mat[2] * m2[8] + mat[6] * m2[9] + mat[10] * m2[10] + mat[14] * m2[11],
|
||||
mat[3] * m2[8] + mat[7] * m2[9] + mat[11] * m2[10] + mat[15] * m2[11],
|
||||
mat[0] * m2[12] + mat[4] * m2[13] + mat[8] * m2[14] + mat[12] * m2[15],
|
||||
mat[1] * m2[12] + mat[5] * m2[13] + mat[9] * m2[14] + mat[13] * m2[15],
|
||||
mat[2] * m2[12] + mat[6] * m2[13] + mat[10] * m2[14] + mat[14] * m2[15],
|
||||
mat[3] * m2[12] + mat[7] * m2[13] + mat[11] * m2[14] + mat[15] * m2[15] };
|
||||
}
|
||||
|
||||
/** The 4x4 matrix values. These are stored in the standard OpenGL order. */
|
||||
Type mat[16];
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,98 +1,98 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Holds a quaternion (a 3D vector and a scalar value).
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Quaternion
|
||||
{
|
||||
public:
|
||||
Quaternion() noexcept : scalar() {}
|
||||
Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
|
||||
Quaternion (Vector3D<Type> vectorPart, Type scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
|
||||
Quaternion (Type x, Type y, Type z, Type w) noexcept : vector (x, y, z), scalar (w) {}
|
||||
|
||||
/** Creates a quaternion from an angle and an axis. */
|
||||
static Quaternion fromAngle (Type angle, Vector3D<Type> axis) noexcept
|
||||
{
|
||||
return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
|
||||
}
|
||||
|
||||
Quaternion& operator= (Quaternion other) noexcept
|
||||
{
|
||||
vector = other.vector;
|
||||
scalar = other.scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Quaternion& operator*= (Quaternion other) noexcept
|
||||
{
|
||||
const Type oldScalar (scalar);
|
||||
scalar = (scalar * other.scalar) - (vector * other.vector);
|
||||
vector = (other.vector * oldScalar) + (vector * other.scalar) + (vector ^ other.vector);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type length() const noexcept { return std::sqrt (normal()); }
|
||||
Type normal() const noexcept { return scalar * scalar + vector.lengthSquared(); }
|
||||
|
||||
Quaternion normalised() const noexcept
|
||||
{
|
||||
const Type len (length());
|
||||
jassert (len > 0);
|
||||
return Quaternion (vector / len, scalar / len);
|
||||
}
|
||||
|
||||
/** Returns the matrix that will perform the rotation specified by this quaternion. */
|
||||
Matrix3D<Type> getRotationMatrix() const noexcept
|
||||
{
|
||||
const Type norm (normal());
|
||||
const Type s (norm > 0 ? ((Type) 2) / norm : 0);
|
||||
const Type xs (s * vector.x), ys (s * vector.y), zs (s * vector.z);
|
||||
const Type wx (xs * scalar), wy (ys * scalar), wz (zs * scalar);
|
||||
const Type xx (xs * vector.x), xy (ys * vector.x), xz (zs * vector.x);
|
||||
const Type yy (ys * vector.y), yz (zs * vector.y), zz (zs * vector.z);
|
||||
|
||||
return Matrix3D<Type> (((Type) 1) - (yy + zz), xy - wz, xz + wy, 0,
|
||||
xy + wz, ((Type) 1) - (xx+ zz), yz - wx, 0,
|
||||
xz - wy, yz + wx, ((Type) 1) - (xx + yy), 0,
|
||||
0, 0, 0, (Type) 1);
|
||||
}
|
||||
|
||||
/** The vector part of the quaternion. */
|
||||
Vector3D<Type> vector;
|
||||
|
||||
/** The scalar part of the quaternion. */
|
||||
Type scalar;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Holds a quaternion (a 3D vector and a scalar value).
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Quaternion
|
||||
{
|
||||
public:
|
||||
Quaternion() noexcept : scalar() {}
|
||||
Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
|
||||
Quaternion (Vector3D<Type> vectorPart, Type scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
|
||||
Quaternion (Type x, Type y, Type z, Type w) noexcept : vector (x, y, z), scalar (w) {}
|
||||
|
||||
/** Creates a quaternion from an angle and an axis. */
|
||||
static Quaternion fromAngle (Type angle, Vector3D<Type> axis) noexcept
|
||||
{
|
||||
return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
|
||||
}
|
||||
|
||||
Quaternion& operator= (Quaternion other) noexcept
|
||||
{
|
||||
vector = other.vector;
|
||||
scalar = other.scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Quaternion& operator*= (Quaternion other) noexcept
|
||||
{
|
||||
const Type oldScalar (scalar);
|
||||
scalar = (scalar * other.scalar) - (vector * other.vector);
|
||||
vector = (other.vector * oldScalar) + (vector * other.scalar) + (vector ^ other.vector);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type length() const noexcept { return std::sqrt (normal()); }
|
||||
Type normal() const noexcept { return scalar * scalar + vector.lengthSquared(); }
|
||||
|
||||
Quaternion normalised() const noexcept
|
||||
{
|
||||
const Type len (length());
|
||||
jassert (len > 0);
|
||||
return Quaternion (vector / len, scalar / len);
|
||||
}
|
||||
|
||||
/** Returns the matrix that will perform the rotation specified by this quaternion. */
|
||||
Matrix3D<Type> getRotationMatrix() const noexcept
|
||||
{
|
||||
const Type norm (normal());
|
||||
const Type s (norm > 0 ? ((Type) 2) / norm : 0);
|
||||
const Type xs (s * vector.x), ys (s * vector.y), zs (s * vector.z);
|
||||
const Type wx (xs * scalar), wy (ys * scalar), wz (zs * scalar);
|
||||
const Type xx (xs * vector.x), xy (ys * vector.x), xz (zs * vector.x);
|
||||
const Type yy (ys * vector.y), yz (zs * vector.y), zz (zs * vector.z);
|
||||
|
||||
return Matrix3D<Type> (((Type) 1) - (yy + zz), xy - wz, xz + wy, 0,
|
||||
xy + wz, ((Type) 1) - (xx+ zz), yz - wx, 0,
|
||||
xz - wy, yz + wx, ((Type) 1) - (xx + yy), 0,
|
||||
0, 0, 0, (Type) 1);
|
||||
}
|
||||
|
||||
/** The vector part of the quaternion. */
|
||||
Vector3D<Type> vector;
|
||||
|
||||
/** The scalar part of the quaternion. */
|
||||
Type scalar;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,83 +1,83 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A three-coordinate vector.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Vector3D
|
||||
{
|
||||
public:
|
||||
Vector3D() noexcept : x(), y(), z() {}
|
||||
Vector3D (Type xValue, Type yValue, Type zValue) noexcept : x (xValue), y (yValue), z (zValue) {}
|
||||
Vector3D (const Vector3D& other) noexcept : x (other.x), y (other.y), z (other.z) {}
|
||||
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 { (Type) 1, 0, 0 }; }
|
||||
/** Returns a vector that lies along the Y axis. */
|
||||
static Vector3D yAxis() noexcept { return { 0, (Type) 1, 0 }; }
|
||||
/** Returns a vector that lies along the Z axis. */
|
||||
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 { 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 { 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; }
|
||||
|
||||
Vector3D normalised() const noexcept { return *this / length(); }
|
||||
|
||||
/** Returns true if the vector is practically equal to the origin. */
|
||||
bool lengthIsBelowEpsilon() const noexcept
|
||||
{
|
||||
auto epsilon = std::numeric_limits<Type>::epsilon();
|
||||
return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
|
||||
}
|
||||
|
||||
Type x, y, z;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A three-coordinate vector.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
template <typename Type>
|
||||
class Vector3D
|
||||
{
|
||||
public:
|
||||
Vector3D() noexcept : x(), y(), z() {}
|
||||
Vector3D (Type xValue, Type yValue, Type zValue) noexcept : x (xValue), y (yValue), z (zValue) {}
|
||||
Vector3D (const Vector3D& other) noexcept : x (other.x), y (other.y), z (other.z) {}
|
||||
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 { (Type) 1, 0, 0 }; }
|
||||
/** Returns a vector that lies along the Y axis. */
|
||||
static Vector3D yAxis() noexcept { return { 0, (Type) 1, 0 }; }
|
||||
/** Returns a vector that lies along the Z axis. */
|
||||
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 { 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 { 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; }
|
||||
|
||||
Vector3D normalised() const noexcept { return *this / length(); }
|
||||
|
||||
/** Returns true if the vector is practically equal to the origin. */
|
||||
bool lengthIsBelowEpsilon() const noexcept
|
||||
{
|
||||
auto epsilon = std::numeric_limits<Type>::epsilon();
|
||||
return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
|
||||
}
|
||||
|
||||
Type x, y, z;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
562
deps/juce/modules/juce_opengl/juce_opengl.cpp
vendored
562
deps/juce/modules/juce_opengl/juce_opengl.cpp
vendored
@ -1,276 +1,286 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#ifdef JUCE_OPENGL_H_INCLUDED
|
||||
/* When you add this cpp file to your project, you mustn't include it in a file where you've
|
||||
already included any other headers - just put it inside a file on its own, possibly with your config
|
||||
flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
|
||||
header files that the compiler may be using.
|
||||
*/
|
||||
#error "Incorrect use of JUCE cpp file"
|
||||
#endif
|
||||
|
||||
#define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
|
||||
#define JUCE_CORE_INCLUDE_JNI_HELPERS 1
|
||||
#define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
|
||||
#define JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS 1
|
||||
#define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
|
||||
#define JUCE_GUI_BASICS_INCLUDE_SCOPED_THREAD_DPI_AWARENESS_SETTER 1
|
||||
|
||||
#include "juce_opengl.h"
|
||||
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_1 1
|
||||
|
||||
#if JUCE_MAC
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_2 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_3 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_4 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_5 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_2_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_2_1 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_1 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_2 1
|
||||
#endif
|
||||
|
||||
#define JUCE_STATIC_LINK_GL_ES_VERSION_2_0 1
|
||||
#if !JUCE_ANDROID || JUCE_ANDROID_GL_ES_VERSION_3_0
|
||||
#define JUCE_STATIC_LINK_GL_ES_VERSION_3_0 1
|
||||
#endif
|
||||
|
||||
#if JUCE_OPENGL_ES
|
||||
#include "opengl/juce_gles2.cpp"
|
||||
#else
|
||||
#include "opengl/juce_gl.cpp"
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_IOS
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_WINDOWS
|
||||
#include <windowsx.h>
|
||||
|
||||
#if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
|
||||
#pragma comment(lib, "OpenGL32.Lib")
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
/* Got an include error here?
|
||||
|
||||
If you want to install OpenGL support, the packages to get are "mesa-common-dev"
|
||||
and "freeglut3-dev".
|
||||
*/
|
||||
#include <GL/glx.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_MAC
|
||||
#include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
|
||||
#include <OpenGL/OpenGL.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_ANDROID
|
||||
#include <android/native_window.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <EGL/egl.h>
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
namespace juce
|
||||
{
|
||||
|
||||
using namespace ::juce::gl;
|
||||
|
||||
void OpenGLExtensionFunctions::initialise()
|
||||
{
|
||||
gl::loadFunctions();
|
||||
}
|
||||
|
||||
#define X(name) decltype (::juce::gl::name)& OpenGLExtensionFunctions::name = ::juce::gl::name;
|
||||
JUCE_GL_BASE_FUNCTIONS
|
||||
JUCE_GL_EXTENSION_FUNCTIONS
|
||||
JUCE_GL_VERTEXBUFFER_FUNCTIONS
|
||||
#undef X
|
||||
|
||||
#if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
|
||||
static const char* getGLErrorMessage (const GLenum e) noexcept
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
|
||||
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
|
||||
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
|
||||
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
|
||||
#ifdef GL_STACK_OVERFLOW
|
||||
case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
|
||||
#endif
|
||||
#ifdef GL_STACK_UNDERFLOW
|
||||
case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
|
||||
#endif
|
||||
#ifdef GL_INVALID_FRAMEBUFFER_OPERATION
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
|
||||
#endif
|
||||
default: break;
|
||||
}
|
||||
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
|
||||
#ifndef JUCE_IOS_MAC_VIEW
|
||||
#if JUCE_IOS
|
||||
#define JUCE_IOS_MAC_VIEW UIView
|
||||
#define JUCE_IOS_MAC_WINDOW UIWindow
|
||||
#else
|
||||
#define JUCE_IOS_MAC_VIEW NSView
|
||||
#define JUCE_IOS_MAC_WINDOW NSWindow
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
static bool checkPeerIsValid (OpenGLContext* context)
|
||||
{
|
||||
jassert (context != nullptr);
|
||||
|
||||
if (context != nullptr)
|
||||
{
|
||||
if (auto* comp = context->getTargetComponent())
|
||||
{
|
||||
if (auto* peer = comp->getPeer())
|
||||
{
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
|
||||
{
|
||||
if (auto nsWindow = [nsView window])
|
||||
{
|
||||
#if JUCE_MAC
|
||||
return ([nsWindow isVisible]
|
||||
&& (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
|
||||
#else
|
||||
ignoreUnused (nsWindow);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#else
|
||||
ignoreUnused (peer);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void checkGLError (const char* file, const int line)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
const GLenum e = glGetError();
|
||||
|
||||
if (e == GL_NO_ERROR)
|
||||
break;
|
||||
|
||||
// if the peer is not valid then ignore errors
|
||||
if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
|
||||
continue;
|
||||
|
||||
DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
|
||||
#define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
|
||||
#else
|
||||
#define JUCE_CHECK_OPENGL_ERROR ;
|
||||
#endif
|
||||
|
||||
static void clearGLError() noexcept
|
||||
{
|
||||
while (glGetError() != GL_NO_ERROR) {}
|
||||
}
|
||||
|
||||
struct OpenGLTargetSaver
|
||||
{
|
||||
OpenGLTargetSaver (const OpenGLContext& c) noexcept
|
||||
: context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
|
||||
{
|
||||
glGetIntegerv (GL_VIEWPORT, oldViewport);
|
||||
}
|
||||
|
||||
~OpenGLTargetSaver() noexcept
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
|
||||
glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
|
||||
}
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
GLuint oldFramebuffer;
|
||||
GLint oldViewport[4];
|
||||
|
||||
OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
||||
//==============================================================================
|
||||
#include "opengl/juce_OpenGLFrameBuffer.cpp"
|
||||
#include "opengl/juce_OpenGLGraphicsContext.cpp"
|
||||
#include "opengl/juce_OpenGLHelpers.cpp"
|
||||
#include "opengl/juce_OpenGLImage.cpp"
|
||||
#include "opengl/juce_OpenGLPixelFormat.cpp"
|
||||
#include "opengl/juce_OpenGLShaderProgram.cpp"
|
||||
#include "opengl/juce_OpenGLTexture.cpp"
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
|
||||
#if JUCE_MAC
|
||||
#include "native/juce_OpenGL_osx.h"
|
||||
#else
|
||||
#include "native/juce_OpenGL_ios.h"
|
||||
#endif
|
||||
|
||||
#elif JUCE_WINDOWS
|
||||
#include "opengl/juce_wgl.h"
|
||||
#include "native/juce_OpenGL_win32.h"
|
||||
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
#include "native/juce_OpenGL_linux_X11.h"
|
||||
|
||||
#elif JUCE_ANDROID
|
||||
#include "native/juce_OpenGL_android.h"
|
||||
|
||||
#endif
|
||||
|
||||
#include "opengl/juce_OpenGLContext.cpp"
|
||||
#include "utils/juce_OpenGLAppComponent.cpp"
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
#ifdef JUCE_OPENGL_H_INCLUDED
|
||||
/* When you add this cpp file to your project, you mustn't include it in a file where you've
|
||||
already included any other headers - just put it inside a file on its own, possibly with your config
|
||||
flags preceding it, but don't include anything else. That also includes avoiding any automatic prefix
|
||||
header files that the compiler may be using.
|
||||
*/
|
||||
#error "Incorrect use of JUCE cpp file"
|
||||
#endif
|
||||
|
||||
#define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
|
||||
#define JUCE_CORE_INCLUDE_JNI_HELPERS 1
|
||||
#define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
|
||||
#define JUCE_GRAPHICS_INCLUDE_COREGRAPHICS_HELPERS 1
|
||||
#define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
|
||||
#define JUCE_GUI_BASICS_INCLUDE_SCOPED_THREAD_DPI_AWARENESS_SETTER 1
|
||||
|
||||
#include "juce_opengl.h"
|
||||
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_1 1
|
||||
|
||||
#if JUCE_MAC
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_2 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_3 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_4 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_1_5 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_2_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_2_1 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_0 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_1 1
|
||||
#define JUCE_STATIC_LINK_GL_VERSION_3_2 1
|
||||
#endif
|
||||
|
||||
#define JUCE_STATIC_LINK_GL_ES_VERSION_2_0 1
|
||||
#if !JUCE_ANDROID || JUCE_ANDROID_GL_ES_VERSION_3_0
|
||||
#define JUCE_STATIC_LINK_GL_ES_VERSION_3_0 1
|
||||
#endif
|
||||
|
||||
#if JUCE_OPENGL_ES
|
||||
#include "opengl/juce_gles2.cpp"
|
||||
#else
|
||||
#include "opengl/juce_gl.cpp"
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_IOS
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_WINDOWS
|
||||
#include <windowsx.h>
|
||||
|
||||
#if ! JUCE_MINGW && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
|
||||
#pragma comment(lib, "OpenGL32.Lib")
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
/* Got an include error here?
|
||||
|
||||
If you want to install OpenGL support, the packages to get are "mesa-common-dev"
|
||||
and "freeglut3-dev".
|
||||
*/
|
||||
#include <GL/glx.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_MAC
|
||||
#include <OpenGL/CGLCurrent.h> // These are both just needed with the 10.5 SDK
|
||||
#include <OpenGL/OpenGL.h>
|
||||
|
||||
//==============================================================================
|
||||
#elif JUCE_ANDROID
|
||||
#include <android/native_window.h>
|
||||
#include <android/native_window_jni.h>
|
||||
#include <EGL/egl.h>
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
namespace juce
|
||||
{
|
||||
|
||||
using namespace ::juce::gl;
|
||||
|
||||
void OpenGLExtensionFunctions::initialise()
|
||||
{
|
||||
gl::loadFunctions();
|
||||
}
|
||||
|
||||
#define X(name) decltype (::juce::gl::name)& OpenGLExtensionFunctions::name = ::juce::gl::name;
|
||||
JUCE_GL_BASE_FUNCTIONS
|
||||
JUCE_GL_EXTENSION_FUNCTIONS
|
||||
JUCE_GL_VERTEXBUFFER_FUNCTIONS
|
||||
#undef X
|
||||
|
||||
#if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
|
||||
static const char* getGLErrorMessage (const GLenum e) noexcept
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
|
||||
case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
|
||||
case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
|
||||
case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
|
||||
#ifdef GL_STACK_OVERFLOW
|
||||
case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
|
||||
#endif
|
||||
#ifdef GL_STACK_UNDERFLOW
|
||||
case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
|
||||
#endif
|
||||
#ifdef GL_INVALID_FRAMEBUFFER_OPERATION
|
||||
case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
|
||||
#endif
|
||||
default: break;
|
||||
}
|
||||
|
||||
return "Unknown error";
|
||||
}
|
||||
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
|
||||
#ifndef JUCE_IOS_MAC_VIEW
|
||||
#if JUCE_IOS
|
||||
#define JUCE_IOS_MAC_VIEW UIView
|
||||
#define JUCE_IOS_MAC_WINDOW UIWindow
|
||||
#else
|
||||
#define JUCE_IOS_MAC_VIEW NSView
|
||||
#define JUCE_IOS_MAC_WINDOW NSWindow
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
static bool checkPeerIsValid (OpenGLContext* context)
|
||||
{
|
||||
jassert (context != nullptr);
|
||||
|
||||
if (context != nullptr)
|
||||
{
|
||||
if (auto* comp = context->getTargetComponent())
|
||||
{
|
||||
if (auto* peer = comp->getPeer())
|
||||
{
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
if (auto* nsView = (JUCE_IOS_MAC_VIEW*) peer->getNativeHandle())
|
||||
{
|
||||
if (auto nsWindow = [nsView window])
|
||||
{
|
||||
#if JUCE_MAC
|
||||
return ([nsWindow isVisible]
|
||||
&& (! [nsWindow hidesOnDeactivate] || [NSApp isActive]));
|
||||
#else
|
||||
ignoreUnused (nsWindow);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#else
|
||||
ignoreUnused (peer);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void checkGLError (const char* file, const int line)
|
||||
{
|
||||
for (;;)
|
||||
{
|
||||
const GLenum e = glGetError();
|
||||
|
||||
if (e == GL_NO_ERROR)
|
||||
break;
|
||||
|
||||
// if the peer is not valid then ignore errors
|
||||
if (! checkPeerIsValid (OpenGLContext::getCurrentContext()))
|
||||
continue;
|
||||
|
||||
DBG ("***** " << getGLErrorMessage (e) << " at " << file << " : " << line);
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
|
||||
#define JUCE_CHECK_OPENGL_ERROR checkGLError (__FILE__, __LINE__);
|
||||
#else
|
||||
#define JUCE_CHECK_OPENGL_ERROR ;
|
||||
#endif
|
||||
|
||||
static void clearGLError() noexcept
|
||||
{
|
||||
while (glGetError() != GL_NO_ERROR) {}
|
||||
}
|
||||
|
||||
struct OpenGLTargetSaver
|
||||
{
|
||||
OpenGLTargetSaver (const OpenGLContext& c) noexcept
|
||||
: context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
|
||||
{
|
||||
glGetIntegerv (GL_VIEWPORT, oldViewport);
|
||||
}
|
||||
|
||||
~OpenGLTargetSaver() noexcept
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, oldFramebuffer);
|
||||
glViewport (oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
|
||||
}
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
GLuint oldFramebuffer;
|
||||
GLint oldViewport[4];
|
||||
|
||||
OpenGLTargetSaver& operator= (const OpenGLTargetSaver&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
||||
//==============================================================================
|
||||
#include "opengl/juce_OpenGLFrameBuffer.cpp"
|
||||
#include "opengl/juce_OpenGLGraphicsContext.cpp"
|
||||
#include "opengl/juce_OpenGLHelpers.cpp"
|
||||
#include "opengl/juce_OpenGLImage.cpp"
|
||||
#include "opengl/juce_OpenGLPixelFormat.cpp"
|
||||
#include "opengl/juce_OpenGLShaderProgram.cpp"
|
||||
#include "opengl/juce_OpenGLTexture.cpp"
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_MAC || JUCE_IOS
|
||||
|
||||
#if JUCE_MAC
|
||||
#include "native/juce_OpenGL_osx.h"
|
||||
#else
|
||||
#include "native/juce_OpenGL_ios.h"
|
||||
#endif
|
||||
|
||||
#elif JUCE_WINDOWS
|
||||
#include "opengl/juce_wgl.h"
|
||||
#include "native/juce_OpenGL_win32.h"
|
||||
|
||||
#define JUCE_IMPL_WGL_EXTENSION_FUNCTION(name) \
|
||||
decltype (juce::OpenGLContext::NativeContext::name) juce::OpenGLContext::NativeContext::name = nullptr;
|
||||
|
||||
JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB)
|
||||
JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT)
|
||||
JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT)
|
||||
JUCE_IMPL_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB)
|
||||
|
||||
#undef JUCE_IMPL_WGL_EXTENSION_FUNCTION
|
||||
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
#include "native/juce_OpenGL_linux_X11.h"
|
||||
|
||||
#elif JUCE_ANDROID
|
||||
#include "native/juce_OpenGL_android.h"
|
||||
|
||||
#endif
|
||||
|
||||
#include "opengl/juce_OpenGLContext.cpp"
|
||||
#include "utils/juce_OpenGLAppComponent.cpp"
|
||||
|
240
deps/juce/modules/juce_opengl/juce_opengl.h
vendored
240
deps/juce/modules/juce_opengl/juce_opengl.h
vendored
@ -1,120 +1,120 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
The block below describes the properties of this module, and is read by
|
||||
the Projucer to automatically generate project code that uses it.
|
||||
For details about the syntax and how to create or use a module, see the
|
||||
JUCE Module Format.md file.
|
||||
|
||||
|
||||
BEGIN_JUCE_MODULE_DECLARATION
|
||||
|
||||
ID: juce_opengl
|
||||
vendor: juce
|
||||
version: 6.1.2
|
||||
name: JUCE OpenGL classes
|
||||
description: Classes for rendering OpenGL in a JUCE window.
|
||||
website: http://www.juce.com/juce
|
||||
license: GPL/Commercial
|
||||
minimumCppStandard: 14
|
||||
|
||||
dependencies: juce_gui_extra
|
||||
OSXFrameworks: OpenGL
|
||||
iOSFrameworks: OpenGLES
|
||||
linuxLibs: GL
|
||||
mingwLibs: opengl32
|
||||
|
||||
END_JUCE_MODULE_DECLARATION
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
#define JUCE_OPENGL_H_INCLUDED
|
||||
|
||||
#include <juce_core/system/juce_TargetPlatform.h>
|
||||
|
||||
#undef JUCE_OPENGL
|
||||
#define JUCE_OPENGL 1
|
||||
|
||||
#if JUCE_IOS || JUCE_ANDROID
|
||||
#define JUCE_OPENGL_ES 1
|
||||
#include "opengl/juce_gles2.h"
|
||||
#else
|
||||
#include "opengl/juce_gl.h"
|
||||
#endif
|
||||
|
||||
#include <juce_gui_extra/juce_gui_extra.h>
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_OPENGL_ES || DOXYGEN
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_MEDIUMP "mediump"
|
||||
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_HIGHP "highp"
|
||||
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_LOWP "lowp"
|
||||
#else
|
||||
#define JUCE_MEDIUMP
|
||||
#define JUCE_HIGHP
|
||||
#define JUCE_LOWP
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
namespace juce
|
||||
{
|
||||
class OpenGLTexture;
|
||||
class OpenGLFrameBuffer;
|
||||
class OpenGLShaderProgram;
|
||||
}
|
||||
|
||||
#include "geometry/juce_Vector3D.h"
|
||||
#include "geometry/juce_Matrix3D.h"
|
||||
#include "geometry/juce_Quaternion.h"
|
||||
#include "geometry/juce_Draggable3DOrientation.h"
|
||||
#include "opengl/juce_OpenGLHelpers.h"
|
||||
#include "opengl/juce_OpenGLPixelFormat.h"
|
||||
#include "native/juce_OpenGLExtensions.h"
|
||||
#include "opengl/juce_OpenGLRenderer.h"
|
||||
#include "opengl/juce_OpenGLContext.h"
|
||||
#include "opengl/juce_OpenGLFrameBuffer.h"
|
||||
#include "opengl/juce_OpenGLGraphicsContext.h"
|
||||
#include "opengl/juce_OpenGLImage.h"
|
||||
#include "opengl/juce_OpenGLShaderProgram.h"
|
||||
#include "opengl/juce_OpenGLTexture.h"
|
||||
#include "utils/juce_OpenGLAppComponent.h"
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
The block below describes the properties of this module, and is read by
|
||||
the Projucer to automatically generate project code that uses it.
|
||||
For details about the syntax and how to create or use a module, see the
|
||||
JUCE Module Format.md file.
|
||||
|
||||
|
||||
BEGIN_JUCE_MODULE_DECLARATION
|
||||
|
||||
ID: juce_opengl
|
||||
vendor: juce
|
||||
version: 7.0.2
|
||||
name: JUCE OpenGL classes
|
||||
description: Classes for rendering OpenGL in a JUCE window.
|
||||
website: http://www.juce.com/juce
|
||||
license: GPL/Commercial
|
||||
minimumCppStandard: 14
|
||||
|
||||
dependencies: juce_gui_extra
|
||||
OSXFrameworks: OpenGL
|
||||
iOSFrameworks: OpenGLES
|
||||
linuxLibs: GL
|
||||
mingwLibs: opengl32
|
||||
|
||||
END_JUCE_MODULE_DECLARATION
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#pragma once
|
||||
#define JUCE_OPENGL_H_INCLUDED
|
||||
|
||||
#include <juce_core/system/juce_TargetPlatform.h>
|
||||
|
||||
#undef JUCE_OPENGL
|
||||
#define JUCE_OPENGL 1
|
||||
|
||||
#if JUCE_IOS || JUCE_ANDROID
|
||||
#define JUCE_OPENGL_ES 1
|
||||
#include "opengl/juce_gles2.h"
|
||||
#else
|
||||
#include "opengl/juce_gl.h"
|
||||
#endif
|
||||
|
||||
#include <juce_gui_extra/juce_gui_extra.h>
|
||||
|
||||
//==============================================================================
|
||||
#if JUCE_OPENGL_ES || DOXYGEN
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_MEDIUMP "mediump"
|
||||
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_HIGHP "highp"
|
||||
|
||||
/** This macro is a helper for use in GLSL shader code which needs to compile on both GLES and desktop GL.
|
||||
Since it's mandatory in GLES to mark a variable with a precision, but the keywords don't exist in normal GLSL,
|
||||
these macros define the various precision keywords only on GLES.
|
||||
*/
|
||||
#define JUCE_LOWP "lowp"
|
||||
#else
|
||||
#define JUCE_MEDIUMP
|
||||
#define JUCE_HIGHP
|
||||
#define JUCE_LOWP
|
||||
#endif
|
||||
|
||||
//==============================================================================
|
||||
namespace juce
|
||||
{
|
||||
class OpenGLTexture;
|
||||
class OpenGLFrameBuffer;
|
||||
class OpenGLShaderProgram;
|
||||
}
|
||||
|
||||
#include "geometry/juce_Vector3D.h"
|
||||
#include "geometry/juce_Matrix3D.h"
|
||||
#include "geometry/juce_Quaternion.h"
|
||||
#include "geometry/juce_Draggable3DOrientation.h"
|
||||
#include "opengl/juce_OpenGLHelpers.h"
|
||||
#include "opengl/juce_OpenGLPixelFormat.h"
|
||||
#include "native/juce_OpenGLExtensions.h"
|
||||
#include "opengl/juce_OpenGLRenderer.h"
|
||||
#include "opengl/juce_OpenGLContext.h"
|
||||
#include "opengl/juce_OpenGLFrameBuffer.h"
|
||||
#include "opengl/juce_OpenGLGraphicsContext.h"
|
||||
#include "opengl/juce_OpenGLImage.h"
|
||||
#include "opengl/juce_OpenGLShaderProgram.h"
|
||||
#include "opengl/juce_OpenGLTexture.h"
|
||||
#include "utils/juce_OpenGLAppComponent.h"
|
||||
|
8
deps/juce/modules/juce_opengl/juce_opengl.mm
vendored
8
deps/juce/modules/juce_opengl/juce_opengl.mm
vendored
@ -2,15 +2,15 @@
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
|
@ -2,15 +2,15 @@
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
@ -27,6 +27,7 @@ package com.rmsl.juce;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Region;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
public class JuceOpenGLView extends SurfaceView
|
||||
@ -45,6 +46,14 @@ public class JuceOpenGLView extends SurfaceView
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
@Override
|
||||
public boolean gatherTransparentRegion (Region unused)
|
||||
{
|
||||
// Returning true indicates that the view is opaque at this point.
|
||||
// Without this, the green TalkBack borders cannot be seen on OpenGL views.
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow ()
|
||||
{
|
||||
|
@ -1,140 +1,140 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_BASE_FUNCTIONS \
|
||||
X (glActiveTexture) \
|
||||
X (glBindBuffer) \
|
||||
X (glDeleteBuffers) \
|
||||
X (glGenBuffers) \
|
||||
X (glBufferData) \
|
||||
X (glBufferSubData) \
|
||||
X (glCreateProgram) \
|
||||
X (glDeleteProgram) \
|
||||
X (glCreateShader) \
|
||||
X (glDeleteShader) \
|
||||
X (glShaderSource) \
|
||||
X (glCompileShader) \
|
||||
X (glAttachShader) \
|
||||
X (glLinkProgram) \
|
||||
X (glUseProgram) \
|
||||
X (glGetShaderiv) \
|
||||
X (glGetShaderInfoLog) \
|
||||
X (glGetProgramInfoLog) \
|
||||
X (glGetProgramiv) \
|
||||
X (glGetUniformLocation) \
|
||||
X (glGetAttribLocation) \
|
||||
X (glVertexAttribPointer) \
|
||||
X (glEnableVertexAttribArray) \
|
||||
X (glDisableVertexAttribArray) \
|
||||
X (glUniform1f) \
|
||||
X (glUniform1i) \
|
||||
X (glUniform2f) \
|
||||
X (glUniform3f) \
|
||||
X (glUniform4f) \
|
||||
X (glUniform4i) \
|
||||
X (glUniform1fv) \
|
||||
X (glUniformMatrix2fv) \
|
||||
X (glUniformMatrix3fv) \
|
||||
X (glUniformMatrix4fv) \
|
||||
X (glBindAttribLocation)
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_EXTENSION_FUNCTIONS \
|
||||
X (glIsRenderbuffer) \
|
||||
X (glBindRenderbuffer) \
|
||||
X (glDeleteRenderbuffers) \
|
||||
X (glGenRenderbuffers) \
|
||||
X (glRenderbufferStorage) \
|
||||
X (glGetRenderbufferParameteriv) \
|
||||
X (glIsFramebuffer) \
|
||||
X (glBindFramebuffer) \
|
||||
X (glDeleteFramebuffers) \
|
||||
X (glGenFramebuffers) \
|
||||
X (glCheckFramebufferStatus) \
|
||||
X (glFramebufferTexture2D) \
|
||||
X (glFramebufferRenderbuffer) \
|
||||
X (glGetFramebufferAttachmentParameteriv)
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_VERTEXBUFFER_FUNCTIONS \
|
||||
X (glGenVertexArrays) \
|
||||
X (glDeleteVertexArrays) \
|
||||
X (glBindVertexArray)
|
||||
|
||||
/** This class contains a generated list of OpenGL extension functions, which are either dynamically loaded
|
||||
for a specific GL context, or simply call-through to the appropriate OS function where available.
|
||||
|
||||
This class is provided for backwards compatibility. In new code, you should prefer to use
|
||||
functions from the juce::gl namespace. By importing all these symbols with
|
||||
`using namespace ::juce::gl;`, all GL enumerations and functions will be made available at
|
||||
global scope. This may be helpful if you need to write code with C source compatibility, or
|
||||
which is compatible with a different extension-loading library.
|
||||
All the normal guidance about `using namespace` should still apply - don't do this in a header,
|
||||
or at all if you can possibly avoid it!
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
struct OpenGLExtensionFunctions
|
||||
{
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
[[deprecated ("A more complete set of GL commands can be found in the juce::gl namespace. "
|
||||
"You should use juce::gl::loadFunctions() to load GL functions.")]]
|
||||
static void initialise();
|
||||
#endif
|
||||
|
||||
#if JUCE_WINDOWS && ! defined (DOXYGEN)
|
||||
typedef char GLchar;
|
||||
typedef pointer_sized_int GLsizeiptr;
|
||||
typedef pointer_sized_int GLintptr;
|
||||
#endif
|
||||
|
||||
#define X(name) static decltype (::juce::gl::name)& name;
|
||||
JUCE_GL_BASE_FUNCTIONS
|
||||
JUCE_GL_EXTENSION_FUNCTIONS
|
||||
JUCE_GL_VERTEXBUFFER_FUNCTIONS
|
||||
#undef X
|
||||
};
|
||||
|
||||
enum MissingOpenGLDefinitions
|
||||
{
|
||||
#if JUCE_ANDROID
|
||||
JUCE_RGBA_FORMAT = ::juce::gl::GL_RGBA,
|
||||
#else
|
||||
JUCE_RGBA_FORMAT = ::juce::gl::GL_BGRA_EXT,
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_BASE_FUNCTIONS \
|
||||
X (glActiveTexture) \
|
||||
X (glBindBuffer) \
|
||||
X (glDeleteBuffers) \
|
||||
X (glGenBuffers) \
|
||||
X (glBufferData) \
|
||||
X (glBufferSubData) \
|
||||
X (glCreateProgram) \
|
||||
X (glDeleteProgram) \
|
||||
X (glCreateShader) \
|
||||
X (glDeleteShader) \
|
||||
X (glShaderSource) \
|
||||
X (glCompileShader) \
|
||||
X (glAttachShader) \
|
||||
X (glLinkProgram) \
|
||||
X (glUseProgram) \
|
||||
X (glGetShaderiv) \
|
||||
X (glGetShaderInfoLog) \
|
||||
X (glGetProgramInfoLog) \
|
||||
X (glGetProgramiv) \
|
||||
X (glGetUniformLocation) \
|
||||
X (glGetAttribLocation) \
|
||||
X (glVertexAttribPointer) \
|
||||
X (glEnableVertexAttribArray) \
|
||||
X (glDisableVertexAttribArray) \
|
||||
X (glUniform1f) \
|
||||
X (glUniform1i) \
|
||||
X (glUniform2f) \
|
||||
X (glUniform3f) \
|
||||
X (glUniform4f) \
|
||||
X (glUniform4i) \
|
||||
X (glUniform1fv) \
|
||||
X (glUniformMatrix2fv) \
|
||||
X (glUniformMatrix3fv) \
|
||||
X (glUniformMatrix4fv) \
|
||||
X (glBindAttribLocation)
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_EXTENSION_FUNCTIONS \
|
||||
X (glIsRenderbuffer) \
|
||||
X (glBindRenderbuffer) \
|
||||
X (glDeleteRenderbuffers) \
|
||||
X (glGenRenderbuffers) \
|
||||
X (glRenderbufferStorage) \
|
||||
X (glGetRenderbufferParameteriv) \
|
||||
X (glIsFramebuffer) \
|
||||
X (glBindFramebuffer) \
|
||||
X (glDeleteFramebuffers) \
|
||||
X (glGenFramebuffers) \
|
||||
X (glCheckFramebufferStatus) \
|
||||
X (glFramebufferTexture2D) \
|
||||
X (glFramebufferRenderbuffer) \
|
||||
X (glGetFramebufferAttachmentParameteriv)
|
||||
|
||||
/** @internal This macro contains a list of GL extension functions that need to be dynamically loaded on Windows/Linux.
|
||||
@see OpenGLExtensionFunctions
|
||||
*/
|
||||
#define JUCE_GL_VERTEXBUFFER_FUNCTIONS \
|
||||
X (glGenVertexArrays) \
|
||||
X (glDeleteVertexArrays) \
|
||||
X (glBindVertexArray)
|
||||
|
||||
/** This class contains a generated list of OpenGL extension functions, which are either dynamically loaded
|
||||
for a specific GL context, or simply call-through to the appropriate OS function where available.
|
||||
|
||||
This class is provided for backwards compatibility. In new code, you should prefer to use
|
||||
functions from the juce::gl namespace. By importing all these symbols with
|
||||
`using namespace ::juce::gl;`, all GL enumerations and functions will be made available at
|
||||
global scope. This may be helpful if you need to write code with C source compatibility, or
|
||||
which is compatible with a different extension-loading library.
|
||||
All the normal guidance about `using namespace` should still apply - don't do this in a header,
|
||||
or at all if you can possibly avoid it!
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
struct OpenGLExtensionFunctions
|
||||
{
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
[[deprecated ("A more complete set of GL commands can be found in the juce::gl namespace. "
|
||||
"You should use juce::gl::loadFunctions() to load GL functions.")]]
|
||||
static void initialise();
|
||||
#endif
|
||||
|
||||
#if JUCE_WINDOWS && ! defined (DOXYGEN)
|
||||
typedef char GLchar;
|
||||
typedef pointer_sized_int GLsizeiptr;
|
||||
typedef pointer_sized_int GLintptr;
|
||||
#endif
|
||||
|
||||
#define X(name) static decltype (::juce::gl::name)& name;
|
||||
JUCE_GL_BASE_FUNCTIONS
|
||||
JUCE_GL_EXTENSION_FUNCTIONS
|
||||
JUCE_GL_VERTEXBUFFER_FUNCTIONS
|
||||
#undef X
|
||||
};
|
||||
|
||||
enum MissingOpenGLDefinitions
|
||||
{
|
||||
#if JUCE_ANDROID
|
||||
JUCE_RGBA_FORMAT = ::juce::gl::GL_RGBA,
|
||||
#else
|
||||
JUCE_RGBA_FORMAT = ::juce::gl::GL_BGRA_EXT,
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,385 +1,435 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
// This byte-code is generated from native/java/com/rmsl/juce/JuceOpenGLView.java with min sdk version 16
|
||||
// See juce_core/native/java/README.txt on how to generate this byte-code.
|
||||
static const uint8 javaJuceOpenGLView[] =
|
||||
{31,139,8,8,95,114,161,94,0,3,74,117,99,101,79,112,101,110,71,76,86,105,101,119,83,117,114,102,97,99,101,46,100,101,120,0,109,
|
||||
84,75,107,19,81,20,62,119,230,38,169,105,76,99,218,138,86,144,32,5,55,173,83,53,98,33,173,86,90,170,196,193,34,173,81,106,93,
|
||||
12,147,169,153,210,204,196,100,154,22,65,40,110,234,170,43,23,34,46,93,22,87,62,138,27,65,168,235,174,213,133,11,23,254,129,
|
||||
162,136,130,223,125,196,164,208,129,239,126,231,125,206,188,78,217,91,75,142,156,191,64,151,223,190,187,182,183,251,251,212,240,
|
||||
247,216,223,197,205,174,170,255,233,209,230,135,123,183,222,252,225,68,53,34,90,43,229,179,164,175,5,216,142,145,178,199,129,
|
||||
93,205,63,0,6,140,224,72,129,71,153,210,159,225,152,50,137,182,193,239,13,162,143,192,14,240,25,216,3,50,240,13,1,54,48,3,204,
|
||||
2,119,128,5,192,1,124,224,1,240,16,120,2,108,2,207,129,87,192,14,240,5,248,9,196,184,234,7,145,32,82,76,207,149,208,136,233,
|
||||
249,187,180,252,20,189,15,105,249,5,228,164,150,95,66,238,214,242,86,135,253,181,161,234,102,100,15,83,214,50,225,233,209,61,179,
|
||||
154,251,100,127,46,253,226,76,73,86,113,92,199,113,76,218,171,245,62,173,247,75,54,232,168,182,183,238,69,92,134,230,188,42,139,
|
||||
251,226,210,214,205,213,124,181,28,209,57,153,57,15,105,126,144,40,141,92,38,99,251,185,154,191,229,77,203,124,67,246,56,129,
|
||||
227,8,56,204,49,154,163,217,9,68,161,236,89,52,28,197,51,19,122,109,162,155,248,205,180,124,6,76,206,51,216,202,201,136,26,7,
|
||||
230,140,116,17,103,157,57,67,58,231,224,232,36,162,67,124,6,92,206,198,246,221,179,33,117,166,245,182,28,31,243,3,63,186,68,172,
|
||||
72,189,197,21,215,155,169,121,193,85,187,228,123,171,103,150,156,166,67,199,109,39,40,215,67,191,108,185,97,16,121,65,100,77,
|
||||
10,94,139,10,29,174,251,117,167,86,241,221,134,53,233,4,77,167,81,160,129,255,174,38,42,89,179,43,245,69,199,245,68,213,2,157,
|
||||
180,221,176,106,213,171,141,101,107,9,13,173,253,93,11,196,74,100,148,138,100,150,138,54,4,27,130,93,164,184,235,4,174,183,44,
|
||||
25,29,40,225,170,41,40,85,246,27,53,39,114,43,83,117,103,149,120,37,108,68,148,12,156,200,111,122,115,21,191,65,217,48,184,18,
|
||||
69,142,91,241,202,115,225,109,63,40,135,171,212,47,109,194,164,12,55,100,56,245,133,193,148,167,66,167,235,97,85,7,15,28,100,
|
||||
213,25,41,248,208,86,107,60,18,13,79,27,61,217,68,250,226,204,48,13,83,34,125,157,166,89,58,145,30,223,152,167,60,41,30,7,111,
|
||||
220,29,195,11,224,248,248,248,250,58,223,54,249,99,131,12,128,1,49,246,213,100,252,23,176,197,13,254,141,31,214,239,145,117,
|
||||
112,107,111,24,29,187,195,236,216,31,173,239,94,236,144,24,181,247,72,156,218,187,132,229,148,79,236,19,150,105,255,203,70,78,
|
||||
213,23,59,198,212,49,226,255,160,156,202,205,235,159,87,200,98,135,253,3,40,26,5,36,252,4,0,0,0,0};
|
||||
|
||||
//==============================================================================
|
||||
struct AndroidGLCallbacks
|
||||
{
|
||||
static void attachedToWindow (JNIEnv*, jobject, jlong);
|
||||
static void detachedFromWindow (JNIEnv*, jobject, jlong);
|
||||
static void dispatchDraw (JNIEnv*, jobject, jlong, jobject);
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
|
||||
METHOD (constructor, "<init>", "(Landroid/content/Context;J)V") \
|
||||
METHOD (getParent, "getParent", "()Landroid/view/ViewParent;") \
|
||||
METHOD (getHolder, "getHolder", "()Landroid/view/SurfaceHolder;") \
|
||||
METHOD (layout, "layout", "(IIII)V" ) \
|
||||
CALLBACK (AndroidGLCallbacks::attachedToWindow, "onAttchedWindowNative", "(J)V") \
|
||||
CALLBACK (AndroidGLCallbacks::detachedFromWindow, "onDetachedFromWindowNative", "(J)V") \
|
||||
CALLBACK (AndroidGLCallbacks::dispatchDraw, "onDrawNative", "(JLandroid/graphics/Canvas;)V")
|
||||
|
||||
DECLARE_JNI_CLASS_WITH_BYTECODE (JuceOpenGLViewSurface, "com/rmsl/juce/JuceOpenGLView", 16, javaJuceOpenGLView, sizeof(javaJuceOpenGLView))
|
||||
#undef JNI_CLASS_MEMBERS
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext : private SurfaceHolderCallback
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& comp,
|
||||
const OpenGLPixelFormat& pixelFormat,
|
||||
void* /*contextToShareWith*/,
|
||||
bool useMultisamplingIn,
|
||||
OpenGLVersion)
|
||||
: component (comp),
|
||||
surface (EGL_NO_SURFACE), context (EGL_NO_CONTEXT)
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
// Do we have a native peer that we can attach to?
|
||||
if (component.getPeer()->getNativeHandle() == nullptr)
|
||||
return;
|
||||
|
||||
// Initialise the EGL display
|
||||
if (! initEGLDisplay (pixelFormat, useMultisamplingIn))
|
||||
return;
|
||||
|
||||
// create a native surface view
|
||||
surfaceView = GlobalRef (LocalRef<jobject>(env->NewObject (JuceOpenGLViewSurface,
|
||||
JuceOpenGLViewSurface.constructor,
|
||||
getAppContext().get(),
|
||||
reinterpret_cast<jlong> (this))));
|
||||
if (surfaceView.get() == nullptr)
|
||||
return;
|
||||
|
||||
// add the view to the view hierarchy
|
||||
// after this the nativecontext can receive callbacks
|
||||
env->CallVoidMethod ((jobject) component.getPeer()->getNativeHandle(),
|
||||
AndroidViewGroup.addView, surfaceView.get());
|
||||
|
||||
// initialise the geometry of the view
|
||||
auto bounds = component.getTopLevelComponent()->getLocalArea (&component, component.getLocalBounds());
|
||||
bounds *= component.getDesktopScaleFactor();
|
||||
|
||||
updateWindowPosition (bounds);
|
||||
hasInitialised = true;
|
||||
}
|
||||
|
||||
~NativeContext() override
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
if (jobject viewParent = env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getParent))
|
||||
env->CallVoidMethod (viewParent, AndroidViewGroup.removeView, surfaceView.get());
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool initialiseOnRenderThread (OpenGLContext& aContext)
|
||||
{
|
||||
jassert (hasInitialised);
|
||||
|
||||
// has the context already attached?
|
||||
jassert (surface == EGL_NO_SURFACE && context == EGL_NO_CONTEXT);
|
||||
|
||||
auto env = getEnv();
|
||||
|
||||
ANativeWindow* window = nullptr;
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
if (holder != nullptr)
|
||||
{
|
||||
LocalRef<jobject> jSurface (env->CallObjectMethod (holder.get(), AndroidSurfaceHolder.getSurface));
|
||||
|
||||
if (jSurface != nullptr)
|
||||
{
|
||||
window = ANativeWindow_fromSurface(env, jSurface.get());
|
||||
|
||||
// if we didn't succeed the first time, wait 25ms and try again
|
||||
if (window == nullptr)
|
||||
{
|
||||
Thread::sleep (200);
|
||||
window = ANativeWindow_fromSurface (env, jSurface.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (window == nullptr)
|
||||
{
|
||||
// failed to get a pointer to the native window after second try so bail out
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the surface
|
||||
surface = eglCreateWindowSurface (display, config, window, nullptr);
|
||||
jassert (surface != EGL_NO_SURFACE);
|
||||
|
||||
ANativeWindow_release (window);
|
||||
|
||||
// create the OpenGL context
|
||||
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
|
||||
context = eglCreateContext (display, config, EGL_NO_CONTEXT, contextAttribs);
|
||||
jassert (context != EGL_NO_CONTEXT);
|
||||
|
||||
juceContext = &aContext;
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
jassert (hasInitialised);
|
||||
|
||||
// is there a context available to detach?
|
||||
jassert (surface != EGL_NO_SURFACE && context != EGL_NO_CONTEXT);
|
||||
|
||||
eglDestroyContext (display, context);
|
||||
context = EGL_NO_CONTEXT;
|
||||
|
||||
eglDestroySurface (display, surface);
|
||||
surface = EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
if (! hasInitialised)
|
||||
return false;
|
||||
|
||||
if (surface == EGL_NO_SURFACE || context == EGL_NO_CONTEXT)
|
||||
return false;
|
||||
|
||||
if (! eglMakeCurrent (display, surface, surface, context))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept { return eglGetCurrentContext() == context; }
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
eglMakeCurrent (display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void swapBuffers() const noexcept { eglSwapBuffers (display, surface); }
|
||||
bool setSwapInterval (const int) { return false; }
|
||||
int getSwapInterval() const { return 0; }
|
||||
|
||||
//==============================================================================
|
||||
bool createdOk() const noexcept { return hasInitialised; }
|
||||
void* getRawContext() const noexcept { return surfaceView.get(); }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
//==============================================================================
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
if (lastBounds != bounds)
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
lastBounds = bounds;
|
||||
auto r = bounds * Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale;
|
||||
|
||||
env->CallVoidMethod (surfaceView.get(), JuceOpenGLViewSurface.layout,
|
||||
(jint) r.getX(), (jint) r.getY(), (jint) r.getRight(), (jint) r.getBottom());
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
// Android Surface Callbacks:
|
||||
void surfaceChanged (LocalRef<jobject> holder, int format, int width, int height) override
|
||||
{
|
||||
ignoreUnused (holder, format, width, height);
|
||||
}
|
||||
|
||||
void surfaceCreated (LocalRef<jobject> holder) override;
|
||||
void surfaceDestroyed (LocalRef<jobject> holder) override;
|
||||
|
||||
//==============================================================================
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
Component& component;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
friend struct AndroidGLCallbacks;
|
||||
|
||||
void attachedToWindow()
|
||||
{
|
||||
auto* env = getEnv();
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
if (surfaceHolderCallback == nullptr)
|
||||
surfaceHolderCallback = GlobalRef (CreateJavaInterface (this, "android/view/SurfaceHolder$Callback"));
|
||||
|
||||
env->CallVoidMethod (holder, AndroidSurfaceHolder.addCallback, surfaceHolderCallback.get());
|
||||
}
|
||||
|
||||
void detachedFromWindow()
|
||||
{
|
||||
if (surfaceHolderCallback != nullptr)
|
||||
{
|
||||
auto* env = getEnv();
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
env->CallVoidMethod (holder.get(), AndroidSurfaceHolder.removeCallback, surfaceHolderCallback.get());
|
||||
surfaceHolderCallback.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void dispatchDraw (jobject /*canvas*/)
|
||||
{
|
||||
if (juceContext != nullptr)
|
||||
juceContext->triggerRepaint();
|
||||
}
|
||||
|
||||
bool tryChooseConfig (const std::vector<EGLint>& optionalAttribs)
|
||||
{
|
||||
std::vector<EGLint> allAttribs
|
||||
{
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_ALPHA_SIZE, 0,
|
||||
EGL_DEPTH_SIZE, 16
|
||||
};
|
||||
|
||||
allAttribs.insert (allAttribs.end(), optionalAttribs.begin(), optionalAttribs.end());
|
||||
|
||||
allAttribs.push_back (EGL_NONE);
|
||||
|
||||
EGLint numConfigs{};
|
||||
return eglChooseConfig (display, allAttribs.data(), &config, 1, &numConfigs);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool initEGLDisplay (const OpenGLPixelFormat& pixelFormat, bool multisample)
|
||||
{
|
||||
// already initialised?
|
||||
if (display != EGL_NO_DISPLAY)
|
||||
return true;
|
||||
|
||||
if ((display = eglGetDisplay (EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
|
||||
{
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! eglInitialize (display, nullptr, nullptr))
|
||||
{
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tryChooseConfig ({ EGL_SAMPLE_BUFFERS, multisample ? 1 : 0, EGL_SAMPLES, pixelFormat.multisamplingLevel }))
|
||||
return true;
|
||||
|
||||
if (tryChooseConfig ({}))
|
||||
return true;
|
||||
|
||||
eglTerminate (display);
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool hasInitialised = false;
|
||||
|
||||
GlobalRef surfaceView;
|
||||
Rectangle<int> lastBounds;
|
||||
|
||||
OpenGLContext* juceContext = nullptr;
|
||||
EGLSurface surface;
|
||||
EGLContext context;
|
||||
|
||||
GlobalRef surfaceHolderCallback;
|
||||
|
||||
static EGLDisplay display;
|
||||
static EGLConfig config;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
void AndroidGLCallbacks::attachedToWindow (JNIEnv*, jobject /*this*/, jlong host)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->attachedToWindow();
|
||||
}
|
||||
|
||||
void AndroidGLCallbacks::detachedFromWindow (JNIEnv*, jobject /*this*/, jlong host)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->detachedFromWindow();
|
||||
}
|
||||
|
||||
void AndroidGLCallbacks::dispatchDraw (JNIEnv*, jobject /*this*/, jlong host, jobject canvas)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->dispatchDraw (canvas);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return eglGetCurrentContext() != EGL_NO_CONTEXT;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
// This byte-code is generated from native/java/com/rmsl/juce/JuceOpenGLView.java with min sdk version 16
|
||||
// See juce_core/native/java/README.txt on how to generate this byte-code.
|
||||
static const uint8 javaJuceOpenGLView[] =
|
||||
{
|
||||
0x1f, 0x8b, 0x08, 0x08, 0xac, 0xdb, 0x8b, 0x62, 0x00, 0x03, 0x4a, 0x61,
|
||||
0x76, 0x61, 0x44, 0x65, 0x78, 0x42, 0x79, 0x74, 0x65, 0x43, 0x6f, 0x64,
|
||||
0x65, 0x2e, 0x64, 0x65, 0x78, 0x00, 0x6d, 0x54, 0x4f, 0x48, 0x14, 0x51,
|
||||
0x18, 0xff, 0x66, 0xe6, 0xed, 0x6e, 0xea, 0x3a, 0xae, 0xeb, 0x7f, 0xc1,
|
||||
0xd8, 0x20, 0xea, 0x64, 0x6b, 0x7f, 0xac, 0x40, 0x0b, 0x4d, 0xfb, 0xb7,
|
||||
0x0d, 0x4a, 0x69, 0x5b, 0x6c, 0x1d, 0x9a, 0x66, 0x27, 0x77, 0x44, 0x67,
|
||||
0x96, 0xd9, 0xd9, 0x55, 0x28, 0x44, 0xba, 0x78, 0xf1, 0x54, 0x41, 0xd1,
|
||||
0xd9, 0x43, 0x04, 0x45, 0x20, 0x05, 0x75, 0x48, 0xa2, 0x8b, 0xe1, 0xa1,
|
||||
0x53, 0xd8, 0x21, 0xa8, 0x43, 0x07, 0x8f, 0x9d, 0xc2, 0x93, 0xf4, 0x7b,
|
||||
0x6f, 0x9e, 0xad, 0x85, 0xc3, 0xfe, 0xe6, 0xfb, 0xfb, 0xbe, 0xef, 0xf7,
|
||||
0xde, 0xce, 0xfb, 0xf2, 0xf6, 0x6c, 0x6d, 0xcf, 0xd1, 0x5e, 0x7a, 0xbc,
|
||||
0x7e, 0xe1, 0x25, 0x7d, 0xb9, 0xba, 0x35, 0x37, 0x59, 0xff, 0xe2, 0xd1,
|
||||
0xda, 0xeb, 0x7b, 0x2b, 0x67, 0xb6, 0x8c, 0xbb, 0xef, 0x2f, 0x0e, 0x3f,
|
||||
0x89, 0x10, 0x15, 0x89, 0x68, 0x36, 0x7b, 0x2c, 0x49, 0xf2, 0xd9, 0x64,
|
||||
0x44, 0x5d, 0x14, 0xfa, 0xf7, 0x00, 0x3f, 0x81, 0x18, 0xc0, 0x14, 0x22,
|
||||
0xfc, 0xe8, 0x3a, 0x5e, 0xf5, 0x90, 0xb7, 0xa4, 0xbd, 0x8a, 0xd7, 0x2b,
|
||||
0x8d, 0x68, 0x03, 0x32, 0x0a, 0xa9, 0x03, 0x8d, 0xc0, 0x01, 0x60, 0x10,
|
||||
0xb8, 0x09, 0xcc, 0x00, 0x0f, 0x81, 0x65, 0xe0, 0x0d, 0xf0, 0x0e, 0x58,
|
||||
0x01, 0x3e, 0x02, 0xab, 0xc0, 0x1a, 0xf0, 0x19, 0x58, 0x07, 0xbe, 0xf3,
|
||||
0x5a, 0xc0, 0x6f, 0xa0, 0x01, 0x5c, 0x5a, 0x80, 0x7d, 0x40, 0x2f, 0x60,
|
||||
0x00, 0xb7, 0x81, 0x39, 0x60, 0x11, 0x78, 0xc0, 0x42, 0x0e, 0x1a, 0xe7,
|
||||
0x07, 0x60, 0x3b, 0x14, 0x95, 0x7c, 0x39, 0xf7, 0x7a, 0x29, 0xa3, 0x72,
|
||||
0x6f, 0x35, 0x52, 0xff, 0xaa, 0x12, 0xd5, 0x4a, 0xfd, 0x07, 0xf4, 0x3a,
|
||||
0xa9, 0x6f, 0x40, 0x8f, 0x4b, 0xfd, 0xd7, 0x0e, 0xff, 0x26, 0x74, 0x5d,
|
||||
0xd6, 0xe5, 0xcd, 0x78, 0x9f, 0x66, 0xd1, 0x53, 0x13, 0x75, 0x19, 0x3c,
|
||||
0x49, 0xc9, 0xa1, 0x55, 0xca, 0x76, 0xc1, 0x87, 0x89, 0x38, 0xcf, 0x6f,
|
||||
0x10, 0x32, 0xcc, 0x8b, 0xa0, 0x6a, 0x93, 0xf4, 0xb7, 0x0a, 0xa9, 0x50,
|
||||
0x9b, 0xb4, 0xdb, 0xa5, 0xdd, 0x21, 0xa4, 0x4a, 0x9d, 0xd2, 0xaf, 0xc8,
|
||||
0xba, 0xfc, 0x51, 0xa5, 0xfc, 0x24, 0x1d, 0x51, 0x44, 0xb8, 0xef, 0x29,
|
||||
0x0b, 0xf7, 0x55, 0x4c, 0x11, 0x1d, 0x11, 0x95, 0x73, 0xd0, 0x72, 0xfb,
|
||||
0x39, 0x7b, 0x4d, 0x54, 0x20, 0x5a, 0x62, 0xd5, 0xbe, 0x3c, 0xaa, 0x8b,
|
||||
0xf5, 0xaa, 0xa8, 0xfd, 0x1c, 0xaf, 0x46, 0x48, 0x2f, 0xa5, 0xd0, 0x38,
|
||||
0x8d, 0x0d, 0x20, 0x0b, 0x65, 0x0f, 0xa3, 0xe1, 0x49, 0xec, 0x9d, 0xdb,
|
||||
0xc5, 0x81, 0x38, 0xb1, 0xcb, 0xba, 0x38, 0x86, 0x90, 0xc5, 0x32, 0x0b,
|
||||
0xf9, 0x24, 0x13, 0x0d, 0x82, 0x37, 0x3f, 0x91, 0xb7, 0xdb, 0x75, 0x12,
|
||||
0xbc, 0xee, 0xae, 0x75, 0x7a, 0x6a, 0xf0, 0x45, 0xe9, 0x72, 0xaf, 0x7c,
|
||||
0xcd, 0x07, 0xb9, 0x66, 0xf7, 0xec, 0x3a, 0x64, 0x7b, 0x09, 0x0d, 0xd5,
|
||||
0x74, 0x79, 0x16, 0xd5, 0x73, 0x50, 0x85, 0xad, 0x48, 0xfb, 0x7f, 0x5d,
|
||||
0xa3, 0x68, 0xbf, 0xe3, 0x3a, 0xc1, 0x69, 0x52, 0x32, 0xd4, 0x94, 0x29,
|
||||
0x5b, 0xf6, 0x68, 0xd1, 0x76, 0xcf, 0x1b, 0x59, 0xc7, 0x9e, 0x39, 0x34,
|
||||
0x69, 0x56, 0x4c, 0xea, 0x30, 0x4c, 0x37, 0xef, 0x7b, 0x4e, 0x3e, 0x6d,
|
||||
0x79, 0x6e, 0x60, 0xbb, 0x41, 0x7a, 0x88, 0xcb, 0xd9, 0xa0, 0x6f, 0x47,
|
||||
0x68, 0xc2, 0x37, 0x8b, 0x05, 0xc7, 0x2a, 0xa5, 0x87, 0x4c, 0xb7, 0x62,
|
||||
0x96, 0x76, 0x0d, 0x5d, 0xb1, 0x27, 0x1c, 0xcf, 0xed, 0xa3, 0xce, 0xbf,
|
||||
0xa1, 0x0a, 0x9a, 0xa4, 0xc7, 0xca, 0xfe, 0x1d, 0xd3, 0xb2, 0x79, 0xc3,
|
||||
0x3e, 0xda, 0x6b, 0x58, 0xde, 0x74, 0xda, 0x9f, 0x2e, 0x4d, 0xa5, 0x27,
|
||||
0xc1, 0x25, 0xfd, 0x2f, 0xa1, 0x3e, 0x52, 0xb2, 0xa4, 0x66, 0x33, 0xa4,
|
||||
0x65, 0x33, 0x06, 0x14, 0x03, 0x8a, 0x91, 0x21, 0x25, 0x47, 0x6a, 0xce,
|
||||
0xa0, 0xa8, 0x65, 0xba, 0x96, 0x3d, 0x25, 0x24, 0x38, 0x50, 0xcc, 0x0a,
|
||||
0x79, 0x52, 0x3c, 0xef, 0x94, 0x8a, 0x66, 0x60, 0x15, 0x86, 0x7d, 0x73,
|
||||
0x86, 0xda, 0x26, 0xcc, 0xa0, 0x60, 0xfb, 0xe3, 0xbe, 0xe9, 0xc2, 0xeb,
|
||||
0x63, 0x43, 0x21, 0x31, 0x62, 0x05, 0xaf, 0x14, 0x50, 0xad, 0x6b, 0x06,
|
||||
0x4e, 0xc5, 0x1e, 0x2f, 0x38, 0x25, 0x4a, 0x7a, 0xee, 0x60, 0x10, 0x98,
|
||||
0x56, 0xc1, 0xce, 0x8f, 0x7b, 0xd7, 0x1c, 0x37, 0xef, 0xcd, 0x50, 0x8b,
|
||||
0xf0, 0x71, 0x57, 0xe8, 0x18, 0x11, 0xe9, 0xd4, 0xec, 0xb9, 0xc3, 0x76,
|
||||
0x98, 0x7a, 0xce, 0xf7, 0xa6, 0x65, 0x72, 0xe7, 0x6e, 0x5e, 0xb9, 0x22,
|
||||
0x8e, 0x18, 0xf8, 0x48, 0x8b, 0x05, 0xbc, 0x61, 0xb4, 0xec, 0x96, 0x4b,
|
||||
0x76, 0x9e, 0x0e, 0xaa, 0xc9, 0xd6, 0x98, 0x7e, 0x62, 0xb4, 0x9b, 0xba,
|
||||
0x29, 0xa6, 0x5f, 0xa2, 0x11, 0xa5, 0x31, 0xa6, 0x9f, 0x5a, 0xc8, 0xd1,
|
||||
0x71, 0xa5, 0x2b, 0xa6, 0x53, 0x3f, 0x85, 0xd6, 0x59, 0xc8, 0x85, 0x1b,
|
||||
0xfd, 0xf8, 0x27, 0x19, 0xee, 0x02, 0x9b, 0x9f, 0x67, 0x1b, 0x5a, 0xe4,
|
||||
0xbe, 0x4a, 0x2a, 0xa0, 0x00, 0x11, 0x65, 0x91, 0x29, 0xec, 0x19, 0x53,
|
||||
0x94, 0x6f, 0x90, 0xbf, 0x98, 0xca, 0x96, 0x22, 0xf2, 0xde, 0xd3, 0x8e,
|
||||
0xef, 0x84, 0xcb, 0xed, 0x99, 0xa6, 0x52, 0x75, 0xae, 0x69, 0x54, 0x9d,
|
||||
0x6d, 0x8c, 0xaa, 0xf3, 0x6d, 0xbb, 0x06, 0x9f, 0x71, 0x51, 0xaa, 0xce,
|
||||
0x39, 0x25, 0x25, 0xe7, 0x04, 0xd7, 0x13, 0xd5, 0x59, 0xa2, 0xa6, 0xc2,
|
||||
0xfa, 0x7c, 0xfe, 0x69, 0x32, 0x87, 0xdf, 0x45, 0x4a, 0x85, 0x6b, 0xc5,
|
||||
0x3d, 0x4d, 0x84, 0x3a, 0x9f, 0xaf, 0x7f, 0x00, 0x34, 0xf2, 0xd3, 0x47,
|
||||
0x98, 0x05, 0x00, 0x00
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
struct AndroidGLCallbacks
|
||||
{
|
||||
static void attachedToWindow (JNIEnv*, jobject, jlong);
|
||||
static void detachedFromWindow (JNIEnv*, jobject, jlong);
|
||||
static void dispatchDraw (JNIEnv*, jobject, jlong, jobject);
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
|
||||
METHOD (constructor, "<init>", "(Landroid/content/Context;J)V") \
|
||||
METHOD (getParent, "getParent", "()Landroid/view/ViewParent;") \
|
||||
METHOD (getHolder, "getHolder", "()Landroid/view/SurfaceHolder;") \
|
||||
METHOD (layout, "layout", "(IIII)V" ) \
|
||||
CALLBACK (AndroidGLCallbacks::attachedToWindow, "onAttchedWindowNative", "(J)V") \
|
||||
CALLBACK (AndroidGLCallbacks::detachedFromWindow, "onDetachedFromWindowNative", "(J)V") \
|
||||
CALLBACK (AndroidGLCallbacks::dispatchDraw, "onDrawNative", "(JLandroid/graphics/Canvas;)V")
|
||||
|
||||
DECLARE_JNI_CLASS_WITH_BYTECODE (JuceOpenGLViewSurface, "com/rmsl/juce/JuceOpenGLView", 16, javaJuceOpenGLView, sizeof(javaJuceOpenGLView))
|
||||
#undef JNI_CLASS_MEMBERS
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext : private SurfaceHolderCallback
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& comp,
|
||||
const OpenGLPixelFormat& pixelFormat,
|
||||
void* /*contextToShareWith*/,
|
||||
bool useMultisamplingIn,
|
||||
OpenGLVersion)
|
||||
: component (comp),
|
||||
surface (EGL_NO_SURFACE), context (EGL_NO_CONTEXT)
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
// Do we have a native peer that we can attach to?
|
||||
if (component.getPeer()->getNativeHandle() == nullptr)
|
||||
return;
|
||||
|
||||
// Initialise the EGL display
|
||||
if (! initEGLDisplay (pixelFormat, useMultisamplingIn))
|
||||
return;
|
||||
|
||||
// create a native surface view
|
||||
surfaceView = GlobalRef (LocalRef<jobject>(env->NewObject (JuceOpenGLViewSurface,
|
||||
JuceOpenGLViewSurface.constructor,
|
||||
getAppContext().get(),
|
||||
reinterpret_cast<jlong> (this))));
|
||||
if (surfaceView.get() == nullptr)
|
||||
return;
|
||||
|
||||
// add the view to the view hierarchy
|
||||
// after this the nativecontext can receive callbacks
|
||||
env->CallVoidMethod ((jobject) component.getPeer()->getNativeHandle(),
|
||||
AndroidViewGroup.addView, surfaceView.get());
|
||||
|
||||
// initialise the geometry of the view
|
||||
auto bounds = component.getTopLevelComponent()->getLocalArea (&component, component.getLocalBounds());
|
||||
bounds *= component.getDesktopScaleFactor();
|
||||
|
||||
updateWindowPosition (bounds);
|
||||
hasInitialised = true;
|
||||
}
|
||||
|
||||
~NativeContext() override
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
if (jobject viewParent = env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getParent))
|
||||
env->CallVoidMethod (viewParent, AndroidViewGroup.removeView, surfaceView.get());
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool initialiseOnRenderThread (OpenGLContext& aContext)
|
||||
{
|
||||
jassert (hasInitialised);
|
||||
|
||||
// has the context already attached?
|
||||
jassert (surface == EGL_NO_SURFACE && context == EGL_NO_CONTEXT);
|
||||
|
||||
auto env = getEnv();
|
||||
|
||||
ANativeWindow* window = nullptr;
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
if (holder != nullptr)
|
||||
{
|
||||
LocalRef<jobject> jSurface (env->CallObjectMethod (holder.get(), AndroidSurfaceHolder.getSurface));
|
||||
|
||||
if (jSurface != nullptr)
|
||||
{
|
||||
window = ANativeWindow_fromSurface(env, jSurface.get());
|
||||
|
||||
// if we didn't succeed the first time, wait 25ms and try again
|
||||
if (window == nullptr)
|
||||
{
|
||||
Thread::sleep (200);
|
||||
window = ANativeWindow_fromSurface (env, jSurface.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (window == nullptr)
|
||||
{
|
||||
// failed to get a pointer to the native window after second try so bail out
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the surface
|
||||
surface = eglCreateWindowSurface (display, config, window, nullptr);
|
||||
jassert (surface != EGL_NO_SURFACE);
|
||||
|
||||
ANativeWindow_release (window);
|
||||
|
||||
// create the OpenGL context
|
||||
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
|
||||
context = eglCreateContext (display, config, EGL_NO_CONTEXT, contextAttribs);
|
||||
jassert (context != EGL_NO_CONTEXT);
|
||||
|
||||
juceContext = &aContext;
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
jassert (hasInitialised);
|
||||
|
||||
// is there a context available to detach?
|
||||
jassert (surface != EGL_NO_SURFACE && context != EGL_NO_CONTEXT);
|
||||
|
||||
eglDestroyContext (display, context);
|
||||
context = EGL_NO_CONTEXT;
|
||||
|
||||
eglDestroySurface (display, surface);
|
||||
surface = EGL_NO_SURFACE;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
if (! hasInitialised)
|
||||
return false;
|
||||
|
||||
if (surface == EGL_NO_SURFACE || context == EGL_NO_CONTEXT)
|
||||
return false;
|
||||
|
||||
if (! eglMakeCurrent (display, surface, surface, context))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept { return eglGetCurrentContext() == context; }
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
eglMakeCurrent (display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void swapBuffers() const noexcept { eglSwapBuffers (display, surface); }
|
||||
bool setSwapInterval (const int) { return false; }
|
||||
int getSwapInterval() const { return 0; }
|
||||
|
||||
//==============================================================================
|
||||
bool createdOk() const noexcept { return hasInitialised; }
|
||||
void* getRawContext() const noexcept { return surfaceView.get(); }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
//==============================================================================
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
if (lastBounds != bounds)
|
||||
{
|
||||
auto env = getEnv();
|
||||
|
||||
lastBounds = bounds;
|
||||
auto r = bounds * Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale;
|
||||
|
||||
env->CallVoidMethod (surfaceView.get(), JuceOpenGLViewSurface.layout,
|
||||
(jint) r.getX(), (jint) r.getY(), (jint) r.getRight(), (jint) r.getBottom());
|
||||
}
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
// Android Surface Callbacks:
|
||||
void surfaceChanged (LocalRef<jobject> holder, int format, int width, int height) override
|
||||
{
|
||||
ignoreUnused (holder, format, width, height);
|
||||
}
|
||||
|
||||
void surfaceCreated (LocalRef<jobject> holder) override;
|
||||
void surfaceDestroyed (LocalRef<jobject> holder) override;
|
||||
|
||||
//==============================================================================
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
Component& component;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
friend struct AndroidGLCallbacks;
|
||||
|
||||
void attachedToWindow()
|
||||
{
|
||||
auto* env = getEnv();
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
if (surfaceHolderCallback == nullptr)
|
||||
surfaceHolderCallback = GlobalRef (CreateJavaInterface (this, "android/view/SurfaceHolder$Callback"));
|
||||
|
||||
env->CallVoidMethod (holder, AndroidSurfaceHolder.addCallback, surfaceHolderCallback.get());
|
||||
}
|
||||
|
||||
void detachedFromWindow()
|
||||
{
|
||||
if (surfaceHolderCallback != nullptr)
|
||||
{
|
||||
auto* env = getEnv();
|
||||
|
||||
LocalRef<jobject> holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder));
|
||||
|
||||
env->CallVoidMethod (holder.get(), AndroidSurfaceHolder.removeCallback, surfaceHolderCallback.get());
|
||||
surfaceHolderCallback.clear();
|
||||
}
|
||||
}
|
||||
|
||||
void dispatchDraw (jobject /*canvas*/)
|
||||
{
|
||||
if (juceContext != nullptr)
|
||||
juceContext->triggerRepaint();
|
||||
}
|
||||
|
||||
bool tryChooseConfig (const std::vector<EGLint>& optionalAttribs)
|
||||
{
|
||||
std::vector<EGLint> allAttribs
|
||||
{
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_ALPHA_SIZE, 0,
|
||||
EGL_DEPTH_SIZE, 16
|
||||
};
|
||||
|
||||
allAttribs.insert (allAttribs.end(), optionalAttribs.begin(), optionalAttribs.end());
|
||||
|
||||
allAttribs.push_back (EGL_NONE);
|
||||
|
||||
EGLint numConfigs{};
|
||||
return eglChooseConfig (display, allAttribs.data(), &config, 1, &numConfigs);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool initEGLDisplay (const OpenGLPixelFormat& pixelFormat, bool multisample)
|
||||
{
|
||||
// already initialised?
|
||||
if (display != EGL_NO_DISPLAY)
|
||||
return true;
|
||||
|
||||
if ((display = eglGetDisplay (EGL_DEFAULT_DISPLAY)) == EGL_NO_DISPLAY)
|
||||
{
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! eglInitialize (display, nullptr, nullptr))
|
||||
{
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tryChooseConfig ({ EGL_SAMPLE_BUFFERS, multisample ? 1 : 0, EGL_SAMPLES, pixelFormat.multisamplingLevel }))
|
||||
return true;
|
||||
|
||||
if (tryChooseConfig ({}))
|
||||
return true;
|
||||
|
||||
eglTerminate (display);
|
||||
jassertfalse;
|
||||
return false;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool hasInitialised = false;
|
||||
|
||||
GlobalRef surfaceView;
|
||||
Rectangle<int> lastBounds;
|
||||
|
||||
OpenGLContext* juceContext = nullptr;
|
||||
EGLSurface surface;
|
||||
EGLContext context;
|
||||
|
||||
GlobalRef surfaceHolderCallback;
|
||||
|
||||
static EGLDisplay display;
|
||||
static EGLConfig config;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
void AndroidGLCallbacks::attachedToWindow (JNIEnv*, jobject /*this*/, jlong host)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->attachedToWindow();
|
||||
}
|
||||
|
||||
void AndroidGLCallbacks::detachedFromWindow (JNIEnv*, jobject /*this*/, jlong host)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->detachedFromWindow();
|
||||
}
|
||||
|
||||
void AndroidGLCallbacks::dispatchDraw (JNIEnv*, jobject /*this*/, jlong host, jobject canvas)
|
||||
{
|
||||
if (auto* nativeContext = reinterpret_cast<OpenGLContext::NativeContext*> (host))
|
||||
nativeContext->dispatchDraw (canvas);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return eglGetCurrentContext() != EGL_NO_CONTEXT;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,315 +1,315 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
@interface JuceGLView : UIView
|
||||
{
|
||||
}
|
||||
+ (Class) layerClass;
|
||||
@end
|
||||
|
||||
@implementation JuceGLView
|
||||
+ (Class) layerClass
|
||||
{
|
||||
return [CAEAGLLayer class];
|
||||
}
|
||||
@end
|
||||
|
||||
extern "C" GLvoid glResolveMultisampleFramebufferAPPLE();
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& c,
|
||||
const OpenGLPixelFormat& pixFormat,
|
||||
void* contextToShare,
|
||||
bool multisampling,
|
||||
OpenGLVersion version)
|
||||
: component (c), openGLversion (version),
|
||||
useDepthBuffer (pixFormat.depthBufferBits > 0),
|
||||
useMSAA (multisampling)
|
||||
{
|
||||
JUCE_AUTORELEASEPOOL
|
||||
{
|
||||
if (auto* peer = component.getPeer())
|
||||
{
|
||||
auto bounds = peer->getAreaCoveredBy (component);
|
||||
|
||||
view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
|
||||
view.opaque = YES;
|
||||
view.hidden = NO;
|
||||
view.backgroundColor = [UIColor blackColor];
|
||||
view.userInteractionEnabled = NO;
|
||||
|
||||
glLayer = (CAEAGLLayer*) [view layer];
|
||||
glLayer.opaque = true;
|
||||
|
||||
updateWindowPosition (bounds);
|
||||
|
||||
[((UIView*) peer->getNativeHandle()) addSubview: view];
|
||||
|
||||
if (version == openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
|
||||
{
|
||||
if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
|
||||
{
|
||||
releaseContext();
|
||||
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
|
||||
}
|
||||
|
||||
if (context != nil)
|
||||
{
|
||||
// I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
|
||||
// so causes mysterious timing-related failures.
|
||||
[EAGLContext setCurrentContext: context];
|
||||
gl::loadFunctions();
|
||||
createGLBuffers();
|
||||
deactivateCurrentContext();
|
||||
}
|
||||
else
|
||||
{
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
releaseContext();
|
||||
[view removeFromSuperview];
|
||||
[view release];
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
freeGLBuffers();
|
||||
deactivateCurrentContext();
|
||||
}
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return context; }
|
||||
GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
if (! [EAGLContext setCurrentContext: context])
|
||||
return false;
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
|
||||
: frameBufferHandle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
return [EAGLContext currentContext] == context;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
[EAGLContext setCurrentContext: nil];
|
||||
}
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
if (useMSAA)
|
||||
{
|
||||
glBindFramebuffer (GL_DRAW_FRAMEBUFFER, frameBufferHandle);
|
||||
glBindFramebuffer (GL_READ_FRAMEBUFFER, msaaBufferHandle);
|
||||
|
||||
if (openGLversion >= openGL3_2)
|
||||
{
|
||||
auto w = roundToInt (lastBounds.getWidth() * glLayer.contentsScale);
|
||||
auto h = roundToInt (lastBounds.getHeight() * glLayer.contentsScale);
|
||||
|
||||
glBlitFramebuffer (0, 0, w, h,
|
||||
0, 0, w, h,
|
||||
GL_COLOR_BUFFER_BIT,
|
||||
GL_NEAREST);
|
||||
}
|
||||
else
|
||||
{
|
||||
::glResolveMultisampleFramebufferAPPLE();
|
||||
}
|
||||
}
|
||||
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
|
||||
[context presentRenderbuffer: GL_RENDERBUFFER];
|
||||
|
||||
if (needToRebuildBuffers)
|
||||
{
|
||||
needToRebuildBuffers = false;
|
||||
|
||||
freeGLBuffers();
|
||||
createGLBuffers();
|
||||
makeActive();
|
||||
}
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
view.frame = convertToCGRect (bounds);
|
||||
glLayer.contentsScale = (CGFloat) (Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale
|
||||
/ component.getDesktopScaleFactor());
|
||||
|
||||
if (lastBounds != bounds)
|
||||
{
|
||||
lastBounds = bounds;
|
||||
needToRebuildBuffers = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap) noexcept
|
||||
{
|
||||
swapFrames = numFramesPerSwap;
|
||||
return false;
|
||||
}
|
||||
|
||||
int getSwapInterval() const noexcept { return swapFrames; }
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
private:
|
||||
Component& component;
|
||||
JuceGLView* view = nil;
|
||||
CAEAGLLayer* glLayer = nil;
|
||||
EAGLContext* context = nil;
|
||||
const OpenGLVersion openGLversion;
|
||||
const bool useDepthBuffer, useMSAA;
|
||||
|
||||
GLuint frameBufferHandle = 0, colorBufferHandle = 0, depthBufferHandle = 0,
|
||||
msaaColorHandle = 0, msaaBufferHandle = 0;
|
||||
|
||||
Rectangle<int> lastBounds;
|
||||
int swapFrames = 0;
|
||||
bool needToRebuildBuffers = false;
|
||||
|
||||
bool createContext (EAGLRenderingAPI type, void* contextToShare)
|
||||
{
|
||||
jassert (context == nil);
|
||||
context = [EAGLContext alloc];
|
||||
|
||||
context = contextToShare != nullptr
|
||||
? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
|
||||
: [context initWithAPI: type];
|
||||
|
||||
return context != nil;
|
||||
}
|
||||
|
||||
void releaseContext()
|
||||
{
|
||||
[context release];
|
||||
context = nil;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void createGLBuffers()
|
||||
{
|
||||
glGenFramebuffers (1, &frameBufferHandle);
|
||||
glGenRenderbuffers (1, &colorBufferHandle);
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
|
||||
|
||||
bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
|
||||
jassert (ok); ignoreUnused (ok);
|
||||
|
||||
GLint width, height;
|
||||
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
|
||||
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
|
||||
|
||||
if (useMSAA)
|
||||
{
|
||||
glGenFramebuffers (1, &msaaBufferHandle);
|
||||
glGenRenderbuffers (1, &msaaColorHandle);
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
|
||||
|
||||
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
|
||||
}
|
||||
|
||||
if (useDepthBuffer)
|
||||
{
|
||||
glGenRenderbuffers (1, &depthBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
|
||||
|
||||
if (useMSAA)
|
||||
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
|
||||
else
|
||||
glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
|
||||
}
|
||||
|
||||
jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
void freeGLBuffers()
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
[context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
|
||||
|
||||
deleteFrameBuffer (frameBufferHandle);
|
||||
deleteFrameBuffer (msaaBufferHandle);
|
||||
deleteRenderBuffer (colorBufferHandle);
|
||||
deleteRenderBuffer (depthBufferHandle);
|
||||
deleteRenderBuffer (msaaColorHandle);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
|
||||
static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return [EAGLContext currentContext] != nil;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
@interface JuceGLView : UIView
|
||||
{
|
||||
}
|
||||
+ (Class) layerClass;
|
||||
@end
|
||||
|
||||
@implementation JuceGLView
|
||||
+ (Class) layerClass
|
||||
{
|
||||
return [CAEAGLLayer class];
|
||||
}
|
||||
@end
|
||||
|
||||
extern "C" GLvoid glResolveMultisampleFramebufferAPPLE();
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& c,
|
||||
const OpenGLPixelFormat& pixFormat,
|
||||
void* contextToShare,
|
||||
bool multisampling,
|
||||
OpenGLVersion version)
|
||||
: component (c), openGLversion (version),
|
||||
useDepthBuffer (pixFormat.depthBufferBits > 0),
|
||||
useMSAA (multisampling)
|
||||
{
|
||||
JUCE_AUTORELEASEPOOL
|
||||
{
|
||||
if (auto* peer = component.getPeer())
|
||||
{
|
||||
auto bounds = peer->getAreaCoveredBy (component);
|
||||
|
||||
view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
|
||||
view.opaque = YES;
|
||||
view.hidden = NO;
|
||||
view.backgroundColor = [UIColor blackColor];
|
||||
view.userInteractionEnabled = NO;
|
||||
|
||||
glLayer = (CAEAGLLayer*) [view layer];
|
||||
glLayer.opaque = true;
|
||||
|
||||
updateWindowPosition (bounds);
|
||||
|
||||
[((UIView*) peer->getNativeHandle()) addSubview: view];
|
||||
|
||||
if (version == openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
|
||||
{
|
||||
if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
|
||||
{
|
||||
releaseContext();
|
||||
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
|
||||
}
|
||||
|
||||
if (context != nil)
|
||||
{
|
||||
// I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
|
||||
// so causes mysterious timing-related failures.
|
||||
[EAGLContext setCurrentContext: context];
|
||||
gl::loadFunctions();
|
||||
createGLBuffers();
|
||||
deactivateCurrentContext();
|
||||
}
|
||||
else
|
||||
{
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jassertfalse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
releaseContext();
|
||||
[view removeFromSuperview];
|
||||
[view release];
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
freeGLBuffers();
|
||||
deactivateCurrentContext();
|
||||
}
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return context; }
|
||||
GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
if (! [EAGLContext setCurrentContext: context])
|
||||
return false;
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
|
||||
: frameBufferHandle);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
return [EAGLContext currentContext] == context;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
[EAGLContext setCurrentContext: nil];
|
||||
}
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
if (useMSAA)
|
||||
{
|
||||
glBindFramebuffer (GL_DRAW_FRAMEBUFFER, frameBufferHandle);
|
||||
glBindFramebuffer (GL_READ_FRAMEBUFFER, msaaBufferHandle);
|
||||
|
||||
if (openGLversion >= openGL3_2)
|
||||
{
|
||||
auto w = roundToInt (lastBounds.getWidth() * glLayer.contentsScale);
|
||||
auto h = roundToInt (lastBounds.getHeight() * glLayer.contentsScale);
|
||||
|
||||
glBlitFramebuffer (0, 0, w, h,
|
||||
0, 0, w, h,
|
||||
GL_COLOR_BUFFER_BIT,
|
||||
GL_NEAREST);
|
||||
}
|
||||
else
|
||||
{
|
||||
::glResolveMultisampleFramebufferAPPLE();
|
||||
}
|
||||
}
|
||||
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
|
||||
[context presentRenderbuffer: GL_RENDERBUFFER];
|
||||
|
||||
if (needToRebuildBuffers)
|
||||
{
|
||||
needToRebuildBuffers = false;
|
||||
|
||||
freeGLBuffers();
|
||||
createGLBuffers();
|
||||
makeActive();
|
||||
}
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
view.frame = convertToCGRect (bounds);
|
||||
glLayer.contentsScale = (CGFloat) (Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale
|
||||
/ component.getDesktopScaleFactor());
|
||||
|
||||
if (lastBounds != bounds)
|
||||
{
|
||||
lastBounds = bounds;
|
||||
needToRebuildBuffers = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap) noexcept
|
||||
{
|
||||
swapFrames = numFramesPerSwap;
|
||||
return false;
|
||||
}
|
||||
|
||||
int getSwapInterval() const noexcept { return swapFrames; }
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
private:
|
||||
Component& component;
|
||||
JuceGLView* view = nil;
|
||||
CAEAGLLayer* glLayer = nil;
|
||||
EAGLContext* context = nil;
|
||||
const OpenGLVersion openGLversion;
|
||||
const bool useDepthBuffer, useMSAA;
|
||||
|
||||
GLuint frameBufferHandle = 0, colorBufferHandle = 0, depthBufferHandle = 0,
|
||||
msaaColorHandle = 0, msaaBufferHandle = 0;
|
||||
|
||||
Rectangle<int> lastBounds;
|
||||
int swapFrames = 0;
|
||||
bool needToRebuildBuffers = false;
|
||||
|
||||
bool createContext (EAGLRenderingAPI type, void* contextToShare)
|
||||
{
|
||||
jassert (context == nil);
|
||||
context = [EAGLContext alloc];
|
||||
|
||||
context = contextToShare != nullptr
|
||||
? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
|
||||
: [context initWithAPI: type];
|
||||
|
||||
return context != nil;
|
||||
}
|
||||
|
||||
void releaseContext()
|
||||
{
|
||||
[context release];
|
||||
context = nil;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
void createGLBuffers()
|
||||
{
|
||||
glGenFramebuffers (1, &frameBufferHandle);
|
||||
glGenRenderbuffers (1, &colorBufferHandle);
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
|
||||
|
||||
bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
|
||||
jassert (ok); ignoreUnused (ok);
|
||||
|
||||
GLint width, height;
|
||||
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
|
||||
glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
|
||||
|
||||
if (useMSAA)
|
||||
{
|
||||
glGenFramebuffers (1, &msaaBufferHandle);
|
||||
glGenRenderbuffers (1, &msaaColorHandle);
|
||||
|
||||
glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
|
||||
|
||||
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
|
||||
}
|
||||
|
||||
if (useDepthBuffer)
|
||||
{
|
||||
glGenRenderbuffers (1, &depthBufferHandle);
|
||||
glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
|
||||
|
||||
if (useMSAA)
|
||||
glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
|
||||
else
|
||||
glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
|
||||
|
||||
glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
|
||||
}
|
||||
|
||||
jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
void freeGLBuffers()
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
[context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
|
||||
|
||||
deleteFrameBuffer (frameBufferHandle);
|
||||
deleteFrameBuffer (msaaBufferHandle);
|
||||
deleteRenderBuffer (colorBufferHandle);
|
||||
deleteRenderBuffer (depthBufferHandle);
|
||||
deleteRenderBuffer (msaaColorHandle);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
|
||||
static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return [EAGLContext currentContext] != nil;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,286 +1,286 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
extern XContext windowHandleXContext;
|
||||
|
||||
//==============================================================================
|
||||
// Defined juce_linux_Windowing.cpp
|
||||
void juce_LinuxAddRepaintListener (ComponentPeer*, Component* dummy);
|
||||
void juce_LinuxRemoveRepaintListener (ComponentPeer*, Component* dummy);
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
private:
|
||||
struct DummyComponent : public Component
|
||||
{
|
||||
DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
|
||||
: native (nativeParentContext)
|
||||
{
|
||||
}
|
||||
|
||||
void handleCommandMessage (int commandId) override
|
||||
{
|
||||
if (commandId == 0)
|
||||
native.triggerRepaint();
|
||||
}
|
||||
|
||||
OpenGLContext::NativeContext& native;
|
||||
};
|
||||
|
||||
public:
|
||||
NativeContext (Component& comp,
|
||||
const OpenGLPixelFormat& cPixelFormat,
|
||||
void* shareContext,
|
||||
bool useMultisamplingIn,
|
||||
OpenGLVersion)
|
||||
: component (comp), contextToShareWith (shareContext), dummy (*this)
|
||||
{
|
||||
display = XWindowSystem::getInstance()->getDisplay();
|
||||
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
const std::vector<GLint> optionalAttribs
|
||||
{
|
||||
GLX_SAMPLE_BUFFERS, useMultisamplingIn ? 1 : 0,
|
||||
GLX_SAMPLES, cPixelFormat.multisamplingLevel
|
||||
};
|
||||
|
||||
if (! tryChooseVisual (cPixelFormat, optionalAttribs) && ! tryChooseVisual (cPixelFormat, {}))
|
||||
return;
|
||||
|
||||
auto* peer = component.getPeer();
|
||||
jassert (peer != nullptr);
|
||||
|
||||
auto windowH = (Window) peer->getNativeHandle();
|
||||
auto colourMap = X11Symbols::getInstance()->xCreateColormap (display, windowH, bestVisual->visual, AllocNone);
|
||||
|
||||
XSetWindowAttributes swa;
|
||||
swa.colormap = colourMap;
|
||||
swa.border_pixel = 0;
|
||||
swa.event_mask = embeddedWindowEventMask;
|
||||
|
||||
auto glBounds = component.getTopLevelComponent()
|
||||
->getLocalArea (&component, component.getLocalBounds());
|
||||
|
||||
glBounds = Desktop::getInstance().getDisplays().logicalToPhysical (glBounds);
|
||||
|
||||
embeddedWindow = X11Symbols::getInstance()->xCreateWindow (display, windowH,
|
||||
glBounds.getX(), glBounds.getY(),
|
||||
(unsigned int) jmax (1, glBounds.getWidth()),
|
||||
(unsigned int) jmax (1, glBounds.getHeight()),
|
||||
0, bestVisual->depth,
|
||||
InputOutput,
|
||||
bestVisual->visual,
|
||||
CWBorderPixel | CWColormap | CWEventMask,
|
||||
&swa);
|
||||
|
||||
X11Symbols::getInstance()->xSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
|
||||
|
||||
X11Symbols::getInstance()->xMapWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xFreeColormap (display, colourMap);
|
||||
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
juce_LinuxAddRepaintListener (peer, &dummy);
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
if (auto* peer = component.getPeer())
|
||||
{
|
||||
juce_LinuxRemoveRepaintListener (peer, &dummy);
|
||||
|
||||
if (embeddedWindow != 0)
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
|
||||
X11Symbols::getInstance()->xUnmapWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xDestroyWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
XEvent event;
|
||||
while (X11Symbols::getInstance()->xCheckWindowEvent (display,
|
||||
embeddedWindow,
|
||||
embeddedWindowEventMask,
|
||||
&event) == True)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestVisual != nullptr)
|
||||
X11Symbols::getInstance()->xFree (bestVisual);
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext& c)
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
|
||||
c.makeActive();
|
||||
context = &c;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
context = nullptr;
|
||||
deactivateCurrentContext();
|
||||
glXDestroyContext (display, renderContext);
|
||||
renderContext = nullptr;
|
||||
}
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return renderContext != nullptr
|
||||
&& glXMakeCurrent (display, embeddedWindow, renderContext);
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return glXGetCurrentContext() == renderContext && renderContext != nullptr;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
if (auto* display = XWindowSystem::getInstance()->getDisplay())
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
glXMakeCurrent (display, None, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
glXSwapBuffers (display, embeddedWindow);
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> newBounds)
|
||||
{
|
||||
bounds = newBounds;
|
||||
auto physicalBounds = Desktop::getInstance().getDisplays().logicalToPhysical (bounds);
|
||||
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
X11Symbols::getInstance()->xMoveResizeWindow (display, embeddedWindow,
|
||||
physicalBounds.getX(), physicalBounds.getY(),
|
||||
(unsigned int) jmax (1, physicalBounds.getWidth()),
|
||||
(unsigned int) jmax (1, physicalBounds.getHeight()));
|
||||
}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap)
|
||||
{
|
||||
if (numFramesPerSwap == swapFrames)
|
||||
return true;
|
||||
|
||||
if (auto GLXSwapIntervalSGI
|
||||
= (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
swapFrames = numFramesPerSwap;
|
||||
GLXSwapIntervalSGI (numFramesPerSwap);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int getSwapInterval() const { return swapFrames; }
|
||||
bool createdOk() const noexcept { return true; }
|
||||
void* getRawContext() const noexcept { return renderContext; }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
void triggerRepaint()
|
||||
{
|
||||
if (context != nullptr)
|
||||
context->triggerRepaint();
|
||||
}
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
private:
|
||||
bool tryChooseVisual (const OpenGLPixelFormat& format, const std::vector<GLint>& optionalAttribs)
|
||||
{
|
||||
std::vector<GLint> allAttribs
|
||||
{
|
||||
GLX_RGBA,
|
||||
GLX_DOUBLEBUFFER,
|
||||
GLX_RED_SIZE, format.redBits,
|
||||
GLX_GREEN_SIZE, format.greenBits,
|
||||
GLX_BLUE_SIZE, format.blueBits,
|
||||
GLX_ALPHA_SIZE, format.alphaBits,
|
||||
GLX_DEPTH_SIZE, format.depthBufferBits,
|
||||
GLX_STENCIL_SIZE, format.stencilBufferBits,
|
||||
GLX_ACCUM_RED_SIZE, format.accumulationBufferRedBits,
|
||||
GLX_ACCUM_GREEN_SIZE, format.accumulationBufferGreenBits,
|
||||
GLX_ACCUM_BLUE_SIZE, format.accumulationBufferBlueBits,
|
||||
GLX_ACCUM_ALPHA_SIZE, format.accumulationBufferAlphaBits
|
||||
};
|
||||
|
||||
allAttribs.insert (allAttribs.end(), optionalAttribs.begin(), optionalAttribs.end());
|
||||
|
||||
allAttribs.push_back (None);
|
||||
|
||||
bestVisual = glXChooseVisual (display, X11Symbols::getInstance()->xDefaultScreen (display), allAttribs.data());
|
||||
|
||||
return bestVisual != nullptr;
|
||||
}
|
||||
|
||||
static constexpr int embeddedWindowEventMask = ExposureMask | StructureNotifyMask;
|
||||
|
||||
Component& component;
|
||||
GLXContext renderContext = {};
|
||||
Window embeddedWindow = {};
|
||||
|
||||
int swapFrames = 1;
|
||||
Rectangle<int> bounds;
|
||||
XVisualInfo* bestVisual = nullptr;
|
||||
void* contextToShareWith;
|
||||
|
||||
OpenGLContext* context = nullptr;
|
||||
DummyComponent dummy;
|
||||
|
||||
::Display* display = nullptr;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return glXGetCurrentContext() != nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
extern XContext windowHandleXContext;
|
||||
|
||||
//==============================================================================
|
||||
// Defined juce_linux_Windowing.cpp
|
||||
void juce_LinuxAddRepaintListener (ComponentPeer*, Component* dummy);
|
||||
void juce_LinuxRemoveRepaintListener (ComponentPeer*, Component* dummy);
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
private:
|
||||
struct DummyComponent : public Component
|
||||
{
|
||||
DummyComponent (OpenGLContext::NativeContext& nativeParentContext)
|
||||
: native (nativeParentContext)
|
||||
{
|
||||
}
|
||||
|
||||
void handleCommandMessage (int commandId) override
|
||||
{
|
||||
if (commandId == 0)
|
||||
native.triggerRepaint();
|
||||
}
|
||||
|
||||
OpenGLContext::NativeContext& native;
|
||||
};
|
||||
|
||||
public:
|
||||
NativeContext (Component& comp,
|
||||
const OpenGLPixelFormat& cPixelFormat,
|
||||
void* shareContext,
|
||||
bool useMultisamplingIn,
|
||||
OpenGLVersion)
|
||||
: component (comp), contextToShareWith (shareContext), dummy (*this)
|
||||
{
|
||||
display = XWindowSystem::getInstance()->getDisplay();
|
||||
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
const std::vector<GLint> optionalAttribs
|
||||
{
|
||||
GLX_SAMPLE_BUFFERS, useMultisamplingIn ? 1 : 0,
|
||||
GLX_SAMPLES, cPixelFormat.multisamplingLevel
|
||||
};
|
||||
|
||||
if (! tryChooseVisual (cPixelFormat, optionalAttribs) && ! tryChooseVisual (cPixelFormat, {}))
|
||||
return;
|
||||
|
||||
auto* peer = component.getPeer();
|
||||
jassert (peer != nullptr);
|
||||
|
||||
auto windowH = (Window) peer->getNativeHandle();
|
||||
auto colourMap = X11Symbols::getInstance()->xCreateColormap (display, windowH, bestVisual->visual, AllocNone);
|
||||
|
||||
XSetWindowAttributes swa;
|
||||
swa.colormap = colourMap;
|
||||
swa.border_pixel = 0;
|
||||
swa.event_mask = embeddedWindowEventMask;
|
||||
|
||||
auto glBounds = component.getTopLevelComponent()
|
||||
->getLocalArea (&component, component.getLocalBounds());
|
||||
|
||||
glBounds = Desktop::getInstance().getDisplays().logicalToPhysical (glBounds);
|
||||
|
||||
embeddedWindow = X11Symbols::getInstance()->xCreateWindow (display, windowH,
|
||||
glBounds.getX(), glBounds.getY(),
|
||||
(unsigned int) jmax (1, glBounds.getWidth()),
|
||||
(unsigned int) jmax (1, glBounds.getHeight()),
|
||||
0, bestVisual->depth,
|
||||
InputOutput,
|
||||
bestVisual->visual,
|
||||
CWBorderPixel | CWColormap | CWEventMask,
|
||||
&swa);
|
||||
|
||||
X11Symbols::getInstance()->xSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
|
||||
|
||||
X11Symbols::getInstance()->xMapWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xFreeColormap (display, colourMap);
|
||||
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
juce_LinuxAddRepaintListener (peer, &dummy);
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
if (auto* peer = component.getPeer())
|
||||
{
|
||||
juce_LinuxRemoveRepaintListener (peer, &dummy);
|
||||
|
||||
if (embeddedWindow != 0)
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
|
||||
X11Symbols::getInstance()->xUnmapWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xDestroyWindow (display, embeddedWindow);
|
||||
X11Symbols::getInstance()->xSync (display, False);
|
||||
|
||||
XEvent event;
|
||||
while (X11Symbols::getInstance()->xCheckWindowEvent (display,
|
||||
embeddedWindow,
|
||||
embeddedWindowEventMask,
|
||||
&event) == True)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestVisual != nullptr)
|
||||
X11Symbols::getInstance()->xFree (bestVisual);
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext& c)
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
|
||||
c.makeActive();
|
||||
context = &c;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
context = nullptr;
|
||||
deactivateCurrentContext();
|
||||
glXDestroyContext (display, renderContext);
|
||||
renderContext = nullptr;
|
||||
}
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return renderContext != nullptr
|
||||
&& glXMakeCurrent (display, embeddedWindow, renderContext);
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return glXGetCurrentContext() == renderContext && renderContext != nullptr;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
if (auto* display = XWindowSystem::getInstance()->getDisplay())
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
glXMakeCurrent (display, None, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
glXSwapBuffers (display, embeddedWindow);
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> newBounds)
|
||||
{
|
||||
bounds = newBounds;
|
||||
auto physicalBounds = Desktop::getInstance().getDisplays().logicalToPhysical (bounds);
|
||||
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
X11Symbols::getInstance()->xMoveResizeWindow (display, embeddedWindow,
|
||||
physicalBounds.getX(), physicalBounds.getY(),
|
||||
(unsigned int) jmax (1, physicalBounds.getWidth()),
|
||||
(unsigned int) jmax (1, physicalBounds.getHeight()));
|
||||
}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap)
|
||||
{
|
||||
if (numFramesPerSwap == swapFrames)
|
||||
return true;
|
||||
|
||||
if (auto GLXSwapIntervalSGI
|
||||
= (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI"))
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
swapFrames = numFramesPerSwap;
|
||||
GLXSwapIntervalSGI (numFramesPerSwap);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int getSwapInterval() const { return swapFrames; }
|
||||
bool createdOk() const noexcept { return true; }
|
||||
void* getRawContext() const noexcept { return renderContext; }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
void triggerRepaint()
|
||||
{
|
||||
if (context != nullptr)
|
||||
context->triggerRepaint();
|
||||
}
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
private:
|
||||
bool tryChooseVisual (const OpenGLPixelFormat& format, const std::vector<GLint>& optionalAttribs)
|
||||
{
|
||||
std::vector<GLint> allAttribs
|
||||
{
|
||||
GLX_RGBA,
|
||||
GLX_DOUBLEBUFFER,
|
||||
GLX_RED_SIZE, format.redBits,
|
||||
GLX_GREEN_SIZE, format.greenBits,
|
||||
GLX_BLUE_SIZE, format.blueBits,
|
||||
GLX_ALPHA_SIZE, format.alphaBits,
|
||||
GLX_DEPTH_SIZE, format.depthBufferBits,
|
||||
GLX_STENCIL_SIZE, format.stencilBufferBits,
|
||||
GLX_ACCUM_RED_SIZE, format.accumulationBufferRedBits,
|
||||
GLX_ACCUM_GREEN_SIZE, format.accumulationBufferGreenBits,
|
||||
GLX_ACCUM_BLUE_SIZE, format.accumulationBufferBlueBits,
|
||||
GLX_ACCUM_ALPHA_SIZE, format.accumulationBufferAlphaBits
|
||||
};
|
||||
|
||||
allAttribs.insert (allAttribs.end(), optionalAttribs.begin(), optionalAttribs.end());
|
||||
|
||||
allAttribs.push_back (None);
|
||||
|
||||
bestVisual = glXChooseVisual (display, X11Symbols::getInstance()->xDefaultScreen (display), allAttribs.data());
|
||||
|
||||
return bestVisual != nullptr;
|
||||
}
|
||||
|
||||
static constexpr int embeddedWindowEventMask = ExposureMask | StructureNotifyMask;
|
||||
|
||||
Component& component;
|
||||
GLXContext renderContext = {};
|
||||
Window embeddedWindow = {};
|
||||
|
||||
int swapFrames = 1;
|
||||
Rectangle<int> bounds;
|
||||
XVisualInfo* bestVisual = nullptr;
|
||||
void* contextToShareWith;
|
||||
|
||||
OpenGLContext* context = nullptr;
|
||||
DummyComponent dummy;
|
||||
|
||||
::Display* display = nullptr;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
XWindowSystemUtilities::ScopedXLock xLock;
|
||||
return glXGetCurrentContext() != nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,282 +1,296 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
|
||||
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& component,
|
||||
const OpenGLPixelFormat& pixFormat,
|
||||
void* contextToShare,
|
||||
bool shouldUseMultisampling,
|
||||
OpenGLVersion version)
|
||||
{
|
||||
NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
|
||||
createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
|
||||
|
||||
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
|
||||
|
||||
static MouseForwardingNSOpenGLViewClass cls;
|
||||
view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
|
||||
pixelFormat: format];
|
||||
|
||||
if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
|
||||
[view setWantsBestResolutionOpenGLSurface: YES];
|
||||
|
||||
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
|
||||
[[NSNotificationCenter defaultCenter] addObserver: view
|
||||
selector: @selector (_surfaceNeedsUpdate:)
|
||||
name: NSViewGlobalFrameDidChangeNotification
|
||||
object: view];
|
||||
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
|
||||
|
||||
renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
|
||||
shareContext: (NSOpenGLContext*) contextToShare] autorelease];
|
||||
|
||||
[view setOpenGLContext: renderContext];
|
||||
[format release];
|
||||
|
||||
viewAttachment = NSViewComponent::attachViewToComponent (component, view);
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver: view];
|
||||
[renderContext clearDrawable];
|
||||
[renderContext setView: nil];
|
||||
[view setOpenGLContext: nil];
|
||||
[view release];
|
||||
}
|
||||
|
||||
static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
|
||||
const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
|
||||
{
|
||||
ignoreUnused (version);
|
||||
int numAttribs = 0;
|
||||
|
||||
attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile;
|
||||
attribs[numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
|
||||
: NSOpenGLProfileVersionLegacy;
|
||||
|
||||
attribs[numAttribs++] = NSOpenGLPFADoubleBuffer;
|
||||
attribs[numAttribs++] = NSOpenGLPFAClosestPolicy;
|
||||
attribs[numAttribs++] = NSOpenGLPFANoRecovery;
|
||||
attribs[numAttribs++] = NSOpenGLPFAColorSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
|
||||
attribs[numAttribs++] = NSOpenGLPFAAlphaSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFADepthSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFAStencilSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFAAccumSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
|
||||
+ pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
|
||||
|
||||
if (shouldUseMultisampling)
|
||||
{
|
||||
attribs[numAttribs++] = NSOpenGLPFAMultisample;
|
||||
attribs[numAttribs++] = NSOpenGLPFASampleBuffers;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
|
||||
attribs[numAttribs++] = NSOpenGLPFASamples;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
|
||||
}
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
|
||||
void shutdownOnRenderThread() { deactivateCurrentContext(); }
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
jassert (renderContext != nil);
|
||||
|
||||
if ([renderContext view] != view)
|
||||
[renderContext setView: view];
|
||||
|
||||
if (NSOpenGLContext* context = [view openGLContext])
|
||||
{
|
||||
[context makeCurrentContext];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
return [NSOpenGLContext currentContext] == renderContext;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
}
|
||||
|
||||
struct Locker
|
||||
{
|
||||
Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
|
||||
{
|
||||
CGLLockContext (cglContext);
|
||||
}
|
||||
|
||||
~Locker()
|
||||
{
|
||||
CGLUnlockContext (cglContext);
|
||||
}
|
||||
|
||||
private:
|
||||
CGLContextObj cglContext;
|
||||
};
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
auto now = Time::getMillisecondCounterHiRes();
|
||||
[renderContext flushBuffer];
|
||||
|
||||
if (minSwapTimeMs > 0)
|
||||
{
|
||||
// When our window is entirely occluded by other windows, flushBuffer
|
||||
// fails to wait for the swap interval, so the render loop spins at full
|
||||
// speed, burning CPU. This hack detects when things are going too fast
|
||||
// and sleeps if necessary.
|
||||
|
||||
auto swapTime = Time::getMillisecondCounterHiRes() - now;
|
||||
auto frameTime = (int) (now - lastSwapTime);
|
||||
|
||||
if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
|
||||
{
|
||||
if (underrunCounter > 3)
|
||||
{
|
||||
Thread::sleep (2 * (minSwapTimeMs - frameTime));
|
||||
now = Time::getMillisecondCounterHiRes();
|
||||
}
|
||||
else
|
||||
{
|
||||
++underrunCounter;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (underrunCounter > 0)
|
||||
--underrunCounter;
|
||||
}
|
||||
}
|
||||
|
||||
lastSwapTime = now;
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int>) {}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwapIn)
|
||||
{
|
||||
numFramesPerSwap = numFramesPerSwapIn;
|
||||
|
||||
// The macOS OpenGL programming guide says that numFramesPerSwap
|
||||
// can only be 0 or 1.
|
||||
jassert (isPositiveAndBelow (numFramesPerSwap, 2));
|
||||
|
||||
[renderContext setValues: (const GLint*) &numFramesPerSwap
|
||||
#if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
|
||||
forParameter: NSOpenGLContextParameterSwapInterval];
|
||||
#else
|
||||
forParameter: NSOpenGLCPSwapInterval];
|
||||
#endif
|
||||
|
||||
updateMinSwapTime();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int getSwapInterval() const
|
||||
{
|
||||
GLint numFrames = 0;
|
||||
[renderContext getValues: &numFrames
|
||||
#if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
|
||||
forParameter: NSOpenGLContextParameterSwapInterval];
|
||||
#else
|
||||
forParameter: NSOpenGLCPSwapInterval];
|
||||
#endif
|
||||
|
||||
return numFrames;
|
||||
}
|
||||
|
||||
void setNominalVideoRefreshPeriodS (double periodS)
|
||||
{
|
||||
jassert (periodS > 0.0);
|
||||
videoRefreshPeriodS = periodS;
|
||||
updateMinSwapTime();
|
||||
}
|
||||
|
||||
void updateMinSwapTime()
|
||||
{
|
||||
minSwapTimeMs = static_cast<int> (numFramesPerSwap * 1000 * videoRefreshPeriodS);
|
||||
}
|
||||
|
||||
NSOpenGLContext* renderContext = nil;
|
||||
NSOpenGLView* view = nil;
|
||||
ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
|
||||
double lastSwapTime = 0;
|
||||
int minSwapTimeMs = 0, underrunCounter = 0, numFramesPerSwap = 0;
|
||||
double videoRefreshPeriodS = 1.0 / 60.0;
|
||||
|
||||
//==============================================================================
|
||||
struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
|
||||
{
|
||||
MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
|
||||
{
|
||||
addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
|
||||
addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
|
||||
addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
|
||||
|
||||
registerClass();
|
||||
}
|
||||
|
||||
private:
|
||||
static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
|
||||
static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
|
||||
static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
|
||||
};
|
||||
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return CGLGetCurrentContext() != CGLContextObj();
|
||||
}
|
||||
|
||||
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
|
||||
|
||||
class OpenGLContext::NativeContext
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& component,
|
||||
const OpenGLPixelFormat& pixFormat,
|
||||
void* contextToShare,
|
||||
bool shouldUseMultisampling,
|
||||
OpenGLVersion version)
|
||||
: owner (component)
|
||||
{
|
||||
NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
|
||||
createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
|
||||
|
||||
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
|
||||
|
||||
static MouseForwardingNSOpenGLViewClass cls;
|
||||
view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
|
||||
pixelFormat: format];
|
||||
|
||||
if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
|
||||
[view setWantsBestResolutionOpenGLSurface: YES];
|
||||
|
||||
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
|
||||
[[NSNotificationCenter defaultCenter] addObserver: view
|
||||
selector: @selector (_surfaceNeedsUpdate:)
|
||||
name: NSViewGlobalFrameDidChangeNotification
|
||||
object: view];
|
||||
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
|
||||
|
||||
renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
|
||||
shareContext: (NSOpenGLContext*) contextToShare] autorelease];
|
||||
|
||||
[view setOpenGLContext: renderContext];
|
||||
[format release];
|
||||
|
||||
viewAttachment = NSViewComponent::attachViewToComponent (component, view);
|
||||
}
|
||||
|
||||
~NativeContext()
|
||||
{
|
||||
[[NSNotificationCenter defaultCenter] removeObserver: view];
|
||||
[renderContext clearDrawable];
|
||||
[renderContext setView: nil];
|
||||
[view setOpenGLContext: nil];
|
||||
[view release];
|
||||
}
|
||||
|
||||
static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
|
||||
const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
|
||||
{
|
||||
ignoreUnused (version);
|
||||
int numAttribs = 0;
|
||||
|
||||
attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile;
|
||||
attribs[numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
|
||||
: NSOpenGLProfileVersionLegacy;
|
||||
|
||||
attribs[numAttribs++] = NSOpenGLPFADoubleBuffer;
|
||||
attribs[numAttribs++] = NSOpenGLPFAClosestPolicy;
|
||||
attribs[numAttribs++] = NSOpenGLPFANoRecovery;
|
||||
attribs[numAttribs++] = NSOpenGLPFAColorSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
|
||||
attribs[numAttribs++] = NSOpenGLPFAAlphaSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFADepthSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFAStencilSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
|
||||
attribs[numAttribs++] = NSOpenGLPFAAccumSize;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
|
||||
+ pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
|
||||
|
||||
if (shouldUseMultisampling)
|
||||
{
|
||||
attribs[numAttribs++] = NSOpenGLPFAMultisample;
|
||||
attribs[numAttribs++] = NSOpenGLPFASampleBuffers;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
|
||||
attribs[numAttribs++] = NSOpenGLPFASamples;
|
||||
attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
|
||||
}
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext&) { return true; }
|
||||
void shutdownOnRenderThread() { deactivateCurrentContext(); }
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
|
||||
GLuint getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
bool makeActive() const noexcept
|
||||
{
|
||||
jassert (renderContext != nil);
|
||||
|
||||
if ([renderContext view] != view)
|
||||
[renderContext setView: view];
|
||||
|
||||
if (NSOpenGLContext* context = [view openGLContext])
|
||||
{
|
||||
[context makeCurrentContext];
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isActive() const noexcept
|
||||
{
|
||||
return [NSOpenGLContext currentContext] == renderContext;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext()
|
||||
{
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
}
|
||||
|
||||
struct Locker
|
||||
{
|
||||
Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
|
||||
{
|
||||
CGLLockContext (cglContext);
|
||||
}
|
||||
|
||||
~Locker()
|
||||
{
|
||||
CGLUnlockContext (cglContext);
|
||||
}
|
||||
|
||||
private:
|
||||
CGLContextObj cglContext;
|
||||
};
|
||||
|
||||
void swapBuffers()
|
||||
{
|
||||
auto now = Time::getMillisecondCounterHiRes();
|
||||
[renderContext flushBuffer];
|
||||
|
||||
if (minSwapTimeMs > 0)
|
||||
{
|
||||
// When our window is entirely occluded by other windows, flushBuffer
|
||||
// fails to wait for the swap interval, so the render loop spins at full
|
||||
// speed, burning CPU. This hack detects when things are going too fast
|
||||
// and sleeps if necessary.
|
||||
|
||||
auto swapTime = Time::getMillisecondCounterHiRes() - now;
|
||||
auto frameTime = (int) (now - lastSwapTime);
|
||||
|
||||
if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
|
||||
{
|
||||
if (underrunCounter > 3)
|
||||
{
|
||||
Thread::sleep (2 * (minSwapTimeMs - frameTime));
|
||||
now = Time::getMillisecondCounterHiRes();
|
||||
}
|
||||
else
|
||||
{
|
||||
++underrunCounter;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (underrunCounter > 0)
|
||||
--underrunCounter;
|
||||
}
|
||||
}
|
||||
|
||||
lastSwapTime = now;
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int>)
|
||||
{
|
||||
if (auto* peer = owner.getTopLevelComponent()->getPeer())
|
||||
{
|
||||
const auto newArea = peer->getAreaCoveredBy (owner);
|
||||
|
||||
if (convertToRectInt ([view frame]) != newArea)
|
||||
[view setFrame: makeNSRect (newArea)];
|
||||
}
|
||||
}
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwapIn)
|
||||
{
|
||||
numFramesPerSwap = numFramesPerSwapIn;
|
||||
|
||||
// The macOS OpenGL programming guide says that numFramesPerSwap
|
||||
// can only be 0 or 1.
|
||||
jassert (isPositiveAndBelow (numFramesPerSwap, 2));
|
||||
|
||||
[renderContext setValues: (const GLint*) &numFramesPerSwap
|
||||
forParameter: getSwapIntervalParameter()];
|
||||
|
||||
updateMinSwapTime();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int getSwapInterval() const
|
||||
{
|
||||
GLint numFrames = 0;
|
||||
[renderContext getValues: &numFrames
|
||||
forParameter: getSwapIntervalParameter()];
|
||||
|
||||
return numFrames;
|
||||
}
|
||||
|
||||
void setNominalVideoRefreshPeriodS (double periodS)
|
||||
{
|
||||
jassert (periodS > 0.0);
|
||||
videoRefreshPeriodS = periodS;
|
||||
updateMinSwapTime();
|
||||
}
|
||||
|
||||
void updateMinSwapTime()
|
||||
{
|
||||
minSwapTimeMs = static_cast<int> (numFramesPerSwap * 1000 * videoRefreshPeriodS);
|
||||
}
|
||||
|
||||
static NSOpenGLContextParameter getSwapIntervalParameter()
|
||||
{
|
||||
if (@available (macOS 10.12, *))
|
||||
return NSOpenGLContextParameterSwapInterval;
|
||||
|
||||
return NSOpenGLCPSwapInterval;
|
||||
}
|
||||
|
||||
Component& owner;
|
||||
NSOpenGLContext* renderContext = nil;
|
||||
NSOpenGLView* view = nil;
|
||||
ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
|
||||
double lastSwapTime = 0;
|
||||
std::atomic<int> minSwapTimeMs { 0 };
|
||||
int underrunCounter = 0, numFramesPerSwap = 0;
|
||||
double videoRefreshPeriodS = 1.0 / 60.0;
|
||||
|
||||
//==============================================================================
|
||||
struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
|
||||
{
|
||||
MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
|
||||
{
|
||||
addMethod (@selector (rightMouseDown:), rightMouseDown);
|
||||
addMethod (@selector (rightMouseUp:), rightMouseUp);
|
||||
addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse);
|
||||
addMethod (@selector (accessibilityHitTest:), accessibilityHitTest);
|
||||
|
||||
registerClass();
|
||||
}
|
||||
|
||||
private:
|
||||
static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
|
||||
static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
|
||||
static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
|
||||
static id accessibilityHitTest (id self, SEL, NSPoint p) { return [[(NSOpenGLView*) self superview] accessibilityHitTest: p]; }
|
||||
};
|
||||
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return CGLGetCurrentContext() != CGLContextObj();
|
||||
}
|
||||
|
||||
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,329 +1,370 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext : private ComponentPeer::ScaleFactorListener
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& component,
|
||||
const OpenGLPixelFormat& pixelFormat,
|
||||
void* contextToShareWithIn,
|
||||
bool /*useMultisampling*/,
|
||||
OpenGLVersion)
|
||||
{
|
||||
dummyComponent.reset (new DummyComponent (*this));
|
||||
createNativeWindow (component);
|
||||
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
initialisePixelFormatDescriptor (pfd, pixelFormat);
|
||||
|
||||
auto pixFormat = ChoosePixelFormat (dc, &pfd);
|
||||
|
||||
if (pixFormat != 0)
|
||||
SetPixelFormat (dc, pixFormat, &pfd);
|
||||
|
||||
renderContext = wglCreateContext (dc);
|
||||
|
||||
if (renderContext != nullptr)
|
||||
{
|
||||
makeActive();
|
||||
initialiseGLExtensions();
|
||||
|
||||
auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
|
||||
deactivateCurrentContext();
|
||||
|
||||
if (wglFormat != pixFormat && wglFormat != 0)
|
||||
{
|
||||
// can't change the pixel format of a window, so need to delete the
|
||||
// old one and create a new one..
|
||||
releaseDC();
|
||||
nativeWindow = nullptr;
|
||||
createNativeWindow (component);
|
||||
|
||||
if (SetPixelFormat (dc, wglFormat, &pfd))
|
||||
{
|
||||
deleteRenderContext();
|
||||
renderContext = wglCreateContext (dc);
|
||||
}
|
||||
}
|
||||
|
||||
if (contextToShareWithIn != nullptr)
|
||||
wglShareLists ((HGLRC) contextToShareWithIn, renderContext);
|
||||
|
||||
component.getTopLevelComponent()->repaint();
|
||||
component.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
~NativeContext() override
|
||||
{
|
||||
deleteRenderContext();
|
||||
releaseDC();
|
||||
|
||||
if (safeComponent != nullptr)
|
||||
if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
|
||||
peer->removeScaleFactorListener (this);
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext& c)
|
||||
{
|
||||
threadAwarenessSetter = std::make_unique<ScopedThreadDPIAwarenessSetter> (nativeWindow->getNativeHandle());
|
||||
context = &c;
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
deactivateCurrentContext();
|
||||
context = nullptr;
|
||||
threadAwarenessSetter = nullptr;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext() { wglMakeCurrent (nullptr, nullptr); }
|
||||
bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
|
||||
bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
|
||||
void swapBuffers() const noexcept { SwapBuffers (dc); }
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap)
|
||||
{
|
||||
jassert (isActive()); // this can only be called when the context is active..
|
||||
return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
|
||||
}
|
||||
|
||||
int getSwapInterval() const
|
||||
{
|
||||
jassert (isActive()); // this can only be called when the context is active..
|
||||
return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
if (nativeWindow != nullptr)
|
||||
{
|
||||
if (! approximatelyEqual (nativeScaleFactor, 1.0))
|
||||
bounds = (bounds.toDouble() * nativeScaleFactor).toNearestInt();
|
||||
|
||||
SetWindowPos ((HWND) nativeWindow->getNativeHandle(), nullptr,
|
||||
bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
|
||||
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
|
||||
}
|
||||
}
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return renderContext; }
|
||||
unsigned int getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
void triggerRepaint()
|
||||
{
|
||||
if (context != nullptr)
|
||||
context->triggerRepaint();
|
||||
}
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
HWND getNativeHandle()
|
||||
{
|
||||
if (nativeWindow != nullptr)
|
||||
return (HWND) nativeWindow->getNativeHandle();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
struct DummyComponent : public Component
|
||||
{
|
||||
DummyComponent (NativeContext& c) : context (c) {}
|
||||
|
||||
// The windowing code will call this when a paint callback happens
|
||||
void handleCommandMessage (int) override { context.triggerRepaint(); }
|
||||
|
||||
NativeContext& context;
|
||||
};
|
||||
|
||||
std::unique_ptr<DummyComponent> dummyComponent;
|
||||
std::unique_ptr<ComponentPeer> nativeWindow;
|
||||
std::unique_ptr<ScopedThreadDPIAwarenessSetter> threadAwarenessSetter;
|
||||
HGLRC renderContext;
|
||||
HDC dc;
|
||||
OpenGLContext* context = {};
|
||||
|
||||
Component::SafePointer<Component> safeComponent;
|
||||
double nativeScaleFactor = 1.0;
|
||||
|
||||
#define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
|
||||
typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
|
||||
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
|
||||
#undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
|
||||
|
||||
void nativeScaleFactorChanged (double newScaleFactor) override
|
||||
{
|
||||
if (approximatelyEqual (newScaleFactor, nativeScaleFactor)
|
||||
|| safeComponent == nullptr)
|
||||
return;
|
||||
|
||||
if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
|
||||
{
|
||||
nativeScaleFactor = newScaleFactor;
|
||||
updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
|
||||
}
|
||||
}
|
||||
|
||||
void initialiseGLExtensions()
|
||||
{
|
||||
#define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
|
||||
JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
|
||||
JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
|
||||
JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
|
||||
#undef JUCE_INIT_WGL_FUNCTION
|
||||
}
|
||||
|
||||
void createNativeWindow (Component& component)
|
||||
{
|
||||
auto* topComp = component.getTopLevelComponent();
|
||||
|
||||
{
|
||||
auto* parentHWND = topComp->getWindowHandle();
|
||||
|
||||
ScopedThreadDPIAwarenessSetter setter { parentHWND };
|
||||
nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, parentHWND));
|
||||
}
|
||||
|
||||
if (auto* peer = topComp->getPeer())
|
||||
{
|
||||
safeComponent = Component::SafePointer<Component> (&component);
|
||||
|
||||
nativeScaleFactor = peer->getPlatformScaleFactor();
|
||||
updateWindowPosition (peer->getAreaCoveredBy (component));
|
||||
peer->addScaleFactorListener (this);
|
||||
}
|
||||
|
||||
nativeWindow->setVisible (true);
|
||||
dc = GetDC ((HWND) nativeWindow->getNativeHandle());
|
||||
}
|
||||
|
||||
void deleteRenderContext()
|
||||
{
|
||||
if (renderContext != nullptr)
|
||||
{
|
||||
wglDeleteContext (renderContext);
|
||||
renderContext = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseDC()
|
||||
{
|
||||
ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
|
||||
}
|
||||
|
||||
static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
|
||||
{
|
||||
zerostruct (pfd);
|
||||
pfd.nSize = sizeof (pfd);
|
||||
pfd.nVersion = 1;
|
||||
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
|
||||
pfd.cRedBits = (BYTE) pixelFormat.redBits;
|
||||
pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
|
||||
pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
|
||||
pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
|
||||
pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
|
||||
pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
|
||||
pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
|
||||
+ pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
|
||||
pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
|
||||
pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
|
||||
pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
|
||||
pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
|
||||
}
|
||||
|
||||
int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
|
||||
{
|
||||
int format = 0;
|
||||
|
||||
if (wglChoosePixelFormatARB != nullptr)
|
||||
{
|
||||
int atts[64];
|
||||
int n = 0;
|
||||
|
||||
atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
|
||||
atts[n++] = WGL_ACCELERATION_ARB;
|
||||
atts[n++] = WGL_FULL_ACCELERATION_ARB;
|
||||
|
||||
atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
|
||||
atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
|
||||
atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
|
||||
atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
|
||||
atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
|
||||
atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
|
||||
|
||||
atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
|
||||
atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
|
||||
atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
|
||||
atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
|
||||
atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
|
||||
|
||||
if (pixelFormat.multisamplingLevel > 0
|
||||
&& OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
|
||||
{
|
||||
atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
|
||||
atts[n++] = 1;
|
||||
atts[n++] = WGL_SAMPLES_ARB;
|
||||
atts[n++] = pixelFormat.multisamplingLevel;
|
||||
}
|
||||
|
||||
atts[n++] = 0;
|
||||
jassert (n <= numElementsInArray (atts));
|
||||
|
||||
UINT formatsCount = 0;
|
||||
wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return wglGetCurrentContext() != nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLContext::NativeContext : private ComponentPeer::ScaleFactorListener
|
||||
{
|
||||
public:
|
||||
NativeContext (Component& component,
|
||||
const OpenGLPixelFormat& pixelFormat,
|
||||
void* contextToShareWithIn,
|
||||
bool /*useMultisampling*/,
|
||||
OpenGLVersion version)
|
||||
{
|
||||
dummyComponent.reset (new DummyComponent (*this));
|
||||
createNativeWindow (component);
|
||||
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
initialisePixelFormatDescriptor (pfd, pixelFormat);
|
||||
|
||||
auto pixFormat = ChoosePixelFormat (dc, &pfd);
|
||||
|
||||
if (pixFormat != 0)
|
||||
SetPixelFormat (dc, pixFormat, &pfd);
|
||||
|
||||
initialiseWGLExtensions (dc);
|
||||
renderContext = createRenderContext (version, dc);
|
||||
|
||||
if (renderContext != nullptr)
|
||||
{
|
||||
makeActive();
|
||||
|
||||
auto wglFormat = wglChoosePixelFormatExtension (pixelFormat);
|
||||
deactivateCurrentContext();
|
||||
|
||||
if (wglFormat != pixFormat && wglFormat != 0)
|
||||
{
|
||||
// can't change the pixel format of a window, so need to delete the
|
||||
// old one and create a new one..
|
||||
releaseDC();
|
||||
nativeWindow = nullptr;
|
||||
createNativeWindow (component);
|
||||
|
||||
if (SetPixelFormat (dc, wglFormat, &pfd))
|
||||
{
|
||||
deleteRenderContext();
|
||||
renderContext = createRenderContext (version, dc);
|
||||
}
|
||||
}
|
||||
|
||||
if (contextToShareWithIn != nullptr)
|
||||
wglShareLists ((HGLRC) contextToShareWithIn, renderContext);
|
||||
|
||||
component.getTopLevelComponent()->repaint();
|
||||
component.repaint();
|
||||
}
|
||||
}
|
||||
|
||||
~NativeContext() override
|
||||
{
|
||||
deleteRenderContext();
|
||||
releaseDC();
|
||||
|
||||
if (safeComponent != nullptr)
|
||||
if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
|
||||
peer->removeScaleFactorListener (this);
|
||||
}
|
||||
|
||||
bool initialiseOnRenderThread (OpenGLContext& c)
|
||||
{
|
||||
threadAwarenessSetter = std::make_unique<ScopedThreadDPIAwarenessSetter> (nativeWindow->getNativeHandle());
|
||||
context = &c;
|
||||
return true;
|
||||
}
|
||||
|
||||
void shutdownOnRenderThread()
|
||||
{
|
||||
deactivateCurrentContext();
|
||||
context = nullptr;
|
||||
threadAwarenessSetter = nullptr;
|
||||
}
|
||||
|
||||
static void deactivateCurrentContext() { wglMakeCurrent (nullptr, nullptr); }
|
||||
bool makeActive() const noexcept { return isActive() || wglMakeCurrent (dc, renderContext) != FALSE; }
|
||||
bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
|
||||
void swapBuffers() const noexcept { SwapBuffers (dc); }
|
||||
|
||||
bool setSwapInterval (int numFramesPerSwap)
|
||||
{
|
||||
jassert (isActive()); // this can only be called when the context is active..
|
||||
return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
|
||||
}
|
||||
|
||||
int getSwapInterval() const
|
||||
{
|
||||
jassert (isActive()); // this can only be called when the context is active..
|
||||
return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
|
||||
}
|
||||
|
||||
void updateWindowPosition (Rectangle<int> bounds)
|
||||
{
|
||||
if (nativeWindow != nullptr)
|
||||
{
|
||||
if (! approximatelyEqual (nativeScaleFactor, 1.0))
|
||||
bounds = (bounds.toDouble() * nativeScaleFactor).toNearestInt();
|
||||
|
||||
SetWindowPos ((HWND) nativeWindow->getNativeHandle(), nullptr,
|
||||
bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
|
||||
SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
|
||||
}
|
||||
}
|
||||
|
||||
bool createdOk() const noexcept { return getRawContext() != nullptr; }
|
||||
void* getRawContext() const noexcept { return renderContext; }
|
||||
unsigned int getFrameBufferID() const noexcept { return 0; }
|
||||
|
||||
void triggerRepaint()
|
||||
{
|
||||
if (context != nullptr)
|
||||
context->triggerRepaint();
|
||||
}
|
||||
|
||||
struct Locker { Locker (NativeContext&) {} };
|
||||
|
||||
HWND getNativeHandle()
|
||||
{
|
||||
if (nativeWindow != nullptr)
|
||||
return (HWND) nativeWindow->getNativeHandle();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
static void initialiseWGLExtensions (HDC dcIn)
|
||||
{
|
||||
static bool initialised = false;
|
||||
|
||||
if (initialised)
|
||||
return;
|
||||
|
||||
initialised = true;
|
||||
|
||||
const auto dummyContext = wglCreateContext (dcIn);
|
||||
wglMakeCurrent (dcIn, dummyContext);
|
||||
|
||||
#define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
|
||||
JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB)
|
||||
JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT)
|
||||
JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT)
|
||||
JUCE_INIT_WGL_FUNCTION (wglCreateContextAttribsARB)
|
||||
#undef JUCE_INIT_WGL_FUNCTION
|
||||
|
||||
wglMakeCurrent (nullptr, nullptr);
|
||||
wglDeleteContext (dummyContext);
|
||||
}
|
||||
|
||||
static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
|
||||
{
|
||||
zerostruct (pfd);
|
||||
pfd.nSize = sizeof (pfd);
|
||||
pfd.nVersion = 1;
|
||||
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
|
||||
pfd.cRedBits = (BYTE) pixelFormat.redBits;
|
||||
pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
|
||||
pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
|
||||
pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
|
||||
pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
|
||||
pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
|
||||
pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
|
||||
+ pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
|
||||
pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
|
||||
pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
|
||||
pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
|
||||
pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
|
||||
}
|
||||
|
||||
static HGLRC createRenderContext (OpenGLVersion version, HDC dcIn)
|
||||
{
|
||||
if (version >= openGL3_2 && wglCreateContextAttribsARB != nullptr)
|
||||
{
|
||||
const int attribs[] =
|
||||
{
|
||||
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
||||
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
|
||||
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
|
||||
0
|
||||
};
|
||||
|
||||
const auto c = wglCreateContextAttribsARB (dcIn, nullptr, attribs);
|
||||
|
||||
if (c != nullptr)
|
||||
return c;
|
||||
}
|
||||
|
||||
return wglCreateContext (dcIn);
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
struct DummyComponent : public Component
|
||||
{
|
||||
DummyComponent (NativeContext& c) : context (c) {}
|
||||
|
||||
// The windowing code will call this when a paint callback happens
|
||||
void handleCommandMessage (int) override { context.triggerRepaint(); }
|
||||
|
||||
NativeContext& context;
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
void nativeScaleFactorChanged (double newScaleFactor) override
|
||||
{
|
||||
if (approximatelyEqual (newScaleFactor, nativeScaleFactor)
|
||||
|| safeComponent == nullptr)
|
||||
return;
|
||||
|
||||
if (auto* peer = safeComponent->getTopLevelComponent()->getPeer())
|
||||
{
|
||||
nativeScaleFactor = newScaleFactor;
|
||||
updateWindowPosition (peer->getAreaCoveredBy (*safeComponent));
|
||||
}
|
||||
}
|
||||
|
||||
void createNativeWindow (Component& component)
|
||||
{
|
||||
auto* topComp = component.getTopLevelComponent();
|
||||
|
||||
{
|
||||
auto* parentHWND = topComp->getWindowHandle();
|
||||
|
||||
ScopedThreadDPIAwarenessSetter setter { parentHWND };
|
||||
nativeWindow.reset (createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, parentHWND));
|
||||
}
|
||||
|
||||
if (auto* peer = topComp->getPeer())
|
||||
{
|
||||
safeComponent = Component::SafePointer<Component> (&component);
|
||||
|
||||
nativeScaleFactor = peer->getPlatformScaleFactor();
|
||||
updateWindowPosition (peer->getAreaCoveredBy (component));
|
||||
peer->addScaleFactorListener (this);
|
||||
}
|
||||
|
||||
nativeWindow->setVisible (true);
|
||||
dc = GetDC ((HWND) nativeWindow->getNativeHandle());
|
||||
}
|
||||
|
||||
void deleteRenderContext()
|
||||
{
|
||||
if (renderContext != nullptr)
|
||||
{
|
||||
wglDeleteContext (renderContext);
|
||||
renderContext = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void releaseDC()
|
||||
{
|
||||
ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
|
||||
}
|
||||
|
||||
int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
|
||||
{
|
||||
int format = 0;
|
||||
|
||||
if (wglChoosePixelFormatARB != nullptr)
|
||||
{
|
||||
int atts[64];
|
||||
int n = 0;
|
||||
|
||||
atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
|
||||
atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
|
||||
atts[n++] = WGL_ACCELERATION_ARB;
|
||||
atts[n++] = WGL_FULL_ACCELERATION_ARB;
|
||||
|
||||
atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
|
||||
atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
|
||||
atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
|
||||
atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
|
||||
atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
|
||||
atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
|
||||
|
||||
atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
|
||||
atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
|
||||
atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
|
||||
atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
|
||||
atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
|
||||
|
||||
if (pixelFormat.multisamplingLevel > 0
|
||||
&& OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
|
||||
{
|
||||
atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
|
||||
atts[n++] = 1;
|
||||
atts[n++] = WGL_SAMPLES_ARB;
|
||||
atts[n++] = pixelFormat.multisamplingLevel;
|
||||
}
|
||||
|
||||
atts[n++] = 0;
|
||||
jassert (n <= numElementsInArray (atts));
|
||||
|
||||
UINT formatsCount = 0;
|
||||
wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
#define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
|
||||
typedef returnType (__stdcall *type_ ## name) params; static type_ ## name name;
|
||||
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
|
||||
JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglCreateContextAttribsARB, HGLRC, (HDC, HGLRC, const int*))
|
||||
#undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
|
||||
|
||||
//==============================================================================
|
||||
std::unique_ptr<DummyComponent> dummyComponent;
|
||||
std::unique_ptr<ComponentPeer> nativeWindow;
|
||||
std::unique_ptr<ScopedThreadDPIAwarenessSetter> threadAwarenessSetter;
|
||||
Component::SafePointer<Component> safeComponent;
|
||||
HGLRC renderContext;
|
||||
HDC dc;
|
||||
OpenGLContext* context = nullptr;
|
||||
double nativeScaleFactor = 1.0;
|
||||
|
||||
//==============================================================================
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
bool OpenGLHelpers::isContextActive()
|
||||
{
|
||||
return wglGetCurrentContext() != nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,374 +1,368 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLTexture;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an OpenGL context, which can be attached to a component.
|
||||
|
||||
To render some OpenGL, you should create an instance of an OpenGLContext,
|
||||
and call attachTo() to make it use a component as its render target.
|
||||
|
||||
To provide threaded rendering, you can supply an OpenGLRenderer object that
|
||||
will be used to render each frame.
|
||||
|
||||
Before your target component or OpenGLRenderer is deleted, you MUST call
|
||||
detach() or delete the OpenGLContext to allow the background thread to
|
||||
stop and the native resources to be freed safely.
|
||||
|
||||
@see OpenGLRenderer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLContext
|
||||
{
|
||||
public:
|
||||
OpenGLContext();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Gives the context an OpenGLRenderer to use to do the drawing.
|
||||
The object that you give it will not be owned by the context, so it's the caller's
|
||||
responsibility to manage its lifetime and make sure that it doesn't get deleted
|
||||
while the context may be using it. To stop the context using a renderer, just call
|
||||
this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setRenderer (OpenGLRenderer*) noexcept;
|
||||
|
||||
/** Attaches the context to a target component.
|
||||
|
||||
If the component is not fully visible, this call will wait until the component
|
||||
is shown before actually creating a native context for it.
|
||||
|
||||
When a native context is created, a thread is started, and will be used to call
|
||||
the OpenGLRenderer methods. The context will be floated above the target component,
|
||||
and when the target moves, it will track it. If the component is hidden/shown, the
|
||||
context may be deleted and re-created.
|
||||
*/
|
||||
void attachTo (Component&);
|
||||
|
||||
/** Detaches the context from its target component and deletes any native resources.
|
||||
If the context has not been attached, this will do nothing. Otherwise, it will block
|
||||
until the context and its thread have been cleaned up.
|
||||
*/
|
||||
void detach();
|
||||
|
||||
/** Returns true if the context is attached to a component and is on-screen.
|
||||
Note that if you call attachTo() for a non-visible component, this method will
|
||||
return false until the component is made visible.
|
||||
*/
|
||||
bool isAttached() const noexcept;
|
||||
|
||||
/** Returns the component to which this context is currently attached, or nullptr. */
|
||||
Component* getTargetComponent() const noexcept;
|
||||
|
||||
/** If the given component has an OpenGLContext attached, then this will return it. */
|
||||
static OpenGLContext* getContextAttachedTo (Component& component) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Sets the pixel format which you'd like to use for the target GL surface.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
|
||||
|
||||
/** Texture magnification filters, used by setTextureMagnificationFilter(). */
|
||||
enum TextureMagnificationFilter
|
||||
{
|
||||
nearest,
|
||||
linear
|
||||
};
|
||||
|
||||
/** Sets the texture magnification filter. By default the texture magnification
|
||||
filter is linear. However, for faster rendering you may want to use the
|
||||
'nearest' magnification filter. This option will not affect any textures
|
||||
created before this function was called. */
|
||||
void setTextureMagnificationFilter (TextureMagnificationFilter magFilterMode) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Provides a context with which you'd like this context's resources to be shared.
|
||||
The object passed-in here is a platform-dependent native context object, and
|
||||
must not be deleted while this context may still be using it! To turn off sharing,
|
||||
you can call this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
|
||||
|
||||
/** Enables multisampling on platforms where this is implemented.
|
||||
If enabling this, you must call this method before attachTo().
|
||||
*/
|
||||
void setMultisamplingEnabled (bool) noexcept;
|
||||
|
||||
/** Returns true if shaders can be used in this context. */
|
||||
bool areShadersAvailable() const;
|
||||
|
||||
/** HACK option when enabled prevents issues on some mobile opengl drivers */
|
||||
void setMobileBufferBugMitigation(bool flag);
|
||||
bool getMobileBufferBugMitigation() const;
|
||||
|
||||
/** Returns true if non-power-of-two textures are supported in this context. */
|
||||
bool isTextureNpotSupported() const;
|
||||
|
||||
/** OpenGL versions, used by setOpenGLVersionRequired(). */
|
||||
enum OpenGLVersion
|
||||
{
|
||||
defaultGLVersion = 0,
|
||||
openGL3_2
|
||||
};
|
||||
|
||||
/** Sets a preference for the version of GL that this context should use, if possible.
|
||||
Some platforms may ignore this value.
|
||||
*/
|
||||
void setOpenGLVersionRequired (OpenGLVersion) noexcept;
|
||||
|
||||
/** Enables or disables the use of the GL context to perform 2D rendering
|
||||
of the component to which it is attached.
|
||||
If this is false, then only your OpenGLRenderer will be used to perform
|
||||
any rendering. If true, then each time your target's paint() method needs
|
||||
to be called, an OpenGLGraphicsContext will be used to render it, (after
|
||||
calling your OpenGLRenderer if there is one).
|
||||
|
||||
By default this is set to true. If you're not using any paint() method functionality
|
||||
and are doing all your rendering in an OpenGLRenderer, you should disable it
|
||||
to improve performance.
|
||||
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
|
||||
|
||||
/** Enables or disables continuous repainting.
|
||||
If set to true, the context will run a loop, re-rendering itself without waiting
|
||||
for triggerRepaint() to be called, at a frequency determined by the swap interval
|
||||
(see setSwapInterval). If false, then after each render callback, it will wait for
|
||||
another call to triggerRepaint() before rendering again.
|
||||
This is disabled by default.
|
||||
@see setSwapInterval
|
||||
*/
|
||||
void setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept;
|
||||
|
||||
/** Asynchronously causes a repaint to be made. */
|
||||
void triggerRepaint();
|
||||
|
||||
//==============================================================================
|
||||
/** This retrieves an object that was previously stored with setAssociatedObject().
|
||||
If no object is found with the given name, this will return nullptr.
|
||||
This method must only be called from within the GL rendering methods.
|
||||
@see setAssociatedObject
|
||||
*/
|
||||
ReferenceCountedObject* getAssociatedObject (const char* name) const;
|
||||
|
||||
/** Attaches a named object to the context, which will be deleted when the context is
|
||||
destroyed.
|
||||
|
||||
This allows you to store an object which will be released before the context is
|
||||
deleted. The main purpose is for caching GL objects such as shader programs, which
|
||||
will become invalid when the context is deleted.
|
||||
|
||||
This method must only be called from within the GL rendering methods.
|
||||
*/
|
||||
void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
|
||||
|
||||
//==============================================================================
|
||||
/** Makes this context the currently active one.
|
||||
You should never need to call this in normal use - the context will already be
|
||||
active when OpenGLRenderer::renderOpenGL() is invoked.
|
||||
*/
|
||||
bool makeActive() const noexcept;
|
||||
|
||||
/** Returns true if this context is currently active for the calling thread. */
|
||||
bool isActive() const noexcept;
|
||||
|
||||
/** If any context is active on the current thread, this deactivates it.
|
||||
Note that on some platforms, like Android, this isn't possible.
|
||||
*/
|
||||
static void deactivateCurrentContext();
|
||||
|
||||
/** Returns the context that's currently in active use by the calling thread, or
|
||||
nullptr if no context is active.
|
||||
*/
|
||||
static OpenGLContext* getCurrentContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Swaps the buffers (if the context can do this).
|
||||
There's normally no need to call this directly - the buffers will be swapped
|
||||
automatically after your OpenGLRenderer::renderOpenGL() method has been called.
|
||||
*/
|
||||
void swapBuffers();
|
||||
|
||||
/** Sets whether the context checks the vertical sync before swapping.
|
||||
|
||||
The value is the number of frames to allow between buffer-swapping. This is
|
||||
fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
|
||||
and greater numbers indicate that it should swap less often.
|
||||
|
||||
By default, this will be set to 1.
|
||||
|
||||
Returns true if it sets the value successfully - some platforms won't support
|
||||
this setting.
|
||||
|
||||
@see setContinuousRepainting
|
||||
*/
|
||||
bool setSwapInterval (int numFramesPerSwap);
|
||||
|
||||
/** Returns the current swap-sync interval.
|
||||
See setSwapInterval() for info about the value returned.
|
||||
*/
|
||||
int getSwapInterval() const;
|
||||
|
||||
//==============================================================================
|
||||
/** Execute a lambda, function or functor on the OpenGL thread with an active
|
||||
context.
|
||||
|
||||
This method will attempt to execute functor on the OpenGL thread. If
|
||||
blockUntilFinished is true then the method will block until the functor
|
||||
has finished executing.
|
||||
|
||||
This function can only be called if the context is attached to a component.
|
||||
Otherwise, this function will assert.
|
||||
|
||||
This function is useful when you need to execute house-keeping tasks such
|
||||
as allocating, deallocating textures or framebuffers. As such, the functor
|
||||
will execute without locking the message thread. Therefore, it is not
|
||||
intended for any drawing commands or GUI code. Any GUI code should be
|
||||
executed in the OpenGLRenderer::renderOpenGL callback instead.
|
||||
*/
|
||||
template <typename T>
|
||||
void executeOnGLThread (T&& functor, bool blockUntilFinished);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the scale factor used by the display that is being rendered.
|
||||
|
||||
The scale is that of the display - see Displays::Display::scale
|
||||
|
||||
Note that this should only be called during an OpenGLRenderer::renderOpenGL()
|
||||
callback - at other times the value it returns is undefined.
|
||||
*/
|
||||
double getRenderingScale() const noexcept { return currentRenderScale; }
|
||||
|
||||
//==============================================================================
|
||||
/** If this context is backed by a frame buffer, this returns its ID number,
|
||||
or 0 if the context does not use a framebuffer.
|
||||
*/
|
||||
unsigned int getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns an OS-dependent handle to some kind of underlying OS-provided GL context.
|
||||
|
||||
The exact type of the value returned will depend on the OS and may change
|
||||
if the implementation changes. If you want to use this, digging around in the
|
||||
native code is probably the best way to find out what it is.
|
||||
*/
|
||||
void* getRawContext() const noexcept;
|
||||
|
||||
/** This structure holds a set of dynamically loaded GL functions for use on this context. */
|
||||
OpenGLExtensionFunctions extensions;
|
||||
|
||||
//==============================================================================
|
||||
/** Draws the currently selected texture into this context at its original size.
|
||||
|
||||
@param targetClipArea the target area to draw into (in top-left origin coords)
|
||||
@param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
|
||||
anchor position in the target space, and the size must be
|
||||
the total size of the texture.
|
||||
@param contextWidth the width of the context or framebuffer that is being drawn into,
|
||||
used for scaling of the coordinates.
|
||||
@param contextHeight the height of the context or framebuffer that is being drawn into,
|
||||
used for vertical flipping of the y coordinates.
|
||||
@param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
|
||||
(0, 0). If false, it is assumed to be (0, 1)
|
||||
*/
|
||||
void copyTexture (const Rectangle<int>& targetClipArea,
|
||||
const Rectangle<int>& anchorPosAndTextureSize,
|
||||
int contextWidth, int contextHeight,
|
||||
bool textureOriginIsBottomLeft);
|
||||
|
||||
|
||||
/** Changes the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
void setImageCacheSize (size_t cacheSizeBytes) noexcept;
|
||||
|
||||
/** Returns the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
size_t getImageCacheSize() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
class NativeContext;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class OpenGLTexture;
|
||||
|
||||
class CachedImage;
|
||||
class Attachment;
|
||||
NativeContext* nativeContext = nullptr;
|
||||
OpenGLRenderer* renderer = nullptr;
|
||||
double currentRenderScale = 1.0;
|
||||
std::unique_ptr<Attachment> attachment;
|
||||
OpenGLPixelFormat openGLPixelFormat;
|
||||
|
||||
void* contextToShareWith = nullptr;
|
||||
OpenGLVersion versionRequired = defaultGLVersion;
|
||||
size_t imageCacheMaxSize = 8 * 1024 * 1024;
|
||||
bool renderComponents = true, useMultisampling = false, overrideCanAttach = false;
|
||||
std::atomic<bool> continuousRepaint { false };
|
||||
TextureMagnificationFilter texMagFilter = linear;
|
||||
bool mobileBufferBugMitigation = false;
|
||||
|
||||
//==============================================================================
|
||||
struct AsyncWorker : public ReferenceCountedObject
|
||||
{
|
||||
using Ptr = ReferenceCountedObjectPtr<AsyncWorker>;
|
||||
virtual void operator() (OpenGLContext&) = 0;
|
||||
~AsyncWorker() override = default;
|
||||
};
|
||||
|
||||
template <typename FunctionType>
|
||||
struct AsyncWorkerFunctor : public AsyncWorker
|
||||
{
|
||||
AsyncWorkerFunctor (FunctionType functorToUse) : functor (functorToUse) {}
|
||||
void operator() (OpenGLContext& callerContext) override { functor (callerContext); }
|
||||
FunctionType functor;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (AsyncWorkerFunctor)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
CachedImage* getCachedImage() const noexcept;
|
||||
void execute (AsyncWorker::Ptr, bool);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
template <typename FunctionType>
|
||||
void OpenGLContext::executeOnGLThread (FunctionType&& f, bool shouldBlock) { execute (new AsyncWorkerFunctor<FunctionType> (f), shouldBlock); }
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLTexture;
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an OpenGL context, which can be attached to a component.
|
||||
|
||||
To render some OpenGL, you should create an instance of an OpenGLContext,
|
||||
and call attachTo() to make it use a component as its render target.
|
||||
|
||||
To provide threaded rendering, you can supply an OpenGLRenderer object that
|
||||
will be used to render each frame.
|
||||
|
||||
Before your target component or OpenGLRenderer is deleted, you MUST call
|
||||
detach() or delete the OpenGLContext to allow the background thread to
|
||||
stop and the native resources to be freed safely.
|
||||
|
||||
@see OpenGLRenderer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLContext
|
||||
{
|
||||
public:
|
||||
OpenGLContext();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Gives the context an OpenGLRenderer to use to do the drawing.
|
||||
The object that you give it will not be owned by the context, so it's the caller's
|
||||
responsibility to manage its lifetime and make sure that it doesn't get deleted
|
||||
while the context may be using it. To stop the context using a renderer, just call
|
||||
this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setRenderer (OpenGLRenderer*) noexcept;
|
||||
|
||||
/** Attaches the context to a target component.
|
||||
|
||||
If the component is not fully visible, this call will wait until the component
|
||||
is shown before actually creating a native context for it.
|
||||
|
||||
When a native context is created, a thread is started, and will be used to call
|
||||
the OpenGLRenderer methods. The context will be floated above the target component,
|
||||
and when the target moves, it will track it. If the component is hidden/shown, the
|
||||
context may be deleted and re-created.
|
||||
*/
|
||||
void attachTo (Component&);
|
||||
|
||||
/** Detaches the context from its target component and deletes any native resources.
|
||||
If the context has not been attached, this will do nothing. Otherwise, it will block
|
||||
until the context and its thread have been cleaned up.
|
||||
*/
|
||||
void detach();
|
||||
|
||||
/** Returns true if the context is attached to a component and is on-screen.
|
||||
Note that if you call attachTo() for a non-visible component, this method will
|
||||
return false until the component is made visible.
|
||||
*/
|
||||
bool isAttached() const noexcept;
|
||||
|
||||
/** Returns the component to which this context is currently attached, or nullptr. */
|
||||
Component* getTargetComponent() const noexcept;
|
||||
|
||||
/** If the given component has an OpenGLContext attached, then this will return it. */
|
||||
static OpenGLContext* getContextAttachedTo (Component& component) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Sets the pixel format which you'd like to use for the target GL surface.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
|
||||
|
||||
/** Texture magnification filters, used by setTextureMagnificationFilter(). */
|
||||
enum TextureMagnificationFilter
|
||||
{
|
||||
nearest,
|
||||
linear
|
||||
};
|
||||
|
||||
/** Sets the texture magnification filter. By default the texture magnification
|
||||
filter is linear. However, for faster rendering you may want to use the
|
||||
'nearest' magnification filter. This option will not affect any textures
|
||||
created before this function was called. */
|
||||
void setTextureMagnificationFilter (TextureMagnificationFilter magFilterMode) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Provides a context with which you'd like this context's resources to be shared.
|
||||
The object passed-in here is a platform-dependent native context object, and
|
||||
must not be deleted while this context may still be using it! To turn off sharing,
|
||||
you can call this method with a null pointer.
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
|
||||
|
||||
/** Enables multisampling on platforms where this is implemented.
|
||||
If enabling this, you must call this method before attachTo().
|
||||
*/
|
||||
void setMultisamplingEnabled (bool) noexcept;
|
||||
|
||||
/** Returns true if shaders can be used in this context. */
|
||||
bool areShadersAvailable() const;
|
||||
|
||||
/** Returns true if non-power-of-two textures are supported in this context. */
|
||||
bool isTextureNpotSupported() const;
|
||||
|
||||
/** OpenGL versions, used by setOpenGLVersionRequired(). */
|
||||
enum OpenGLVersion
|
||||
{
|
||||
defaultGLVersion = 0,
|
||||
openGL3_2
|
||||
};
|
||||
|
||||
/** Sets a preference for the version of GL that this context should use, if possible.
|
||||
Some platforms may ignore this value.
|
||||
*/
|
||||
void setOpenGLVersionRequired (OpenGLVersion) noexcept;
|
||||
|
||||
/** Enables or disables the use of the GL context to perform 2D rendering
|
||||
of the component to which it is attached.
|
||||
If this is false, then only your OpenGLRenderer will be used to perform
|
||||
any rendering. If true, then each time your target's paint() method needs
|
||||
to be called, an OpenGLGraphicsContext will be used to render it, (after
|
||||
calling your OpenGLRenderer if there is one).
|
||||
|
||||
By default this is set to true. If you're not using any paint() method functionality
|
||||
and are doing all your rendering in an OpenGLRenderer, you should disable it
|
||||
to improve performance.
|
||||
|
||||
Note: This must be called BEFORE attaching your context to a target component!
|
||||
*/
|
||||
void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
|
||||
|
||||
/** Enables or disables continuous repainting.
|
||||
If set to true, the context will run a loop, re-rendering itself without waiting
|
||||
for triggerRepaint() to be called, at a frequency determined by the swap interval
|
||||
(see setSwapInterval). If false, then after each render callback, it will wait for
|
||||
another call to triggerRepaint() before rendering again.
|
||||
This is disabled by default.
|
||||
@see setSwapInterval
|
||||
*/
|
||||
void setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept;
|
||||
|
||||
/** Asynchronously causes a repaint to be made. */
|
||||
void triggerRepaint();
|
||||
|
||||
//==============================================================================
|
||||
/** This retrieves an object that was previously stored with setAssociatedObject().
|
||||
If no object is found with the given name, this will return nullptr.
|
||||
This method must only be called from within the GL rendering methods.
|
||||
@see setAssociatedObject
|
||||
*/
|
||||
ReferenceCountedObject* getAssociatedObject (const char* name) const;
|
||||
|
||||
/** Attaches a named object to the context, which will be deleted when the context is
|
||||
destroyed.
|
||||
|
||||
This allows you to store an object which will be released before the context is
|
||||
deleted. The main purpose is for caching GL objects such as shader programs, which
|
||||
will become invalid when the context is deleted.
|
||||
|
||||
This method must only be called from within the GL rendering methods.
|
||||
*/
|
||||
void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
|
||||
|
||||
//==============================================================================
|
||||
/** Makes this context the currently active one.
|
||||
You should never need to call this in normal use - the context will already be
|
||||
active when OpenGLRenderer::renderOpenGL() is invoked.
|
||||
*/
|
||||
bool makeActive() const noexcept;
|
||||
|
||||
/** Returns true if this context is currently active for the calling thread. */
|
||||
bool isActive() const noexcept;
|
||||
|
||||
/** If any context is active on the current thread, this deactivates it.
|
||||
Note that on some platforms, like Android, this isn't possible.
|
||||
*/
|
||||
static void deactivateCurrentContext();
|
||||
|
||||
/** Returns the context that's currently in active use by the calling thread, or
|
||||
nullptr if no context is active.
|
||||
*/
|
||||
static OpenGLContext* getCurrentContext();
|
||||
|
||||
//==============================================================================
|
||||
/** Swaps the buffers (if the context can do this).
|
||||
There's normally no need to call this directly - the buffers will be swapped
|
||||
automatically after your OpenGLRenderer::renderOpenGL() method has been called.
|
||||
*/
|
||||
void swapBuffers();
|
||||
|
||||
/** Sets whether the context checks the vertical sync before swapping.
|
||||
|
||||
The value is the number of frames to allow between buffer-swapping. This is
|
||||
fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
|
||||
and greater numbers indicate that it should swap less often.
|
||||
|
||||
By default, this will be set to 1.
|
||||
|
||||
Returns true if it sets the value successfully - some platforms won't support
|
||||
this setting.
|
||||
|
||||
@see setContinuousRepainting
|
||||
*/
|
||||
bool setSwapInterval (int numFramesPerSwap);
|
||||
|
||||
/** Returns the current swap-sync interval.
|
||||
See setSwapInterval() for info about the value returned.
|
||||
*/
|
||||
int getSwapInterval() const;
|
||||
|
||||
//==============================================================================
|
||||
/** Execute a lambda, function or functor on the OpenGL thread with an active
|
||||
context.
|
||||
|
||||
This method will attempt to execute functor on the OpenGL thread. If
|
||||
blockUntilFinished is true then the method will block until the functor
|
||||
has finished executing.
|
||||
|
||||
This function can only be called if the context is attached to a component.
|
||||
Otherwise, this function will assert.
|
||||
|
||||
This function is useful when you need to execute house-keeping tasks such
|
||||
as allocating, deallocating textures or framebuffers. As such, the functor
|
||||
will execute without locking the message thread. Therefore, it is not
|
||||
intended for any drawing commands or GUI code. Any GUI code should be
|
||||
executed in the OpenGLRenderer::renderOpenGL callback instead.
|
||||
*/
|
||||
template <typename T>
|
||||
void executeOnGLThread (T&& functor, bool blockUntilFinished);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns the scale factor used by the display that is being rendered.
|
||||
|
||||
The scale is that of the display - see Displays::Display::scale
|
||||
|
||||
Note that this should only be called during an OpenGLRenderer::renderOpenGL()
|
||||
callback - at other times the value it returns is undefined.
|
||||
*/
|
||||
double getRenderingScale() const noexcept { return currentRenderScale; }
|
||||
|
||||
//==============================================================================
|
||||
/** If this context is backed by a frame buffer, this returns its ID number,
|
||||
or 0 if the context does not use a framebuffer.
|
||||
*/
|
||||
unsigned int getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns an OS-dependent handle to some kind of underlying OS-provided GL context.
|
||||
|
||||
The exact type of the value returned will depend on the OS and may change
|
||||
if the implementation changes. If you want to use this, digging around in the
|
||||
native code is probably the best way to find out what it is.
|
||||
*/
|
||||
void* getRawContext() const noexcept;
|
||||
|
||||
/** This structure holds a set of dynamically loaded GL functions for use on this context. */
|
||||
OpenGLExtensionFunctions extensions;
|
||||
|
||||
//==============================================================================
|
||||
/** Draws the currently selected texture into this context at its original size.
|
||||
|
||||
@param targetClipArea the target area to draw into (in top-left origin coords)
|
||||
@param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
|
||||
anchor position in the target space, and the size must be
|
||||
the total size of the texture.
|
||||
@param contextWidth the width of the context or framebuffer that is being drawn into,
|
||||
used for scaling of the coordinates.
|
||||
@param contextHeight the height of the context or framebuffer that is being drawn into,
|
||||
used for vertical flipping of the y coordinates.
|
||||
@param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
|
||||
(0, 0). If false, it is assumed to be (0, 1)
|
||||
*/
|
||||
void copyTexture (const Rectangle<int>& targetClipArea,
|
||||
const Rectangle<int>& anchorPosAndTextureSize,
|
||||
int contextWidth, int contextHeight,
|
||||
bool textureOriginIsBottomLeft);
|
||||
|
||||
|
||||
/** Changes the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
void setImageCacheSize (size_t cacheSizeBytes) noexcept;
|
||||
|
||||
/** Returns the amount of GPU memory that the internal cache for Images is allowed to use. */
|
||||
size_t getImageCacheSize() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
class NativeContext;
|
||||
#endif
|
||||
|
||||
private:
|
||||
friend class OpenGLTexture;
|
||||
|
||||
class CachedImage;
|
||||
class Attachment;
|
||||
NativeContext* nativeContext = nullptr;
|
||||
OpenGLRenderer* renderer = nullptr;
|
||||
double currentRenderScale = 1.0;
|
||||
std::unique_ptr<Attachment> attachment;
|
||||
OpenGLPixelFormat openGLPixelFormat;
|
||||
void* contextToShareWith = nullptr;
|
||||
OpenGLVersion versionRequired = defaultGLVersion;
|
||||
size_t imageCacheMaxSize = 8 * 1024 * 1024;
|
||||
bool renderComponents = true, useMultisampling = false, overrideCanAttach = false;
|
||||
std::atomic<bool> continuousRepaint { false };
|
||||
TextureMagnificationFilter texMagFilter = linear;
|
||||
|
||||
//==============================================================================
|
||||
struct AsyncWorker : public ReferenceCountedObject
|
||||
{
|
||||
using Ptr = ReferenceCountedObjectPtr<AsyncWorker>;
|
||||
virtual void operator() (OpenGLContext&) = 0;
|
||||
~AsyncWorker() override = default;
|
||||
};
|
||||
|
||||
template <typename FunctionType>
|
||||
struct AsyncWorkerFunctor : public AsyncWorker
|
||||
{
|
||||
AsyncWorkerFunctor (FunctionType functorToUse) : functor (functorToUse) {}
|
||||
void operator() (OpenGLContext& callerContext) override { functor (callerContext); }
|
||||
FunctionType functor;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (AsyncWorkerFunctor)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
CachedImage* getCachedImage() const noexcept;
|
||||
void execute (AsyncWorker::Ptr, bool);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
#ifndef DOXYGEN
|
||||
template <typename FunctionType>
|
||||
void OpenGLContext::executeOnGLThread (FunctionType&& f, bool shouldBlock) { execute (new AsyncWorkerFunctor<FunctionType> (f), shouldBlock); }
|
||||
#endif
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,9 +0,0 @@
|
||||
diff a/modules/juce_opengl/opengl/juce_OpenGLContext.h b/modules/juce_opengl/opengl/juce_OpenGLContext.h (rejected hunks)
|
||||
@@ -288,6 +292,7 @@ private:
|
||||
OpenGLVersion versionRequired;
|
||||
size_t imageCacheMaxSize;
|
||||
bool renderComponents, useMultisampling, continuousRepaint;
|
||||
+ bool mobileBufferBugMitigation;
|
||||
|
||||
CachedImage* getCachedImage() const noexcept;
|
||||
|
@ -1,354 +1,354 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBuffer::Pimpl
|
||||
{
|
||||
public:
|
||||
Pimpl (OpenGLContext& c, const int w, const int h,
|
||||
const bool wantsDepthBuffer, const bool wantsStencilBuffer)
|
||||
: context (c), width (w), height (h),
|
||||
textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
|
||||
hasDepthBuffer (false), hasStencilBuffer (false)
|
||||
{
|
||||
// Framebuffer objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD
|
||||
if (context.extensions.glGenFramebuffers == nullptr)
|
||||
return;
|
||||
#endif
|
||||
|
||||
context.extensions.glGenFramebuffers (1, &frameBufferID);
|
||||
bind();
|
||||
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
|
||||
|
||||
if (wantsDepthBuffer || wantsStencilBuffer)
|
||||
{
|
||||
context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
|
||||
context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
|
||||
|
||||
context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
|
||||
(wantsDepthBuffer && wantsStencilBuffer) ? (GLenum) GL_DEPTH24_STENCIL8
|
||||
#if JUCE_OPENGL_ES
|
||||
: (GLenum) GL_DEPTH_COMPONENT16,
|
||||
#else
|
||||
: (GLenum) GL_DEPTH_COMPONENT,
|
||||
#endif
|
||||
width, height);
|
||||
|
||||
GLint params = 0;
|
||||
context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, ¶ms);
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
if (wantsStencilBuffer)
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
hasDepthBuffer = wantsDepthBuffer;
|
||||
hasStencilBuffer = wantsStencilBuffer;
|
||||
}
|
||||
|
||||
unbind();
|
||||
}
|
||||
|
||||
~Pimpl()
|
||||
{
|
||||
if (OpenGLHelpers::isContextActive())
|
||||
{
|
||||
if (textureID != 0)
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
if (depthOrStencilBuffer != 0)
|
||||
context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
|
||||
|
||||
if (frameBufferID != 0)
|
||||
context.extensions.glDeleteFramebuffers (1, &frameBufferID);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
bool createdOk() const
|
||||
{
|
||||
return frameBufferID != 0 && textureID != 0;
|
||||
}
|
||||
|
||||
void bind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
void unbind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
const int width, height;
|
||||
GLuint textureID, frameBufferID, depthOrStencilBuffer;
|
||||
bool hasDepthBuffer, hasStencilBuffer;
|
||||
|
||||
private:
|
||||
bool checkStatus() noexcept
|
||||
{
|
||||
const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
|
||||
|
||||
return status == GL_NO_ERROR
|
||||
|| status == GL_FRAMEBUFFER_COMPLETE;
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Pimpl)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLFrameBuffer::SavedState
|
||||
{
|
||||
public:
|
||||
SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
|
||||
: width (w), height (h),
|
||||
data ((size_t) (w * h))
|
||||
{
|
||||
buffer.readPixels (data, Rectangle<int> (w, h));
|
||||
}
|
||||
|
||||
bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
|
||||
{
|
||||
if (buffer.initialise (context, width, height))
|
||||
{
|
||||
buffer.writePixels (data, Rectangle<int> (width, height));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
const int width, height;
|
||||
HeapBlock<PixelARGB> data;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
OpenGLFrameBuffer::OpenGLFrameBuffer() {}
|
||||
OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
|
||||
{
|
||||
jassert (context.isActive()); // The context must be active when creating a framebuffer!
|
||||
|
||||
pimpl.reset();
|
||||
pimpl.reset (new Pimpl (context, width, height, false, false));
|
||||
|
||||
if (! pimpl->createdOk())
|
||||
pimpl.reset();
|
||||
|
||||
return pimpl != nullptr;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
|
||||
{
|
||||
if (! image.isARGB())
|
||||
return initialise (context, image.convertedToFormat (Image::ARGB));
|
||||
|
||||
Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
|
||||
|
||||
return initialise (context, bitmap.width, bitmap.height)
|
||||
&& writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
|
||||
{
|
||||
auto* p = other.pimpl.get();
|
||||
|
||||
if (p == nullptr)
|
||||
{
|
||||
pimpl.reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
const Rectangle<int> area (pimpl->width, pimpl->height);
|
||||
|
||||
if (initialise (p->context, area.getWidth(), area.getHeight()))
|
||||
{
|
||||
pimpl->bind();
|
||||
|
||||
#if ! JUCE_ANDROID
|
||||
glEnable (GL_TEXTURE_2D);
|
||||
clearGLError();
|
||||
#endif
|
||||
glBindTexture (GL_TEXTURE_2D, p->textureID);
|
||||
pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::release()
|
||||
{
|
||||
pimpl.reset();
|
||||
savedState.reset();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::saveAndRelease()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
{
|
||||
savedState.reset (new SavedState (*this, pimpl->width, pimpl->height));
|
||||
pimpl.reset();
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
|
||||
{
|
||||
if (savedState != nullptr)
|
||||
{
|
||||
std::unique_ptr<SavedState> state;
|
||||
std::swap (state, savedState);
|
||||
|
||||
if (state->restore (context, *this))
|
||||
return true;
|
||||
|
||||
std::swap (state, savedState);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
|
||||
int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
|
||||
GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
|
||||
|
||||
bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
|
||||
{
|
||||
// trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
|
||||
// reloadSavedCopy() to put it back into GPU memory before using it..
|
||||
jassert (savedState == nullptr);
|
||||
|
||||
if (pimpl == nullptr)
|
||||
return false;
|
||||
|
||||
pimpl->bind();
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
|
||||
{
|
||||
return pimpl != nullptr ? pimpl->frameBufferID : 0;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
|
||||
{
|
||||
GLint fb = {};
|
||||
glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
|
||||
return (GLuint) fb;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::releaseAsRenderingTarget()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
pimpl->unbind();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::clear (Colour colour)
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
OpenGLHelpers::clear (colour);
|
||||
releaseAsRenderingTarget();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::makeCurrentAndClear()
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
glClearColor (0, 0, 0, 0);
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
|
||||
{
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glPixelStorei (GL_PACK_ALIGNMENT, 4);
|
||||
glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
|
||||
JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
|
||||
{
|
||||
OpenGLTargetSaver ts (pimpl->context);
|
||||
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glDisable (GL_DEPTH_TEST);
|
||||
glDisable (GL_BLEND);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
OpenGLTexture tex;
|
||||
tex.loadARGB (data, area.getWidth(), area.getHeight());
|
||||
|
||||
glViewport (0, 0, pimpl->width, pimpl->height);
|
||||
pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
|
||||
tex.getWidth(), tex.getHeight()),
|
||||
pimpl->width, pimpl->height, true);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBuffer::Pimpl
|
||||
{
|
||||
public:
|
||||
Pimpl (OpenGLContext& c, const int w, const int h,
|
||||
const bool wantsDepthBuffer, const bool wantsStencilBuffer)
|
||||
: context (c), width (w), height (h),
|
||||
textureID (0), frameBufferID (0), depthOrStencilBuffer (0),
|
||||
hasDepthBuffer (false), hasStencilBuffer (false)
|
||||
{
|
||||
// Framebuffer objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
#if JUCE_WINDOWS || JUCE_LINUX || JUCE_BSD
|
||||
if (context.extensions.glGenFramebuffers == nullptr)
|
||||
return;
|
||||
#endif
|
||||
|
||||
context.extensions.glGenFramebuffers (1, &frameBufferID);
|
||||
bind();
|
||||
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
context.extensions.glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
|
||||
|
||||
if (wantsDepthBuffer || wantsStencilBuffer)
|
||||
{
|
||||
context.extensions.glGenRenderbuffers (1, &depthOrStencilBuffer);
|
||||
context.extensions.glBindRenderbuffer (GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
jassert (context.extensions.glIsRenderbuffer (depthOrStencilBuffer));
|
||||
|
||||
context.extensions.glRenderbufferStorage (GL_RENDERBUFFER,
|
||||
(wantsDepthBuffer && wantsStencilBuffer) ? (GLenum) GL_DEPTH24_STENCIL8
|
||||
#if JUCE_OPENGL_ES
|
||||
: (GLenum) GL_DEPTH_COMPONENT16,
|
||||
#else
|
||||
: (GLenum) GL_DEPTH_COMPONENT,
|
||||
#endif
|
||||
width, height);
|
||||
|
||||
GLint params = 0;
|
||||
context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, ¶ms);
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
if (wantsStencilBuffer)
|
||||
context.extensions.glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthOrStencilBuffer);
|
||||
|
||||
hasDepthBuffer = wantsDepthBuffer;
|
||||
hasStencilBuffer = wantsStencilBuffer;
|
||||
}
|
||||
|
||||
unbind();
|
||||
}
|
||||
|
||||
~Pimpl()
|
||||
{
|
||||
if (OpenGLHelpers::isContextActive())
|
||||
{
|
||||
if (textureID != 0)
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
if (depthOrStencilBuffer != 0)
|
||||
context.extensions.glDeleteRenderbuffers (1, &depthOrStencilBuffer);
|
||||
|
||||
if (frameBufferID != 0)
|
||||
context.extensions.glDeleteFramebuffers (1, &frameBufferID);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
}
|
||||
|
||||
bool createdOk() const
|
||||
{
|
||||
return frameBufferID != 0 && textureID != 0;
|
||||
}
|
||||
|
||||
void bind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, frameBufferID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
void unbind()
|
||||
{
|
||||
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, context.getFrameBufferID());
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
const int width, height;
|
||||
GLuint textureID, frameBufferID, depthOrStencilBuffer;
|
||||
bool hasDepthBuffer, hasStencilBuffer;
|
||||
|
||||
private:
|
||||
bool checkStatus() noexcept
|
||||
{
|
||||
const GLenum status = context.extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER);
|
||||
|
||||
return status == GL_NO_ERROR
|
||||
|| status == GL_FRAMEBUFFER_COMPLETE;
|
||||
}
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Pimpl)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
class OpenGLFrameBuffer::SavedState
|
||||
{
|
||||
public:
|
||||
SavedState (OpenGLFrameBuffer& buffer, const int w, const int h)
|
||||
: width (w), height (h),
|
||||
data ((size_t) (w * h))
|
||||
{
|
||||
buffer.readPixels (data, Rectangle<int> (w, h));
|
||||
}
|
||||
|
||||
bool restore (OpenGLContext& context, OpenGLFrameBuffer& buffer)
|
||||
{
|
||||
if (buffer.initialise (context, width, height))
|
||||
{
|
||||
buffer.writePixels (data, Rectangle<int> (width, height));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
const int width, height;
|
||||
HeapBlock<PixelARGB> data;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
OpenGLFrameBuffer::OpenGLFrameBuffer() {}
|
||||
OpenGLFrameBuffer::~OpenGLFrameBuffer() {}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, int width, int height)
|
||||
{
|
||||
jassert (context.isActive()); // The context must be active when creating a framebuffer!
|
||||
|
||||
pimpl.reset();
|
||||
pimpl.reset (new Pimpl (context, width, height, false, false));
|
||||
|
||||
if (! pimpl->createdOk())
|
||||
pimpl.reset();
|
||||
|
||||
return pimpl != nullptr;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLContext& context, const Image& image)
|
||||
{
|
||||
if (! image.isARGB())
|
||||
return initialise (context, image.convertedToFormat (Image::ARGB));
|
||||
|
||||
Image::BitmapData bitmap (image, Image::BitmapData::readOnly);
|
||||
|
||||
return initialise (context, bitmap.width, bitmap.height)
|
||||
&& writePixels ((const PixelARGB*) bitmap.data, image.getBounds());
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::initialise (OpenGLFrameBuffer& other)
|
||||
{
|
||||
auto* p = other.pimpl.get();
|
||||
|
||||
if (p == nullptr)
|
||||
{
|
||||
pimpl.reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
const Rectangle<int> area (pimpl->width, pimpl->height);
|
||||
|
||||
if (initialise (p->context, area.getWidth(), area.getHeight()))
|
||||
{
|
||||
pimpl->bind();
|
||||
|
||||
#if ! JUCE_ANDROID
|
||||
glEnable (GL_TEXTURE_2D);
|
||||
clearGLError();
|
||||
#endif
|
||||
glBindTexture (GL_TEXTURE_2D, p->textureID);
|
||||
pimpl->context.copyTexture (area, area, area.getWidth(), area.getHeight(), false);
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::release()
|
||||
{
|
||||
pimpl.reset();
|
||||
savedState.reset();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::saveAndRelease()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
{
|
||||
savedState.reset (new SavedState (*this, pimpl->width, pimpl->height));
|
||||
pimpl.reset();
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
|
||||
{
|
||||
if (savedState != nullptr)
|
||||
{
|
||||
std::unique_ptr<SavedState> state;
|
||||
std::swap (state, savedState);
|
||||
|
||||
if (state->restore (context, *this))
|
||||
return true;
|
||||
|
||||
std::swap (state, savedState);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int OpenGLFrameBuffer::getWidth() const noexcept { return pimpl != nullptr ? pimpl->width : 0; }
|
||||
int OpenGLFrameBuffer::getHeight() const noexcept { return pimpl != nullptr ? pimpl->height : 0; }
|
||||
GLuint OpenGLFrameBuffer::getTextureID() const noexcept { return pimpl != nullptr ? pimpl->textureID : 0; }
|
||||
|
||||
bool OpenGLFrameBuffer::makeCurrentRenderingTarget()
|
||||
{
|
||||
// trying to use a framebuffer after saving it with saveAndRelease()! Be sure to call
|
||||
// reloadSavedCopy() to put it back into GPU memory before using it..
|
||||
jassert (savedState == nullptr);
|
||||
|
||||
if (pimpl == nullptr)
|
||||
return false;
|
||||
|
||||
pimpl->bind();
|
||||
return true;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getFrameBufferID() const noexcept
|
||||
{
|
||||
return pimpl != nullptr ? pimpl->frameBufferID : 0;
|
||||
}
|
||||
|
||||
GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget() noexcept
|
||||
{
|
||||
GLint fb = {};
|
||||
glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fb);
|
||||
return (GLuint) fb;
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::releaseAsRenderingTarget()
|
||||
{
|
||||
if (pimpl != nullptr)
|
||||
pimpl->unbind();
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::clear (Colour colour)
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
OpenGLHelpers::clear (colour);
|
||||
releaseAsRenderingTarget();
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLFrameBuffer::makeCurrentAndClear()
|
||||
{
|
||||
if (makeCurrentRenderingTarget())
|
||||
{
|
||||
glClearColor (0, 0, 0, 0);
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::readPixels (PixelARGB* target, const Rectangle<int>& area)
|
||||
{
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glPixelStorei (GL_PACK_ALIGNMENT, 4);
|
||||
glReadPixels (area.getX(), area.getY(), area.getWidth(), area.getHeight(),
|
||||
JUCE_RGBA_FORMAT, GL_UNSIGNED_BYTE, target);
|
||||
|
||||
pimpl->unbind();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLFrameBuffer::writePixels (const PixelARGB* data, const Rectangle<int>& area)
|
||||
{
|
||||
OpenGLTargetSaver ts (pimpl->context);
|
||||
|
||||
if (! makeCurrentRenderingTarget())
|
||||
return false;
|
||||
|
||||
glDisable (GL_DEPTH_TEST);
|
||||
glDisable (GL_BLEND);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
OpenGLTexture tex;
|
||||
tex.loadARGB (data, area.getWidth(), area.getHeight());
|
||||
|
||||
glViewport (0, 0, pimpl->width, pimpl->height);
|
||||
pimpl->context.copyTexture (area, Rectangle<int> (area.getX(), area.getY(),
|
||||
tex.getWidth(), tex.getHeight()),
|
||||
pimpl->width, pimpl->height, true);
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,133 +1,133 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL frame buffer.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLFrameBuffer
|
||||
{
|
||||
public:
|
||||
/** Creates an uninitialised buffer.
|
||||
To actually allocate the buffer, use initialise().
|
||||
*/
|
||||
OpenGLFrameBuffer();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLFrameBuffer();
|
||||
|
||||
//==============================================================================
|
||||
/** Tries to allocates a buffer of the given size.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, int width, int height);
|
||||
|
||||
/** Tries to allocates a buffer containing a copy of a given image.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, const Image& content);
|
||||
|
||||
/** Tries to allocate a copy of another framebuffer.
|
||||
*/
|
||||
bool initialise (OpenGLFrameBuffer& other);
|
||||
|
||||
/** Releases the buffer, if one has been allocated.
|
||||
Any saved state that was created with saveAndRelease() will also be freed by this call.
|
||||
*/
|
||||
void release();
|
||||
|
||||
/** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
|
||||
and will release the GL buffer.
|
||||
After saving, the original state can be restored again by calling reloadSavedCopy().
|
||||
*/
|
||||
void saveAndRelease();
|
||||
|
||||
/** Restores the framebuffer content that was previously saved using saveAndRelease().
|
||||
After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
|
||||
*/
|
||||
bool reloadSavedCopy (OpenGLContext& context);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns true if a valid buffer has been allocated. */
|
||||
bool isValid() const noexcept { return pimpl != nullptr; }
|
||||
|
||||
/** Returns the width of the buffer. */
|
||||
int getWidth() const noexcept;
|
||||
|
||||
/** Returns the height of the buffer. */
|
||||
int getHeight() const noexcept;
|
||||
|
||||
/** Returns the texture ID number for using this buffer as a texture. */
|
||||
GLuint getTextureID() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Selects this buffer as the current OpenGL rendering target. */
|
||||
bool makeCurrentRenderingTarget();
|
||||
|
||||
/** Deselects this buffer as the current OpenGL rendering target. */
|
||||
void releaseAsRenderingTarget();
|
||||
|
||||
/** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
|
||||
GLuint getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns the current frame buffer ID for the current context. */
|
||||
static GLuint getCurrentFrameBufferTarget() noexcept;
|
||||
|
||||
/** Clears the framebuffer with the specified colour. */
|
||||
void clear (Colour colour);
|
||||
|
||||
/** Selects the framebuffer as the current target, and clears it to transparent. */
|
||||
void makeCurrentAndClear();
|
||||
|
||||
/** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
|
||||
|
||||
/** Writes an area of pixels into the framebuffer from a specified pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
|
||||
|
||||
private:
|
||||
class Pimpl;
|
||||
std::unique_ptr<Pimpl> pimpl;
|
||||
|
||||
class SavedState;
|
||||
std::unique_ptr<SavedState> savedState;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL frame buffer.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLFrameBuffer
|
||||
{
|
||||
public:
|
||||
/** Creates an uninitialised buffer.
|
||||
To actually allocate the buffer, use initialise().
|
||||
*/
|
||||
OpenGLFrameBuffer();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLFrameBuffer();
|
||||
|
||||
//==============================================================================
|
||||
/** Tries to allocates a buffer of the given size.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, int width, int height);
|
||||
|
||||
/** Tries to allocates a buffer containing a copy of a given image.
|
||||
Note that a valid openGL context must be selected when you call this method,
|
||||
or it will fail.
|
||||
*/
|
||||
bool initialise (OpenGLContext& context, const Image& content);
|
||||
|
||||
/** Tries to allocate a copy of another framebuffer.
|
||||
*/
|
||||
bool initialise (OpenGLFrameBuffer& other);
|
||||
|
||||
/** Releases the buffer, if one has been allocated.
|
||||
Any saved state that was created with saveAndRelease() will also be freed by this call.
|
||||
*/
|
||||
void release();
|
||||
|
||||
/** If the framebuffer is active, this will save a stashed copy of its contents in main memory,
|
||||
and will release the GL buffer.
|
||||
After saving, the original state can be restored again by calling reloadSavedCopy().
|
||||
*/
|
||||
void saveAndRelease();
|
||||
|
||||
/** Restores the framebuffer content that was previously saved using saveAndRelease().
|
||||
After saving to main memory, the original state can be restored by calling restoreToGPUMemory().
|
||||
*/
|
||||
bool reloadSavedCopy (OpenGLContext& context);
|
||||
|
||||
//==============================================================================
|
||||
/** Returns true if a valid buffer has been allocated. */
|
||||
bool isValid() const noexcept { return pimpl != nullptr; }
|
||||
|
||||
/** Returns the width of the buffer. */
|
||||
int getWidth() const noexcept;
|
||||
|
||||
/** Returns the height of the buffer. */
|
||||
int getHeight() const noexcept;
|
||||
|
||||
/** Returns the texture ID number for using this buffer as a texture. */
|
||||
GLuint getTextureID() const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Selects this buffer as the current OpenGL rendering target. */
|
||||
bool makeCurrentRenderingTarget();
|
||||
|
||||
/** Deselects this buffer as the current OpenGL rendering target. */
|
||||
void releaseAsRenderingTarget();
|
||||
|
||||
/** Returns the ID of this framebuffer, or 0 if it isn't initialised. */
|
||||
GLuint getFrameBufferID() const noexcept;
|
||||
|
||||
/** Returns the current frame buffer ID for the current context. */
|
||||
static GLuint getCurrentFrameBufferTarget() noexcept;
|
||||
|
||||
/** Clears the framebuffer with the specified colour. */
|
||||
void clear (Colour colour);
|
||||
|
||||
/** Selects the framebuffer as the current target, and clears it to transparent. */
|
||||
void makeCurrentAndClear();
|
||||
|
||||
/** Reads an area of pixels from the framebuffer into a 32-bit ARGB pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool readPixels (PixelARGB* targetData, const Rectangle<int>& sourceArea);
|
||||
|
||||
/** Writes an area of pixels into the framebuffer from a specified pixel array.
|
||||
The lineStride is measured as a number of pixels, not bytes - pass a stride
|
||||
of 0 to indicate a packed array.
|
||||
*/
|
||||
bool writePixels (const PixelARGB* srcData, const Rectangle<int>& targetArea);
|
||||
|
||||
private:
|
||||
class Pimpl;
|
||||
std::unique_ptr<Pimpl> pimpl;
|
||||
|
||||
class SavedState;
|
||||
std::unique_ptr<SavedState> savedState;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,96 +1,96 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL target. */
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&, int width, int height);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer. */
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&, OpenGLFrameBuffer&);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer,
|
||||
with the given size.
|
||||
*/
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&,
|
||||
unsigned int frameBufferID,
|
||||
int width, int height);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used to create custom shaders for use with an openGL 2D rendering context.
|
||||
|
||||
Given a GL-based rendering context, you can write a fragment shader that applies some
|
||||
kind of per-pixel effect.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
struct JUCE_API OpenGLGraphicsContextCustomShader
|
||||
{
|
||||
/** Creates a custom shader.
|
||||
|
||||
The shader code will not be compiled until actually needed, so it's OK to call this
|
||||
constructor when no GL context is active.
|
||||
|
||||
The code should be a normal fragment shader. As well as the usual GLSL variables, there is
|
||||
also an automatically declared varying vec2 called "pixelPos", which indicates the pixel
|
||||
position within the graphics context of the pixel being drawn. There is also a varying value
|
||||
"pixelAlpha", which indicates the alpha by which the pixel should be multiplied, so that the
|
||||
edges of any clip-region masks are anti-aliased correctly.
|
||||
*/
|
||||
OpenGLGraphicsContextCustomShader (const String& fragmentShaderCode);
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLGraphicsContextCustomShader();
|
||||
|
||||
/** Returns the program, if it has been linked and is active.
|
||||
This can be called when you're about to use fillRect, to set up any uniforms/textures that
|
||||
the program may require.
|
||||
*/
|
||||
OpenGLShaderProgram* getProgram (LowLevelGraphicsContext&) const;
|
||||
|
||||
/** Applies the shader to a rectangle within the graphics context. */
|
||||
void fillRect (LowLevelGraphicsContext&, Rectangle<int> area) const;
|
||||
|
||||
/** Attempts to compile the program if necessary, and returns an error message if it fails. */
|
||||
Result checkCompilation (LowLevelGraphicsContext&);
|
||||
|
||||
/** Returns the code that was used to create this object. */
|
||||
const String& getFragmentShaderCode() const noexcept { return code; }
|
||||
|
||||
/** Optional lambda that will be called when the shader is activated, to allow
|
||||
user code to do setup tasks.
|
||||
*/
|
||||
std::function<void (OpenGLShaderProgram&)> onShaderActivated;
|
||||
|
||||
private:
|
||||
String code, hashName;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLGraphicsContextCustomShader)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL target. */
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&, int width, int height);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer. */
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&, OpenGLFrameBuffer&);
|
||||
|
||||
/** Creates a graphics context object that will render into the given OpenGL framebuffer,
|
||||
with the given size.
|
||||
*/
|
||||
std::unique_ptr<LowLevelGraphicsContext> createOpenGLGraphicsContext (OpenGLContext&,
|
||||
unsigned int frameBufferID,
|
||||
int width, int height);
|
||||
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Used to create custom shaders for use with an openGL 2D rendering context.
|
||||
|
||||
Given a GL-based rendering context, you can write a fragment shader that applies some
|
||||
kind of per-pixel effect.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
struct JUCE_API OpenGLGraphicsContextCustomShader
|
||||
{
|
||||
/** Creates a custom shader.
|
||||
|
||||
The shader code will not be compiled until actually needed, so it's OK to call this
|
||||
constructor when no GL context is active.
|
||||
|
||||
The code should be a normal fragment shader. As well as the usual GLSL variables, there is
|
||||
also an automatically declared varying vec2 called "pixelPos", which indicates the pixel
|
||||
position within the graphics context of the pixel being drawn. There is also a varying value
|
||||
"pixelAlpha", which indicates the alpha by which the pixel should be multiplied, so that the
|
||||
edges of any clip-region masks are anti-aliased correctly.
|
||||
*/
|
||||
OpenGLGraphicsContextCustomShader (const String& fragmentShaderCode);
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLGraphicsContextCustomShader();
|
||||
|
||||
/** Returns the program, if it has been linked and is active.
|
||||
This can be called when you're about to use fillRect, to set up any uniforms/textures that
|
||||
the program may require.
|
||||
*/
|
||||
OpenGLShaderProgram* getProgram (LowLevelGraphicsContext&) const;
|
||||
|
||||
/** Applies the shader to a rectangle within the graphics context. */
|
||||
void fillRect (LowLevelGraphicsContext&, Rectangle<int> area) const;
|
||||
|
||||
/** Attempts to compile the program if necessary, and returns an error message if it fails. */
|
||||
Result checkCompilation (LowLevelGraphicsContext&);
|
||||
|
||||
/** Returns the code that was used to create this object. */
|
||||
const String& getFragmentShaderCode() const noexcept { return code; }
|
||||
|
||||
/** Optional lambda that will be called when the shader is activated, to allow
|
||||
user code to do setup tasks.
|
||||
*/
|
||||
std::function<void (OpenGLShaderProgram&)> onShaderActivated;
|
||||
|
||||
private:
|
||||
String code, hashName;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLGraphicsContextCustomShader)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,226 +1,246 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class Version
|
||||
{
|
||||
public:
|
||||
constexpr Version() = default;
|
||||
|
||||
constexpr explicit Version (int majorIn)
|
||||
: Version (majorIn, 0) {}
|
||||
|
||||
constexpr Version (int majorIn, int minorIn)
|
||||
: major (majorIn), minor (minorIn) {}
|
||||
|
||||
int major = 0, minor = 0;
|
||||
|
||||
constexpr bool operator== (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() == other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator!= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() != other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator< (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() < other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator<= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() <= other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator> (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() > other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator>= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() >= other.toTuple();
|
||||
}
|
||||
|
||||
private:
|
||||
constexpr std::tuple<int, int> toTuple() const noexcept
|
||||
{
|
||||
return std::make_tuple (major, minor);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Char>
|
||||
static auto* findNullTerminator (const Char* ptr)
|
||||
{
|
||||
while (*ptr != 0)
|
||||
++ptr;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static Version getOpenGLVersion()
|
||||
{
|
||||
const auto* versionBegin = glGetString (GL_VERSION);
|
||||
|
||||
if (versionBegin == nullptr)
|
||||
return {};
|
||||
|
||||
const auto* versionEnd = findNullTerminator (versionBegin);
|
||||
const std::string versionString (versionBegin, versionEnd);
|
||||
const auto spaceSeparated = StringArray::fromTokens (versionString.c_str(), false);
|
||||
|
||||
if (spaceSeparated.isEmpty())
|
||||
return {};
|
||||
|
||||
const auto pointSeparated = StringArray::fromTokens (spaceSeparated[0], ".", "");
|
||||
|
||||
const auto major = pointSeparated[0].getIntValue();
|
||||
const auto minor = pointSeparated[1].getIntValue();
|
||||
|
||||
return { major, minor };
|
||||
}
|
||||
|
||||
void OpenGLHelpers::resetErrorState()
|
||||
{
|
||||
while (glGetError() != GL_NO_ERROR) {}
|
||||
}
|
||||
|
||||
void* OpenGLHelpers::getExtensionFunction (const char* functionName)
|
||||
{
|
||||
#if JUCE_WINDOWS
|
||||
return (void*) wglGetProcAddress (functionName);
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
return (void*) glXGetProcAddress ((const GLubyte*) functionName);
|
||||
#else
|
||||
static void* handle = dlopen (nullptr, RTLD_LAZY);
|
||||
return dlsym (handle, functionName);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
|
||||
{
|
||||
jassert (extensionName != nullptr); // you must supply a genuine string for this.
|
||||
jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
|
||||
|
||||
const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
|
||||
jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const char* found = strstr (extensions, extensionName);
|
||||
|
||||
if (found == nullptr)
|
||||
break;
|
||||
|
||||
extensions = found + strlen (extensionName);
|
||||
|
||||
if (extensions[0] == ' ' || extensions[0] == 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLHelpers::clear (Colour colour)
|
||||
{
|
||||
glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
|
||||
colour.getFloatBlue(), colour.getFloatAlpha());
|
||||
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
|
||||
{
|
||||
glEnable (GL_SCISSOR_TEST);
|
||||
glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
|
||||
}
|
||||
|
||||
String OpenGLHelpers::getGLSLVersionString()
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
{
|
||||
#if JUCE_OPENGL_ES
|
||||
return "#version 300 es";
|
||||
#else
|
||||
return "#version 150";
|
||||
#endif
|
||||
}
|
||||
|
||||
return "#version 110";
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
{
|
||||
String output;
|
||||
|
||||
#if JUCE_ANDROID
|
||||
{
|
||||
int numAttributes = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
numAttributes++;
|
||||
|
||||
int last = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
{
|
||||
output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
|
||||
|
||||
last = p + 10;
|
||||
}
|
||||
|
||||
output += code.substring (last);
|
||||
}
|
||||
#else
|
||||
output = code.replace ("attribute", "in");
|
||||
#endif
|
||||
|
||||
return getGLSLVersionString() + "\n" + output.replace ("varying", "out");
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
return getGLSLVersionString() + "\n"
|
||||
"out " JUCE_MEDIUMP " vec4 fragColor;\n"
|
||||
+ code.replace ("varying", "in")
|
||||
.replace ("texture2D", "texture")
|
||||
.replace ("gl_FragColor", "fragColor");
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class Version
|
||||
{
|
||||
public:
|
||||
constexpr Version() = default;
|
||||
|
||||
constexpr explicit Version (int majorIn)
|
||||
: Version (majorIn, 0) {}
|
||||
|
||||
constexpr Version (int majorIn, int minorIn)
|
||||
: major (majorIn), minor (minorIn) {}
|
||||
|
||||
int major = 0, minor = 0;
|
||||
|
||||
constexpr bool operator== (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() == other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator!= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() != other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator< (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() < other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator<= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() <= other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator> (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() > other.toTuple();
|
||||
}
|
||||
|
||||
constexpr bool operator>= (const Version& other) const noexcept
|
||||
{
|
||||
return toTuple() >= other.toTuple();
|
||||
}
|
||||
|
||||
private:
|
||||
constexpr std::tuple<int, int> toTuple() const noexcept
|
||||
{
|
||||
return std::make_tuple (major, minor);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Char>
|
||||
static auto* findNullTerminator (const Char* ptr)
|
||||
{
|
||||
while (*ptr != 0)
|
||||
++ptr;
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
static Version getOpenGLVersion()
|
||||
{
|
||||
const auto* versionBegin = glGetString (GL_VERSION);
|
||||
|
||||
if (versionBegin == nullptr)
|
||||
return {};
|
||||
|
||||
const auto* versionEnd = findNullTerminator (versionBegin);
|
||||
const std::string versionString (versionBegin, versionEnd);
|
||||
const auto spaceSeparated = StringArray::fromTokens (versionString.c_str(), false);
|
||||
|
||||
for (const auto& token : spaceSeparated)
|
||||
{
|
||||
const auto pointSeparated = StringArray::fromTokens (token, ".", "");
|
||||
|
||||
const auto major = pointSeparated[0].getIntValue();
|
||||
const auto minor = pointSeparated[1].getIntValue();
|
||||
|
||||
if (major != 0)
|
||||
return { major, minor };
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void OpenGLHelpers::resetErrorState()
|
||||
{
|
||||
while (glGetError() != GL_NO_ERROR) {}
|
||||
}
|
||||
|
||||
void* OpenGLHelpers::getExtensionFunction (const char* functionName)
|
||||
{
|
||||
#if JUCE_WINDOWS
|
||||
return (void*) wglGetProcAddress (functionName);
|
||||
#elif JUCE_LINUX || JUCE_BSD
|
||||
return (void*) glXGetProcAddress ((const GLubyte*) functionName);
|
||||
#else
|
||||
static void* handle = dlopen (nullptr, RTLD_LAZY);
|
||||
return dlsym (handle, functionName);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
|
||||
{
|
||||
jassert (extensionName != nullptr); // you must supply a genuine string for this.
|
||||
jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
|
||||
|
||||
if (getOpenGLVersion().major >= 3)
|
||||
{
|
||||
using GetStringi = const GLubyte* (*) (GLenum, GLuint);
|
||||
|
||||
if (auto* thisGlGetStringi = reinterpret_cast<GetStringi> (getExtensionFunction ("glGetStringi")))
|
||||
{
|
||||
GLint n = 0;
|
||||
glGetIntegerv (GL_NUM_EXTENSIONS, &n);
|
||||
|
||||
for (auto i = (decltype (n)) 0; i < n; ++i)
|
||||
if (StringRef (extensionName) == StringRef ((const char*) thisGlGetStringi (GL_EXTENSIONS, (GLuint) i)))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
|
||||
jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const char* found = strstr (extensions, extensionName);
|
||||
|
||||
if (found == nullptr)
|
||||
break;
|
||||
|
||||
extensions = found + strlen (extensionName);
|
||||
|
||||
if (extensions[0] == ' ' || extensions[0] == 0)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void OpenGLHelpers::clear (Colour colour)
|
||||
{
|
||||
glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
|
||||
colour.getFloatBlue(), colour.getFloatAlpha());
|
||||
|
||||
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
}
|
||||
|
||||
void OpenGLHelpers::enableScissorTest (Rectangle<int> clip)
|
||||
{
|
||||
glEnable (GL_SCISSOR_TEST);
|
||||
glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
|
||||
}
|
||||
|
||||
String OpenGLHelpers::getGLSLVersionString()
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
{
|
||||
#if JUCE_OPENGL_ES
|
||||
return "#version 300 es";
|
||||
#else
|
||||
return "#version 150";
|
||||
#endif
|
||||
}
|
||||
|
||||
return "#version 110";
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
{
|
||||
String output;
|
||||
|
||||
#if JUCE_ANDROID
|
||||
{
|
||||
int numAttributes = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
numAttributes++;
|
||||
|
||||
int last = 0;
|
||||
|
||||
for (int p = code.indexOf (0, "attribute "); p >= 0; p = code.indexOf (p + 1, "attribute "))
|
||||
{
|
||||
output += code.substring (last, p) + "layout(location=" + String (--numAttributes) + ") in ";
|
||||
|
||||
last = p + 10;
|
||||
}
|
||||
|
||||
output += code.substring (last);
|
||||
}
|
||||
#else
|
||||
output = code.replace ("attribute", "in");
|
||||
#endif
|
||||
|
||||
return getGLSLVersionString() + "\n" + output.replace ("varying", "out");
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
|
||||
{
|
||||
if (getOpenGLVersion() >= Version (3, 2))
|
||||
return getGLSLVersionString() + "\n"
|
||||
"out " JUCE_MEDIUMP " vec4 fragColor;\n"
|
||||
+ code.replace ("varying", "in")
|
||||
.replace ("texture2D", "texture")
|
||||
.replace ("gl_FragColor", "fragColor");
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,79 +1,79 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A set of miscellaneous openGL helper functions.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLHelpers
|
||||
{
|
||||
public:
|
||||
/** Clears the GL error state. */
|
||||
static void resetErrorState();
|
||||
|
||||
/** Returns true if the current thread has an active OpenGL context. */
|
||||
static bool isContextActive();
|
||||
|
||||
/** Clears the current context using the given colour. */
|
||||
static void clear (Colour colour);
|
||||
|
||||
static void enableScissorTest (Rectangle<int> clip);
|
||||
|
||||
/** Checks whether the current context supports the specified extension. */
|
||||
static bool isExtensionSupported (const char* extensionName);
|
||||
|
||||
/** Returns the address of a named GL extension function */
|
||||
static void* getExtensionFunction (const char* functionName);
|
||||
|
||||
/** Returns a version string such as "#version 150" suitable for prefixing a GLSL
|
||||
shader on this platform.
|
||||
*/
|
||||
static String getGLSLVersionString();
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateVertexShaderToV3 (const String&);
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateFragmentShaderToV3 (const String&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A set of miscellaneous openGL helper functions.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLHelpers
|
||||
{
|
||||
public:
|
||||
/** Clears the GL error state. */
|
||||
static void resetErrorState();
|
||||
|
||||
/** Returns true if the current thread has an active OpenGL context. */
|
||||
static bool isContextActive();
|
||||
|
||||
/** Clears the current context using the given colour. */
|
||||
static void clear (Colour colour);
|
||||
|
||||
static void enableScissorTest (Rectangle<int> clip);
|
||||
|
||||
/** Checks whether the current context supports the specified extension. */
|
||||
static bool isExtensionSupported (const char* extensionName);
|
||||
|
||||
/** Returns the address of a named GL extension function */
|
||||
static void* getExtensionFunction (const char* functionName);
|
||||
|
||||
/** Returns a version string such as "#version 150" suitable for prefixing a GLSL
|
||||
shader on this platform.
|
||||
*/
|
||||
static String getGLSLVersionString();
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateVertexShaderToV3 (const String&);
|
||||
|
||||
/** Makes some simple textual changes to a shader program to try to convert old GLSL
|
||||
keywords to their v3 equivalents.
|
||||
|
||||
Before doing this, the function will check whether the current context is actually
|
||||
using a later version of the language, and if not it will not make any changes.
|
||||
Obviously this is not a real parser, so will only work on simple code!
|
||||
*/
|
||||
static String translateFragmentShaderToV3 (const String&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,214 +1,217 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBufferImage : public ImagePixelData
|
||||
{
|
||||
public:
|
||||
OpenGLFrameBufferImage (OpenGLContext& c, int w, int h)
|
||||
: ImagePixelData (Image::ARGB, w, h),
|
||||
context (c),
|
||||
pixelStride (4),
|
||||
lineStride (width * pixelStride)
|
||||
{
|
||||
}
|
||||
|
||||
bool initialise()
|
||||
{
|
||||
if (! frameBuffer.initialise (context, width, height))
|
||||
return false;
|
||||
|
||||
frameBuffer.clear (Colours::transparentBlack);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<LowLevelGraphicsContext> createLowLevelContext() override
|
||||
{
|
||||
sendDataChangeMessage();
|
||||
return createOpenGLGraphicsContext (context, frameBuffer);
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageType> createType() const override { return std::make_unique<OpenGLImageType>(); }
|
||||
|
||||
ImagePixelData::Ptr clone() override
|
||||
{
|
||||
std::unique_ptr<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (context, width, height));
|
||||
|
||||
if (! im->initialise())
|
||||
return ImagePixelData::Ptr();
|
||||
|
||||
Image newImage (im.release());
|
||||
Graphics g (newImage);
|
||||
g.drawImageAt (Image (*this), 0, 0, false);
|
||||
|
||||
return ImagePixelData::Ptr (newImage.getPixelData());
|
||||
}
|
||||
|
||||
void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
|
||||
{
|
||||
bitmapData.pixelFormat = pixelFormat;
|
||||
bitmapData.lineStride = lineStride;
|
||||
bitmapData.pixelStride = pixelStride;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
default: jassertfalse; break;
|
||||
}
|
||||
|
||||
if (mode != Image::BitmapData::readOnly)
|
||||
sendDataChangeMessage();
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
OpenGLFrameBuffer frameBuffer;
|
||||
|
||||
private:
|
||||
int pixelStride, lineStride;
|
||||
|
||||
struct Dummy
|
||||
{
|
||||
Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
|
||||
static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
|
||||
static void write (const PixelARGB*) noexcept {}
|
||||
};
|
||||
|
||||
struct Reader
|
||||
{
|
||||
static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
|
||||
Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
|
||||
|
||||
verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
|
||||
}
|
||||
|
||||
static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> tempRow (w);
|
||||
auto rowSize = (size_t) w * sizeof (PixelARGB);
|
||||
|
||||
for (int y = 0; y < h / 2; ++y)
|
||||
{
|
||||
PixelARGB* const row1 = data + y * w;
|
||||
PixelARGB* const row2 = data + (h - 1 - y) * w;
|
||||
memcpy (tempRow, row1, rowSize);
|
||||
memcpy (row1, row2, rowSize);
|
||||
memcpy (row2, tempRow, rowSize);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Writer
|
||||
{
|
||||
Writer (OpenGLFrameBuffer& fb, int x, int y, int w, int h) noexcept
|
||||
: frameBuffer (fb), area (x, y, w, h)
|
||||
{}
|
||||
|
||||
void write (const PixelARGB* const data) const noexcept
|
||||
{
|
||||
HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
|
||||
auto rowSize = (size_t) area.getWidth() * sizeof (PixelARGB);
|
||||
|
||||
for (int y = 0; y < area.getHeight(); ++y)
|
||||
memcpy (invertedCopy + area.getWidth() * y,
|
||||
data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
|
||||
|
||||
frameBuffer.writePixels (invertedCopy, area);
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer& frameBuffer;
|
||||
const Rectangle<int> area;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Writer)
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
|
||||
{
|
||||
DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
|
||||
: data ((size_t) (w * h)),
|
||||
writer (fb, x, y, w, h)
|
||||
{}
|
||||
|
||||
~DataReleaser()
|
||||
{
|
||||
writer.write (data);
|
||||
}
|
||||
|
||||
static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
auto* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
|
||||
bitmapData.dataReleaser.reset (r);
|
||||
|
||||
bitmapData.data = (uint8*) r->data.get();
|
||||
bitmapData.lineStride = (bitmapData.width * bitmapData.pixelStride + 3) & ~3;
|
||||
|
||||
ReaderType::read (frameBuffer, bitmapData, x, y);
|
||||
}
|
||||
|
||||
HeapBlock<PixelARGB> data;
|
||||
WriterType writer;
|
||||
};
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
OpenGLImageType::OpenGLImageType() {}
|
||||
OpenGLImageType::~OpenGLImageType() {}
|
||||
|
||||
int OpenGLImageType::getTypeID() const
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
ImagePixelData::Ptr OpenGLImageType::create (Image::PixelFormat, int width, int height, bool /*shouldClearImage*/) const
|
||||
{
|
||||
OpenGLContext* currentContext = OpenGLContext::getCurrentContext();
|
||||
jassert (currentContext != nullptr); // an OpenGL image can only be created when a valid context is active!
|
||||
|
||||
std::unique_ptr<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
|
||||
|
||||
if (! im->initialise())
|
||||
return ImagePixelData::Ptr();
|
||||
|
||||
return *im.release();
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
|
||||
{
|
||||
if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
|
||||
return &(glImage->frameBuffer);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
class OpenGLFrameBufferImage : public ImagePixelData
|
||||
{
|
||||
public:
|
||||
OpenGLFrameBufferImage (OpenGLContext& c, int w, int h)
|
||||
: ImagePixelData (Image::ARGB, w, h),
|
||||
context (c),
|
||||
pixelStride (4),
|
||||
lineStride (width * pixelStride)
|
||||
{
|
||||
}
|
||||
|
||||
bool initialise()
|
||||
{
|
||||
if (! frameBuffer.initialise (context, width, height))
|
||||
return false;
|
||||
|
||||
frameBuffer.clear (Colours::transparentBlack);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<LowLevelGraphicsContext> createLowLevelContext() override
|
||||
{
|
||||
sendDataChangeMessage();
|
||||
return createOpenGLGraphicsContext (context, frameBuffer);
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageType> createType() const override { return std::make_unique<OpenGLImageType>(); }
|
||||
|
||||
ImagePixelData::Ptr clone() override
|
||||
{
|
||||
std::unique_ptr<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (context, width, height));
|
||||
|
||||
if (! im->initialise())
|
||||
return ImagePixelData::Ptr();
|
||||
|
||||
Image newImage (im.release());
|
||||
Graphics g (newImage);
|
||||
g.drawImageAt (Image (*this), 0, 0, false);
|
||||
|
||||
return ImagePixelData::Ptr (newImage.getPixelData());
|
||||
}
|
||||
|
||||
void initialiseBitmapData (Image::BitmapData& bitmapData, int x, int y, Image::BitmapData::ReadWriteMode mode) override
|
||||
{
|
||||
bitmapData.pixelFormat = pixelFormat;
|
||||
bitmapData.lineStride = lineStride;
|
||||
bitmapData.pixelStride = pixelStride;
|
||||
|
||||
switch (mode)
|
||||
{
|
||||
case Image::BitmapData::writeOnly: DataReleaser<Dummy, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readOnly: DataReleaser<Reader, Dummy> ::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
case Image::BitmapData::readWrite: DataReleaser<Reader, Writer>::initialise (frameBuffer, bitmapData, x, y); break;
|
||||
default: jassertfalse; break;
|
||||
}
|
||||
|
||||
if (mode != Image::BitmapData::readOnly)
|
||||
sendDataChangeMessage();
|
||||
}
|
||||
|
||||
OpenGLContext& context;
|
||||
OpenGLFrameBuffer frameBuffer;
|
||||
|
||||
private:
|
||||
int pixelStride, lineStride;
|
||||
|
||||
struct Dummy
|
||||
{
|
||||
Dummy (OpenGLFrameBuffer&, int, int, int, int) noexcept {}
|
||||
static void read (OpenGLFrameBuffer&, Image::BitmapData& , int, int) noexcept {}
|
||||
static void write (const PixelARGB*) noexcept {}
|
||||
};
|
||||
|
||||
struct Reader
|
||||
{
|
||||
static void read (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
frameBuffer.readPixels ((PixelARGB*) bitmapData.data,
|
||||
Rectangle<int> (x, frameBuffer.getHeight() - (y + bitmapData.height), bitmapData.width, bitmapData.height));
|
||||
|
||||
verticalRowFlip ((PixelARGB*) bitmapData.data, bitmapData.width, bitmapData.height);
|
||||
}
|
||||
|
||||
static void verticalRowFlip (PixelARGB* const data, const int w, const int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> tempRow (w);
|
||||
auto rowSize = (size_t) w * sizeof (PixelARGB);
|
||||
|
||||
for (int y = 0; y < h / 2; ++y)
|
||||
{
|
||||
PixelARGB* const row1 = data + y * w;
|
||||
PixelARGB* const row2 = data + (h - 1 - y) * w;
|
||||
memcpy (tempRow, row1, rowSize);
|
||||
memcpy (row1, row2, rowSize);
|
||||
memcpy (row2, tempRow, rowSize);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Writer
|
||||
{
|
||||
Writer (OpenGLFrameBuffer& fb, int x, int y, int w, int h) noexcept
|
||||
: frameBuffer (fb), area (x, y, w, h)
|
||||
{}
|
||||
|
||||
void write (const PixelARGB* const data) const noexcept
|
||||
{
|
||||
HeapBlock<PixelARGB> invertedCopy (area.getWidth() * area.getHeight());
|
||||
auto rowSize = (size_t) area.getWidth() * sizeof (PixelARGB);
|
||||
|
||||
for (int y = 0; y < area.getHeight(); ++y)
|
||||
memcpy (invertedCopy + area.getWidth() * y,
|
||||
data + area.getWidth() * (area.getHeight() - 1 - y), rowSize);
|
||||
|
||||
frameBuffer.writePixels (invertedCopy, area);
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer& frameBuffer;
|
||||
const Rectangle<int> area;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE (Writer)
|
||||
};
|
||||
|
||||
template <class ReaderType, class WriterType>
|
||||
struct DataReleaser : public Image::BitmapData::BitmapDataReleaser
|
||||
{
|
||||
DataReleaser (OpenGLFrameBuffer& fb, int x, int y, int w, int h)
|
||||
: data ((size_t) (w * h)),
|
||||
writer (fb, x, y, w, h)
|
||||
{}
|
||||
|
||||
~DataReleaser()
|
||||
{
|
||||
writer.write (data);
|
||||
}
|
||||
|
||||
static void initialise (OpenGLFrameBuffer& frameBuffer, Image::BitmapData& bitmapData, int x, int y)
|
||||
{
|
||||
auto* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
|
||||
bitmapData.dataReleaser.reset (r);
|
||||
|
||||
bitmapData.data = (uint8*) r->data.get();
|
||||
bitmapData.size = (size_t) bitmapData.width
|
||||
* (size_t) bitmapData.height
|
||||
* sizeof (PixelARGB);
|
||||
bitmapData.lineStride = (bitmapData.width * bitmapData.pixelStride + 3) & ~3;
|
||||
|
||||
ReaderType::read (frameBuffer, bitmapData, x, y);
|
||||
}
|
||||
|
||||
HeapBlock<PixelARGB> data;
|
||||
WriterType writer;
|
||||
};
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBufferImage)
|
||||
};
|
||||
|
||||
|
||||
//==============================================================================
|
||||
OpenGLImageType::OpenGLImageType() {}
|
||||
OpenGLImageType::~OpenGLImageType() {}
|
||||
|
||||
int OpenGLImageType::getTypeID() const
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
ImagePixelData::Ptr OpenGLImageType::create (Image::PixelFormat, int width, int height, bool /*shouldClearImage*/) const
|
||||
{
|
||||
OpenGLContext* currentContext = OpenGLContext::getCurrentContext();
|
||||
jassert (currentContext != nullptr); // an OpenGL image can only be created when a valid context is active!
|
||||
|
||||
std::unique_ptr<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
|
||||
|
||||
if (! im->initialise())
|
||||
return ImagePixelData::Ptr();
|
||||
|
||||
return *im.release();
|
||||
}
|
||||
|
||||
OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
|
||||
{
|
||||
if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
|
||||
return &(glImage->frameBuffer);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,54 +1,54 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A type of ImagePixelData that stores its image data in an OpenGL
|
||||
framebuffer, allowing a JUCE Image object to wrap a framebuffer.
|
||||
|
||||
By creating an Image from an instance of an OpenGLFrameBufferImage,
|
||||
you can then use a Graphics object to draw into the framebuffer using normal
|
||||
JUCE 2D operations.
|
||||
|
||||
@see Image, ImageType, ImagePixelData, OpenGLFrameBuffer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLImageType : public ImageType
|
||||
{
|
||||
public:
|
||||
OpenGLImageType();
|
||||
~OpenGLImageType() override;
|
||||
|
||||
ImagePixelData::Ptr create (Image::PixelFormat, int width, int height, bool shouldClearImage) const override;
|
||||
int getTypeID() const override;
|
||||
|
||||
static OpenGLFrameBuffer* getFrameBufferFrom (const Image&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A type of ImagePixelData that stores its image data in an OpenGL
|
||||
framebuffer, allowing a JUCE Image object to wrap a framebuffer.
|
||||
|
||||
By creating an Image from an instance of an OpenGLFrameBufferImage,
|
||||
you can then use a Graphics object to draw into the framebuffer using normal
|
||||
JUCE 2D operations.
|
||||
|
||||
@see Image, ImageType, ImagePixelData, OpenGLFrameBuffer
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLImageType : public ImageType
|
||||
{
|
||||
public:
|
||||
OpenGLImageType();
|
||||
~OpenGLImageType() override;
|
||||
|
||||
ImagePixelData::Ptr create (Image::PixelFormat, int width, int height, bool shouldClearImage) const override;
|
||||
int getTypeID() const override;
|
||||
|
||||
static OpenGLFrameBuffer* getFrameBufferFrom (const Image&);
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,72 +1,72 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLPixelFormat::OpenGLPixelFormat (const int bitsPerRGBComponent,
|
||||
const int alphaBits_,
|
||||
const int depthBufferBits_,
|
||||
const int stencilBufferBits_) noexcept
|
||||
: redBits (bitsPerRGBComponent),
|
||||
greenBits (bitsPerRGBComponent),
|
||||
blueBits (bitsPerRGBComponent),
|
||||
alphaBits (alphaBits_),
|
||||
depthBufferBits (depthBufferBits_),
|
||||
stencilBufferBits (stencilBufferBits_),
|
||||
accumulationBufferRedBits (0),
|
||||
accumulationBufferGreenBits (0),
|
||||
accumulationBufferBlueBits (0),
|
||||
accumulationBufferAlphaBits (0),
|
||||
multisamplingLevel (0)
|
||||
{
|
||||
}
|
||||
|
||||
static auto tie (const OpenGLPixelFormat& fmt)
|
||||
{
|
||||
return std::tie (fmt.redBits,
|
||||
fmt.greenBits,
|
||||
fmt.blueBits,
|
||||
fmt.alphaBits,
|
||||
fmt.depthBufferBits,
|
||||
fmt.stencilBufferBits,
|
||||
fmt.accumulationBufferRedBits,
|
||||
fmt.accumulationBufferGreenBits,
|
||||
fmt.accumulationBufferBlueBits,
|
||||
fmt.accumulationBufferAlphaBits,
|
||||
fmt.multisamplingLevel);
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return tie (*this) == tie (other);
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator!= (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLPixelFormat::OpenGLPixelFormat (const int bitsPerRGBComponent,
|
||||
const int alphaBits_,
|
||||
const int depthBufferBits_,
|
||||
const int stencilBufferBits_) noexcept
|
||||
: redBits (bitsPerRGBComponent),
|
||||
greenBits (bitsPerRGBComponent),
|
||||
blueBits (bitsPerRGBComponent),
|
||||
alphaBits (alphaBits_),
|
||||
depthBufferBits (depthBufferBits_),
|
||||
stencilBufferBits (stencilBufferBits_),
|
||||
accumulationBufferRedBits (0),
|
||||
accumulationBufferGreenBits (0),
|
||||
accumulationBufferBlueBits (0),
|
||||
accumulationBufferAlphaBits (0),
|
||||
multisamplingLevel (0)
|
||||
{
|
||||
}
|
||||
|
||||
static auto tie (const OpenGLPixelFormat& fmt)
|
||||
{
|
||||
return std::tie (fmt.redBits,
|
||||
fmt.greenBits,
|
||||
fmt.blueBits,
|
||||
fmt.alphaBits,
|
||||
fmt.depthBufferBits,
|
||||
fmt.stencilBufferBits,
|
||||
fmt.accumulationBufferRedBits,
|
||||
fmt.accumulationBufferGreenBits,
|
||||
fmt.accumulationBufferBlueBits,
|
||||
fmt.accumulationBufferAlphaBits,
|
||||
fmt.multisamplingLevel);
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return tie (*this) == tie (other);
|
||||
}
|
||||
|
||||
bool OpenGLPixelFormat::operator!= (const OpenGLPixelFormat& other) const noexcept
|
||||
{
|
||||
return ! operator== (other);
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,71 +1,71 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Represents the various properties of an OpenGL pixel format.
|
||||
|
||||
@see OpenGLContext::setPixelFormat
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLPixelFormat
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an OpenGLPixelFormat.
|
||||
|
||||
The default constructor just initialises the object as a simple 8-bit
|
||||
RGBA format.
|
||||
*/
|
||||
OpenGLPixelFormat (int bitsPerRGBComponent = 8,
|
||||
int alphaBits = 8,
|
||||
int depthBufferBits = 16,
|
||||
int stencilBufferBits = 0) noexcept;
|
||||
|
||||
bool operator== (const OpenGLPixelFormat&) const noexcept;
|
||||
bool operator!= (const OpenGLPixelFormat&) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
int redBits; /**< The number of bits per pixel to use for the red channel. */
|
||||
int greenBits; /**< The number of bits per pixel to use for the green channel. */
|
||||
int blueBits; /**< The number of bits per pixel to use for the blue channel. */
|
||||
int alphaBits; /**< The number of bits per pixel to use for the alpha channel. */
|
||||
|
||||
int depthBufferBits; /**< The number of bits per pixel to use for a depth buffer. */
|
||||
int stencilBufferBits; /**< The number of bits per pixel to use for a stencil buffer. */
|
||||
|
||||
int accumulationBufferRedBits; /**< The number of bits per pixel to use for an accumulation buffer's red channel. */
|
||||
int accumulationBufferGreenBits; /**< The number of bits per pixel to use for an accumulation buffer's green channel. */
|
||||
int accumulationBufferBlueBits; /**< The number of bits per pixel to use for an accumulation buffer's blue channel. */
|
||||
int accumulationBufferAlphaBits; /**< The number of bits per pixel to use for an accumulation buffer's alpha channel. */
|
||||
|
||||
uint8 multisamplingLevel; /**< The number of samples to use for full-scene multisampled anti-aliasing (if available). */
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Represents the various properties of an OpenGL pixel format.
|
||||
|
||||
@see OpenGLContext::setPixelFormat
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLPixelFormat
|
||||
{
|
||||
public:
|
||||
//==============================================================================
|
||||
/** Creates an OpenGLPixelFormat.
|
||||
|
||||
The default constructor just initialises the object as a simple 8-bit
|
||||
RGBA format.
|
||||
*/
|
||||
OpenGLPixelFormat (int bitsPerRGBComponent = 8,
|
||||
int alphaBits = 8,
|
||||
int depthBufferBits = 16,
|
||||
int stencilBufferBits = 0) noexcept;
|
||||
|
||||
bool operator== (const OpenGLPixelFormat&) const noexcept;
|
||||
bool operator!= (const OpenGLPixelFormat&) const noexcept;
|
||||
|
||||
//==============================================================================
|
||||
int redBits; /**< The number of bits per pixel to use for the red channel. */
|
||||
int greenBits; /**< The number of bits per pixel to use for the green channel. */
|
||||
int blueBits; /**< The number of bits per pixel to use for the blue channel. */
|
||||
int alphaBits; /**< The number of bits per pixel to use for the alpha channel. */
|
||||
|
||||
int depthBufferBits; /**< The number of bits per pixel to use for a depth buffer. */
|
||||
int stencilBufferBits; /**< The number of bits per pixel to use for a stencil buffer. */
|
||||
|
||||
int accumulationBufferRedBits; /**< The number of bits per pixel to use for an accumulation buffer's red channel. */
|
||||
int accumulationBufferGreenBits; /**< The number of bits per pixel to use for an accumulation buffer's green channel. */
|
||||
int accumulationBufferBlueBits; /**< The number of bits per pixel to use for an accumulation buffer's blue channel. */
|
||||
int accumulationBufferAlphaBits; /**< The number of bits per pixel to use for an accumulation buffer's alpha channel. */
|
||||
|
||||
uint8 multisamplingLevel; /**< The number of samples to use for full-scene multisampled anti-aliasing (if available). */
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,88 +1,88 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class that should be implemented by classes which want to render openGL
|
||||
on a background thread.
|
||||
|
||||
@see OpenGLContext
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLRenderer
|
||||
{
|
||||
public:
|
||||
OpenGLRenderer() = default;
|
||||
virtual ~OpenGLRenderer() = default;
|
||||
|
||||
/** Called when a new GL context has been created.
|
||||
You can use this as an opportunity to create your textures, shaders, etc.
|
||||
When the method is invoked, the new GL context will be active.
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
*/
|
||||
virtual void newOpenGLContextCreated() = 0;
|
||||
|
||||
/** Called when you should render the next openGL frame.
|
||||
|
||||
Note that this callback will be made on a background thread.
|
||||
|
||||
If the context is attached to a component in order to do component rendering,
|
||||
then the MessageManager will be locked when this callback is made.
|
||||
|
||||
If no component rendering is being done, then the MessageManager will not be
|
||||
locked, and you'll need to make sure your code is thread-safe in any
|
||||
interactions it has with your GUI classes.
|
||||
|
||||
For information about how to trigger a render callback, see
|
||||
OpenGLContext::triggerRepaint() and OpenGLContext::setContinuousRepainting().
|
||||
|
||||
IMPORTANT: Never take a MessageManagerLock inside this function! On
|
||||
macOS, the OpenGL context will be locked for the duration of this call.
|
||||
The main thread may also attempt to interact with the OpenGL context at
|
||||
any time, which will also require locking the OpenGL context. As a
|
||||
result, taking a MessageManagerLock inside renderOpenGL() may cause a
|
||||
hierarchical deadlock.
|
||||
*/
|
||||
virtual void renderOpenGL() = 0;
|
||||
|
||||
/** Called when the current openGL context is about to close.
|
||||
You can use this opportunity to release any GL resources that you may have
|
||||
created.
|
||||
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
|
||||
(Also note that on Android, this callback won't happen, because there's currently
|
||||
no way to implement it..)
|
||||
*/
|
||||
virtual void openGLContextClosing() = 0;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class that should be implemented by classes which want to render openGL
|
||||
on a background thread.
|
||||
|
||||
@see OpenGLContext
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLRenderer
|
||||
{
|
||||
public:
|
||||
OpenGLRenderer() = default;
|
||||
virtual ~OpenGLRenderer() = default;
|
||||
|
||||
/** Called when a new GL context has been created.
|
||||
You can use this as an opportunity to create your textures, shaders, etc.
|
||||
When the method is invoked, the new GL context will be active.
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
*/
|
||||
virtual void newOpenGLContextCreated() = 0;
|
||||
|
||||
/** Called when you should render the next openGL frame.
|
||||
|
||||
Note that this callback will be made on a background thread.
|
||||
|
||||
If the context is attached to a component in order to do component rendering,
|
||||
then the MessageManager will be locked when this callback is made.
|
||||
|
||||
If no component rendering is being done, then the MessageManager will not be
|
||||
locked, and you'll need to make sure your code is thread-safe in any
|
||||
interactions it has with your GUI classes.
|
||||
|
||||
For information about how to trigger a render callback, see
|
||||
OpenGLContext::triggerRepaint() and OpenGLContext::setContinuousRepainting().
|
||||
|
||||
IMPORTANT: Never take a MessageManagerLock inside this function! On
|
||||
macOS, the OpenGL context will be locked for the duration of this call.
|
||||
The main thread may also attempt to interact with the OpenGL context at
|
||||
any time, which will also require locking the OpenGL context. As a
|
||||
result, taking a MessageManagerLock inside renderOpenGL() may cause a
|
||||
hierarchical deadlock.
|
||||
*/
|
||||
virtual void renderOpenGL() = 0;
|
||||
|
||||
/** Called when the current openGL context is about to close.
|
||||
You can use this opportunity to release any GL resources that you may have
|
||||
created.
|
||||
|
||||
Note that this callback will be made on a background thread, so make sure
|
||||
that your implementation is thread-safe.
|
||||
|
||||
(Also note that on Android, this callback won't happen, because there's currently
|
||||
no way to implement it..)
|
||||
*/
|
||||
virtual void openGLContextClosing() = 0;
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,192 +1,192 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept : context (c)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
GLuint OpenGLShaderProgram::getProgramID() const noexcept
|
||||
{
|
||||
if (programID == 0)
|
||||
{
|
||||
// This method should only be called when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
programID = context.extensions.glCreateProgram();
|
||||
}
|
||||
|
||||
return programID;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::release() noexcept
|
||||
{
|
||||
if (programID != 0)
|
||||
{
|
||||
context.extensions.glDeleteProgram (programID);
|
||||
programID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double OpenGLShaderProgram::getLanguageVersion()
|
||||
{
|
||||
return String::fromUTF8 ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
|
||||
.retainCharacters("1234567890.").getDoubleValue();
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addShader (const String& code, GLenum type)
|
||||
{
|
||||
GLuint shaderID = context.extensions.glCreateShader (type);
|
||||
|
||||
const GLchar* c = code.toRawUTF8();
|
||||
context.extensions.glShaderSource (shaderID, 1, &c, nullptr);
|
||||
|
||||
context.extensions.glCompileShader (shaderID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (status == (GLint) GL_FALSE)
|
||||
{
|
||||
std::vector<GLchar> infoLog (16384);
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetShaderInfoLog (shaderID, (GLsizei) infoLog.size(), &infoLogLength, infoLog.data());
|
||||
errorLog = String (infoLog.data(), (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained compile errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
context.extensions.glAttachShader (getProgramID(), shaderID);
|
||||
context.extensions.glDeleteShader (shaderID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addVertexShader (const String& code) { return addShader (code, GL_VERTEX_SHADER); }
|
||||
bool OpenGLShaderProgram::addFragmentShader (const String& code) { return addShader (code, GL_FRAGMENT_SHADER); }
|
||||
|
||||
bool OpenGLShaderProgram::link() noexcept
|
||||
{
|
||||
// This method can only be used when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
GLuint progID = getProgramID();
|
||||
|
||||
context.extensions.glLinkProgram (progID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetProgramiv (progID, GL_LINK_STATUS, &status);
|
||||
|
||||
if (status == (GLint) GL_FALSE)
|
||||
{
|
||||
std::vector<GLchar> infoLog (16384);
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetProgramInfoLog (progID, (GLsizei) infoLog.size(), &infoLogLength, infoLog.data());
|
||||
errorLog = String (infoLog.data(), (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained link errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return status != (GLint) GL_FALSE;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::use() const noexcept
|
||||
{
|
||||
// The shader program must have been successfully linked when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
context.extensions.glUseProgram (programID);
|
||||
}
|
||||
|
||||
GLint OpenGLShaderProgram::getUniformIDFromName (const char* uniformName) const noexcept
|
||||
{
|
||||
// The shader program must be active when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
return (GLint) context.extensions.glGetUniformLocation (programID, uniformName);
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1) noexcept { context.extensions.glUniform1f (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1) noexcept { context.extensions.glUniform1i (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2) noexcept { context.extensions.glUniform2f (getUniformIDFromName (name), n1, n2); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3) noexcept { context.extensions.glUniform3f (getUniformIDFromName (name), n1, n2, n3); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) noexcept { context.extensions.glUniform4f (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1, GLint n2, GLint n3, GLint n4) noexcept { context.extensions.glUniform4i (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, const GLfloat* values, GLsizei numValues) noexcept { context.extensions.glUniform1fv (getUniformIDFromName (name), numValues, values); }
|
||||
void OpenGLShaderProgram::setUniformMat2 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix2fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat3 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix3fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat4 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix4fv (getUniformIDFromName (name), num, trns, v); }
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
|
||||
: attributeID ((GLuint) program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert ((GLint) attributeID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
|
||||
: uniformID (program.context.extensions.glGetUniformLocation (program.getProgramID(), name)), context (program.context)
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert (uniformID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
|
||||
|
||||
void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept : context (c)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
GLuint OpenGLShaderProgram::getProgramID() const noexcept
|
||||
{
|
||||
if (programID == 0)
|
||||
{
|
||||
// This method should only be called when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
programID = context.extensions.glCreateProgram();
|
||||
}
|
||||
|
||||
return programID;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::release() noexcept
|
||||
{
|
||||
if (programID != 0)
|
||||
{
|
||||
context.extensions.glDeleteProgram (programID);
|
||||
programID = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double OpenGLShaderProgram::getLanguageVersion()
|
||||
{
|
||||
return String::fromUTF8 ((const char*) glGetString (GL_SHADING_LANGUAGE_VERSION))
|
||||
.retainCharacters("1234567890.").getDoubleValue();
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addShader (const String& code, GLenum type)
|
||||
{
|
||||
GLuint shaderID = context.extensions.glCreateShader (type);
|
||||
|
||||
const GLchar* c = code.toRawUTF8();
|
||||
context.extensions.glShaderSource (shaderID, 1, &c, nullptr);
|
||||
|
||||
context.extensions.glCompileShader (shaderID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetShaderiv (shaderID, GL_COMPILE_STATUS, &status);
|
||||
|
||||
if (status == (GLint) GL_FALSE)
|
||||
{
|
||||
std::vector<GLchar> infoLog (16384);
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetShaderInfoLog (shaderID, (GLsizei) infoLog.size(), &infoLogLength, infoLog.data());
|
||||
errorLog = String (infoLog.data(), (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained compile errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
context.extensions.glAttachShader (getProgramID(), shaderID);
|
||||
context.extensions.glDeleteShader (shaderID);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OpenGLShaderProgram::addVertexShader (const String& code) { return addShader (code, GL_VERTEX_SHADER); }
|
||||
bool OpenGLShaderProgram::addFragmentShader (const String& code) { return addShader (code, GL_FRAGMENT_SHADER); }
|
||||
|
||||
bool OpenGLShaderProgram::link() noexcept
|
||||
{
|
||||
// This method can only be used when the current thread has an active OpenGL context.
|
||||
jassert (OpenGLHelpers::isContextActive());
|
||||
|
||||
GLuint progID = getProgramID();
|
||||
|
||||
context.extensions.glLinkProgram (progID);
|
||||
|
||||
GLint status = GL_FALSE;
|
||||
context.extensions.glGetProgramiv (progID, GL_LINK_STATUS, &status);
|
||||
|
||||
if (status == (GLint) GL_FALSE)
|
||||
{
|
||||
std::vector<GLchar> infoLog (16384);
|
||||
GLsizei infoLogLength = 0;
|
||||
context.extensions.glGetProgramInfoLog (progID, (GLsizei) infoLog.size(), &infoLogLength, infoLog.data());
|
||||
errorLog = String (infoLog.data(), (size_t) infoLogLength);
|
||||
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
// Your GLSL code contained link errors!
|
||||
// Hopefully this compile log should help to explain what went wrong.
|
||||
DBG (errorLog);
|
||||
jassertfalse;
|
||||
#endif
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
return status != (GLint) GL_FALSE;
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::use() const noexcept
|
||||
{
|
||||
// The shader program must have been successfully linked when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
context.extensions.glUseProgram (programID);
|
||||
}
|
||||
|
||||
GLint OpenGLShaderProgram::getUniformIDFromName (const char* uniformName) const noexcept
|
||||
{
|
||||
// The shader program must be active when this method is called!
|
||||
jassert (programID != 0);
|
||||
|
||||
return (GLint) context.extensions.glGetUniformLocation (programID, uniformName);
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1) noexcept { context.extensions.glUniform1f (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1) noexcept { context.extensions.glUniform1i (getUniformIDFromName (name), n1); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2) noexcept { context.extensions.glUniform2f (getUniformIDFromName (name), n1, n2); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3) noexcept { context.extensions.glUniform3f (getUniformIDFromName (name), n1, n2, n3); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) noexcept { context.extensions.glUniform4f (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, GLint n1, GLint n2, GLint n3, GLint n4) noexcept { context.extensions.glUniform4i (getUniformIDFromName (name), n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::setUniform (const char* name, const GLfloat* values, GLsizei numValues) noexcept { context.extensions.glUniform1fv (getUniformIDFromName (name), numValues, values); }
|
||||
void OpenGLShaderProgram::setUniformMat2 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix2fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat3 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix3fv (getUniformIDFromName (name), num, trns, v); }
|
||||
void OpenGLShaderProgram::setUniformMat4 (const char* name, const GLfloat* v, GLint num, GLboolean trns) noexcept { context.extensions.glUniformMatrix4fv (getUniformIDFromName (name), num, trns, v); }
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
|
||||
: attributeID ((GLuint) program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert ((GLint) attributeID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
OpenGLShaderProgram::Uniform::Uniform (const OpenGLShaderProgram& program, const char* const name)
|
||||
: uniformID (program.context.extensions.glGetUniformLocation (program.getProgramID(), name)), context (program.context)
|
||||
{
|
||||
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
|
||||
jassert (uniformID >= 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1) const noexcept { context.extensions.glUniform1f (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1) const noexcept { context.extensions.glUniform1i (uniformID, n1); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2) const noexcept { context.extensions.glUniform2f (uniformID, n1, n2); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept { context.extensions.glUniform3f (uniformID, n1, n2, n3); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) const noexcept { context.extensions.glUniform4f (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept { context.extensions.glUniform4i (uniformID, n1, n2, n3, n4); }
|
||||
void OpenGLShaderProgram::Uniform::set (const GLfloat* values, GLsizei numValues) const noexcept { context.extensions.glUniform1fv (uniformID, numValues, values); }
|
||||
|
||||
void OpenGLShaderProgram::Uniform::setMatrix2 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix2fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix3 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix3fv (uniformID, num, trns, v); }
|
||||
void OpenGLShaderProgram::Uniform::setMatrix4 (const GLfloat* v, GLint num, GLboolean trns) const noexcept { context.extensions.glUniformMatrix4fv (uniformID, num, trns, v); }
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,204 +1,204 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Manages an OpenGL shader program.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLShaderProgram
|
||||
{
|
||||
public:
|
||||
/** Creates a shader for use in a particular GL context. */
|
||||
OpenGLShaderProgram (const OpenGLContext&) noexcept;
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLShaderProgram() noexcept;
|
||||
|
||||
/** Returns the version of GLSL that the current context supports.
|
||||
E.g.
|
||||
@code
|
||||
if (OpenGLShaderProgram::getLanguageVersion() > 1.199)
|
||||
{
|
||||
// ..do something that requires GLSL 1.2 or above..
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
static double getLanguageVersion();
|
||||
|
||||
/** Compiles and adds a shader to this program.
|
||||
|
||||
After adding all your shaders, remember to call link() to link them into
|
||||
a usable program.
|
||||
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to compile correctly.
|
||||
|
||||
The shaderType parameter could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc.
|
||||
|
||||
@returns true if the shader compiled successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool addShader (const String& shaderSourceCode, GLenum shaderType);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_VERTEX_SHADER.
|
||||
*/
|
||||
bool addVertexShader (const String& shaderSourceCode);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_FRAGMENT_SHADER.
|
||||
*/
|
||||
bool addFragmentShader (const String& shaderSourceCode);
|
||||
|
||||
/** Links all the compiled shaders into a usable program.
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to link correctly.
|
||||
@returns true if the program linked successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool link() noexcept;
|
||||
|
||||
/** Get the output for the last shader compilation or link that failed. */
|
||||
const String& getLastError() const noexcept { return errorLog; }
|
||||
|
||||
/** Selects this program into the current context. */
|
||||
void use() const noexcept;
|
||||
|
||||
/** Deletes the program. */
|
||||
void release() noexcept;
|
||||
|
||||
//==============================================================================
|
||||
// Methods for setting shader uniforms without using a Uniform object (see below).
|
||||
// You must make sure this shader is the currently bound one before setting uniforms
|
||||
// with these functions.
|
||||
|
||||
/** Get the uniform ID from the variable name */
|
||||
GLint getUniformIDFromName (const char* uniformName) const noexcept;
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat value) noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void setUniform (const char* uniformName, GLint value) noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y) noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z, GLfloat w) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLint x, GLint y, GLint z, GLint w) noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void setUniform (const char* uniformName, const GLfloat* values, GLsizei numValues) noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setUniformMat2 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setUniformMat3 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setUniformMat4 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL uniform value.
|
||||
After a program has been linked, you can create Uniform objects to let you
|
||||
set the uniforms that your shaders use.
|
||||
|
||||
Be careful not to call the set() functions unless the appropriate program
|
||||
is loaded into the current context.
|
||||
*/
|
||||
struct JUCE_API Uniform
|
||||
{
|
||||
/** Initialises a uniform.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Uniform (const OpenGLShaderProgram& program, const char* uniformName);
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void set (GLfloat n1) const noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void set (GLint n1) const noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2) const noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) const noexcept;
|
||||
/** Sets an ivec4 uniform. */
|
||||
void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void set (const GLfloat* values, int numValues) const noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
|
||||
/** The uniform's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLint uniformID;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL vertex attribute value.
|
||||
After a program has been linked, you can create Attribute objects to let you
|
||||
set the attributes that your vertex shaders use.
|
||||
*/
|
||||
struct JUCE_API Attribute
|
||||
{
|
||||
/** Initialises an attribute.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Attribute (const OpenGLShaderProgram&, const char* attributeName);
|
||||
|
||||
/** The attribute's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLuint attributeID;
|
||||
};
|
||||
|
||||
/** The ID number of the compiled program. */
|
||||
GLuint getProgramID() const noexcept;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
mutable GLuint programID = 0;
|
||||
String errorLog;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Manages an OpenGL shader program.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLShaderProgram
|
||||
{
|
||||
public:
|
||||
/** Creates a shader for use in a particular GL context. */
|
||||
OpenGLShaderProgram (const OpenGLContext&) noexcept;
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLShaderProgram() noexcept;
|
||||
|
||||
/** Returns the version of GLSL that the current context supports.
|
||||
E.g.
|
||||
@code
|
||||
if (OpenGLShaderProgram::getLanguageVersion() > 1.199)
|
||||
{
|
||||
// ..do something that requires GLSL 1.2 or above..
|
||||
}
|
||||
@endcode
|
||||
*/
|
||||
static double getLanguageVersion();
|
||||
|
||||
/** Compiles and adds a shader to this program.
|
||||
|
||||
After adding all your shaders, remember to call link() to link them into
|
||||
a usable program.
|
||||
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to compile correctly.
|
||||
|
||||
The shaderType parameter could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc.
|
||||
|
||||
@returns true if the shader compiled successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool addShader (const String& shaderSourceCode, GLenum shaderType);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_VERTEX_SHADER.
|
||||
*/
|
||||
bool addVertexShader (const String& shaderSourceCode);
|
||||
|
||||
/** Compiles and adds a fragment shader to this program.
|
||||
This is equivalent to calling addShader() with a type of GL_FRAGMENT_SHADER.
|
||||
*/
|
||||
bool addFragmentShader (const String& shaderSourceCode);
|
||||
|
||||
/** Links all the compiled shaders into a usable program.
|
||||
If your app is built in debug mode, this method will assert if the program
|
||||
fails to link correctly.
|
||||
@returns true if the program linked successfully. If not, you can call
|
||||
getLastError() to find out what happened.
|
||||
*/
|
||||
bool link() noexcept;
|
||||
|
||||
/** Get the output for the last shader compilation or link that failed. */
|
||||
const String& getLastError() const noexcept { return errorLog; }
|
||||
|
||||
/** Selects this program into the current context. */
|
||||
void use() const noexcept;
|
||||
|
||||
/** Deletes the program. */
|
||||
void release() noexcept;
|
||||
|
||||
//==============================================================================
|
||||
// Methods for setting shader uniforms without using a Uniform object (see below).
|
||||
// You must make sure this shader is the currently bound one before setting uniforms
|
||||
// with these functions.
|
||||
|
||||
/** Get the uniform ID from the variable name */
|
||||
GLint getUniformIDFromName (const char* uniformName) const noexcept;
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat value) noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void setUniform (const char* uniformName, GLint value) noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y) noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLfloat x, GLfloat y, GLfloat z, GLfloat w) noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void setUniform (const char* uniformName, GLint x, GLint y, GLint z, GLint w) noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void setUniform (const char* uniformName, const GLfloat* values, GLsizei numValues) noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setUniformMat2 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setUniformMat3 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setUniformMat4 (const char* uniformName, const GLfloat* values, GLint count, GLboolean transpose) noexcept;
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL uniform value.
|
||||
After a program has been linked, you can create Uniform objects to let you
|
||||
set the uniforms that your shaders use.
|
||||
|
||||
Be careful not to call the set() functions unless the appropriate program
|
||||
is loaded into the current context.
|
||||
*/
|
||||
struct JUCE_API Uniform
|
||||
{
|
||||
/** Initialises a uniform.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Uniform (const OpenGLShaderProgram& program, const char* uniformName);
|
||||
|
||||
/** Sets a float uniform. */
|
||||
void set (GLfloat n1) const noexcept;
|
||||
/** Sets an int uniform. */
|
||||
void set (GLint n1) const noexcept;
|
||||
/** Sets a vec2 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2) const noexcept;
|
||||
/** Sets a vec3 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3) const noexcept;
|
||||
/** Sets a vec4 uniform. */
|
||||
void set (GLfloat n1, GLfloat n2, GLfloat n3, GLfloat n4) const noexcept;
|
||||
/** Sets an ivec4 uniform. */
|
||||
void set (GLint n1, GLint n2, GLint n3, GLint n4) const noexcept;
|
||||
/** Sets a vector float uniform. */
|
||||
void set (const GLfloat* values, int numValues) const noexcept;
|
||||
/** Sets a 2x2 matrix float uniform. */
|
||||
void setMatrix2 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 3x3 matrix float uniform. */
|
||||
void setMatrix3 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
/** Sets a 4x4 matrix float uniform. */
|
||||
void setMatrix4 (const GLfloat* values, GLint count, GLboolean transpose) const noexcept;
|
||||
|
||||
/** The uniform's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLint uniformID;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Uniform)
|
||||
};
|
||||
|
||||
//==============================================================================
|
||||
/** Represents an openGL vertex attribute value.
|
||||
After a program has been linked, you can create Attribute objects to let you
|
||||
set the attributes that your vertex shaders use.
|
||||
*/
|
||||
struct JUCE_API Attribute
|
||||
{
|
||||
/** Initialises an attribute.
|
||||
The program must have been successfully linked when this
|
||||
constructor is called.
|
||||
*/
|
||||
Attribute (const OpenGLShaderProgram&, const char* attributeName);
|
||||
|
||||
/** The attribute's ID number.
|
||||
If the uniform couldn't be found, this value will be < 0.
|
||||
*/
|
||||
GLuint attributeID;
|
||||
};
|
||||
|
||||
/** The ID number of the compiled program. */
|
||||
GLuint getProgramID() const noexcept;
|
||||
|
||||
private:
|
||||
const OpenGLContext& context;
|
||||
mutable GLuint programID = 0;
|
||||
String errorLog;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,192 +1,192 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLTexture::OpenGLTexture()
|
||||
: textureID (0), width (0), height (0), ownerContext (nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLTexture::~OpenGLTexture()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
bool OpenGLTexture::isValidSize (int width, int height)
|
||||
{
|
||||
return isPowerOfTwo (width) && isPowerOfTwo (height);
|
||||
}
|
||||
|
||||
void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
|
||||
{
|
||||
ownerContext = OpenGLContext::getCurrentContext();
|
||||
|
||||
// Texture objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (ownerContext != nullptr);
|
||||
|
||||
if (textureID == 0)
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
auto glMagFilter = (GLint) (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR;
|
||||
}
|
||||
|
||||
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
const auto textureNpotSupported = ownerContext->isTextureNpotSupported();
|
||||
|
||||
const auto getAllowedTextureSize = [&] (int n)
|
||||
{
|
||||
return textureNpotSupported ? n : nextPowerOfTwo (n);
|
||||
};
|
||||
|
||||
width = getAllowedTextureSize (w);
|
||||
height = getAllowedTextureSize (h);
|
||||
|
||||
const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
|
||||
|
||||
if (width != w || height != h)
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
|
||||
|
||||
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
|
||||
type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
template <class PixelType>
|
||||
struct Flipper
|
||||
{
|
||||
static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
|
||||
const int w, const int h)
|
||||
{
|
||||
dataCopy.malloc (w * h);
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
auto* src = (const PixelType*) srcData;
|
||||
auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
|
||||
|
||||
for (int x = 0; x < w; ++x)
|
||||
dst[x].set (src[x]);
|
||||
|
||||
srcData += lineStride;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void OpenGLTexture::loadImage (const Image& image)
|
||||
{
|
||||
const int imageW = image.getWidth();
|
||||
const int imageH = image.getHeight();
|
||||
|
||||
HeapBlock<PixelARGB> dataCopy;
|
||||
Image::BitmapData srcData (image, Image::BitmapData::readOnly);
|
||||
|
||||
switch (srcData.pixelFormat)
|
||||
{
|
||||
case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::UnknownFormat:
|
||||
default: break;
|
||||
}
|
||||
|
||||
create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
|
||||
{
|
||||
create (w, h, pixels, JUCE_RGBA_FORMAT, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
|
||||
{
|
||||
create (w, h, pixels, GL_ALPHA, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> flippedCopy;
|
||||
Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
|
||||
|
||||
create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::release()
|
||||
{
|
||||
if (textureID != 0)
|
||||
{
|
||||
// If the texture is deleted while the owner context is not active, it's
|
||||
// impossible to delete it, so this will be a leak until the context itself
|
||||
// is deleted.
|
||||
jassert (ownerContext == OpenGLContext::getCurrentContext());
|
||||
|
||||
if (ownerContext == OpenGLContext::getCurrentContext())
|
||||
{
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
textureID = 0;
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTexture::bind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
}
|
||||
|
||||
void OpenGLTexture::unbind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLTexture::OpenGLTexture()
|
||||
: textureID (0), width (0), height (0), ownerContext (nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
OpenGLTexture::~OpenGLTexture()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
bool OpenGLTexture::isValidSize (int width, int height)
|
||||
{
|
||||
return isPowerOfTwo (width) && isPowerOfTwo (height);
|
||||
}
|
||||
|
||||
void OpenGLTexture::create (const int w, const int h, const void* pixels, GLenum type, bool topLeft)
|
||||
{
|
||||
ownerContext = OpenGLContext::getCurrentContext();
|
||||
|
||||
// Texture objects can only be created when the current thread has an active OpenGL
|
||||
// context. You'll need to create this object in one of the OpenGLContext's callbacks.
|
||||
jassert (ownerContext != nullptr);
|
||||
|
||||
if (textureID == 0)
|
||||
{
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
glGenTextures (1, &textureID);
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
auto glMagFilter = (GLint) (ownerContext->texMagFilter == OpenGLContext::linear ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilter);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
else
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
JUCE_CHECK_OPENGL_ERROR;
|
||||
}
|
||||
|
||||
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
|
||||
const auto textureNpotSupported = ownerContext->isTextureNpotSupported();
|
||||
|
||||
const auto getAllowedTextureSize = [&] (int n)
|
||||
{
|
||||
return textureNpotSupported ? n : nextPowerOfTwo (n);
|
||||
};
|
||||
|
||||
width = getAllowedTextureSize (w);
|
||||
height = getAllowedTextureSize (h);
|
||||
|
||||
const GLint internalformat = type == GL_ALPHA ? GL_ALPHA : GL_RGBA;
|
||||
|
||||
if (width != w || height != h)
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
width, height, 0, type, GL_UNSIGNED_BYTE, nullptr);
|
||||
|
||||
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, topLeft ? (height - h) : 0, w, h,
|
||||
type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D (GL_TEXTURE_2D, 0, internalformat,
|
||||
w, h, 0, type, GL_UNSIGNED_BYTE, pixels);
|
||||
}
|
||||
|
||||
JUCE_CHECK_OPENGL_ERROR
|
||||
}
|
||||
|
||||
template <class PixelType>
|
||||
struct Flipper
|
||||
{
|
||||
static void flip (HeapBlock<PixelARGB>& dataCopy, const uint8* srcData, const int lineStride,
|
||||
const int w, const int h)
|
||||
{
|
||||
dataCopy.malloc (w * h);
|
||||
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
auto* src = (const PixelType*) srcData;
|
||||
auto* dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
|
||||
|
||||
for (int x = 0; x < w; ++x)
|
||||
dst[x].set (src[x]);
|
||||
|
||||
srcData += lineStride;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void OpenGLTexture::loadImage (const Image& image)
|
||||
{
|
||||
const int imageW = image.getWidth();
|
||||
const int imageH = image.getHeight();
|
||||
|
||||
HeapBlock<PixelARGB> dataCopy;
|
||||
Image::BitmapData srcData (image, Image::BitmapData::readOnly);
|
||||
|
||||
switch (srcData.pixelFormat)
|
||||
{
|
||||
case Image::ARGB: Flipper<PixelARGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::RGB: Flipper<PixelRGB> ::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::SingleChannel: Flipper<PixelAlpha>::flip (dataCopy, srcData.data, srcData.lineStride, imageW, imageH); break;
|
||||
case Image::UnknownFormat:
|
||||
default: break;
|
||||
}
|
||||
|
||||
create (imageW, imageH, dataCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGB (const PixelARGB* pixels, const int w, const int h)
|
||||
{
|
||||
create (w, h, pixels, JUCE_RGBA_FORMAT, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadAlpha (const uint8* pixels, int w, int h)
|
||||
{
|
||||
create (w, h, pixels, GL_ALPHA, false);
|
||||
}
|
||||
|
||||
void OpenGLTexture::loadARGBFlipped (const PixelARGB* pixels, int w, int h)
|
||||
{
|
||||
HeapBlock<PixelARGB> flippedCopy;
|
||||
Flipper<PixelARGB>::flip (flippedCopy, (const uint8*) pixels, 4 * w, w, h);
|
||||
|
||||
create (w, h, flippedCopy, JUCE_RGBA_FORMAT, true);
|
||||
}
|
||||
|
||||
void OpenGLTexture::release()
|
||||
{
|
||||
if (textureID != 0)
|
||||
{
|
||||
// If the texture is deleted while the owner context is not active, it's
|
||||
// impossible to delete it, so this will be a leak until the context itself
|
||||
// is deleted.
|
||||
jassert (ownerContext == OpenGLContext::getCurrentContext());
|
||||
|
||||
if (ownerContext == OpenGLContext::getCurrentContext())
|
||||
{
|
||||
glDeleteTextures (1, &textureID);
|
||||
|
||||
textureID = 0;
|
||||
width = 0;
|
||||
height = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLTexture::bind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, textureID);
|
||||
}
|
||||
|
||||
void OpenGLTexture::unbind() const
|
||||
{
|
||||
glBindTexture (GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,104 +1,104 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL texture from an Image.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLTexture
|
||||
{
|
||||
public:
|
||||
OpenGLTexture();
|
||||
~OpenGLTexture();
|
||||
|
||||
/** Creates a texture from the given image.
|
||||
|
||||
Note that if the image's dimensions aren't a power-of-two, the texture may
|
||||
be created with a larger size.
|
||||
|
||||
The image will be arranged so that its top-left corner is at texture
|
||||
coordinate (0, 1).
|
||||
*/
|
||||
void loadImage (const Image& image);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadARGB (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
This is like loadARGB, but will vertically flip the data so that the first
|
||||
pixel ends up at texture coordinate (0, 1), and if the width and height are
|
||||
not powers-of-two, it will compensate by using a larger texture size.
|
||||
*/
|
||||
void loadARGBFlipped (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates an alpha-channel texture from an array of alpha values.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadAlpha (const uint8* pixels, int width, int height);
|
||||
|
||||
/** Frees the texture, if there is one. */
|
||||
void release();
|
||||
|
||||
/** Binds the texture to the currently active openGL context. */
|
||||
void bind() const;
|
||||
|
||||
/** Unbinds the texture to the currently active openGL context. */
|
||||
void unbind() const;
|
||||
|
||||
/** Returns the GL texture ID number. */
|
||||
GLuint getTextureID() const noexcept { return textureID; }
|
||||
|
||||
int getWidth() const noexcept { return width; }
|
||||
int getHeight() const noexcept { return height; }
|
||||
|
||||
/** Returns true if a texture can be created with the given size.
|
||||
Some systems may require that the sizes are powers-of-two.
|
||||
*/
|
||||
static bool isValidSize (int width, int height);
|
||||
|
||||
private:
|
||||
GLuint textureID;
|
||||
int width, height;
|
||||
OpenGLContext* ownerContext;
|
||||
|
||||
void create (int w, int h, const void*, GLenum, bool topLeft);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLTexture)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
Creates an openGL texture from an Image.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLTexture
|
||||
{
|
||||
public:
|
||||
OpenGLTexture();
|
||||
~OpenGLTexture();
|
||||
|
||||
/** Creates a texture from the given image.
|
||||
|
||||
Note that if the image's dimensions aren't a power-of-two, the texture may
|
||||
be created with a larger size.
|
||||
|
||||
The image will be arranged so that its top-left corner is at texture
|
||||
coordinate (0, 1).
|
||||
*/
|
||||
void loadImage (const Image& image);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadARGB (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates a texture from a raw array of pixels.
|
||||
This is like loadARGB, but will vertically flip the data so that the first
|
||||
pixel ends up at texture coordinate (0, 1), and if the width and height are
|
||||
not powers-of-two, it will compensate by using a larger texture size.
|
||||
*/
|
||||
void loadARGBFlipped (const PixelARGB* pixels, int width, int height);
|
||||
|
||||
/** Creates an alpha-channel texture from an array of alpha values.
|
||||
If width and height are not powers-of-two, the texture will be created with a
|
||||
larger size, and only the subsection (0, 0, width, height) will be initialised.
|
||||
The data is sent directly to the OpenGL driver without being flipped vertically,
|
||||
so the first pixel will be mapped onto texture coordinate (0, 0).
|
||||
*/
|
||||
void loadAlpha (const uint8* pixels, int width, int height);
|
||||
|
||||
/** Frees the texture, if there is one. */
|
||||
void release();
|
||||
|
||||
/** Binds the texture to the currently active openGL context. */
|
||||
void bind() const;
|
||||
|
||||
/** Unbinds the texture to the currently active openGL context. */
|
||||
void unbind() const;
|
||||
|
||||
/** Returns the GL texture ID number. */
|
||||
GLuint getTextureID() const noexcept { return textureID; }
|
||||
|
||||
int getWidth() const noexcept { return width; }
|
||||
int getHeight() const noexcept { return height; }
|
||||
|
||||
/** Returns true if a texture can be created with the given size.
|
||||
Some systems may require that the sizes are powers-of-two.
|
||||
*/
|
||||
static bool isValidSize (int width, int height);
|
||||
|
||||
private:
|
||||
GLuint textureID;
|
||||
int width, height;
|
||||
OpenGLContext* ownerContext;
|
||||
|
||||
void create (int w, int h, const void*, GLenum, bool topLeft);
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLTexture)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
8150
deps/juce/modules/juce_opengl/opengl/juce_gl.cpp
vendored
8150
deps/juce/modules/juce_opengl/opengl/juce_gl.cpp
vendored
File diff suppressed because it is too large
Load Diff
24016
deps/juce/modules/juce_opengl/opengl/juce_gl.h
vendored
24016
deps/juce/modules/juce_opengl/opengl/juce_gl.h
vendored
File diff suppressed because it is too large
Load Diff
2836
deps/juce/modules/juce_opengl/opengl/juce_gles2.cpp
vendored
2836
deps/juce/modules/juce_opengl/opengl/juce_gles2.cpp
vendored
File diff suppressed because it is too large
Load Diff
10540
deps/juce/modules/juce_opengl/opengl/juce_gles2.h
vendored
10540
deps/juce/modules/juce_opengl/opengl/juce_gles2.h
vendored
File diff suppressed because it is too large
Load Diff
@ -1,290 +1,290 @@
|
||||
#ifndef __khrplatform_h_
|
||||
#define __khrplatform_h_
|
||||
|
||||
/*
|
||||
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||
**
|
||||
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||
** copy of this software and/or associated documentation files (the
|
||||
** "Materials"), to deal in the Materials without restriction, including
|
||||
** without limitation the rights to use, copy, modify, merge, publish,
|
||||
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
** permit persons to whom the Materials are furnished to do so, subject to
|
||||
** the following conditions:
|
||||
**
|
||||
** The above copyright notice and this permission notice shall be included
|
||||
** in all copies or substantial portions of the Materials.
|
||||
**
|
||||
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
*/
|
||||
|
||||
/* Khronos platform-specific types and definitions.
|
||||
*
|
||||
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||
* The last semantic modification to khrplatform.h was at commit ID:
|
||||
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||
*
|
||||
* Adopters may modify this file to suit their platform. Adopters are
|
||||
* encouraged to submit platform specific modifications to the Khronos
|
||||
* group so that they can be included in future versions of this file.
|
||||
* Please submit changes by filing pull requests or issues on
|
||||
* the EGL Registry repository linked above.
|
||||
*
|
||||
*
|
||||
* See the Implementer's Guidelines for information about where this file
|
||||
* should be located on your system and for more details of its use:
|
||||
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||
*
|
||||
* This file should be included as
|
||||
* #include <KHR/khrplatform.h>
|
||||
* by Khronos client API header files that use its types and defines.
|
||||
*
|
||||
* The types in khrplatform.h should only be used to define API-specific types.
|
||||
*
|
||||
* Types defined in khrplatform.h:
|
||||
* khronos_int8_t signed 8 bit
|
||||
* khronos_uint8_t unsigned 8 bit
|
||||
* khronos_int16_t signed 16 bit
|
||||
* khronos_uint16_t unsigned 16 bit
|
||||
* khronos_int32_t signed 32 bit
|
||||
* khronos_uint32_t unsigned 32 bit
|
||||
* khronos_int64_t signed 64 bit
|
||||
* khronos_uint64_t unsigned 64 bit
|
||||
* khronos_intptr_t signed same number of bits as a pointer
|
||||
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||
* khronos_ssize_t signed size
|
||||
* khronos_usize_t unsigned size
|
||||
* khronos_float_t signed 32 bit floating point
|
||||
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||
* nanoseconds
|
||||
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||
* only be used as a base type when a client API's boolean type is
|
||||
* an enum. Client APIs which use an integer or other type for
|
||||
* booleans cannot use this as the base type for their boolean.
|
||||
*
|
||||
* Tokens defined in khrplatform.h:
|
||||
*
|
||||
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||
*
|
||||
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||
*
|
||||
* Calling convention macros defined in this file:
|
||||
* KHRONOS_APICALL
|
||||
* KHRONOS_APIENTRY
|
||||
* KHRONOS_APIATTRIBUTES
|
||||
*
|
||||
* These may be used in function prototypes as:
|
||||
*
|
||||
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||
* int arg1,
|
||||
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||
*/
|
||||
|
||||
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||
# define KHRONOS_STATIC 1
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APICALL
|
||||
*-------------------------------------------------------------------------
|
||||
* This precedes the return type of the function in the function prototype.
|
||||
*/
|
||||
#if defined(KHRONOS_STATIC)
|
||||
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||
* header compatible with static linking. */
|
||||
# define KHRONOS_APICALL
|
||||
#elif defined(_WIN32)
|
||||
# define KHRONOS_APICALL __declspec(dllimport)
|
||||
#elif defined (__SYMBIAN32__)
|
||||
# define KHRONOS_APICALL IMPORT_C
|
||||
#elif defined(__ANDROID__)
|
||||
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||
#else
|
||||
# define KHRONOS_APICALL
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIENTRY
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the return type of the function and precedes the function
|
||||
* name in the function prototype.
|
||||
*/
|
||||
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||
/* Win32 but not WinCE */
|
||||
# define KHRONOS_APIENTRY __stdcall
|
||||
#else
|
||||
# define KHRONOS_APIENTRY
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIATTRIBUTES
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the closing parenthesis of the function prototype arguments.
|
||||
*/
|
||||
#if defined (__ARMCC_2__)
|
||||
#define KHRONOS_APIATTRIBUTES __softfp
|
||||
#else
|
||||
#define KHRONOS_APIATTRIBUTES
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* basic type definitions
|
||||
*-----------------------------------------------------------------------*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||
|
||||
|
||||
/*
|
||||
* Using <stdint.h>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__VMS ) || defined(__sgi)
|
||||
|
||||
/*
|
||||
* Using <inttypes.h>
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||
|
||||
/*
|
||||
* Win32
|
||||
*/
|
||||
typedef __int32 khronos_int32_t;
|
||||
typedef unsigned __int32 khronos_uint32_t;
|
||||
typedef __int64 khronos_int64_t;
|
||||
typedef unsigned __int64 khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
|
||||
/*
|
||||
* Sun or Digital
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int khronos_int64_t;
|
||||
typedef unsigned long int khronos_uint64_t;
|
||||
#else
|
||||
typedef long long int khronos_int64_t;
|
||||
typedef unsigned long long int khronos_uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif 0
|
||||
|
||||
/*
|
||||
* Hypothetical platform with no float or int64 support
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#define KHRONOS_SUPPORT_INT64 0
|
||||
#define KHRONOS_SUPPORT_FLOAT 0
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* Generic fallback
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Types that are (so far) the same on all platforms
|
||||
*/
|
||||
typedef signed char khronos_int8_t;
|
||||
typedef unsigned char khronos_uint8_t;
|
||||
typedef signed short int khronos_int16_t;
|
||||
typedef unsigned short int khronos_uint16_t;
|
||||
|
||||
/*
|
||||
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||
* to be the only LLP64 architecture in current use.
|
||||
*/
|
||||
#ifdef _WIN64
|
||||
typedef signed long long int khronos_intptr_t;
|
||||
typedef unsigned long long int khronos_uintptr_t;
|
||||
typedef signed long long int khronos_ssize_t;
|
||||
typedef unsigned long long int khronos_usize_t;
|
||||
#else
|
||||
typedef signed long int khronos_intptr_t;
|
||||
typedef unsigned long int khronos_uintptr_t;
|
||||
typedef signed long int khronos_ssize_t;
|
||||
typedef unsigned long int khronos_usize_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_FLOAT
|
||||
/*
|
||||
* Float type
|
||||
*/
|
||||
typedef float khronos_float_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_INT64
|
||||
/* Time types
|
||||
*
|
||||
* These types can be used to represent a time interval in nanoseconds or
|
||||
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||
* time the system booted). The Unadjusted System Time is an unsigned
|
||||
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||
* may be either signed or unsigned.
|
||||
*/
|
||||
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Dummy value used to pad enum types to 32 bits.
|
||||
*/
|
||||
#ifndef KHRONOS_MAX_ENUM
|
||||
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enumerated boolean type
|
||||
*
|
||||
* Values other than zero should be considered to be true. Therefore
|
||||
* comparisons should not be made against KHRONOS_TRUE.
|
||||
*/
|
||||
typedef enum {
|
||||
KHRONOS_FALSE = 0,
|
||||
KHRONOS_TRUE = 1,
|
||||
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||
} khronos_boolean_enum_t;
|
||||
|
||||
#endif /* __khrplatform_h_ */
|
||||
#ifndef __khrplatform_h_
|
||||
#define __khrplatform_h_
|
||||
|
||||
/*
|
||||
** Copyright (c) 2008-2018 The Khronos Group Inc.
|
||||
**
|
||||
** Permission is hereby granted, free of charge, to any person obtaining a
|
||||
** copy of this software and/or associated documentation files (the
|
||||
** "Materials"), to deal in the Materials without restriction, including
|
||||
** without limitation the rights to use, copy, modify, merge, publish,
|
||||
** distribute, sublicense, and/or sell copies of the Materials, and to
|
||||
** permit persons to whom the Materials are furnished to do so, subject to
|
||||
** the following conditions:
|
||||
**
|
||||
** The above copyright notice and this permission notice shall be included
|
||||
** in all copies or substantial portions of the Materials.
|
||||
**
|
||||
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
||||
*/
|
||||
|
||||
/* Khronos platform-specific types and definitions.
|
||||
*
|
||||
* The master copy of khrplatform.h is maintained in the Khronos EGL
|
||||
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
|
||||
* The last semantic modification to khrplatform.h was at commit ID:
|
||||
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
|
||||
*
|
||||
* Adopters may modify this file to suit their platform. Adopters are
|
||||
* encouraged to submit platform specific modifications to the Khronos
|
||||
* group so that they can be included in future versions of this file.
|
||||
* Please submit changes by filing pull requests or issues on
|
||||
* the EGL Registry repository linked above.
|
||||
*
|
||||
*
|
||||
* See the Implementer's Guidelines for information about where this file
|
||||
* should be located on your system and for more details of its use:
|
||||
* http://www.khronos.org/registry/implementers_guide.pdf
|
||||
*
|
||||
* This file should be included as
|
||||
* #include <KHR/khrplatform.h>
|
||||
* by Khronos client API header files that use its types and defines.
|
||||
*
|
||||
* The types in khrplatform.h should only be used to define API-specific types.
|
||||
*
|
||||
* Types defined in khrplatform.h:
|
||||
* khronos_int8_t signed 8 bit
|
||||
* khronos_uint8_t unsigned 8 bit
|
||||
* khronos_int16_t signed 16 bit
|
||||
* khronos_uint16_t unsigned 16 bit
|
||||
* khronos_int32_t signed 32 bit
|
||||
* khronos_uint32_t unsigned 32 bit
|
||||
* khronos_int64_t signed 64 bit
|
||||
* khronos_uint64_t unsigned 64 bit
|
||||
* khronos_intptr_t signed same number of bits as a pointer
|
||||
* khronos_uintptr_t unsigned same number of bits as a pointer
|
||||
* khronos_ssize_t signed size
|
||||
* khronos_usize_t unsigned size
|
||||
* khronos_float_t signed 32 bit floating point
|
||||
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
|
||||
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
|
||||
* nanoseconds
|
||||
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
|
||||
* khronos_boolean_enum_t enumerated boolean type. This should
|
||||
* only be used as a base type when a client API's boolean type is
|
||||
* an enum. Client APIs which use an integer or other type for
|
||||
* booleans cannot use this as the base type for their boolean.
|
||||
*
|
||||
* Tokens defined in khrplatform.h:
|
||||
*
|
||||
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
|
||||
*
|
||||
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
|
||||
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
|
||||
*
|
||||
* Calling convention macros defined in this file:
|
||||
* KHRONOS_APICALL
|
||||
* KHRONOS_APIENTRY
|
||||
* KHRONOS_APIATTRIBUTES
|
||||
*
|
||||
* These may be used in function prototypes as:
|
||||
*
|
||||
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
|
||||
* int arg1,
|
||||
* int arg2) KHRONOS_APIATTRIBUTES;
|
||||
*/
|
||||
|
||||
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
|
||||
# define KHRONOS_STATIC 1
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APICALL
|
||||
*-------------------------------------------------------------------------
|
||||
* This precedes the return type of the function in the function prototype.
|
||||
*/
|
||||
#if defined(KHRONOS_STATIC)
|
||||
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
|
||||
* header compatible with static linking. */
|
||||
# define KHRONOS_APICALL
|
||||
#elif defined(_WIN32)
|
||||
# define KHRONOS_APICALL __declspec(dllimport)
|
||||
#elif defined (__SYMBIAN32__)
|
||||
# define KHRONOS_APICALL IMPORT_C
|
||||
#elif defined(__ANDROID__)
|
||||
# define KHRONOS_APICALL __attribute__((visibility("default")))
|
||||
#else
|
||||
# define KHRONOS_APICALL
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIENTRY
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the return type of the function and precedes the function
|
||||
* name in the function prototype.
|
||||
*/
|
||||
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
|
||||
/* Win32 but not WinCE */
|
||||
# define KHRONOS_APIENTRY __stdcall
|
||||
#else
|
||||
# define KHRONOS_APIENTRY
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* Definition of KHRONOS_APIATTRIBUTES
|
||||
*-------------------------------------------------------------------------
|
||||
* This follows the closing parenthesis of the function prototype arguments.
|
||||
*/
|
||||
#if defined (__ARMCC_2__)
|
||||
#define KHRONOS_APIATTRIBUTES __softfp
|
||||
#else
|
||||
#define KHRONOS_APIATTRIBUTES
|
||||
#endif
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* basic type definitions
|
||||
*-----------------------------------------------------------------------*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
|
||||
|
||||
|
||||
/*
|
||||
* Using <stdint.h>
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__VMS ) || defined(__sgi)
|
||||
|
||||
/*
|
||||
* Using <inttypes.h>
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
|
||||
|
||||
/*
|
||||
* Win32
|
||||
*/
|
||||
typedef __int32 khronos_int32_t;
|
||||
typedef unsigned __int32 khronos_uint32_t;
|
||||
typedef __int64 khronos_int64_t;
|
||||
typedef unsigned __int64 khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif defined(__sun__) || defined(__digital__)
|
||||
|
||||
/*
|
||||
* Sun or Digital
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#if defined(__arch64__) || defined(_LP64)
|
||||
typedef long int khronos_int64_t;
|
||||
typedef unsigned long int khronos_uint64_t;
|
||||
#else
|
||||
typedef long long int khronos_int64_t;
|
||||
typedef unsigned long long int khronos_uint64_t;
|
||||
#endif /* __arch64__ */
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#elif 0
|
||||
|
||||
/*
|
||||
* Hypothetical platform with no float or int64 support
|
||||
*/
|
||||
typedef int khronos_int32_t;
|
||||
typedef unsigned int khronos_uint32_t;
|
||||
#define KHRONOS_SUPPORT_INT64 0
|
||||
#define KHRONOS_SUPPORT_FLOAT 0
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
* Generic fallback
|
||||
*/
|
||||
#include <stdint.h>
|
||||
typedef int32_t khronos_int32_t;
|
||||
typedef uint32_t khronos_uint32_t;
|
||||
typedef int64_t khronos_int64_t;
|
||||
typedef uint64_t khronos_uint64_t;
|
||||
#define KHRONOS_SUPPORT_INT64 1
|
||||
#define KHRONOS_SUPPORT_FLOAT 1
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Types that are (so far) the same on all platforms
|
||||
*/
|
||||
typedef signed char khronos_int8_t;
|
||||
typedef unsigned char khronos_uint8_t;
|
||||
typedef signed short int khronos_int16_t;
|
||||
typedef unsigned short int khronos_uint16_t;
|
||||
|
||||
/*
|
||||
* Types that differ between LLP64 and LP64 architectures - in LLP64,
|
||||
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
|
||||
* to be the only LLP64 architecture in current use.
|
||||
*/
|
||||
#ifdef _WIN64
|
||||
typedef signed long long int khronos_intptr_t;
|
||||
typedef unsigned long long int khronos_uintptr_t;
|
||||
typedef signed long long int khronos_ssize_t;
|
||||
typedef unsigned long long int khronos_usize_t;
|
||||
#else
|
||||
typedef signed long int khronos_intptr_t;
|
||||
typedef unsigned long int khronos_uintptr_t;
|
||||
typedef signed long int khronos_ssize_t;
|
||||
typedef unsigned long int khronos_usize_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_FLOAT
|
||||
/*
|
||||
* Float type
|
||||
*/
|
||||
typedef float khronos_float_t;
|
||||
#endif
|
||||
|
||||
#if KHRONOS_SUPPORT_INT64
|
||||
/* Time types
|
||||
*
|
||||
* These types can be used to represent a time interval in nanoseconds or
|
||||
* an absolute Unadjusted System Time. Unadjusted System Time is the number
|
||||
* of nanoseconds since some arbitrary system event (e.g. since the last
|
||||
* time the system booted). The Unadjusted System Time is an unsigned
|
||||
* 64 bit value that wraps back to 0 every 584 years. Time intervals
|
||||
* may be either signed or unsigned.
|
||||
*/
|
||||
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
|
||||
typedef khronos_int64_t khronos_stime_nanoseconds_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Dummy value used to pad enum types to 32 bits.
|
||||
*/
|
||||
#ifndef KHRONOS_MAX_ENUM
|
||||
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Enumerated boolean type
|
||||
*
|
||||
* Values other than zero should be considered to be true. Therefore
|
||||
* comparisons should not be made against KHRONOS_TRUE.
|
||||
*/
|
||||
typedef enum {
|
||||
KHRONOS_FALSE = 0,
|
||||
KHRONOS_TRUE = 1,
|
||||
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
|
||||
} khronos_boolean_enum_t;
|
||||
|
||||
#endif /* __khrplatform_h_ */
|
||||
|
1294
deps/juce/modules/juce_opengl/opengl/juce_wgl.h
vendored
1294
deps/juce/modules/juce_opengl/opengl/juce_wgl.h
vendored
File diff suppressed because it is too large
Load Diff
@ -1,69 +1,69 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLAppComponent::OpenGLAppComponent()
|
||||
{
|
||||
setOpaque (true);
|
||||
openGLContext.setRenderer (this);
|
||||
openGLContext.attachTo (*this);
|
||||
openGLContext.setContinuousRepainting (true);
|
||||
}
|
||||
|
||||
OpenGLAppComponent::~OpenGLAppComponent()
|
||||
{
|
||||
// Before your subclass's destructor has completed, you must call
|
||||
// shutdownOpenGL() to release the GL context. (Otherwise there's
|
||||
// a danger that it may invoke a GL callback on your class while
|
||||
// it's in the process of being deleted.
|
||||
jassert (! openGLContext.isAttached());
|
||||
|
||||
shutdownOpenGL();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::shutdownOpenGL()
|
||||
{
|
||||
openGLContext.detach();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::newOpenGLContextCreated()
|
||||
{
|
||||
initialise();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::renderOpenGL()
|
||||
{
|
||||
++frameCounter;
|
||||
render();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::openGLContextClosing()
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
OpenGLAppComponent::OpenGLAppComponent()
|
||||
{
|
||||
setOpaque (true);
|
||||
openGLContext.setRenderer (this);
|
||||
openGLContext.attachTo (*this);
|
||||
openGLContext.setContinuousRepainting (true);
|
||||
}
|
||||
|
||||
OpenGLAppComponent::~OpenGLAppComponent()
|
||||
{
|
||||
// Before your subclass's destructor has completed, you must call
|
||||
// shutdownOpenGL() to release the GL context. (Otherwise there's
|
||||
// a danger that it may invoke a GL callback on your class while
|
||||
// it's in the process of being deleted.
|
||||
jassert (! openGLContext.isAttached());
|
||||
|
||||
shutdownOpenGL();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::shutdownOpenGL()
|
||||
{
|
||||
openGLContext.detach();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::newOpenGLContextCreated()
|
||||
{
|
||||
initialise();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::renderOpenGL()
|
||||
{
|
||||
++frameCounter;
|
||||
render();
|
||||
}
|
||||
|
||||
void OpenGLAppComponent::openGLContextClosing()
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
|
||||
} // namespace juce
|
||||
|
@ -1,97 +1,97 @@
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2020 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
|
||||
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
|
||||
|
||||
End User License Agreement: www.juce.com/juce-6-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class for writing simple one-page graphical apps.
|
||||
|
||||
A subclass can inherit from this and implement just a few methods such as
|
||||
paint() and mouse-handling. The base class provides some simple abstractions
|
||||
to take care of continuously repainting itself.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLAppComponent : public Component,
|
||||
private OpenGLRenderer
|
||||
{
|
||||
public:
|
||||
OpenGLAppComponent();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLAppComponent() override;
|
||||
|
||||
/** Returns the number of times that the render method has been called since
|
||||
the component started running.
|
||||
*/
|
||||
int getFrameCounter() const noexcept { return frameCounter; }
|
||||
|
||||
/** This must be called from your subclass's destructor, to shut down
|
||||
the GL system and stop it calling render() before your class is destroyed.
|
||||
*/
|
||||
void shutdownOpenGL();
|
||||
|
||||
/** Implement this method to set up any GL objects that you need for rendering.
|
||||
The GL context will be active when this method is called.
|
||||
|
||||
Note that because the GL context could be destroyed and re-created ad-hoc by
|
||||
the underlying platform, the shutdown() and initialise() calls could be called
|
||||
multiple times while your app is running. So don't make your code assume that
|
||||
this will only be called once!
|
||||
*/
|
||||
virtual void initialise() = 0;
|
||||
|
||||
/** Implement this method to free any GL objects that you created during rendering.
|
||||
The GL context will still be active when this method is called.
|
||||
|
||||
Note that because the GL context could be destroyed and re-created ad-hoc by
|
||||
the underlying platform, the shutdown() and initialise() calls could be called
|
||||
multiple times while your app is running. So don't make your code assume that
|
||||
this will only be called once!
|
||||
*/
|
||||
virtual void shutdown() = 0;
|
||||
|
||||
/** Called to render your openGL.
|
||||
@see OpenGLRenderer::render()
|
||||
*/
|
||||
virtual void render() = 0;
|
||||
|
||||
/** The GL context */
|
||||
OpenGLContext openGLContext;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
int frameCounter = 0;
|
||||
|
||||
void newOpenGLContextCreated() override;
|
||||
void renderOpenGL() override;
|
||||
void openGLContextClosing() override;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAppComponent)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
/*
|
||||
==============================================================================
|
||||
|
||||
This file is part of the JUCE library.
|
||||
Copyright (c) 2022 - Raw Material Software Limited
|
||||
|
||||
JUCE is an open source library subject to commercial or open-source
|
||||
licensing.
|
||||
|
||||
By using JUCE, you agree to the terms of both the JUCE 7 End-User License
|
||||
Agreement and JUCE Privacy Policy.
|
||||
|
||||
End User License Agreement: www.juce.com/juce-7-licence
|
||||
Privacy Policy: www.juce.com/juce-privacy-policy
|
||||
|
||||
Or: You may also use this code under the terms of the GPL v3 (see
|
||||
www.gnu.org/licenses).
|
||||
|
||||
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
|
||||
DISCLAIMED.
|
||||
|
||||
==============================================================================
|
||||
*/
|
||||
|
||||
namespace juce
|
||||
{
|
||||
|
||||
//==============================================================================
|
||||
/**
|
||||
A base class for writing simple one-page graphical apps.
|
||||
|
||||
A subclass can inherit from this and implement just a few methods such as
|
||||
paint() and mouse-handling. The base class provides some simple abstractions
|
||||
to take care of continuously repainting itself.
|
||||
|
||||
@tags{OpenGL}
|
||||
*/
|
||||
class JUCE_API OpenGLAppComponent : public Component,
|
||||
private OpenGLRenderer
|
||||
{
|
||||
public:
|
||||
OpenGLAppComponent();
|
||||
|
||||
/** Destructor. */
|
||||
~OpenGLAppComponent() override;
|
||||
|
||||
/** Returns the number of times that the render method has been called since
|
||||
the component started running.
|
||||
*/
|
||||
int getFrameCounter() const noexcept { return frameCounter; }
|
||||
|
||||
/** This must be called from your subclass's destructor, to shut down
|
||||
the GL system and stop it calling render() before your class is destroyed.
|
||||
*/
|
||||
void shutdownOpenGL();
|
||||
|
||||
/** Implement this method to set up any GL objects that you need for rendering.
|
||||
The GL context will be active when this method is called.
|
||||
|
||||
Note that because the GL context could be destroyed and re-created ad-hoc by
|
||||
the underlying platform, the shutdown() and initialise() calls could be called
|
||||
multiple times while your app is running. So don't make your code assume that
|
||||
this will only be called once!
|
||||
*/
|
||||
virtual void initialise() = 0;
|
||||
|
||||
/** Implement this method to free any GL objects that you created during rendering.
|
||||
The GL context will still be active when this method is called.
|
||||
|
||||
Note that because the GL context could be destroyed and re-created ad-hoc by
|
||||
the underlying platform, the shutdown() and initialise() calls could be called
|
||||
multiple times while your app is running. So don't make your code assume that
|
||||
this will only be called once!
|
||||
*/
|
||||
virtual void shutdown() = 0;
|
||||
|
||||
/** Called to render your openGL.
|
||||
@see OpenGLRenderer::render()
|
||||
*/
|
||||
virtual void render() = 0;
|
||||
|
||||
/** The GL context */
|
||||
OpenGLContext openGLContext;
|
||||
|
||||
private:
|
||||
//==============================================================================
|
||||
int frameCounter = 0;
|
||||
|
||||
void newOpenGLContextCreated() override;
|
||||
void renderOpenGL() override;
|
||||
void openGLContextClosing() override;
|
||||
|
||||
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAppComponent)
|
||||
};
|
||||
|
||||
} // namespace juce
|
||||
|
Reference in New Issue
Block a user