The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
9.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. void OpenGLHelpers::resetErrorState()
  19. {
  20. while (glGetError() != GL_NO_ERROR) {}
  21. }
  22. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  23. {
  24. #if JUCE_WINDOWS
  25. return (void*) wglGetProcAddress (functionName);
  26. #elif JUCE_LINUX
  27. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  28. #else
  29. static void* handle = dlopen (nullptr, RTLD_LAZY);
  30. return dlsym (handle, functionName);
  31. #endif
  32. }
  33. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  34. {
  35. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  36. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  37. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  38. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  39. for (;;)
  40. {
  41. const char* found = strstr (extensions, extensionName);
  42. if (found == nullptr)
  43. break;
  44. extensions = found + strlen (extensionName);
  45. if (extensions[0] == ' ' || extensions[0] == 0)
  46. return true;
  47. }
  48. return false;
  49. }
  50. void OpenGLHelpers::clear (const Colour& colour)
  51. {
  52. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  53. colour.getFloatBlue(), colour.getFloatAlpha());
  54. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  55. }
  56. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  57. void OpenGLHelpers::setColour (const Colour& colour)
  58. {
  59. glColor4f (colour.getFloatRed(), colour.getFloatGreen(),
  60. colour.getFloatBlue(), colour.getFloatAlpha());
  61. }
  62. void OpenGLHelpers::prepareFor2D (const int width, const int height)
  63. {
  64. glMatrixMode (GL_PROJECTION);
  65. glLoadIdentity();
  66. #if JUCE_OPENGL_ES
  67. glOrthof (0.0f, (GLfloat) width, 0.0f, (GLfloat) height, 0.0f, 1.0f);
  68. #else
  69. glOrtho (0.0, width, 0.0, height, 0, 1);
  70. #endif
  71. glViewport (0, 0, width, height);
  72. }
  73. void OpenGLHelpers::setPerspective (double fovy, double aspect, double zNear, double zFar)
  74. {
  75. glLoadIdentity();
  76. #if JUCE_OPENGL_ES
  77. const GLfloat ymax = (GLfloat) (zNear * tan (fovy * double_Pi / 360.0));
  78. const GLfloat ymin = -ymax;
  79. glFrustumf (ymin * (GLfloat) aspect, ymax * (GLfloat) aspect, ymin, ymax, (GLfloat) zNear, (GLfloat) zFar);
  80. #else
  81. const double ymax = zNear * tan (fovy * double_Pi / 360.0);
  82. const double ymin = -ymax;
  83. glFrustum (ymin * aspect, ymax * aspect, ymin, ymax, zNear, zFar);
  84. #endif
  85. }
  86. void OpenGLHelpers::applyTransform (const AffineTransform& t)
  87. {
  88. const GLfloat m[] = { t.mat00, t.mat10, 0, 0,
  89. t.mat01, t.mat11, 0, 0,
  90. 0, 0, 1, 0,
  91. t.mat02, t.mat12, 0, 1 };
  92. glMultMatrixf (m);
  93. }
  94. void OpenGLHelpers::applyMatrix (const float matrixValues[16])
  95. {
  96. glMultMatrixf (matrixValues);
  97. }
  98. #if ! JUCE_OPENGL_ES
  99. void OpenGLHelpers::applyMatrix (const double matrixValues[16])
  100. {
  101. glMultMatrixd (matrixValues);
  102. }
  103. #endif
  104. #endif
  105. void OpenGLHelpers::enableScissorTest (const Rectangle<int>& clip)
  106. {
  107. glEnable (GL_SCISSOR_TEST);
  108. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  109. }
  110. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  111. void OpenGLHelpers::drawQuad2D (float x1, float y1,
  112. float x2, float y2,
  113. float x3, float y3,
  114. float x4, float y4,
  115. const Colour& colour)
  116. {
  117. const GLfloat vertices[] = { x1, y1, x2, y2, x4, y4, x3, y3 };
  118. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  119. setColour (colour);
  120. glEnableClientState (GL_VERTEX_ARRAY);
  121. glVertexPointer (2, GL_FLOAT, 0, vertices);
  122. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  123. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  124. glDisableClientState (GL_COLOR_ARRAY);
  125. glDisableClientState (GL_NORMAL_ARRAY);
  126. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  127. }
  128. void OpenGLHelpers::drawQuad3D (float x1, float y1, float z1,
  129. float x2, float y2, float z2,
  130. float x3, float y3, float z3,
  131. float x4, float y4, float z4,
  132. const Colour& colour)
  133. {
  134. const GLfloat vertices[] = { x1, y1, z1, x2, y2, z2, x4, y4, z4, x3, y3, z3 };
  135. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  136. setColour (colour);
  137. glEnableClientState (GL_VERTEX_ARRAY);
  138. glVertexPointer (3, GL_FLOAT, 0, vertices);
  139. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  140. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  141. glDisableClientState (GL_COLOR_ARRAY);
  142. glDisableClientState (GL_NORMAL_ARRAY);
  143. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  144. }
  145. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords, const int numVertices) noexcept
  146. {
  147. #if ! JUCE_ANDROID
  148. glEnable (GL_TEXTURE_2D);
  149. clearGLError();
  150. #endif
  151. glDisableClientState (GL_COLOR_ARRAY);
  152. glDisableClientState (GL_NORMAL_ARRAY);
  153. glEnableClientState (GL_VERTEX_ARRAY);
  154. glVertexPointer (2, GL_FLOAT, 0, vertices);
  155. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  156. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  157. glDrawArrays (GL_TRIANGLE_STRIP, 0, numVertices);
  158. }
  159. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords,
  160. const int numVertices, const GLuint textureID) noexcept
  161. {
  162. jassert (textureID != 0);
  163. glBindTexture (GL_TEXTURE_2D, textureID);
  164. drawTriangleStrip (vertices, textureCoords, numVertices);
  165. glBindTexture (GL_TEXTURE_2D, 0);
  166. }
  167. void OpenGLHelpers::drawTextureQuad (GLuint textureID, const Rectangle<int>& rect)
  168. {
  169. const GLfloat l = (GLfloat) rect.getX();
  170. const GLfloat t = (GLfloat) rect.getY();
  171. const GLfloat r = (GLfloat) rect.getRight();
  172. const GLfloat b = (GLfloat) rect.getBottom();
  173. const GLfloat vertices[] = { l, t, r, t, l, b, r, b };
  174. const GLfloat textureCoords[] = { 0, 1.0f, 1.0f, 1.0f, 0, 0, 1.0f, 0 };
  175. drawTriangleStrip (vertices, textureCoords, 4, textureID);
  176. }
  177. void OpenGLHelpers::fillRectWithTexture (const Rectangle<int>& rect, GLuint textureID, const float alpha)
  178. {
  179. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  180. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  181. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  182. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  183. glColor4f (alpha, alpha, alpha, alpha);
  184. drawTextureQuad (textureID, rect);
  185. }
  186. void OpenGLHelpers::fillRectWithColour (const Rectangle<int>& rect, const Colour& colour)
  187. {
  188. glEnableClientState (GL_VERTEX_ARRAY);
  189. glDisableClientState (GL_TEXTURE_COORD_ARRAY);
  190. glDisableClientState (GL_COLOR_ARRAY);
  191. glDisableClientState (GL_NORMAL_ARRAY);
  192. setColour (colour);
  193. fillRect (rect);
  194. }
  195. void OpenGLHelpers::fillRect (const Rectangle<int>& rect)
  196. {
  197. const GLfloat vertices[] = { (GLfloat) rect.getX(), (GLfloat) rect.getY(),
  198. (GLfloat) rect.getRight(), (GLfloat) rect.getY(),
  199. (GLfloat) rect.getX(), (GLfloat) rect.getBottom(),
  200. (GLfloat) rect.getRight(), (GLfloat) rect.getBottom() };
  201. glVertexPointer (2, GL_FLOAT, 0, vertices);
  202. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  203. }
  204. #endif
  205. //==============================================================================
  206. OpenGLTextureFromImage::OpenGLTextureFromImage (const Image& image)
  207. : imageWidth (image.getWidth()),
  208. imageHeight (image.getHeight())
  209. {
  210. JUCE_CHECK_OPENGL_ERROR
  211. if (OpenGLFrameBuffer* const fb = OpenGLImageType::getFrameBufferFrom (image))
  212. {
  213. textureID = fb->getTextureID();
  214. fullWidthProportion = 1.0f;
  215. fullHeightProportion = 1.0f;
  216. }
  217. else
  218. {
  219. texture = new OpenGLTexture();
  220. texture->loadImage (image);
  221. textureID = texture->getTextureID();
  222. fullWidthProportion = imageWidth / (float) texture->getWidth();
  223. fullHeightProportion = imageHeight / (float) texture->getHeight();
  224. }
  225. JUCE_CHECK_OPENGL_ERROR
  226. }
  227. OpenGLTextureFromImage::~OpenGLTextureFromImage() {}