1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-13 00:04:19 +00:00

Added Animated App template and examples

This commit is contained in:
Felix Faire 2014-10-29 15:55:23 +00:00
parent fefcf7aca6
commit ff6520a89a
1141 changed files with 438491 additions and 94 deletions

View file

@ -0,0 +1,157 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED
#define JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED
//==============================================================================
/**
Stores a 3D orientation, which can be rotated by dragging with the mouse.
*/
class Draggable3DOrientation
{
public:
typedef Vector3D<GLfloat> VectorType;
typedef Quaternion<GLfloat> QuaternionType;
/** 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<GLfloat>& 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 (const 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
{
const VectorType oldPos (projectOnSphere (lastMouse));
lastMouse = mousePosToProportion (mousePos.toFloat());
const VectorType newPos (projectOnSphere (lastMouse));
quaternion *= rotationFromMove (oldPos, newPos);
}
/** Returns the matrix that should be used to apply the current orientation.
@see applyToOpenGLMatrix
*/
Matrix3D<GLfloat> 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 (const Point<float> mousePos) const noexcept
{
const int 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 Point<float> ((mousePos.x - area.getCentreX()) / scale,
(area.getCentreY() - mousePos.y) / scale);
}
VectorType projectOnSphere (const Point<float> pos) const noexcept
{
const GLfloat radiusSquared = radius * radius;
const GLfloat xySquared = pos.x * pos.x + pos.y * pos.y;
return VectorType (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
{
VectorType rotationAxis (to ^ from);
if (rotationAxis.lengthIsBelowEpsilon())
rotationAxis = VectorType::xAxis();
const GLfloat d = jlimit (-1.0f, 1.0f, (from - to).length() / (2.0f * radius));
return QuaternionType::fromAngle (2.0f * std::asin (d), rotationAxis);
}
};
#endif // JUCE_DRAGGABLE3DORIENTATION_H_INCLUDED

View file

@ -0,0 +1,154 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_MATRIX3D_H_INCLUDED
#define JUCE_MATRIX3D_H_INCLUDED
//==============================================================================
/**
A 4x4 3D transformation matrix.
@see Vector3D, Quaternion, AffineTransform
*/
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 (const Type& m00, const Type& m10, const Type& m20, const Type& m30,
const Type& m01, const Type& m11, const Type& m21, const Type& m31,
const Type& m02, const Type& m12, const Type& m22, const Type& m32,
const Type& m03, const Type& m13, const Type& m23, const Type& m33) noexcept
{
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 frustrum values. */
static Matrix3D fromFrustum (Type left, Type right, Type bottom, Type top, Type nearDistance, Type farDistance) noexcept
{
return Matrix3D ((2.0f * nearDistance) / (right - left), 0.0f, 0.0f, 0.0f,
0.0f, (2.0f * nearDistance) / (top - bottom), 0.0f, 0.0f,
(right + left) / (right - left), (top + bottom) / (top - bottom), -(farDistance + nearDistance) / (farDistance - nearDistance), -1.0f,
0.0f, 0.0f, -(2.0f * farDistance * nearDistance) / (farDistance - nearDistance), 0.0f);
}
/** 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
{
const Type* const m2 = other.mat;
return Matrix3D (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]);
}
/** Returns a copy of this matrix after rotation through the Y, X and then Z angles
specified by the vector.
*/
Matrix3D rotated (Vector3D<Type> eulerAngleRadians) const noexcept
{
const Type cx = std::cos (eulerAngleRadians.x), sx = std::sin (eulerAngleRadians.x),
cy = std::cos (eulerAngleRadians.y), sy = std::sin (eulerAngleRadians.y),
cz = std::cos (eulerAngleRadians.z), sz = std::sin (eulerAngleRadians.z);
return Matrix3D ((cy * cz) + (sx * sy * sz), cx * sz, (cy * sx * sz) - (cz * sy), 0.0f,
(cz * sx * sy) - (cy * sz), cx * cz, (cy * cz * sx) + (sy * sz), 0.0f,
cx * sy, -sx, cx * cy, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
/** The 4x4 matrix values. These are stored in the standard OpenGL order. */
Type mat[16];
};
#endif // JUCE_MATRIX3D_H_INCLUDED

View file

@ -0,0 +1,99 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_QUATERNION_H_INCLUDED
#define JUCE_QUATERNION_H_INCLUDED
#include "juce_Vector3D.h"
#include "juce_Matrix3D.h"
//==============================================================================
/**
Holds a quaternion (a 3D vector and a scalar value).
*/
template <typename Type>
class Quaternion
{
public:
Quaternion() noexcept : scalar() {}
Quaternion (const Quaternion& other) noexcept : vector (other.vector), scalar (other.scalar) {}
Quaternion (const Vector3D<Type>& vectorPart, const Type& scalarPart) noexcept : vector (vectorPart), scalar (scalarPart) {}
Quaternion (const Type& x, const Type& y, const Type& z, const Type& w) noexcept : vector (x, y, z), scalar (w) {}
/** Creates a quaternion from an angle and an axis. */
static Quaternion fromAngle (const Type& angle, const Vector3D<Type>& axis) noexcept
{
return Quaternion (axis.normalised() * std::sin (angle / (Type) 2), std::cos (angle / (Type) 2));
}
Quaternion& operator= (const Quaternion& other) noexcept
{
vector = other.vector;
scalar = other.scalar;
return *this;
}
Quaternion& operator*= (const 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;
};
#endif // JUCE_QUATERNION_H_INCLUDED

View file

@ -0,0 +1,81 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_VECTOR3D_H_INCLUDED
#define JUCE_VECTOR3D_H_INCLUDED
//==============================================================================
/**
A three-coordinate vector.
*/
template <typename Type>
class Vector3D
{
public:
Vector3D() noexcept : x(), y(), z() {}
Vector3D (const Type& xValue, const Type& yValue, const 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= (const Vector3D& other) noexcept { x = other.x; y = other.y; z = other.z; return *this; }
/** Returns a vector that lies along the X axis. */
static Vector3D xAxis() noexcept { return Vector3D ((Type) 1, 0, 0); }
/** Returns a vector that lies along the Y axis. */
static Vector3D yAxis() noexcept { return Vector3D (0, (Type) 1, 0); }
/** Returns a vector that lies along the Z axis. */
static Vector3D zAxis() noexcept { return Vector3D (0, 0, (Type) 1); }
Vector3D& operator+= (const Vector3D& other) noexcept { x += other.x; y += other.y; z += other.z; return *this; }
Vector3D& operator-= (const Vector3D& other) noexcept { x -= other.x; y -= other.y; z -= other.z; return *this; }
Vector3D& operator*= (const Type& scaleFactor) noexcept { x *= scaleFactor; y *= scaleFactor; z *= scaleFactor; return *this; }
Vector3D& operator/= (const Type& scaleFactor) noexcept { x /= scaleFactor; y /= scaleFactor; z /= scaleFactor; return *this; }
Vector3D operator+ (const Vector3D& other) const noexcept { return Vector3D (x + other.x, y + other.y, z + other.z); }
Vector3D operator- (const Vector3D& other) const noexcept { return Vector3D (x - other.x, y - other.y, z - other.z); }
Vector3D operator* (const Type& scaleFactor) const noexcept { return Vector3D (x * scaleFactor, y * scaleFactor, z * scaleFactor); }
Vector3D operator/ (const Type& scaleFactor) const noexcept { return Vector3D (x / scaleFactor, y / scaleFactor, z / scaleFactor); }
Vector3D operator-() const noexcept { return Vector3D (-x, -y, -z); }
/** Returns the dot-product of these two vectors. */
Type operator* (const Vector3D& other) const noexcept { return x * other.x + y * other.y + z * other.z; }
/** Returns the cross-product of these two vectors. */
Vector3D operator^ (const Vector3D& other) const noexcept { return Vector3D (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
{
const Type epsilon (std::numeric_limits<Type>::epsilon());
return ! (x < -epsilon || x > epsilon || y < -epsilon || y > epsilon || z < -epsilon || z > epsilon);
}
Type x, y, z;
};
#endif // JUCE_VECTOR3D_H_INCLUDED

View file

@ -0,0 +1,25 @@
{
"id": "juce_opengl",
"name": "JUCE OpenGL classes",
"version": "3.0.8",
"description": "Classes for rendering OpenGL in a JUCE window.",
"website": "http://www.juce.com/juce",
"license": "GPL/Commercial",
"dependencies": [ { "id": "juce_gui_basics", "version": "matching" } ],
"include": "juce_opengl.h",
"compile": [ { "file": "juce_opengl.cpp", "target": "! xcode" },
{ "file": "juce_opengl.mm", "target": "xcode" } ],
"browse": [ "opengl/*",
"geometry/*",
"utils/*",
"native/*" ],
"OSXFrameworks": "OpenGL",
"iOSFrameworks": "OpenGLES",
"LinuxLibs": "GL",
"mingwLibs": "opengl32"
}

View file

@ -0,0 +1,212 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#if defined (JUCE_OPENGL_H_INCLUDED) && ! JUCE_AMALGAMATED_INCLUDE
/* 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
// Your project must contain an AppConfig.h file with your project-specific settings in it,
// and your header search path must make it accessible to the module's files.
#include "AppConfig.h"
#include "../juce_core/native/juce_BasicNativeHeaders.h"
#include "juce_opengl.h"
//==============================================================================
#if JUCE_IOS
#import <QuartzCore/QuartzCore.h>
//==============================================================================
#elif JUCE_WINDOWS
#include <windowsx.h>
#if JUCE_MSVC && ! JUCE_DONT_AUTOLINK_TO_WIN32_LIBRARIES
#pragma comment(lib, "OpenGL32.Lib")
#endif
//==============================================================================
#elif JUCE_LINUX
/* 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
#ifndef GL_GLEXT_PROTOTYPES
#define GL_GLEXT_PROTOTYPES 1
#endif
#include <GLES2/gl2.h>
#endif
namespace juce
{
//==============================================================================
#include "native/juce_OpenGLExtensions.h"
void OpenGLExtensionFunctions::initialise()
{
#if JUCE_WINDOWS || JUCE_LINUX
#define JUCE_INIT_GL_FUNCTION(name, returnType, params, callparams) \
name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
#define JUCE_INIT_GL_FUNCTION_EXT(name, returnType, params, callparams) \
name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name); \
if (name == nullptr) \
name = (type_ ## name) OpenGLHelpers::getExtensionFunction (JUCE_STRINGIFY (name ## EXT));
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_INIT_GL_FUNCTION, JUCE_INIT_GL_FUNCTION_EXT)
#undef JUCE_INIT_GL_FUNCTION
#undef JUCE_INIT_GL_FUNCTION_EXT
#endif
}
#if JUCE_OPENGL_ES
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) \
returnType OpenGLExtensionFunctions::name params { return ::name callparams; }
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
#undef JUCE_DECLARE_GL_FUNCTION
#endif
#undef JUCE_GL_EXTENSION_FUNCTIONS
#if JUCE_DEBUG && ! defined (JUCE_CHECK_OPENGL_ERROR)
static const char* getGLErrorMessage (const GLenum e)
{
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";
}
static void checkGLError (const char* file, const int line)
{
for (;;)
{
const GLenum e = glGetError();
if (e == GL_NO_ERROR)
break;
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()
{
while (glGetError() != GL_NO_ERROR) {}
}
struct OpenGLTargetSaver
{
OpenGLTargetSaver (const OpenGLContext& c)
: context (c), oldFramebuffer (OpenGLFrameBuffer::getCurrentFrameBufferTarget())
{
glGetIntegerv (GL_VIEWPORT, oldViewport);
}
~OpenGLTargetSaver()
{
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&);
};
//==============================================================================
#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
#include "../juce_core/native/juce_osx_ObjCHelpers.h"
#include "../juce_graphics/native/juce_mac_CoreGraphicsHelpers.h"
#if JUCE_MAC
#include "native/juce_OpenGL_osx.h"
#else
#include "native/juce_OpenGL_ios.h"
#endif
#elif JUCE_WINDOWS
#include "native/juce_OpenGL_win32.h"
#elif JUCE_LINUX
#include "native/juce_OpenGL_linux.h"
#elif JUCE_ANDROID
#include "../juce_core/native/juce_android_JNIHelpers.h"
#include "native/juce_OpenGL_android.h"
#endif
#include "opengl/juce_OpenGLContext.cpp"
#include "utils/juce_OpenGLAppComponent.cpp"
}

View file

@ -0,0 +1,149 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGL_H_INCLUDED
#define JUCE_OPENGL_H_INCLUDED
#include "../juce_gui_extra/juce_gui_extra.h"
#undef JUCE_OPENGL
#define JUCE_OPENGL 1
#if JUCE_IOS || JUCE_ANDROID
#define JUCE_OPENGL_ES 1
#endif
#if JUCE_WINDOWS
#ifndef APIENTRY
#define APIENTRY __stdcall
#define CLEAR_TEMP_APIENTRY 1
#endif
#ifndef WINGDIAPI
#define WINGDIAPI __declspec(dllimport)
#define CLEAR_TEMP_WINGDIAPI 1
#endif
#include <gl/GL.h>
#ifdef CLEAR_TEMP_WINGDIAPI
#undef WINGDIAPI
#undef CLEAR_TEMP_WINGDIAPI
#endif
#ifdef CLEAR_TEMP_APIENTRY
#undef APIENTRY
#undef CLEAR_TEMP_APIENTRY
#endif
#elif JUCE_LINUX
#include <GL/gl.h>
#undef KeyPress
#elif JUCE_IOS
#if defined (__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
#include <OpenGLES/ES3/gl.h>
#else
#include <OpenGLES/ES2/gl.h>
#endif
#elif JUCE_MAC
#if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
#define JUCE_OPENGL3 1
#include <OpenGL/gl3.h>
#include <OpenGL/gl3ext.h>
#else
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#endif
#elif JUCE_ANDROID
#include <GLES2/gl2.h>
#endif
#if GL_ES_VERSION_3_0
#define JUCE_OPENGL3 1
#endif
//=============================================================================
/** This macro is a helper for use in GLSL shader code which needs to compile on both OpenGL 2.1 and OpenGL 3.0.
It's mandatory in OpenGL 3.0 to specify the GLSL version.
*/
#if JUCE_OPENGL3
#if JUCE_OPENGL_ES
#define JUCE_GLSL_VERSION "#version 300 es"
#else
#define JUCE_GLSL_VERSION "#version 150"
#endif
#else
#define JUCE_GLSL_VERSION ""
#endif
//=============================================================================
#if JUCE_OPENGL_ES || defined (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_Quaternion.h"
#include "geometry/juce_Matrix3D.h"
#include "geometry/juce_Vector3D.h"
#include "geometry/juce_Draggable3DOrientation.h"
#include "native/juce_MissingGLDefinitions.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_OpenGLHelpers.h"
#include "opengl/juce_OpenGLImage.h"
#include "opengl/juce_OpenGLRenderer.h"
#include "opengl/juce_OpenGLShaderProgram.h"
#include "opengl/juce_OpenGLTexture.h"
#include "utils/juce_OpenGLAppComponent.h"
}
#endif // JUCE_OPENGL_H_INCLUDED

View file

@ -0,0 +1,25 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#include "juce_opengl.cpp"

View file

@ -0,0 +1,148 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
/** These are important openGL values that aren't defined by default
by the GL headers on various platforms.
*/
enum MissingOpenGLDefinitions
{
#ifndef GL_CLAMP_TO_EDGE
GL_CLAMP_TO_EDGE = 0x812f,
#endif
#ifndef GL_NUM_EXTENSIONS
GL_NUM_EXTENSIONS = 0x821d,
#endif
#ifndef GL_BGRA_EXT
GL_BGRA_EXT = 0x80e1,
#endif
#ifndef GL_DEPTH24_STENCIL8
GL_DEPTH24_STENCIL8 = 0x88F0,
#endif
#ifndef GL_RGBA8
GL_RGBA8 = GL_RGBA,
#endif
#ifndef GL_COLOR_ATTACHMENT0
GL_COLOR_ATTACHMENT0 = 0x8CE0,
#endif
#ifndef GL_DEPTH_ATTACHMENT
GL_DEPTH_ATTACHMENT = 0x8D00,
#endif
#ifndef GL_FRAMEBUFFER
GL_FRAMEBUFFER = 0x8D40,
#endif
#ifndef GL_FRAMEBUFFER_BINDING
GL_FRAMEBUFFER_BINDING = 0x8CA6,
#endif
#ifndef GL_FRAMEBUFFER_COMPLETE
GL_FRAMEBUFFER_COMPLETE = 0x8CD5,
#endif
#ifndef GL_RENDERBUFFER
GL_RENDERBUFFER = 0x8D41,
#endif
#ifndef GL_RENDERBUFFER_DEPTH_SIZE
GL_RENDERBUFFER_DEPTH_SIZE = 0x8D54,
#endif
#ifndef GL_STENCIL_ATTACHMENT
GL_STENCIL_ATTACHMENT = 0x8D20,
#endif
#if JUCE_WINDOWS && ! defined (GL_TEXTURE0)
GL_OPERAND0_RGB = 0x8590,
GL_OPERAND1_RGB = 0x8591,
GL_OPERAND0_ALPHA = 0x8598,
GL_OPERAND1_ALPHA = 0x8599,
GL_SRC0_RGB = 0x8580,
GL_SRC1_RGB = 0x8581,
GL_SRC0_ALPHA = 0x8588,
GL_SRC1_ALPHA = 0x8589,
GL_TEXTURE0 = 0x84C0,
GL_TEXTURE1 = 0x84C1,
GL_TEXTURE2 = 0x84C2,
GL_COMBINE = 0x8570,
GL_COMBINE_RGB = 0x8571,
GL_COMBINE_ALPHA = 0x8572,
GL_PREVIOUS = 0x8578,
GL_COMPILE_STATUS = 0x8B81,
GL_LINK_STATUS = 0x8B82,
GL_SHADING_LANGUAGE_VERSION = 0x8B8C,
GL_FRAGMENT_SHADER = 0x8B30,
GL_VERTEX_SHADER = 0x8B31,
GL_ARRAY_BUFFER = 0x8892,
GL_ELEMENT_ARRAY_BUFFER = 0x8893,
GL_STATIC_DRAW = 0x88E4,
GL_DYNAMIC_DRAW = 0x88E8,
GL_STREAM_DRAW = 0x88E0,
WGL_NUMBER_PIXEL_FORMATS_ARB = 0x2000,
WGL_DRAW_TO_WINDOW_ARB = 0x2001,
WGL_ACCELERATION_ARB = 0x2003,
WGL_SWAP_METHOD_ARB = 0x2007,
WGL_SUPPORT_OPENGL_ARB = 0x2010,
WGL_PIXEL_TYPE_ARB = 0x2013,
WGL_DOUBLE_BUFFER_ARB = 0x2011,
WGL_COLOR_BITS_ARB = 0x2014,
WGL_RED_BITS_ARB = 0x2015,
WGL_GREEN_BITS_ARB = 0x2017,
WGL_BLUE_BITS_ARB = 0x2019,
WGL_ALPHA_BITS_ARB = 0x201B,
WGL_DEPTH_BITS_ARB = 0x2022,
WGL_STENCIL_BITS_ARB = 0x2023,
WGL_FULL_ACCELERATION_ARB = 0x2027,
WGL_ACCUM_RED_BITS_ARB = 0x201E,
WGL_ACCUM_GREEN_BITS_ARB = 0x201F,
WGL_ACCUM_BLUE_BITS_ARB = 0x2020,
WGL_ACCUM_ALPHA_BITS_ARB = 0x2021,
WGL_STEREO_ARB = 0x2012,
WGL_SAMPLE_BUFFERS_ARB = 0x2041,
WGL_SAMPLES_ARB = 0x2042,
WGL_TYPE_RGBA_ARB = 0x202B,
WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091,
WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092,
WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126,
#endif
#if JUCE_ANDROID
JUCE_RGBA_FORMAT = GL_RGBA
#else
JUCE_RGBA_FORMAT = GL_BGRA_EXT
#endif
};
#if JUCE_WINDOWS
typedef char GLchar;
typedef pointer_sized_int GLsizeiptr;
typedef pointer_sized_int GLintptr;
#endif

View file

@ -0,0 +1,128 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLEXTENSIONS_H_INCLUDED
#define JUCE_OPENGLEXTENSIONS_H_INCLUDED
#if JUCE_MAC && (JUCE_PPC || ((! defined (MAC_OS_X_VERSION_10_6)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
#define JUCE_EXT(func) func ## EXT
#else
#define JUCE_EXT(func) func
#endif
/** @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(USE_FUNCTION, EXT_FUNCTION) \
USE_FUNCTION (glActiveTexture, void, (GLenum p1), (p1))\
USE_FUNCTION (glBindBuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glDeleteBuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
USE_FUNCTION (glGenBuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
USE_FUNCTION (glBufferData, void, (GLenum p1, GLsizeiptr p2, const GLvoid* p3, GLenum p4), (p1, p2, p3, p4))\
USE_FUNCTION (glBufferSubData, void, (GLenum p1, GLintptr p2, GLsizeiptr p3, const GLvoid* p4), (p1, p2, p3, p4))\
EXT_FUNCTION (glIsRenderbuffer, GLboolean, (GLuint p1), (p1))\
EXT_FUNCTION (glBindRenderbuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
EXT_FUNCTION (glDeleteRenderbuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
EXT_FUNCTION (glGenRenderbuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
EXT_FUNCTION (glRenderbufferStorage, void, (GLenum p1, GLenum p2, GLsizei p3, GLsizei p4), (p1, p2, p3, p4))\
EXT_FUNCTION (glGetRenderbufferParameteriv, void, (GLenum p1, GLenum p2, GLint* p3), (p1, p2, p3))\
EXT_FUNCTION (glIsFramebuffer, GLboolean, (GLuint p1), (p1))\
EXT_FUNCTION (glBindFramebuffer, void, (GLenum p1, GLuint p2), (p1, p2))\
EXT_FUNCTION (glDeleteFramebuffers, void, (GLsizei p1, const GLuint* p2), (p1, p2))\
EXT_FUNCTION (glGenFramebuffers, void, (GLsizei p1, GLuint* p2), (p1, p2))\
EXT_FUNCTION (glCheckFramebufferStatus, GLenum, (GLenum p1), (p1))\
EXT_FUNCTION (glFramebufferTexture2D, void, (GLenum p1, GLenum p2, GLenum p3, GLuint p4, GLint p5), (p1, p2, p3, p4, p5))\
EXT_FUNCTION (glFramebufferRenderbuffer, void, (GLenum p1, GLenum p2, GLenum p3, GLuint p4), (p1, p2, p3, p4))\
EXT_FUNCTION (glGetFramebufferAttachmentParameteriv, void, (GLenum p1, GLenum p2, GLenum p3, GLint* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glCreateProgram, GLuint, (), ())\
USE_FUNCTION (glDeleteProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glCreateShader, GLuint, (GLenum p1), (p1))\
USE_FUNCTION (glDeleteShader, void, (GLuint p1), (p1))\
USE_FUNCTION (glShaderSource, void, (GLuint p1, GLsizei p2, const GLchar** p3, const GLint* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glCompileShader, void, (GLuint p1), (p1))\
USE_FUNCTION (glAttachShader, void, (GLuint p1, GLuint p2), (p1, p2))\
USE_FUNCTION (glLinkProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glUseProgram, void, (GLuint p1), (p1))\
USE_FUNCTION (glGetShaderiv, void, (GLuint p1, GLenum p2, GLint* p3), (p1, p2, p3))\
USE_FUNCTION (glGetShaderInfoLog, void, (GLuint p1, GLsizei p2, GLsizei* p3, GLchar* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetProgramInfoLog, void, (GLuint p1, GLsizei p2, GLsizei* p3, GLchar* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glGetProgramiv, void, (GLuint p1, GLenum p2, GLint* p3), (p1, p2, p3))\
USE_FUNCTION (glGetUniformLocation, GLint, (GLuint p1, const GLchar* p2), (p1, p2))\
USE_FUNCTION (glGetAttribLocation, GLint, (GLuint p1, const GLchar* p2), (p1, p2))\
USE_FUNCTION (glVertexAttribPointer, void, (GLuint p1, GLint p2, GLenum p3, GLboolean p4, GLsizei p5, const GLvoid* p6), (p1, p2, p3, p4, p5, p6))\
USE_FUNCTION (glEnableVertexAttribArray, void, (GLuint p1), (p1))\
USE_FUNCTION (glDisableVertexAttribArray, void, (GLuint p1), (p1))\
USE_FUNCTION (glUniform1f, void, (GLint p1, GLfloat p2), (p1, p2))\
USE_FUNCTION (glUniform1i, void, (GLint p1, GLint p2), (p1, p2))\
USE_FUNCTION (glUniform2f, void, (GLint p1, GLfloat p2, GLfloat p3), (p1, p2, p3))\
USE_FUNCTION (glUniform3f, void, (GLint p1, GLfloat p2, GLfloat p3, GLfloat p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniform4f, void, (GLint p1, GLfloat p2, GLfloat p3, GLfloat p4, GLfloat p5), (p1, p2, p3, p4, p5))\
USE_FUNCTION (glUniform4i, void, (GLint p1, GLint p2, GLint p3, GLint p4, GLint p5), (p1, p2, p3, p4, p5))\
USE_FUNCTION (glUniform1fv, void, (GLint p1, GLsizei p2, const GLfloat* p3), (p1, p2, p3))\
USE_FUNCTION (glUniformMatrix2fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniformMatrix3fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))\
USE_FUNCTION (glUniformMatrix4fv, void, (GLint p1, GLsizei p2, GLboolean p3, const GLfloat* p4), (p1, p2, p3, p4))
/** 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.
*/
struct OpenGLExtensionFunctions
{
void initialise();
#if JUCE_WINDOWS && ! DOXYGEN
typedef char GLchar;
typedef pointer_sized_int GLsizeiptr;
typedef pointer_sized_int GLintptr;
#endif
//==============================================================================
#if JUCE_WINDOWS
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
//==============================================================================
#elif JUCE_LINUX
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) typedef returnType (*type_ ## name) params; type_ ## name name;
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
//==============================================================================
#elif JUCE_OPENGL_ES
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) static returnType name params;
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
//==============================================================================
#else
#define JUCE_DECLARE_GL_FUNCTION(name, returnType, params, callparams) inline static returnType name params { return ::name callparams; }
#if JUCE_OPENGL3
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION)
#else
#define JUCE_DECLARE_GL_FUNCTION_EXT(name, returnType, params, callparams) inline static returnType name params { return ::name ## EXT callparams; }
JUCE_GL_EXTENSION_FUNCTIONS (JUCE_DECLARE_GL_FUNCTION, JUCE_DECLARE_GL_FUNCTION_EXT)
#undef JUCE_DECLARE_GL_FUNCTION_EXT
#endif
#endif
#undef JUCE_DECLARE_GL_FUNCTION
};
#endif // JUCE_OPENGLEXTENSIONS_H_INCLUDED

View file

@ -0,0 +1,182 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
METHOD (layout, "layout", "(IIII)V") \
METHOD (requestRender, "requestRender", "()V") \
DECLARE_JNI_CLASS (OpenGLView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$OpenGLView");
#undef JNI_CLASS_MEMBERS
extern jobject createOpenGLView (ComponentPeer*);
//==============================================================================
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& comp,
const OpenGLPixelFormat& pixelFormat,
void* /*contextToShareWith*/,
bool /*useMultisampling*/,
OpenGLVersion)
: component (comp),
isInsideGLCallback (false)
{
{
const ScopedLock sl (contextListLock);
glView = GlobalRef (createOpenGLView (component.getPeer()));
contextList.add (this);
}
updateWindowPosition (component.getTopLevelComponent()
->getLocalArea (&component, component.getLocalBounds()));
}
~NativeContext()
{
{
const ScopedLock sl (contextListLock);
contextList.removeFirstMatchingValue (this);
}
android.activity.callVoidMethod (JuceAppActivity.deleteView, glView.get());
glView.clear();
}
void initialiseOnRenderThread (OpenGLContext&) {}
void shutdownOnRenderThread() {}
bool makeActive() const noexcept { return isInsideGLCallback; }
bool isActive() const noexcept { return isInsideGLCallback; }
static void deactivateCurrentContext() {}
void swapBuffers() const noexcept {}
bool setSwapInterval (const int) { return false; }
int getSwapInterval() const { return 0; }
bool createdOk() const noexcept { return getRawContext() != nullptr; }
void* getRawContext() const noexcept { return glView.get(); }
GLuint getFrameBufferID() const noexcept { return 0; }
void updateWindowPosition (const Rectangle<int>& bounds)
{
if (lastBounds != bounds)
{
lastBounds = bounds;
glView.callVoidMethod (OpenGLView.layout,
bounds.getX(), bounds.getY(),
bounds.getRight(), bounds.getBottom());
}
}
void triggerRepaint()
{
glView.callVoidMethod (OpenGLView.requestRender);
}
//==============================================================================
void contextCreatedCallback();
void contextChangedSize() {}
void renderCallback();
//==============================================================================
static NativeContext* findContextFor (JNIEnv* env, jobject glView)
{
const ScopedLock sl (contextListLock);
for (int i = contextList.size(); --i >= 0;)
{
NativeContext* const c = contextList.getUnchecked(i);
if (env->IsSameObject (c->glView.get(), glView))
return c;
}
return nullptr;
}
static NativeContext* getActiveContext() noexcept
{
const ScopedLock sl (contextListLock);
for (int i = contextList.size(); --i >= 0;)
{
NativeContext* const c = contextList.getUnchecked(i);
if (c->isInsideGLCallback)
return c;
}
return nullptr;
}
struct Locker { Locker (NativeContext&) {} };
Component& component;
private:
GlobalRef glView;
Rectangle<int> lastBounds;
bool isInsideGLCallback;
typedef Array<NativeContext*> ContextArray;
static CriticalSection contextListLock;
static ContextArray contextList;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
CriticalSection OpenGLContext::NativeContext::contextListLock;
OpenGLContext::NativeContext::ContextArray OpenGLContext::NativeContext::contextList;
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
return OpenGLContext::NativeContext::getActiveContext() != nullptr;
}
//==============================================================================
#define GL_VIEW_CLASS_NAME JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024OpenGLView)
JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextCreated, void, (JNIEnv* env, jobject view))
{
threadLocalJNIEnvHolder.getOrAttach();
if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
context->contextCreatedCallback();
else
jassertfalse;
}
JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, contextChangedSize, void, (JNIEnv* env, jobject view))
{
if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
context->contextChangedSize();
}
JUCE_JNI_CALLBACK (GL_VIEW_CLASS_NAME, render, void, (JNIEnv* env, jobject view))
{
if (OpenGLContext::NativeContext* const context = OpenGLContext::NativeContext::findContextFor (env, view))
context->renderCallback();
}

View file

@ -0,0 +1,294 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
} // (juce namespace)
@interface JuceGLView : UIView
{
}
+ (Class) layerClass;
@end
@implementation JuceGLView
+ (Class) layerClass
{
return [CAEAGLLayer class];
}
@end
namespace juce
{
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixFormat,
void* contextToShare,
bool multisampling,
OpenGLVersion version)
: context (nil), frameBufferHandle (0), colorBufferHandle (0),
depthBufferHandle (0), msaaColorHandle (0), msaaBufferHandle (0),
lastWidth (0), lastHeight (0), needToRebuildBuffers (false),
swapFrames (0), useDepthBuffer (pixFormat.depthBufferBits > 0),
useMSAA (multisampling)
{
JUCE_AUTORELEASEPOOL
{
ComponentPeer* const peer = component.getPeer();
jassert (peer != nullptr);
const Rectangle<int> bounds (peer->getComponent().getLocalArea (&component, component.getLocalBounds()));
lastWidth = bounds.getWidth();
lastHeight = bounds.getHeight();
view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
view.opaque = YES;
view.hidden = NO;
view.backgroundColor = [UIColor blackColor];
view.userInteractionEnabled = NO;
glLayer = (CAEAGLLayer*) [view layer];
glLayer.contentsScale = Desktop::getInstance().getDisplays().getMainDisplay().scale;
glLayer.opaque = true;
[((UIView*) peer->getNativeHandle()) addSubview: view];
#if defined (__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
if (version == OpenGLContext::openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
{
if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
{
releaseContext();
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
}
}
else
#endif
{
createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
}
jassert (context != nil);
// I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
// so causes myserious timing-related failures.
[EAGLContext setCurrentContext: context];
createGLBuffers();
deactivateCurrentContext();
}
}
~NativeContext()
{
releaseContext();
[view removeFromSuperview];
[view release];
}
void initialiseOnRenderThread (OpenGLContext&) {}
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 defined (__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
glBlitFramebuffer (0, 0, lastWidth, lastHeight, 0, 0, lastWidth, lastHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
#else
glResolveMultisampleFramebufferAPPLE();
#endif
}
glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
[context presentRenderbuffer: GL_RENDERBUFFER];
if (needToRebuildBuffers)
{
needToRebuildBuffers = false;
freeGLBuffers();
createGLBuffers();
makeActive();
}
}
void updateWindowPosition (const Rectangle<int>& bounds)
{
view.frame = convertToCGRect (bounds);
if (lastWidth != bounds.getWidth() || lastHeight != bounds.getHeight())
{
lastWidth = bounds.getWidth();
lastHeight = bounds.getHeight();
needToRebuildBuffers = true;
}
}
bool setSwapInterval (const int numFramesPerSwap) noexcept
{
swapFrames = numFramesPerSwap;
return false;
}
int getSwapInterval() const noexcept { return swapFrames; }
struct Locker { Locker (NativeContext&) {} };
private:
JuceGLView* view;
CAEAGLLayer* glLayer;
EAGLContext* context;
GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle,
msaaColorHandle, msaaBufferHandle;
int volatile lastWidth, lastHeight;
bool volatile needToRebuildBuffers;
int swapFrames;
bool useDepthBuffer, useMSAA;
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); (void) 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;
}

