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.

266 lines
10KB

  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. void OpenGLHelpers::resetErrorState()
  20. {
  21. while (glGetError() != GL_NO_ERROR) {}
  22. }
  23. void OpenGLHelpers::clear (const Colour& colour)
  24. {
  25. glClearColor (colour.getFloatRed(), colour.getFloatGreen(),
  26. colour.getFloatBlue(), colour.getFloatAlpha());
  27. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  28. }
  29. void OpenGLHelpers::setColour (const Colour& colour)
  30. {
  31. glColor4f (colour.getFloatRed(), colour.getFloatGreen(),
  32. colour.getFloatBlue(), colour.getFloatAlpha());
  33. }
  34. void OpenGLHelpers::prepareFor2D (int width, int height)
  35. {
  36. glMatrixMode (GL_PROJECTION);
  37. glLoadIdentity();
  38. #if JUCE_OPENGL_ES
  39. glOrthof (0.0f, (float) width, 0.0f, (float) height, 0.0f, 1.0f);
  40. #else
  41. glOrtho (0.0, width, 0.0, height, 0, 1);
  42. #endif
  43. glViewport (0, 0, width, height);
  44. }
  45. void OpenGLHelpers::setPerspective (double fovy, double aspect, double zNear, double zFar)
  46. {
  47. glLoadIdentity();
  48. #if JUCE_OPENGL_ES
  49. const float ymax = (float) (zNear * tan (fovy * double_Pi / 360.0));
  50. const float ymin = -ymax;
  51. glFrustumf (ymin * (float) aspect, ymax * (float) aspect, ymin, ymax, (float) zNear, (float) zFar);
  52. #else
  53. const double ymax = zNear * tan (fovy * double_Pi / 360.0);
  54. const double ymin = -ymax;
  55. glFrustum (ymin * aspect, ymax * aspect, ymin, ymax, zNear, zFar);
  56. #endif
  57. }
  58. void OpenGLHelpers::drawQuad2D (float x1, float y1,
  59. float x2, float y2,
  60. float x3, float y3,
  61. float x4, float y4,
  62. const Colour& colour)
  63. {
  64. const GLfloat vertices[] = { x1, y1, x2, y2, x4, y4, x3, y3 };
  65. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  66. setColour (colour);
  67. glEnableClientState (GL_VERTEX_ARRAY);
  68. glVertexPointer (2, GL_FLOAT, 0, vertices);
  69. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  70. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  71. glDisableClientState (GL_COLOR_ARRAY);
  72. glDisableClientState (GL_NORMAL_ARRAY);
  73. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  74. }
  75. void OpenGLHelpers::drawQuad3D (float x1, float y1, float z1,
  76. float x2, float y2, float z2,
  77. float x3, float y3, float z3,
  78. float x4, float y4, float z4,
  79. const Colour& colour)
  80. {
  81. const GLfloat vertices[] = { x1, y1, z1, x2, y2, z2, x4, y4, z4, x3, y3, z3 };
  82. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  83. setColour (colour);
  84. glEnableClientState (GL_VERTEX_ARRAY);
  85. glVertexPointer (3, GL_FLOAT, 0, vertices);
  86. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  87. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  88. glDisableClientState (GL_COLOR_ARRAY);
  89. glDisableClientState (GL_NORMAL_ARRAY);
  90. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  91. }
  92. namespace OpenGLGradientHelpers
  93. {
  94. void drawTriangles (GLenum mode, const GLfloat* vertices, const GLfloat* textureCoords, const int numElements)
  95. {
  96. glEnableClientState (GL_VERTEX_ARRAY);
  97. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  98. glDisableClientState (GL_COLOR_ARRAY);
  99. glDisableClientState (GL_NORMAL_ARRAY);
  100. glVertexPointer (2, GL_FLOAT, 0, vertices);
  101. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  102. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  103. glDrawArrays (mode, 0, numElements);
  104. }
  105. void fillWithLinearGradient (const Rectangle<int>& rect,
  106. const ColourGradient& grad,
  107. const AffineTransform& transform,
  108. const int textureSize)
  109. {
  110. const Point<float> p1 (grad.point1.transformedBy (transform));
  111. const Point<float> p2 (grad.point2.transformedBy (transform));
  112. const Point<float> p3 (Point<float> (grad.point1.getX() - (grad.point2.getY() - grad.point1.getY()) / textureSize,
  113. grad.point1.getY() + (grad.point2.getX() - grad.point1.getX()) / textureSize).transformedBy (transform));
  114. const AffineTransform textureTransform (AffineTransform::fromTargetPoints (p1.getX(), p1.getY(), 0.0f, 0.0f,
  115. p2.getX(), p2.getY(), 1.0f, 0.0f,
  116. p3.getX(), p3.getY(), 0.0f, 1.0f));
  117. const float l = (float) rect.getX();
  118. const float r = (float) rect.getRight();
  119. const float t = (float) rect.getY();
  120. const float b = (float) rect.getBottom();
  121. const GLfloat vertices[] = { l, t, r, t, l, b, r, b };
  122. GLfloat textureCoords[] = { l, t, r, t, l, b, r, b };
  123. textureTransform.transformPoints (textureCoords[0], textureCoords[1], textureCoords[2], textureCoords[3]);
  124. textureTransform.transformPoints (textureCoords[4], textureCoords[5], textureCoords[6], textureCoords[7]);
  125. drawTriangles (GL_TRIANGLE_STRIP, vertices, textureCoords, 4);
  126. }
  127. void fillWithRadialGradient (const Rectangle<int>& rect,
  128. const ColourGradient& grad,
  129. const AffineTransform& transform)
  130. {
  131. const Point<float> centre (grad.point1.transformedBy (transform));
  132. const float screenRadius = centre.getDistanceFrom (rect.getCentre().toFloat())
  133. + Point<int> (rect.getWidth() / 2,
  134. rect.getHeight() / 2).getDistanceFromOrigin()
  135. + 8.0f;
  136. const AffineTransform inverse (transform.inverted());
  137. const float sourceRadius = jmax (Point<float> (screenRadius, 0.0f).transformedBy (inverse).getDistanceFromOrigin(),
  138. Point<float> (0.0f, screenRadius).transformedBy (inverse).getDistanceFromOrigin());
  139. const int numDivisions = 80;
  140. GLfloat vertices [6 + numDivisions * 4];
  141. GLfloat textureCoords [6 + numDivisions * 4];
  142. {
  143. const float originalRadius = grad.point1.getDistanceFrom (grad.point2);
  144. const float texturePos = sourceRadius / originalRadius;
  145. GLfloat* t = textureCoords;
  146. *t++ = 0.0f;
  147. *t++ = 0.0f;
  148. for (int i = numDivisions + 1; --i >= 0;)
  149. {
  150. *t++ = texturePos;
  151. *t++ = 0.0f;
  152. *t++ = texturePos;
  153. *t++ = 1.0f;
  154. }
  155. jassert (t == textureCoords + numElementsInArray (vertices));
  156. }
  157. {
  158. GLfloat* v = vertices;
  159. *v++ = centre.getX();
  160. *v++ = centre.getY();
  161. const Point<float> first (grad.point1.translated (sourceRadius, -sourceRadius).transformedBy (transform));
  162. Point<float> last (first);
  163. for (int i = 0; i < numDivisions; ++i)
  164. {
  165. const float angle = (i + 1) * (float_Pi * 4.0f / numDivisions);
  166. const Point<float> next (grad.point1.translated (std::sin (angle) * sourceRadius,
  167. std::cos (angle) * -sourceRadius)
  168. .transformedBy (transform));
  169. *v++ = last.getX();
  170. *v++ = last.getY();
  171. *v++ = next.getX();
  172. *v++ = next.getY();
  173. last = next;
  174. }
  175. *v++ = last.getX();
  176. *v++ = last.getY();
  177. *v++ = first.getX();
  178. *v++ = first.getY();
  179. jassert (v == vertices + numElementsInArray (vertices));
  180. }
  181. glEnable (GL_SCISSOR_TEST);
  182. glScissor (rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
  183. drawTriangles (GL_TRIANGLE_FAN, vertices, textureCoords, numDivisions + 3);
  184. glDisable (GL_SCISSOR_TEST);
  185. }
  186. }
  187. void OpenGLHelpers::fillRectWithColourGradient (const Rectangle<int>& rect,
  188. const ColourGradient& gradient,
  189. const AffineTransform& transform)
  190. {
  191. const int textureSize = 256;
  192. OpenGLTexture texture;
  193. HeapBlock<PixelARGB> lookup (textureSize);
  194. gradient.createLookupTable (lookup, textureSize);
  195. texture.load (lookup, textureSize, 1);
  196. texture.bind();
  197. if (gradient.isOpaque())
  198. glDisable (GL_BLEND);
  199. else
  200. glEnable (GL_BLEND);
  201. if (gradient.isRadial)
  202. OpenGLGradientHelpers::fillWithRadialGradient (rect, gradient, transform);
  203. else
  204. OpenGLGradientHelpers::fillWithLinearGradient (rect, gradient, transform, textureSize);
  205. }
  206. END_JUCE_NAMESPACE