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.

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