View file

@ -0,0 +1,195 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
extern Display* display;
extern XContext windowHandleXContext;
//==============================================================================
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixelFormat,
void* shareContext,
bool /*useMultisampling*/,
OpenGLVersion)
: renderContext (0), embeddedWindow (0), swapFrames (0), bestVisual (0),
contextToShareWith (shareContext)
{
ScopedXLock xlock;
XSync (display, False);
GLint attribs[] =
{
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_RED_SIZE, pixelFormat.redBits,
GLX_GREEN_SIZE, pixelFormat.greenBits,
GLX_BLUE_SIZE, pixelFormat.blueBits,
GLX_ALPHA_SIZE, pixelFormat.alphaBits,
GLX_DEPTH_SIZE, pixelFormat.depthBufferBits,
GLX_STENCIL_SIZE, pixelFormat.stencilBufferBits,
GLX_ACCUM_RED_SIZE, pixelFormat.accumulationBufferRedBits,
GLX_ACCUM_GREEN_SIZE, pixelFormat.accumulationBufferGreenBits,
GLX_ACCUM_BLUE_SIZE, pixelFormat.accumulationBufferBlueBits,
GLX_ACCUM_ALPHA_SIZE, pixelFormat.accumulationBufferAlphaBits,
None
};
bestVisual = glXChooseVisual (display, DefaultScreen (display), attribs);
if (bestVisual == nullptr)
return;
ComponentPeer* const peer = component.getPeer();
Window windowH = (Window) peer->getNativeHandle();
Colormap colourMap = XCreateColormap (display, windowH, bestVisual->visual, AllocNone);
XSetWindowAttributes swa;
swa.colormap = colourMap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | StructureNotifyMask;
const Rectangle<int> bounds (component.getTopLevelComponent()
->getLocalArea (&component, component.getLocalBounds()));
embeddedWindow = XCreateWindow (display, windowH,
bounds.getX(), bounds.getY(),
jmax (1, bounds.getWidth()),
jmax (1, bounds.getHeight()),
0, bestVisual->depth,
InputOutput,
bestVisual->visual,
CWBorderPixel | CWColormap | CWEventMask,
&swa);
XSaveContext (display, (XID) embeddedWindow, windowHandleXContext, (XPointer) peer);
XMapWindow (display, embeddedWindow);
XFreeColormap (display, colourMap);
XSync (display, False);
}
~NativeContext()
{
if (embeddedWindow != 0)
{
ScopedXLock xlock;
XUnmapWindow (display, embeddedWindow);
XDestroyWindow (display, embeddedWindow);
}
if (bestVisual != nullptr)
XFree (bestVisual);
}
void initialiseOnRenderThread (OpenGLContext& context)
{
ScopedXLock xlock;
renderContext = glXCreateContext (display, bestVisual, (GLXContext) contextToShareWith, GL_TRUE);
context.makeActive();
}
void shutdownOnRenderThread()
{
deactivateCurrentContext();
glXDestroyContext (display, renderContext);
renderContext = nullptr;
}
bool makeActive() const noexcept
{
return renderContext != 0
&& glXMakeCurrent (display, embeddedWindow, renderContext);
}
bool isActive() const noexcept
{
return glXGetCurrentContext() == renderContext && renderContext != 0;
}
static void deactivateCurrentContext()
{
glXMakeCurrent (display, None, 0);
}
void swapBuffers()
{
glXSwapBuffers (display, embeddedWindow);
}
void updateWindowPosition (const Rectangle<int>& newBounds)
{
bounds = newBounds;
ScopedXLock xlock;
XMoveResizeWindow (display, embeddedWindow,
bounds.getX(), bounds.getY(),
jmax (1, bounds.getWidth()),
jmax (1, bounds.getHeight()));
}
bool setSwapInterval (int numFramesPerSwap)
{
if (numFramesPerSwap == swapFrames)
return true;
PFNGLXSWAPINTERVALSGIPROC GLXSwapIntervalSGI
= (PFNGLXSWAPINTERVALSGIPROC) OpenGLHelpers::getExtensionFunction ("glXSwapIntervalSGI");
if (GLXSwapIntervalSGI != nullptr)
{
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; }
struct Locker { Locker (NativeContext&) {} };
private:
GLXContext renderContext;
Window embeddedWindow;
int swapFrames;
Rectangle<int> bounds;
XVisualInfo* bestVisual;
void* contextToShareWith;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
};
//==============================================================================
bool OpenGLHelpers::isContextActive()
{
ScopedXLock xlock;
return glXGetCurrentContext() != 0;
}

