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.

321 lines
11KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. void OpenGLHelpers::resetErrorState()
  21. {
  22. while (glGetError() != GL_NO_ERROR) {}
  23. }
  24. void* OpenGLHelpers::getExtensionFunction (const char* functionName)
  25. {
  26. #if JUCE_WINDOWS
  27. return (void*) wglGetProcAddress (functionName);
  28. #elif JUCE_LINUX
  29. return (void*) glXGetProcAddress ((const GLubyte*) functionName);
  30. #else
  31. static void* handle = dlopen (nullptr, RTLD_LAZY);
  32. return dlsym (handle, functionName);
  33. #endif
  34. }
  35. #if ! JUCE_OPENGL_ES
  36. namespace
  37. {
  38. bool isExtensionSupportedV3 (const char* extensionName)
  39. {
  40. #ifndef GL_NUM_EXTENSIONS
  41. enum { GL_NUM_EXTENSIONS = 0x821d };
  42. #endif
  43. JUCE_DECLARE_GL_EXTENSION_FUNCTION (glGetStringi, const GLubyte*, (GLenum, GLuint))
  44. if (glGetStringi == nullptr)
  45. glGetStringi = (type_glGetStringi) OpenGLHelpers::getExtensionFunction ("glGetStringi");
  46. if (glGetStringi != nullptr)
  47. {
  48. GLint numExtensions = 0;
  49. glGetIntegerv (GL_NUM_EXTENSIONS, &numExtensions);
  50. for (int i = 0; i < numExtensions; ++i)
  51. if (strcmp (extensionName, (const char*) glGetStringi (GL_EXTENSIONS, i)) == 0)
  52. return true;
  53. }
  54. return false;
  55. }
  56. }
  57. #endif
  58. bool OpenGLHelpers::isExtensionSupported (const char* const extensionName)
  59. {
  60. jassert (extensionName != nullptr); // you must supply a genuine string for this.
  61. jassert (isContextActive()); // An OpenGL context will need to be active before calling this.
  62. #if ! JUCE_OPENGL_ES
  63. const GLubyte* version = glGetString (GL_VERSION);
  64. if (version != nullptr && version[0] >= '3')
  65. {
  66. return isExtensionSupportedV3 (extensionName);
  67. }
  68. else
  69. #endif
  70. {
  71. const char* extensions = (const char*) glGetString (GL_EXTENSIONS);
  72. jassert (extensions != nullptr); // Perhaps you didn't activate an OpenGL context before calling this?
  73. for (;;)
  74. {
  75. const char* found = strstr (extensions, extensionName);
  76. if (found == nullptr)
  77. break;
  78. extensions = found + strlen (extensionName);
  79. if (extensions[0] == ' ' || extensions[0] == 0)
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. void OpenGLHelpers::clear (const Colour& colour)
  86. {
  87. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  88. colour.getFloatBlue(), colour.getFloatAlpha());
  89. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  90. }
  91. void OpenGLHelpers::setColour (const Colour& colour)
  92. {
  93. glColor4f (colour.getFloatRed(), colour.getFloatGreen(),
  94. colour.getFloatBlue(), colour.getFloatAlpha());
  95. }
  96. void OpenGLHelpers::prepareFor2D (const int width, const int height)
  97. {
  98. glMatrixMode (GL_PROJECTION);
  99. glLoadIdentity();
  100. #if JUCE_OPENGL_ES
  101. glOrthof (0.0f, (GLfloat) width, 0.0f, (GLfloat) height, 0.0f, 1.0f);
  102. #else
  103. glOrtho (0.0, width, 0.0, height, 0, 1);
  104. #endif
  105. glViewport (0, 0, width, height);
  106. }
  107. void OpenGLHelpers::setPerspective (double fovy, double aspect, double zNear, double zFar)
  108. {
  109. glLoadIdentity();
  110. #if JUCE_OPENGL_ES
  111. const GLfloat ymax = (GLfloat) (zNear * tan (fovy * double_Pi / 360.0));
  112. const GLfloat ymin = -ymax;
  113. glFrustumf (ymin * (GLfloat) aspect, ymax * (GLfloat) aspect, ymin, ymax, (GLfloat) zNear, (GLfloat) zFar);
  114. #else
  115. const double ymax = zNear * tan (fovy * double_Pi / 360.0);
  116. const double ymin = -ymax;
  117. glFrustum (ymin * aspect, ymax * aspect, ymin, ymax, zNear, zFar);
  118. #endif
  119. }
  120. void OpenGLHelpers::applyTransform (const AffineTransform& t)
  121. {
  122. const GLfloat m[] = { t.mat00, t.mat10, 0, 0,
  123. t.mat01, t.mat11, 0, 0,
  124. 0, 0, 1, 0,
  125. t.mat02, t.mat12, 0, 1 };
  126. glMultMatrixf (m);
  127. }
  128. void OpenGLHelpers::enableScissorTest (const Rectangle<int>& clip)
  129. {
  130. glEnable (GL_SCISSOR_TEST);
  131. glScissor (clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
  132. }
  133. void OpenGLHelpers::drawQuad2D (float x1, float y1,
  134. float x2, float y2,
  135. float x3, float y3,
  136. float x4, float y4,
  137. const Colour& colour)
  138. {
  139. const GLfloat vertices[] = { x1, y1, x2, y2, x4, y4, x3, y3 };
  140. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  141. setColour (colour);
  142. glEnableClientState (GL_VERTEX_ARRAY);
  143. glVertexPointer (2, GL_FLOAT, 0, vertices);
  144. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  145. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  146. glDisableClientState (GL_COLOR_ARRAY);
  147. glDisableClientState (GL_NORMAL_ARRAY);
  148. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  149. }
  150. void OpenGLHelpers::drawQuad3D (float x1, float y1, float z1,
  151. float x2, float y2, float z2,
  152. float x3, float y3, float z3,
  153. float x4, float y4, float z4,
  154. const Colour& colour)
  155. {
  156. const GLfloat vertices[] = { x1, y1, z1, x2, y2, z2, x4, y4, z4, x3, y3, z3 };
  157. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  158. setColour (colour);
  159. glEnableClientState (GL_VERTEX_ARRAY);
  160. glVertexPointer (3, GL_FLOAT, 0, vertices);
  161. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  162. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  163. glDisableClientState (GL_COLOR_ARRAY);
  164. glDisableClientState (GL_NORMAL_ARRAY);
  165. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  166. }
  167. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords, const int numVertices) noexcept
  168. {
  169. glEnable (GL_TEXTURE_2D);
  170. glDisableClientState (GL_COLOR_ARRAY);
  171. glDisableClientState (GL_NORMAL_ARRAY);
  172. glEnableClientState (GL_VERTEX_ARRAY);
  173. glVertexPointer (2, GL_FLOAT, 0, vertices);
  174. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  175. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  176. glDrawArrays (GL_TRIANGLE_STRIP, 0, numVertices);
  177. }
  178. void OpenGLHelpers::drawTriangleStrip (const GLfloat* const vertices, const GLfloat* const textureCoords,
  179. const int numVertices, const GLuint textureID) noexcept
  180. {
  181. jassert (textureID != 0);
  182. glBindTexture (GL_TEXTURE_2D, textureID);
  183. drawTriangleStrip (vertices, textureCoords, numVertices);
  184. glBindTexture (GL_TEXTURE_2D, 0);
  185. }
  186. void OpenGLHelpers::drawTextureQuad (GLuint textureID, const Rectangle<int>& rect)
  187. {
  188. const GLfloat l = (GLfloat) rect.getX();
  189. const GLfloat t = (GLfloat) rect.getY();
  190. const GLfloat r = (GLfloat) rect.getRight();
  191. const GLfloat b = (GLfloat) rect.getBottom();
  192. const GLfloat vertices[] = { l, t, r, t, l, b, r, b };
  193. const GLfloat textureCoords[] = { 0, 1.0f, 1.0f, 1.0f, 0, 0, 1.0f, 0 };
  194. drawTriangleStrip (vertices, textureCoords, 4, textureID);
  195. }
  196. void OpenGLHelpers::fillRectWithTexture (const Rectangle<int>& rect, GLuint textureID, const float alpha)
  197. {
  198. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  199. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  200. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  201. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  202. glColor4f (alpha, alpha, alpha, alpha);
  203. drawTextureQuad (textureID, rect);
  204. }
  205. //==============================================================================
  206. void OpenGLHelpers::fillRectWithColour (const Rectangle<int>& rect, const Colour& colour)
  207. {
  208. glEnableClientState (GL_VERTEX_ARRAY);
  209. glDisableClientState (GL_TEXTURE_COORD_ARRAY);
  210. glDisableClientState (GL_COLOR_ARRAY);
  211. glDisableClientState (GL_NORMAL_ARRAY);
  212. setColour (colour);
  213. fillRect (rect);
  214. }
  215. void OpenGLHelpers::fillRect (const Rectangle<int>& rect)
  216. {
  217. const GLfloat vertices[] = { (GLfloat) rect.getX(), (GLfloat) rect.getY(),
  218. (GLfloat) rect.getRight(), (GLfloat) rect.getY(),
  219. (GLfloat) rect.getX(), (GLfloat) rect.getBottom(),
  220. (GLfloat) rect.getRight(), (GLfloat) rect.getBottom() };
  221. glVertexPointer (2, GL_FLOAT, 0, vertices);
  222. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  223. }
  224. //==============================================================================
  225. OpenGLTextureFromImage::OpenGLTextureFromImage (const Image& image)
  226. : imageWidth (image.getWidth()),
  227. imageHeight (image.getHeight())
  228. {
  229. OpenGLFrameBuffer* const fb = OpenGLImageType::getFrameBufferFrom (image);
  230. if (fb != nullptr)
  231. {
  232. textureID = fb->getTextureID();
  233. fullWidthProportion = 1.0f;
  234. fullHeightProportion = 1.0f;
  235. }
  236. else
  237. {
  238. texture = new OpenGLTexture();
  239. texture->loadImage (image);
  240. textureID = texture->getTextureID();
  241. fullWidthProportion = imageWidth / (float) texture->getWidth();
  242. fullHeightProportion = imageHeight / (float) texture->getHeight();
  243. }
  244. }
  245. OpenGLTextureFromImage::~OpenGLTextureFromImage() {}
  246. //==============================================================================
  247. OpenGLRenderingTarget::OpenGLRenderingTarget() {}
  248. OpenGLRenderingTarget::~OpenGLRenderingTarget() {}
  249. void OpenGLRenderingTarget::prepareFor2D()
  250. {
  251. OpenGLHelpers::prepareFor2D (getRenderingTargetWidth(),
  252. getRenderingTargetHeight());
  253. }
  254. END_JUCE_NAMESPACE