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.

241 lines
9.8KB

  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 OpenGLComponent,
  22. public Timer
  23. {
  24. public:
  25. DemoOpenGLCanvas()
  26. : OpenGLComponent (openGLDefault), rotation (0.0f),
  27. textScrollPos (200)
  28. {
  29. infoLabel.setText ("These sliders demonstrate how components can be added as children "
  30. "of an OpenGLComponent, in which case, their content will be rendered into "
  31. "an OpenGL framebuffer and efficiently overlaid onto your GL content.", 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. startTimer (1000 / 30);
  50. }
  51. // when the component creates a new internal context, this is called, and
  52. // we'll use the opportunity to create some images to use as textures.
  53. void newOpenGLContextCreated()
  54. {
  55. logoImage = createLogoImage();
  56. dynamicTextureImage = Image (Image::ARGB, 128, 128, true, OpenGLImageType());
  57. }
  58. void mouseDown (const MouseEvent& e)
  59. {
  60. draggableOrientation.mouseDown (e.getPosition());
  61. }
  62. void mouseDrag (const MouseEvent& e)
  63. {
  64. draggableOrientation.mouseDrag (e.getPosition());
  65. triggerRepaint();
  66. }
  67. void resized()
  68. {
  69. draggableOrientation.setViewport (getLocalBounds());
  70. }
  71. void renderOpenGL()
  72. {
  73. OpenGLHelpers::clear (Colours::darkgrey.withAlpha (1.0f));
  74. updateTextureImage(); // this will update our dynamically-changing texture image.
  75. drawBackground2DStuff(); // draws some 2D content to demonstrate the OpenGLGraphicsContext class
  76. // Having used the juce 2D renderer, it will have messed-up a whole load of GL state, so
  77. // we'll put back any important settings before doing our normal GL 3D drawing..
  78. glEnable (GL_DEPTH_TEST);
  79. glDepthFunc (GL_LESS);
  80. glEnable (GL_BLEND);
  81. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  82. glEnable (GL_TEXTURE_2D);
  83. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  84. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  85. glTranslatef (0.0f, 0.0f, -5.0f);
  86. draggableOrientation.applyToOpenGLMatrix();
  87. // logoImage and dynamicTextureImage are actually OpenGL images, so we can use this utility function to
  88. // extract the frame buffer which is their backing store, and use it directly.
  89. OpenGLFrameBuffer* tex1 = OpenGLImageType::getFrameBufferFrom (logoImage);
  90. OpenGLFrameBuffer* tex2 = OpenGLImageType::getFrameBufferFrom (dynamicTextureImage);
  91. jassert (tex1 != nullptr && tex2 != nullptr); // (this would mean that our images weren't created correctly)
  92. // This draws the sides of our spinning cube.
  93. // I've used some of the juce helper functions, but you can also just use normal GL calls here too.
  94. 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);
  95. 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);
  96. 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);
  97. 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);
  98. 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);
  99. 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);
  100. }
  101. void updateTextureImage()
  102. {
  103. // This image is a special framebuffer-backed image, so when we draw to it, the context
  104. // will render directly into its framebuffer
  105. dynamicTextureImage.clear (dynamicTextureImage.getBounds(),
  106. Colours::red.withRotatedHue (fabsf (::sinf (rotation / 300.0f))).withAlpha (0.7f));
  107. Graphics g (dynamicTextureImage);
  108. g.setFont (dynamicTextureImage.getHeight() / 3.0f);
  109. g.setColour (Colours::black);
  110. drawScrollingMessage (g, dynamicTextureImage.getHeight() / 2);
  111. }
  112. void drawBackground2DStuff()
  113. {
  114. OpenGLGraphicsContext glRenderer (*this); // Create an OpenGLGraphicsContext that will draw into this GL window..
  115. Graphics g (&glRenderer); // ..and then wrap it in a normal Graphics object so we can draw with it.
  116. // This stuff just creates a spinning star shape and fills it..
  117. Path p;
  118. const float scale = getHeight() * 0.4f;
  119. p.addStar (Point<float> (getWidth() * 0.7f, getHeight() * 0.4f), 7,
  120. scale * (float) sizeSlider.getValue(), scale,
  121. rotation / 50.0f);
  122. g.setGradientFill (ColourGradient (Colours::green.withRotatedHue (fabsf (::sinf (rotation / 300.0f))),
  123. 0, 0,
  124. Colours::green.withRotatedHue (fabsf (::cosf (rotation / -431.0f))),
  125. 0, (float) getHeight(), false));
  126. g.fillPath (p);
  127. }
  128. void timerCallback()
  129. {
  130. rotation += (float) speedSlider.getValue();
  131. textScrollPos += 1.4f;
  132. triggerRepaint();
  133. }
  134. private:
  135. Image logoImage, dynamicTextureImage;
  136. float rotation, textScrollPos;
  137. Draggable3DOrientation draggableOrientation;
  138. Slider speedSlider, sizeSlider;
  139. Label infoLabel;
  140. // Functions to create a couple of images to use as textures..
  141. static Image createLogoImage()
  142. {
  143. Image image (Image::ARGB, 256, 256, true, OpenGLImageType());
  144. Graphics g (image);
  145. g.fillAll (Colours::lightgrey.withAlpha (0.8f));
  146. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  147. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  148. drawRandomStars (g, image.getWidth(), image.getHeight());
  149. return image;
  150. }
  151. static void drawRandomStars (Graphics& g, int w, int h)
  152. {
  153. Random r;
  154. for (int i = 10; --i >= 0;)
  155. {
  156. Path pp;
  157. pp.addStar (Point<float> (r.nextFloat() * w, r.nextFloat() * h), r.nextInt (8) + 3, 10.0f, 20.0f, 0.0f);
  158. g.setColour (Colours::pink.withAlpha (0.4f));
  159. g.fillPath (pp);
  160. }
  161. }
  162. void drawScrollingMessage (Graphics& g, int y) const
  163. {
  164. g.drawSingleLineText ("The background, foreground and texture are all being drawn using the OpenGLGraphicsContext class, which "
  165. "lets you use a standard JUCE 2D graphics context to render directly onto an OpenGL window or framebuffer... ",
  166. (int) -std::fmod (textScrollPos, 2500.0f), y);
  167. }
  168. };
  169. //==============================================================================
  170. class OpenGLDemo : public Component
  171. {
  172. public:
  173. OpenGLDemo()
  174. : Component ("OpenGL")
  175. {
  176. addAndMakeVisible (&canvas);
  177. }
  178. void resized()
  179. {
  180. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  181. }
  182. private:
  183. DemoOpenGLCanvas canvas;
  184. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo);
  185. };
  186. //==============================================================================
  187. Component* createOpenGLDemo()
  188. {
  189. return new OpenGLDemo();
  190. }
  191. #endif