View file

@ -0,0 +1,225 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixFormat,
void* contextToShare,
bool /*useMultisampling*/,
OpenGLVersion version)
: lastSwapTime (0), minSwapTimeMs (0), underrunCounter (0)
{
(void) version;
NSOpenGLPixelFormatAttribute attribs[] =
{
#if JUCE_OPENGL3
NSOpenGLPFAOpenGLProfile, version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core : NSOpenGLProfileVersionLegacy,
#endif
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAClosestPolicy,
NSOpenGLPFANoRecovery,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits),
NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits,
NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits,
NSOpenGLPFAAccumSize, (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
+ pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits),
pixFormat.multisamplingLevel > 0 ? NSOpenGLPFASamples : (NSOpenGLPixelFormatAttribute) 0,
(NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel,
0
};
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
static MouseForwardingNSOpenGLViewClass cls;
view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
pixelFormat: format];
#if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
[view setWantsBestResolutionOpenGLSurface: YES];
#endif
[[NSNotificationCenter defaultCenter] addObserver: view
selector: @selector (_surfaceNeedsUpdate:)
name: NSViewGlobalFrameDidChangeNotification
object: view];
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];
renderContext = nil;
}
void initialiseOnRenderThread (OpenGLContext&) {}
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()
{
[renderContext flushBuffer];
sleepIfRenderingTooFast();
}
void updateWindowPosition (const Rectangle<int>&) {}
bool setSwapInterval (int numFramesPerSwap)
{
minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
[renderContext setValues: (const GLint*) &numFramesPerSwap
forParameter: NSOpenGLCPSwapInterval];
return true;
}
int getSwapInterval() const
{
GLint numFrames = 0;
[renderContext getValues: &numFrames
forParameter: NSOpenGLCPSwapInterval];
return numFrames;
}
void sleepIfRenderingTooFast()
{
// When our window is entirely occluded by other windows, the system
// fails to correctly implement the swap interval time, so the render
// loop spins at full speed, burning CPU. This hack detects when things
// are going too fast and slows things down if necessary.
if (minSwapTimeMs > 0)
{
const double now = Time::getMillisecondCounterHiRes();
const int elapsed = (int) (now - lastSwapTime);
lastSwapTime = now;
if (isPositiveAndBelow (elapsed, minSwapTimeMs - 3))
{
if (underrunCounter > 3)
Thread::sleep (minSwapTimeMs - elapsed);
else
++underrunCounter;
}
else
{
underrunCounter = 0;
}
}
}
NSOpenGLContext* renderContext;
NSOpenGLView* view;
ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
double lastSwapTime;
int minSwapTimeMs, underrunCounter;
//==============================================================================
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() != 0;
}

