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.

281 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, false, false);
  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, false, false);
  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. {
  90. MessageManagerLock mm (Thread::getCurrentThread());
  91. if (! mm.lockWasGained())
  92. return;
  93. updateTextureImage(); // this will update our dynamically-changing texture image.
  94. drawBackground2DStuff(); // draws some 2D content to demonstrate the OpenGLGraphicsContext class
  95. }
  96. // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
  97. // we'll put back any important settings before doing our normal GL 3D drawing..
  98. glEnable (GL_DEPTH_TEST);
  99. glDepthFunc (GL_LESS);
  100. glEnable (GL_BLEND);
  101. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  102. glEnable (GL_TEXTURE_2D);
  103. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  104. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  105. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  106. glTranslatef (0.0f, 0.0f, -5.0f);
  107. draggableOrientation.applyToOpenGLMatrix();
  108. // logoImage and dynamicTextureImage are actually OpenGL images, so we can use this utility function to
  109. // extract the frame buffer which is their backing store, and use it directly.
  110. OpenGLFrameBuffer* tex1 = OpenGLImageType::getFrameBufferFrom (logoImage);
  111. OpenGLFrameBuffer* tex2 = OpenGLImageType::getFrameBufferFrom (dynamicTextureImage);
  112. if (tex1 != nullptr && tex2 != nullptr)
  113. {
  114. // This draws the sides of our spinning cube.
  115. // I've used some of the juce helper functions, but you can also just use normal GL calls here too.
  116. 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);
  117. 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);
  118. 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);
  119. 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);
  120. 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);
  121. 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);
  122. }
  123. #endif
  124. }
  125. void updateTextureImage()
  126. {
  127. // This image is a special framebuffer-backed image, so when we draw to it, the context
  128. // will render directly into its framebuffer
  129. if (dynamicTextureImage.isValid())
  130. {
  131. dynamicTextureImage.clear (dynamicTextureImage.getBounds(),
  132. Colours::red.withRotatedHue (fabsf (::sinf (rotation / 300.0f))).withAlpha (0.7f));
  133. Graphics g (dynamicTextureImage);
  134. g.setFont (dynamicTextureImage.getHeight() / 3.0f);
  135. g.setColour (Colours::black);
  136. drawScrollingMessage (g, dynamicTextureImage.getHeight() / 2);
  137. }
  138. }
  139. void drawBackground2DStuff()
  140. {
  141. // Create an OpenGLGraphicsContext that will draw into this GL window..
  142. ScopedPointer<LowLevelGraphicsContext> glRenderer (createOpenGLGraphicsContext (openGLContext));
  143. if (glRenderer != nullptr)
  144. {
  145. Graphics g (glRenderer);
  146. // This stuff just creates a spinning star shape and fills it..
  147. Path p;
  148. const float scale = getHeight() * 0.4f;
  149. p.addStar (Point<float> (getWidth() * 0.7f, getHeight() * 0.4f), 7,
  150. scale * (float) sizeSlider.getValue(), scale,
  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. openGLContext.triggerRepaint();
  164. }
  165. private:
  166. OpenGLContext openGLContext;
  167. Image logoImage, dynamicTextureImage;
  168. float rotation, textScrollPos;
  169. Draggable3DOrientation draggableOrientation;
  170. Slider speedSlider, sizeSlider;
  171. Label infoLabel;
  172. // Functions to create a couple of images to use as textures..
  173. static Image createLogoImage()
  174. {
  175. Image image (Image::ARGB, 256, 256, true, OpenGLImageType());
  176. if (image.isValid())
  177. {
  178. Graphics g (image);
  179. g.fillAll (Colours::lightgrey.withAlpha (0.8f));
  180. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  181. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  182. drawRandomStars (g, image.getWidth(), image.getHeight());
  183. }
  184. return image;
  185. }
  186. static void drawRandomStars (Graphics& g, int w, int h)
  187. {
  188. Random r;
  189. for (int i = 10; --i >= 0;)
  190. {
  191. Path pp;
  192. pp.addStar (Point<float> (r.nextFloat() * w, r.nextFloat() * h), r.nextInt (8) + 3, 10.0f, 20.0f, 0.0f);
  193. g.setColour (Colours::pink.withAlpha (0.4f));
  194. g.fillPath (pp);
  195. }
  196. }
  197. void drawScrollingMessage (Graphics& g, int y) const
  198. {
  199. g.drawSingleLineText ("The background, foreground and texture are all being drawn using the OpenGLGraphicsContext class, which "
  200. "lets you use a standard JUCE 2D graphics context to render directly onto an OpenGL window or framebuffer... ",
  201. (int) -std::fmod (textScrollPos, 2500.0f), y);
  202. }
  203. };
  204. //==============================================================================
  205. class OpenGLDemo : public Component
  206. {
  207. public:
  208. OpenGLDemo()
  209. : Component ("OpenGL")
  210. {
  211. addAndMakeVisible (&canvas);
  212. }
  213. void resized()
  214. {
  215. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  216. }
  217. private:
  218. DemoOpenGLCanvas canvas;
  219. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo);
  220. };
  221. //==============================================================================
  222. Component* createOpenGLDemo()
  223. {
  224. return new OpenGLDemo();
  225. }
  226. #endif