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.

282 lines
11KB

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