View file

@ -0,0 +1,266 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component&, void* parent);
//==============================================================================
class OpenGLContext::NativeContext
{
public:
NativeContext (Component& component,
const OpenGLPixelFormat& pixelFormat,
void* contextToShareWith,
bool /*useMultisampling*/,
OpenGLVersion)
: context (nullptr)
{
dummyComponent = new DummyComponent (*this);
createNativeWindow (component);
PIXELFORMATDESCRIPTOR pfd;
initialisePixelFormatDescriptor (pfd, pixelFormat);
const int pixFormat = ChoosePixelFormat (dc, &pfd);
if (pixFormat != 0)
SetPixelFormat (dc, pixFormat, &pfd);
renderContext = wglCreateContext (dc);
if (renderContext != 0)
{
makeActive();
initialiseGLExtensions();
const int 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 (contextToShareWith != nullptr)
wglShareLists ((HGLRC) contextToShareWith, renderContext);
component.getTopLevelComponent()->repaint();
component.repaint();
}
}
~NativeContext()
{
deleteRenderContext();
releaseDC();
}
void initialiseOnRenderThread (OpenGLContext& c) { context = &c; }
void shutdownOnRenderThread() { deactivateCurrentContext(); context = nullptr; }
static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
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 (const Rectangle<int>& bounds)
{
if (nativeWindow != nullptr)
SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
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&) {} };
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;
};
ScopedPointer<DummyComponent> dummyComponent;
ScopedPointer<ComponentPeer> nativeWindow;
HGLRC renderContext;
HDC dc;
OpenGLContext* context;
#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 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)
{
Component* topComp = component.getTopLevelComponent();
nativeWindow = createNonRepaintingEmbeddedWindowsPeer (*dummyComponent, topComp->getWindowHandle());
if (ComponentPeer* peer = topComp->getPeer())
updateWindowPosition (peer->getAreaCoveredBy (component));
nativeWindow->setVisible (true);
dc = GetDC ((HWND) nativeWindow->getNativeHandle());
}
void deleteRenderContext()
{
if (renderContext != 0)
{
wglDeleteContext (renderContext);
renderContext = 0;
}
}
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() != 0;
}

