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.

243 lines
9.9KB

  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. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  84. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  85. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  86. glTranslatef (0.0f, 0.0f, -5.0f);
  87. draggableOrientation.applyToOpenGLMatrix();
  88. // logoImage and dynamicTextureImage are actually OpenGL images, so we can use this utility function to
  89. // extract the frame buffer which is their backing store, and use it directly.
  90. OpenGLFrameBuffer* tex1 = OpenGLImageType::getFrameBufferFrom (logoImage);
  91. OpenGLFrameBuffer* tex2 = OpenGLImageType::getFrameBufferFrom (dynamicTextureImage);
  92. jassert (tex1 != nullptr && tex2 != nullptr); // (this would mean that our images weren't created correctly)
  93. // This draws the sides of our spinning cube.
  94. // I've used some of the juce helper functions, but you can also just use normal GL calls here too.
  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. 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);
  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. 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);
  101. #endif
  102. }
  103. void updateTextureImage()
  104. {
  105. // This image is a special framebuffer-backed image, so when we draw to it, the context
  106. // will render directly into its framebuffer
  107. dynamicTextureImage.clear (dynamicTextureImage.getBounds(),
  108. Colours::red.withRotatedHue (fabsf (::sinf (rotation / 300.0f))).withAlpha (0.7f));
  109. Graphics g (dynamicTextureImage);
  110. g.setFont (dynamicTextureImage.getHeight() / 3.0f);
  111. g.setColour (Colours::black);
  112. drawScrollingMessage (g, dynamicTextureImage.getHeight() / 2);
  113. }
  114. void drawBackground2DStuff()
  115. {
  116. OpenGLGraphicsContext glRenderer (*this); // Create an OpenGLGraphicsContext that will draw into this GL window..
  117. Graphics g (&glRenderer); // ..and then wrap it in a normal Graphics object so we can draw with it.
  118. // This stuff just creates a spinning star shape and fills it..
  119. Path p;
  120. const float scale = getHeight() * 0.4f;
  121. p.addStar (Point<float> (getWidth() * 0.7f, getHeight() * 0.4f), 7,
  122. scale * (float) sizeSlider.getValue(), scale,
  123. rotation / 50.0f);
  124. g.setGradientFill (ColourGradient (Colours::green.withRotatedHue (fabsf (::sinf (rotation / 300.0f))),
  125. 0, 0,
  126. Colours::green.withRotatedHue (fabsf (::cosf (rotation / -431.0f))),
  127. 0, (float) getHeight(), false));
  128. g.fillPath (p);
  129. }
  130. void timerCallback()
  131. {
  132. rotation += (float) speedSlider.getValue();
  133. textScrollPos += 1.4f;
  134. triggerRepaint();
  135. }
  136. private:
  137. Image logoImage, dynamicTextureImage;
  138. float rotation, textScrollPos;
  139. Draggable3DOrientation draggableOrientation;
  140. Slider speedSlider, sizeSlider;
  141. Label infoLabel;
  142. // Functions to create a couple of images to use as textures..
  143. static Image createLogoImage()
  144. {
  145. Image image (Image::ARGB, 256, 256, true, OpenGLImageType());
  146. Graphics g (image);
  147. g.fillAll (Colours::lightgrey.withAlpha (0.8f));
  148. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  149. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  150. drawRandomStars (g, image.getWidth(), image.getHeight());
  151. return image;
  152. }
  153. static void drawRandomStars (Graphics& g, int w, int h)
  154. {
  155. Random r;
  156. for (int i = 10; --i >= 0;)
  157. {
  158. Path pp;
  159. pp.addStar (Point<float> (r.nextFloat() * w, r.nextFloat() * h), r.nextInt (8) + 3, 10.0f, 20.0f, 0.0f);
  160. g.setColour (Colours::pink.withAlpha (0.4f));
  161. g.fillPath (pp);
  162. }
  163. }
  164. void drawScrollingMessage (Graphics& g, int y) const
  165. {
  166. g.drawSingleLineText ("The background, foreground and texture are all being drawn using the OpenGLGraphicsContext class, which "
  167. "lets you use a standard JUCE 2D graphics context to render directly onto an OpenGL window or framebuffer... ",
  168. (int) -std::fmod (textScrollPos, 2500.0f), y);
  169. }
  170. };
  171. //==============================================================================
  172. class OpenGLDemo : public Component
  173. {
  174. public:
  175. OpenGLDemo()
  176. : Component ("OpenGL")
  177. {
  178. addAndMakeVisible (&canvas);
  179. }
  180. void resized()
  181. {
  182. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  183. }
  184. private:
  185. DemoOpenGLCanvas canvas;
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo);
  187. };
  188. //==============================================================================
  189. Component* createOpenGLDemo()
  190. {
  191. return new OpenGLDemo();
  192. }
  193. #endif