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.

280 lines
11KB

  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. #include "../jucedemo_headers.h"
  18. #if JUCE_OPENGL
  19. //==============================================================================
  20. class DemoOpenGLCanvas : public Component,
  21. public OpenGLRenderer,
  22. public Timer
  23. {
  24. public:
  25. DemoOpenGLCanvas()
  26. : rotation (0.0f),
  27. textScrollPos (200)
  28. {
  29. infoLabel.setText ("These sliders demonstrate how components and 2D graphics can be rendered "
  30. "using OpenGL by using the OpenGLContext class.", dontSendNotification);
  31. infoLabel.setInterceptsMouseClicks (false, false);
  32. addAndMakeVisible (&infoLabel);
  33. infoLabel.setBounds ("parent.width * 0.05, bottom - 150, parent.width * 0.4, parent.height - 60");
  34. speedSlider.setRange (-10.0, 10.0, 0.1);
  35. speedSlider.setPopupMenuEnabled (true);
  36. speedSlider.setValue (Random::getSystemRandom().nextDouble() * 3.0, dontSendNotification);
  37. speedSlider.setSliderStyle (Slider::LinearHorizontal);
  38. speedSlider.setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
  39. addAndMakeVisible (&speedSlider);
  40. speedSlider.setBounds ("parent.width * 0.05, parent.height - 65, parent.width * 0.6, top + 24");
  41. sizeSlider.setRange (0.2, 2.0, 0.01);
  42. sizeSlider.setPopupMenuEnabled (true);
  43. sizeSlider.setValue (Random::getSystemRandom().nextDouble() + 0.5, dontSendNotification);
  44. sizeSlider.setSliderStyle (Slider::LinearHorizontal);
  45. sizeSlider.setTextBoxStyle (Slider::TextBoxLeft, false, 80, 20);
  46. addAndMakeVisible (&sizeSlider);
  47. sizeSlider.setBounds ("parent.width * 0.05, parent.height - 35, parent.width * 0.6, top + 24");
  48. openGLContext.setRenderer (this);
  49. openGLContext.setComponentPaintingEnabled (true);
  50. openGLContext.setContinuousRepainting (true);
  51. openGLContext.attachTo (*this);
  52. startTimer (1000 / 30);
  53. }
  54. ~DemoOpenGLCanvas()
  55. {
  56. openGLContext.detach();
  57. }
  58. // when the component creates a new internal context, this is called, and
  59. // we'll use the opportunity to create some images to use as textures.
  60. void newOpenGLContextCreated()
  61. {
  62. logoImage = createLogoImage();
  63. dynamicTextureImage = Image (Image::ARGB, 128, 128, true, OpenGLImageType());
  64. }
  65. void openGLContextClosing()
  66. {
  67. // We have to make sure we release any openGL images before the
  68. // GL context gets closed..
  69. logoImage = Image::null;
  70. dynamicTextureImage = Image::null;
  71. }
  72. void mouseDown (const MouseEvent& e)
  73. {
  74. draggableOrientation.mouseDown (e.getPosition());
  75. }
  76. void mouseDrag (const MouseEvent& e)
  77. {
  78. draggableOrientation.mouseDrag (e.getPosition());
  79. openGLContext.triggerRepaint();
  80. }
  81. void resized()
  82. {
  83. draggableOrientation.setViewport (getLocalBounds());
  84. }
  85. void paint (Graphics&) {}
  86. void renderOpenGL()
  87. {
  88. OpenGLHelpers::clear (Colours::darkgrey.withAlpha (1.0f));
  89. updateTextureImage(); // this will update our dynamically-changing texture image.
  90. const float scale = (float) openGLContext.getRenderingScale();
  91. drawBackground2DStuff (scale); // draws some 2D content to demonstrate the OpenGLGraphicsContext class
  92. // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
  93. // we'll put back any important settings before doing our normal GL 3D drawing..
  94. glEnable (GL_DEPTH_TEST);
  95. glDepthFunc (GL_LESS);
  96. glEnable (GL_BLEND);
  97. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  98. glEnable (GL_TEXTURE_2D);
  99. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  100. OpenGLHelpers::prepareFor2D (roundToInt (scale * getWidth()),
  101. roundToInt (scale * getHeight()));
  102. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  103. glTranslatef (0.0f, 0.0f, -5.0f);
  104. draggableOrientation.applyToOpenGLMatrix();
  105. // logoImage and dynamicTextureImage are actually OpenGL images, so we can use this utility function to
  106. // extract the frame buffer which is their backing store, and use it directly.
  107. OpenGLFrameBuffer* tex1 = OpenGLImageType::getFrameBufferFrom (logoImage);
  108. OpenGLFrameBuffer* tex2 = OpenGLImageType::getFrameBufferFrom (dynamicTextureImage);
  109. if (tex1 != nullptr && tex2 != nullptr)
  110. {
  111. // This draws the sides of our spinning cube.
  112. // I've used some of the juce helper functions, but you can also just use normal GL calls here too.
  113. tex1->draw3D (-1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, Colours::white);
  114. tex1->draw3D (-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, Colours::white);
  115. tex1->draw3D (-1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, Colours::white);
  116. tex2->draw3D (-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, Colours::white);
  117. tex2->draw3D ( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, Colours::white);
  118. tex2->draw3D (-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, Colours::white);
  119. }
  120. #endif
  121. }
  122. void updateTextureImage()
  123. {
  124. // This image is a special framebuffer-backed image, so when we draw to it, the context
  125. // will render directly into its framebuffer
  126. if (dynamicTextureImage.isValid())
  127. {
  128. dynamicTextureImage.clear (dynamicTextureImage.getBounds(),
  129. Colours::red.withRotatedHue (fabsf (::sinf (rotation / 300.0f))).withAlpha (0.7f));
  130. Graphics g (dynamicTextureImage);
  131. g.setFont (dynamicTextureImage.getHeight() / 3.0f);
  132. g.setColour (Colours::black);
  133. drawScrollingMessage (g, dynamicTextureImage.getHeight() / 2);
  134. }
  135. }
  136. void drawBackground2DStuff (float scale)
  137. {
  138. // Create an OpenGLGraphicsContext that will draw into this GL window..
  139. ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext,
  140. roundToInt (scale * getWidth()),
  141. roundToInt (scale * getHeight())));
  142. if (glRenderer != nullptr)
  143. {
  144. Graphics g (*glRenderer);
  145. g.addTransform (AffineTransform::scale (scale));
  146. // This stuff just creates a spinning star shape and fills it..
  147. Path p;
  148. p.addStar (Point<float> (getWidth() * 0.7f, getHeight() * 0.4f), 7,
  149. getHeight() * 0.4f * (float) sizeSlider.getValue(),
  150. getHeight() * 0.4f,
  151. rotation / 50.0f);
  152. g.setGradientFill (ColourGradient (Colours::green.withRotatedHue (fabsf (::sinf (rotation / 300.0f))),
  153. 0, 0,
  154. Colours::green.withRotatedHue (fabsf (::cosf (rotation / -431.0f))),
  155. 0, (float) getHeight(), false));
  156. g.fillPath (p);
  157. }
  158. }
  159. void timerCallback()
  160. {
  161. rotation += (float) speedSlider.getValue();
  162. textScrollPos += 1.4f;
  163. }
  164. private:
  165. OpenGLContext openGLContext;
  166. Image logoImage, dynamicTextureImage;
  167. float rotation, textScrollPos;
  168. Draggable3DOrientation draggableOrientation;
  169. Slider speedSlider, sizeSlider;
  170. Label infoLabel;
  171. // Functions to create a couple of images to use as textures..
  172. static Image createLogoImage()
  173. {
  174. Image image (Image::ARGB, 256, 256, true, OpenGLImageType());
  175. if (image.isValid())
  176. {
  177. Graphics g (image);
  178. g.fillAll (Colours::lightgrey.withAlpha (0.8f));
  179. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  180. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  181. drawRandomStars (g, image.getWidth(), image.getHeight());
  182. }
  183. return image;
  184. }
  185. static void drawRandomStars (Graphics& g, int w, int h)
  186. {
  187. Random r;
  188. for (int i = 10; --i >= 0;)
  189. {
  190. Path pp;
  191. pp.addStar (Point<float> (r.nextFloat() * w, r.nextFloat() * h), r.nextInt (8) + 3, 10.0f, 20.0f, 0.0f);
  192. g.setColour (Colours::pink.withAlpha (0.4f));
  193. g.fillPath (pp);
  194. }
  195. }
  196. void drawScrollingMessage (Graphics& g, int y) const
  197. {
  198. g.drawSingleLineText ("The background, foreground and texture are all being drawn using the OpenGLGraphicsContext class, which "
  199. "lets you use a standard JUCE 2D graphics context to render directly onto an OpenGL window or framebuffer... ",
  200. (int) -std::fmod (textScrollPos, 2500.0f), y);
  201. }
  202. };
  203. //==============================================================================
  204. class OpenGLDemo : public Component
  205. {
  206. public:
  207. OpenGLDemo()
  208. : Component ("OpenGL")
  209. {
  210. addAndMakeVisible (&canvas);
  211. }
  212. void resized()
  213. {
  214. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  215. }
  216. private:
  217. DemoOpenGLCanvas canvas;
  218. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo)
  219. };
  220. //==============================================================================
  221. Component* createOpenGLDemo()
  222. {
  223. return new OpenGLDemo();
  224. }
  225. #endif