View file

@ -0,0 +1,955 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
class OpenGLContext::CachedImage : public CachedComponentImage,
public Thread
{
public:
CachedImage (OpenGLContext& c, Component& comp,
const OpenGLPixelFormat& pixFormat, void* contextToShare)
: Thread ("OpenGL Rendering"),
context (c), component (comp),
scale (1.0),
#if JUCE_OPENGL3
vertexArrayObject (0),
#endif
#if JUCE_OPENGL_ES
shadersAvailable (true),
#else
shadersAvailable (false),
#endif
hasInitialised (false),
needsUpdate (1), lastMMLockReleaseTime (0)
{
nativeContext = new NativeContext (component, pixFormat, contextToShare,
c.useMultisampling, c.versionRequired);
if (nativeContext->createdOk())
context.nativeContext = nativeContext;
else
nativeContext = nullptr;
}
~CachedImage()
{
stop();
}
void start()
{
#if ! JUCE_ANDROID
if (nativeContext != nullptr)
startThread (6);
#endif
}
void stop()
{
#if ! JUCE_ANDROID
stopThread (10000);
#endif
hasInitialised = false;
}
//==============================================================================
void paint (Graphics&) override {}
bool invalidateAll() override
{
validArea.clear();
triggerRepaint();
return false;
}
bool invalidate (const Rectangle<int>& area) override
{
validArea.subtract (area * scale);
triggerRepaint();
return false;
}
void releaseResources() override {}
void triggerRepaint()
{
needsUpdate = 1;
#if JUCE_ANDROID
if (nativeContext != nullptr)
nativeContext->triggerRepaint();
#else
notify();
#endif
}
//==============================================================================
bool ensureFrameBufferSize()
{
const int fbW = cachedImageFrameBuffer.getWidth();
const int fbH = cachedImageFrameBuffer.getHeight();
if (fbW != viewportArea.getWidth() || fbH != viewportArea.getHeight() || ! cachedImageFrameBuffer.isValid())
{
if (! cachedImageFrameBuffer.initialise (context, viewportArea.getWidth(), viewportArea.getHeight()))
return false;
validArea.clear();
JUCE_CHECK_OPENGL_ERROR
}
return true;
}
void clearRegionInFrameBuffer (const RectangleList<int>& list)
{
glClearColor (0, 0, 0, 0);
glEnable (GL_SCISSOR_TEST);
const GLuint previousFrameBufferTarget = OpenGLFrameBuffer::getCurrentFrameBufferTarget();
cachedImageFrameBuffer.makeCurrentRenderingTarget();
const int imageH = cachedImageFrameBuffer.getHeight();
for (const Rectangle<int>* i = list.begin(), * const e = list.end(); i != e; ++i)
{
glScissor (i->getX(), imageH - i->getBottom(), i->getWidth(), i->getHeight());
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
glDisable (GL_SCISSOR_TEST);
context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, previousFrameBufferTarget);
JUCE_CHECK_OPENGL_ERROR
}
bool renderFrame()
{
ScopedPointer<MessageManagerLock> mmLock;
const bool isUpdating = needsUpdate.compareAndSetBool (0, 1);
if (context.renderComponents && isUpdating)
{
// This avoids hogging the message thread when doing intensive rendering.
if (lastMMLockReleaseTime + 1 >= Time::getMillisecondCounter())
wait (2);
mmLock = new MessageManagerLock (this); // need to acquire this before locking the context.
if (! mmLock->lockWasGained())
return false;
updateViewportSize (false);
}
if (! context.makeActive())
return false;
NativeContext::Locker locker (*nativeContext);
JUCE_CHECK_OPENGL_ERROR
if (context.renderer != nullptr)
{
glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
context.currentRenderScale = scale;
context.renderer->renderOpenGL();
clearGLError();
}
if (context.renderComponents)
{
if (isUpdating)
{
paintComponent();
mmLock = nullptr;
lastMMLockReleaseTime = Time::getMillisecondCounter();
}
glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
drawComponentBuffer();
}
context.swapBuffers();
return true;
}
void updateViewportSize (bool canTriggerUpdate)
{
if (ComponentPeer* peer = component.getPeer())
{
lastScreenBounds = component.getTopLevelComponent()->getScreenBounds();
const double newScale = Desktop::getInstance().getDisplays()
.getDisplayContaining (lastScreenBounds.getCentre()).scale;
Rectangle<int> newArea (peer->getComponent().getLocalArea (&component, component.getLocalBounds())
.withZeroOrigin()
* newScale);
if (scale != newScale || viewportArea != newArea)
{
scale = newScale;
viewportArea = newArea;
if (canTriggerUpdate)
invalidateAll();
}
}
}
void checkViewportBounds()
{
const Rectangle<int> screenBounds (component.getTopLevelComponent()->getScreenBounds());
if (lastScreenBounds != screenBounds)
updateViewportSize (true);
}
void paintComponent()
{
// you mustn't set your own cached image object when attaching a GL context!
jassert (get (component) == this);
if (! ensureFrameBufferSize())
return;
RectangleList<int> invalid (viewportArea);
invalid.subtract (validArea);
validArea = viewportArea;
if (! invalid.isEmpty())
{
clearRegionInFrameBuffer (invalid);
{
ScopedPointer<LowLevelGraphicsContext> g (createOpenGLGraphicsContext (context, cachedImageFrameBuffer));
g->clipToRectangleList (invalid);
g->addTransform (AffineTransform::scale ((float) scale));
paintOwner (*g);
JUCE_CHECK_OPENGL_ERROR
}
if (! context.isActive())
context.makeActive();
}
JUCE_CHECK_OPENGL_ERROR
}
void drawComponentBuffer()
{
#if ! JUCE_ANDROID
glEnable (GL_TEXTURE_2D);
clearGLError();
#endif
#if JUCE_WINDOWS
// some stupidly old drivers are missing this function, so try to at least avoid a crash here,
// but if you hit this assertion you may want to have your own version check before using the
// component rendering stuff on such old drivers.
jassert (context.extensions.glActiveTexture != nullptr);
if (context.extensions.glActiveTexture != nullptr)
#endif
context.extensions.glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, cachedImageFrameBuffer.getTextureID());
const Rectangle<int> cacheBounds (cachedImageFrameBuffer.getWidth(), cachedImageFrameBuffer.getHeight());
context.copyTexture (cacheBounds, cacheBounds, cacheBounds.getWidth(), cacheBounds.getHeight(), false);
glBindTexture (GL_TEXTURE_2D, 0);
JUCE_CHECK_OPENGL_ERROR
}
void paintOwner (LowLevelGraphicsContext& llgc)
{
Graphics g (llgc);
#if JUCE_ENABLE_REPAINT_DEBUGGING
#ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
#endif
{
g.saveState();
}
#endif
JUCE_TRY
{
component.paintEntireComponent (g, false);
}
JUCE_CATCH_EXCEPTION
#if JUCE_ENABLE_REPAINT_DEBUGGING
#ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
#endif
{
// enabling this code will fill all areas that get repainted with a colour overlay, to show
// clearly when things are being repainted.
g.restoreState();
static Random rng;
g.fillAll (Colour ((uint8) rng.nextInt (255),
(uint8) rng.nextInt (255),
(uint8) rng.nextInt (255),
(uint8) 0x50));
}
#endif
}
void handleResize()
{
updateViewportSize (true);
#if JUCE_MAC
if (hasInitialised)
{
[nativeContext->view update];
renderFrame();
}
#endif
}
//==============================================================================
void run() override
{
{
// Allow the message thread to finish setting-up the context before using it..
MessageManagerLock mml (this);
if (! mml.lockWasGained())
return;
}
initialiseOnThread();
hasInitialised = true;
while (! threadShouldExit())
{
#if JUCE_IOS
// NB: on iOS, all GL calls will crash when the app is running
// in the background..
if (! Process::isForegroundProcess())
{
wait (500);
continue;
}
#endif
if (! renderFrame())
wait (5); // failed to render, so avoid a tight fail-loop.
else if (! context.continuousRepaint)
wait (-1);
}
shutdownOnThread();
}
void initialiseOnThread()
{
// On android, this can get called twice, so drop any previous state..
associatedObjectNames.clear();
associatedObjects.clear();
cachedImageFrameBuffer.release();
context.makeActive();
nativeContext->initialiseOnRenderThread (context);
#if JUCE_OPENGL3
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
{
glGenVertexArrays (1, &vertexArrayObject);
glBindVertexArray (vertexArrayObject);
}
#endif
glViewport (0, 0, component.getWidth(), component.getHeight());
context.extensions.initialise();
nativeContext->setSwapInterval (1);
#if ! JUCE_OPENGL_ES
shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
#endif
if (context.renderer != nullptr)
context.renderer->newOpenGLContextCreated();
}
void shutdownOnThread()
{
if (context.renderer != nullptr)
context.renderer->openGLContextClosing();
#if JUCE_OPENGL3
if (vertexArrayObject != 0)
glDeleteVertexArrays (1, &vertexArrayObject);
#endif
cachedImageFrameBuffer.release();
nativeContext->shutdownOnRenderThread();
associatedObjectNames.clear();
associatedObjects.clear();
}
//==============================================================================
static CachedImage* get (Component& c) noexcept
{
return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
}
//==============================================================================
ScopedPointer<NativeContext> nativeContext;
OpenGLContext& context;
Component& component;
OpenGLFrameBuffer cachedImageFrameBuffer;
RectangleList<int> validArea;
Rectangle<int> viewportArea, lastScreenBounds;
double scale;
#if JUCE_OPENGL3
GLuint vertexArrayObject;
#endif
StringArray associatedObjectNames;
ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
WaitableEvent canPaintNowFlag, finishedPaintingFlag;
bool shadersAvailable, hasInitialised;
Atomic<int> needsUpdate;
uint32 lastMMLockReleaseTime;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
};
//==============================================================================
#if JUCE_ANDROID
void OpenGLContext::NativeContext::contextCreatedCallback()
{
isInsideGLCallback = true;
if (CachedImage* const c = CachedImage::get (component))
c->initialiseOnThread();
else
jassertfalse;
isInsideGLCallback = false;
}
void OpenGLContext::NativeContext::renderCallback()
{
isInsideGLCallback = true;
if (CachedImage* const c = CachedImage::get (component))
{
if (c->context.continuousRepaint)
c->context.triggerRepaint();
c->renderFrame();
}
isInsideGLCallback = false;
}
#endif
//==============================================================================
class OpenGLContext::Attachment : public ComponentMovementWatcher,
private Timer
{
public:
Attachment (OpenGLContext& c, Component& comp)
: ComponentMovementWatcher (&comp), context (c)
{
if (canBeAttached (comp))
attach();
}
~Attachment()
{
detach();
}
void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
{
Component& comp = *getComponent();
if (isAttached (comp) != canBeAttached (comp))
componentVisibilityChanged();
if (comp.getWidth() > 0 && comp.getHeight() > 0
&& context.nativeContext != nullptr)
{
if (CachedImage* const c = CachedImage::get (comp))
c->handleResize();
if (ComponentPeer* peer = comp.getTopLevelComponent()->getPeer())
context.nativeContext->updateWindowPosition (peer->getAreaCoveredBy (comp));
}
}
void componentPeerChanged() override
{
detach();
componentVisibilityChanged();
}
void componentVisibilityChanged() override
{
Component& comp = *getComponent();
if (canBeAttached (comp))
{
if (isAttached (comp))
comp.repaint(); // (needed when windows are un-minimised)
else
attach();
}
else
{
detach();
}
}
#if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
void componentBeingDeleted (Component& c) override
{
/* You must call detach() or delete your OpenGLContext to remove it
from a component BEFORE deleting the component that it is using!
*/
jassertfalse;
ComponentMovementWatcher::componentBeingDeleted (c);
}
#endif
private:
OpenGLContext& context;
static bool canBeAttached (const Component& comp) noexcept
{
return comp.getWidth() > 0 && comp.getHeight() > 0 && isShowingOrMinimised (comp);
}
static bool isShowingOrMinimised (const Component& c)
{
if (! c.isVisible())
return false;
if (Component* p = c.getParentComponent())
return isShowingOrMinimised (*p);
return c.getPeer() != nullptr;
}
static bool isAttached (const Component& comp) noexcept
{
return comp.getCachedComponentImage() != nullptr;
}
void attach()
{
Component& comp = *getComponent();
CachedImage* const newCachedImage = new CachedImage (context, comp,
context.pixelFormat,
context.contextToShareWith);
comp.setCachedComponentImage (newCachedImage);
newCachedImage->start(); // (must wait until this is attached before starting its thread)
newCachedImage->updateViewportSize (true);
startTimer (400);
}
void detach()
{
stopTimer();
Component& comp = *getComponent();
#if JUCE_MAC
[[(NSView*) comp.getWindowHandle() window] disableScreenUpdatesUntilFlush];
#endif
if (CachedImage* const oldCachedImage = CachedImage::get (comp))
oldCachedImage->stop(); // (must stop this before detaching it from the component)
comp.setCachedComponentImage (nullptr);
context.nativeContext = nullptr;
}
void timerCallback() override
{
if (CachedImage* const cachedImage = CachedImage::get (*getComponent()))
cachedImage->checkViewportBounds();
}
};
//==============================================================================
OpenGLContext::OpenGLContext()
: nativeContext (nullptr), renderer (nullptr), currentRenderScale (1.0),
contextToShareWith (nullptr), versionRequired (OpenGLContext::defaultGLVersion),
renderComponents (true), useMultisampling (false), continuousRepaint (false)
{
}
OpenGLContext::~OpenGLContext()
{
detach();
}
void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
{
// This method must not be called when the context has already been attached!
// Call it before attaching your context, or use detach() first, before calling this!
jassert (nativeContext == nullptr);
renderer = rendererToUse;
}
void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
{
// This method must not be called when the context has already been attached!
// Call it before attaching your context, or use detach() first, before calling this!
jassert (nativeContext == nullptr);
renderComponents = shouldPaintComponent;
}
void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept
{
continuousRepaint = shouldContinuouslyRepaint;
triggerRepaint();
}
void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
{
// This method must not be called when the context has already been attached!
// Call it before attaching your context, or use detach() first, before calling this!
jassert (nativeContext == nullptr);
pixelFormat = preferredPixelFormat;
}
void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
{
// This method must not be called when the context has already been attached!
// Call it before attaching your context, or use detach() first, before calling this!
jassert (nativeContext == nullptr);
contextToShareWith = nativeContextToShareWith;
}
void OpenGLContext::setMultisamplingEnabled (bool b) noexcept
{
// This method must not be called when the context has already been attached!
// Call it before attaching your context, or use detach() first, before calling this!
jassert (nativeContext == nullptr);
useMultisampling = b;
}
void OpenGLContext::setOpenGLVersionRequired (OpenGLVersion v) noexcept
{
versionRequired = v;
}
void OpenGLContext::attachTo (Component& component)
{
component.repaint();
if (getTargetComponent() != &component)
{
detach();
attachment = new Attachment (*this, component);
}
}
void OpenGLContext::detach()
{
attachment = nullptr;
nativeContext = nullptr;
}
bool OpenGLContext::isAttached() const noexcept
{
return nativeContext != nullptr;
}
Component* OpenGLContext::getTargetComponent() const noexcept
{
return attachment != nullptr ? attachment->getComponent() : nullptr;
}
static ThreadLocalValue<OpenGLContext*> currentThreadActiveContext;
OpenGLContext* OpenGLContext::getCurrentContext()
{
return currentThreadActiveContext.get();
}
bool OpenGLContext::makeActive() const noexcept
{
OpenGLContext*& current = currentThreadActiveContext.get();
if (nativeContext != nullptr && nativeContext->makeActive())
{
current = const_cast<OpenGLContext*> (this);
return true;
}
current = nullptr;
return false;
}
bool OpenGLContext::isActive() const noexcept
{
return nativeContext != nullptr && nativeContext->isActive();
}
void OpenGLContext::deactivateCurrentContext()
{
NativeContext::deactivateCurrentContext();
currentThreadActiveContext.get() = nullptr;
}
void OpenGLContext::triggerRepaint()
{
if (CachedImage* const cachedImage = getCachedImage())
cachedImage->triggerRepaint();
}
void OpenGLContext::swapBuffers()
{
if (nativeContext != nullptr)
nativeContext->swapBuffers();
}
unsigned int OpenGLContext::getFrameBufferID() const noexcept
{
return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
}
bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
{
return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
}
int OpenGLContext::getSwapInterval() const
{
return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
}
void* OpenGLContext::getRawContext() const noexcept
{
return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
}
OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
{
if (Component* const comp = getTargetComponent())
return CachedImage::get (*comp);
return nullptr;
}
bool OpenGLContext::areShadersAvailable() const
{
CachedImage* const c = getCachedImage();
return c != nullptr && c->shadersAvailable;
}
ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
{
jassert (name != nullptr);
CachedImage* const c = getCachedImage();
// This method must only be called from an openGL rendering callback.
jassert (c != nullptr && nativeContext != nullptr);
jassert (getCurrentContext() != nullptr);
const int index = c->associatedObjectNames.indexOf (name);
return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
}
void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
{
jassert (name != nullptr);
if (CachedImage* const c = getCachedImage())
{
// This method must only be called from an openGL rendering callback.
jassert (nativeContext != nullptr);
jassert (getCurrentContext() != nullptr);
const int index = c->associatedObjectNames.indexOf (name);
if (index >= 0)
{
if (newObject != nullptr)
{
c->associatedObjects.set (index, newObject);
}
else
{
c->associatedObjectNames.remove (index);
c->associatedObjects.remove (index);
}
}
else if (newObject != nullptr)
{
c->associatedObjectNames.add (name);
c->associatedObjects.add (newObject);
}
}
}
void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
const Rectangle<int>& anchorPosAndTextureSize,
const int contextWidth, const int contextHeight,
bool flippedVertically)
{
if (contextWidth <= 0 || contextHeight <= 0)
return;
JUCE_CHECK_OPENGL_ERROR
glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_BLEND);
if (areShadersAvailable())
{
struct OverlayShaderProgram : public ReferenceCountedObject
{
OverlayShaderProgram (OpenGLContext& context)
: program (context), builder (program), params (program)
{}
static const OverlayShaderProgram& select (OpenGLContext& context)
{
static const char programValueID[] = "juceGLComponentOverlayShader";
OverlayShaderProgram* program = static_cast <OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
if (program == nullptr)
{
program = new OverlayShaderProgram (context);
context.setAssociatedObject (programValueID, program);
}
program->program.use();
return *program;
}
struct ProgramBuilder
{
ProgramBuilder (OpenGLShaderProgram& prog)
{
prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (
"attribute " JUCE_HIGHP " vec2 position;"
"uniform " JUCE_HIGHP " vec2 screenSize;"
"uniform " JUCE_HIGHP " float textureBounds[4];"
"uniform " JUCE_HIGHP " vec2 vOffsetAndScale;"
"varying " JUCE_HIGHP " vec2 texturePos;"
"void main()"
"{"
JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
"gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
"texturePos = (position - vec2 (textureBounds[0], textureBounds[1])) / vec2 (textureBounds[2], textureBounds[3]);"
"texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);"
"}"));
prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (
"uniform sampler2D imageTexture;"
"varying " JUCE_HIGHP " vec2 texturePos;"
"void main()"
"{"
"gl_FragColor = texture2D (imageTexture, texturePos);"
"}"));
prog.link();
}
};
struct Params
{
Params (OpenGLShaderProgram& prog)
: positionAttribute (prog, "position"),
screenSize (prog, "screenSize"),
imageTexture (prog, "imageTexture"),
textureBounds (prog, "textureBounds"),
vOffsetAndScale (prog, "vOffsetAndScale")
{}
void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds, bool flipVertically) const
{
const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
textureBounds.set (m, 4);
imageTexture.set (0);
screenSize.set (targetWidth, targetHeight);
vOffsetAndScale.set (flipVertically ? 0.0f : 1.0f,
flipVertically ? 1.0f : -1.0f);
}
OpenGLShaderProgram::Attribute positionAttribute;
OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale;
};
OpenGLShaderProgram program;
ProgramBuilder builder;
Params params;
};
const GLshort left = (GLshort) targetClipArea.getX();
const GLshort top = (GLshort) targetClipArea.getY();
const GLshort right = (GLshort) targetClipArea.getRight();
const GLshort bottom = (GLshort) targetClipArea.getBottom();
const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat(), flippedVertically);
GLuint vertexBuffer = 0;
extensions.glGenBuffers (1, &vertexBuffer);
extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
extensions.glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
const GLuint index = (GLuint) program.params.positionAttribute.attributeID;
extensions.glVertexAttribPointer (index, 2, GL_SHORT, GL_FALSE, 4, 0);
extensions.glEnableVertexAttribArray (index);
JUCE_CHECK_OPENGL_ERROR
glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
extensions.glUseProgram (0);
extensions.glDisableVertexAttribArray (index);
extensions.glDeleteBuffers (1, &vertexBuffer);
}
else
{
jassert (attachment == nullptr); // Running on an old graphics card!
}
JUCE_CHECK_OPENGL_ERROR
}

