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.

283 lines
9.4KB

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