View file

@ -0,0 +1,288 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLCONTEXT_H_INCLUDED
#define JUCE_OPENGLCONTEXT_H_INCLUDED
//==============================================================================
/**
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
*/
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;
//==============================================================================
/** 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;
/** 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;
/** 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;
//==============================================================================
/** Returns the scale factor used by the display that is being rendered.
The scale is that of the display - see Desktop::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 underlting 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);
//==============================================================================
#ifndef DOXYGEN
class NativeContext;
#endif
private:
class CachedImage;
class Attachment;
NativeContext* nativeContext;
OpenGLRenderer* renderer;
double currentRenderScale;
ScopedPointer<Attachment> attachment;
OpenGLPixelFormat pixelFormat;
void* contextToShareWith;
OpenGLVersion versionRequired;
bool renderComponents, useMultisampling, continuousRepaint;
CachedImage* getCachedImage() const noexcept;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
};
#endif // JUCE_OPENGLCONTEXT_H_INCLUDED

View file

@ -0,0 +1,347 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
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
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, 0);
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) ? GL_DEPTH24_STENCIL8
#if JUCE_OPENGL_ES
: GL_DEPTH_COMPONENT16,
#else
: GL_DEPTH_COMPONENT,
#endif
width, height);
GLint params = 0;
context.extensions.glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_DEPTH_SIZE, &params);
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 = nullptr;
pimpl = new Pimpl (context, width, height, false, false);
if (! pimpl->createdOk())
pimpl = nullptr;
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)
{
const Pimpl* const p = other.pimpl;
if (p == nullptr)
{
pimpl = nullptr;
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 = nullptr;
savedState = nullptr;
}
void OpenGLFrameBuffer::saveAndRelease()
{
if (pimpl != nullptr)
{
savedState = new SavedState (*this, pimpl->width, pimpl->height);
pimpl = nullptr;
}
}
bool OpenGLFrameBuffer::reloadSavedCopy (OpenGLContext& context)
{
if (savedState != nullptr)
{
ScopedPointer<SavedState> state (savedState);
if (state->restore (context, *this))
return true;
savedState = state;
}
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
{
return pimpl != nullptr ? pimpl->frameBufferID : 0;
}
GLuint OpenGLFrameBuffer::getCurrentFrameBufferTarget()
{
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;
}

View file

@ -0,0 +1,134 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLFRAMEBUFFER_H_INCLUDED
#define JUCE_OPENGLFRAMEBUFFER_H_INCLUDED
//==============================================================================
/**
Creates an openGL frame buffer.
*/
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;
/** Returns the current frame buffer ID for the current context. */
static GLuint getCurrentFrameBufferTarget();
/** 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;
friend struct ContainerDeletePolicy<Pimpl>;
ScopedPointer<Pimpl> pimpl;
class SavedState;
friend struct ContainerDeletePolicy<SavedState>;
ScopedPointer<SavedState> savedState;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLFrameBuffer)
};
#endif // JUCE_OPENGLFRAMEBUFFER_H_INCLUDED

View file

@ -0,0 +1,96 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLGRAPHICSCONTEXT_H_INCLUDED
#define JUCE_OPENGLGRAPHICSCONTEXT_H_INCLUDED
/** Creates a graphics context object that will render into the given OpenGL target.
The caller is responsible for deleting this object when no longer needed.
*/
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext& target,
int width, int height);
/** Creates a graphics context object that will render into the given OpenGL target.
The caller is responsible for deleting this object when no longer needed.
*/
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext& context,
OpenGLFrameBuffer& target);
/** Creates a graphics context object that will render into the given OpenGL target.
The caller is responsible for deleting this object when no longer needed.
*/
LowLevelGraphicsContext* createOpenGLGraphicsContext (OpenGLContext& context,
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.
*/
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&, const 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; }
private:
String code, hashName;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLGraphicsContextCustomShader)
};
#endif // JUCE_OPENGLGRAPHICSCONTEXT_H_INCLUDED

View file

@ -0,0 +1,103 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
void OpenGLHelpers::resetErrorState()
{
while (glGetError() != GL_NO_ERROR) {}
}
void* OpenGLHelpers::getExtensionFunction (const char* functionName)
{
#if JUCE_WINDOWS
return (void*) wglGetProcAddress (functionName);
#elif JUCE_LINUX
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 (const Rectangle<int>& clip)
{
glEnable (GL_SCISSOR_TEST);
glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
}
String OpenGLHelpers::translateVertexShaderToV3 (const String& code)
{
#if JUCE_OPENGL3
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
return JUCE_GLSL_VERSION "\n" + code.replace ("attribute", "in")
.replace ("varying", "out");
#endif
return code;
}
String OpenGLHelpers::translateFragmentShaderToV3 (const String& code)
{
#if JUCE_OPENGL3
if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
return JUCE_GLSL_VERSION "\n"
"out vec4 fragColor;\n"
+ code.replace ("varying", "in")
.replace ("texture2D", "texture")
.replace ("gl_FragColor", "fragColor");
#endif
return code;
}

View file

@ -0,0 +1,73 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLHELPERS_H_INCLUDED
#define JUCE_OPENGLHELPERS_H_INCLUDED
//==============================================================================
/**
A set of miscellaneous openGL helper functions.
*/
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 (const 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);
/** 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&);
};
#endif // JUCE_OPENGLHELPERS_H_INCLUDED

View file

@ -0,0 +1,206 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
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()
{
return frameBuffer.initialise (context, width, height);
}
LowLevelGraphicsContext* createLowLevelContext() override
{
sendDataChangeMessage();
return createOpenGLGraphicsContext (context, frameBuffer);
}
ImageType* createType() const override { return new OpenGLImageType(); }
ImagePixelData* clone() override
{
OpenGLFrameBufferImage* im = new OpenGLFrameBufferImage (context, width, height);
im->incReferenceCount();
{
Image newImage (im);
Graphics g (newImage);
g.drawImageAt (Image (this), 0, 0, false);
}
im->resetReferenceCount();
return im;
}
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 ((size_t) w);
const size_t rowSize = sizeof (PixelARGB) * (size_t) w;
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 ((size_t) (area.getWidth() * area.getHeight()));
const size_t rowSize = sizeof (PixelARGB) * (size_t) area.getWidth();
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)
{
DataReleaser* r = new DataReleaser (frameBuffer, x, y, bitmapData.width, bitmapData.height);
bitmapData.dataReleaser = r;
bitmapData.data = (uint8*) r->data.getData();
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!
ScopedPointer<OpenGLFrameBufferImage> im (new OpenGLFrameBufferImage (*currentContext, width, height));
if (! im->initialise())
return ImagePixelData::Ptr();
im->frameBuffer.clear (Colours::transparentBlack);
return im.release();
}
OpenGLFrameBuffer* OpenGLImageType::getFrameBufferFrom (const Image& image)
{
if (OpenGLFrameBufferImage* const glImage = dynamic_cast<OpenGLFrameBufferImage*> (image.getPixelData()))
return &(glImage->frameBuffer);
return nullptr;
}

View file

@ -0,0 +1,52 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLIMAGE_H_INCLUDED
#define JUCE_OPENGLIMAGE_H_INCLUDED
//==============================================================================
/**
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
*/
class JUCE_API OpenGLImageType : public ImageType
{
public:
OpenGLImageType();
~OpenGLImageType();
ImagePixelData::Ptr create (Image::PixelFormat, int width, int height, bool shouldClearImage) const override;
int getTypeID() const override;
static OpenGLFrameBuffer* getFrameBufferFrom (const Image&);
};
#endif // JUCE_OPENGLIMAGE_H_INCLUDED

View file

@ -0,0 +1,61 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
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)
{
}
bool OpenGLPixelFormat::operator== (const OpenGLPixelFormat& other) const noexcept
{
return redBits == other.redBits
&& greenBits == other.greenBits
&& blueBits == other.blueBits
&& alphaBits == other.alphaBits
&& depthBufferBits == other.depthBufferBits
&& stencilBufferBits == other.stencilBufferBits
&& accumulationBufferRedBits == other.accumulationBufferRedBits
&& accumulationBufferGreenBits == other.accumulationBufferGreenBits
&& accumulationBufferBlueBits == other.accumulationBufferBlueBits
&& accumulationBufferAlphaBits == other.accumulationBufferAlphaBits
&& multisamplingLevel == other.multisamplingLevel;
}
bool OpenGLPixelFormat::operator!= (const OpenGLPixelFormat& other) const noexcept
{
return ! operator== (other);
}

View file

@ -0,0 +1,70 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLPIXELFORMAT_H_INCLUDED
#define JUCE_OPENGLPIXELFORMAT_H_INCLUDED
//==============================================================================
/**
Represents the various properties of an OpenGL pixel format.
@see OpenGLContext::setPixelFormat
*/
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). */
};
#endif // JUCE_OPENGLPIXELFORMAT_H_INCLUDED

View file

@ -0,0 +1,72 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLRENDERER_H_INCLUDED
#define JUCE_OPENGLRENDERER_H_INCLUDED
//==============================================================================
/**
A base class that should be implemented by classes which want to render openGL
on a background thread.
@see OpenGLContext
*/
class JUCE_API OpenGLRenderer
{
public:
OpenGLRenderer() {}
virtual ~OpenGLRenderer() {}
/** 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, not the message
thread, so make sure that your implementation is thread-safe.
For information about how to trigger a render callback, see
OpenGLContext::triggerRepaint() and OpenGLContext::setContinuousRepainting().
*/
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;
};
#endif // JUCE_OPENGLRENDERER_H_INCLUDED

View file

@ -0,0 +1,161 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
OpenGLShaderProgram::OpenGLShaderProgram (const OpenGLContext& c) noexcept
: context (c), programID (0)
{
}
OpenGLShaderProgram::~OpenGLShaderProgram() noexcept
{
release();
}
GLuint OpenGLShaderProgram::getProgramID() const noexcept
{
// This method can only be used when the current thread has an active OpenGL context.
jassert (OpenGLHelpers::isContextActive());
if (programID == 0)
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 == GL_FALSE)
{
GLchar infoLog [16384];
GLsizei infoLogLength = 0;
context.extensions.glGetShaderInfoLog (shaderID, sizeof (infoLog), &infoLogLength, infoLog);
errorLog = String (infoLog, (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 == GL_FALSE)
{
GLchar infoLog [16384];
GLsizei infoLogLength = 0;
context.extensions.glGetProgramInfoLog (progID, sizeof (infoLog), &infoLogLength, infoLog);
errorLog = String (infoLog, (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 != GL_FALSE;
}
void OpenGLShaderProgram::use() const noexcept
{
context.extensions.glUseProgram (programID);
}
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
}
OpenGLShaderProgram::Attribute::Attribute (const OpenGLShaderProgram& program, const char* name)
: attributeID (program.context.extensions.glGetAttribLocation (program.getProgramID(), name))
{
#if JUCE_DEBUG && ! JUCE_DONT_ASSERT_ON_GLSL_COMPILE_ERROR
jassert (attributeID >= 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, float 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); }

View file

@ -0,0 +1,167 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLSHADERPROGRAM_H_INCLUDED
#define JUCE_OPENGLSHADERPROGRAM_H_INCLUDED
//==============================================================================
/**
Manages an OpenGL shader program.
*/
class JUCE_API OpenGLShaderProgram
{
public:
OpenGLShaderProgram (const OpenGLContext&) noexcept;
~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;
/** 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 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, float 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 Attribute
{
/** Initialises an attribute.
The program must have been successfully linked when this
constructor is called.
*/
Attribute (const OpenGLShaderProgram& program, const char* attributeName);
/** The attribute's ID number.
If the uniform couldn't be found, this value will be < 0.
*/
GLint attributeID;
};
/** The ID number of the compiled program. */
GLuint getProgramID() const noexcept;
private:
const OpenGLContext& context;
mutable GLuint programID;
String errorLog;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLShaderProgram)
};
#endif // JUCE_OPENGLSHADERPROGRAM_H_INCLUDED

View file

@ -0,0 +1,193 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
static int getAllowedTextureSize (int x)
{
#if JUCE_OPENGL_ALLOW_NON_POWER_OF_TWO_TEXTURES
return x;
#else
return nextPowerOfTwo (x);
#endif
}
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);
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
}
else
{
glBindTexture (GL_TEXTURE_2D, textureID);
JUCE_CHECK_OPENGL_ERROR;
}
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
JUCE_CHECK_OPENGL_ERROR
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 ((size_t) (w * h));
for (int y = 0; y < h; ++y)
{
const PixelType* src = (const PixelType*) srcData;
PixelARGB* const dst = (PixelARGB*) (dataCopy + w * (h - 1 - y));
for (int x = 0; x < w; ++x)
{
#if JUCE_ANDROID
PixelType s (src[x]);
dst[x].setARGB (s.getAlpha(), s.getBlue(), s.getGreen(), s.getRed());
#else
dst[x].set (src[x]);
#endif
}
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;
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);
}

View file

@ -0,0 +1,102 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLTEXTURE_H_INCLUDED
#define JUCE_OPENGLTEXTURE_H_INCLUDED
//==============================================================================
/**
Creates an openGL texture from an Image.
*/
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)
};
#endif // JUCE_OPENGLTEXTURE_H_INCLUDED

View file

@ -0,0 +1,52 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
OpenGLAppComponent::OpenGLAppComponent() : frameCounter (0)
{
setOpaque (true);
openGLContext.setRenderer (this);
openGLContext.attachTo (*this);
openGLContext.setContinuousRepainting (true);
}
OpenGLAppComponent::~OpenGLAppComponent()
{
openGLContext.detach();
}
void OpenGLAppComponent::newOpenGLContextCreated()
{
initialise();
}
void OpenGLAppComponent::renderOpenGL()
{
++frameCounter;
render();
}
void OpenGLAppComponent::openGLContextClosing()
{
shutdown();
}

View file

@ -0,0 +1,77 @@
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2013 - Raw Material Software Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#ifndef JUCE_OPENGLAPPCOMPONENT_H_INCLUDED
#define JUCE_OPENGLAPPCOMPONENT_H_INCLUDED
//==============================================================================
/**
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.
*/
class OpenGLAppComponent : public Component,
private OpenGLRenderer
{
public:
OpenGLAppComponent();
~OpenGLAppComponent();
/** Returns the number of times that the render method has been called since
the component started running.
*/
int getFrameCounter() const noexcept { return frameCounter; }
/** 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.
*/
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.
*/
virtual void shutdown() = 0;
/**
*/
virtual void render() = 0;
private:
//==============================================================================
OpenGLContext openGLContext;
int frameCounter;
void newOpenGLContextCreated() override;
void renderOpenGL() override;
void openGLContextClosing() override;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLAppComponent)
};
#endif // JUCE_OPENGLAPPCOMPONENT_H_INCLUDED