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.

211 lines
7.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. : rotation (0.0f),
  27. delta (1.0f)
  28. {
  29. startTimer (20);
  30. // Just for demo purposes, let's dump a list of all the available pixel formats..
  31. OwnedArray <OpenGLPixelFormat> availablePixelFormats;
  32. OpenGLPixelFormat::getAvailablePixelFormats (this, availablePixelFormats);
  33. for (int i = 0; i < availablePixelFormats.size(); ++i)
  34. {
  35. const OpenGLPixelFormat* const pixFormat = availablePixelFormats[i];
  36. DBG (i << ": RGBA=(" << pixFormat->redBits
  37. << ", " << pixFormat->greenBits
  38. << ", " << pixFormat->blueBits
  39. << ", " << pixFormat->alphaBits
  40. << "), depth=" << pixFormat->depthBufferBits
  41. << ", stencil=" << pixFormat->stencilBufferBits
  42. << ", accum RGBA=(" << pixFormat->accumulationBufferRedBits
  43. << ", " << pixFormat->accumulationBufferGreenBits
  44. << ", " << pixFormat->accumulationBufferBlueBits
  45. << ", " << pixFormat->accumulationBufferAlphaBits
  46. << "), full-scene AA="
  47. << (int) pixFormat->fullSceneAntiAliasingNumSamples);
  48. }
  49. }
  50. // when the component creates a new internal context, this is called, and
  51. // we'll use the opportunity to create the textures needed.
  52. void newOpenGLContextCreated()
  53. {
  54. texture1 = createImage1();
  55. texture2 = createImage2();
  56. // (no need to call makeCurrentContextActive(), as that will have
  57. // been done for us before the method call).
  58. glDepthFunc (GL_LESS);
  59. glEnable (GL_DEPTH_TEST);
  60. glEnable (GL_TEXTURE_2D);
  61. glEnable (GL_BLEND);
  62. glShadeModel (GL_SMOOTH);
  63. glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
  64. glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
  65. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  66. }
  67. void mouseDrag (const MouseEvent& e)
  68. {
  69. delta = e.getDistanceFromDragStartX() / 100.0f;
  70. repaint();
  71. }
  72. void renderOpenGL()
  73. {
  74. OpenGLHelpers::clear (Colours::darkgrey.withAlpha (0.0f));
  75. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  76. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  77. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  78. OpenGLFrameBuffer& frameBuffer1 = dynamic_cast <OpenGLFrameBufferImage*> (texture1.getSharedImage())->frameBuffer;
  79. OpenGLFrameBuffer& frameBuffer2 = dynamic_cast <OpenGLFrameBufferImage*> (texture2.getSharedImage())->frameBuffer;
  80. frameBuffer1.draw2D (50.0f, getHeight() - 50.0f,
  81. getWidth() - 50.0f, getHeight() - 50.0f,
  82. getWidth() - 50.0f, 50.0f,
  83. 50.0f, 50.0f,
  84. Colours::white.withAlpha (fabsf (::sinf (rotation / 100.0f))));
  85. glClear (GL_DEPTH_BUFFER_BIT);
  86. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  87. glTranslatef (0.0f, 0.0f, -5.0f);
  88. glRotatef (rotation, 0.5f, 1.0f, 0.0f);
  89. // this draws the sides of our spinning cube..
  90. frameBuffer1.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);
  91. frameBuffer1.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);
  92. frameBuffer1.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);
  93. frameBuffer2.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);
  94. frameBuffer2.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. frameBuffer2.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. }
  97. void timerCallback()
  98. {
  99. rotation += delta;
  100. repaint();
  101. }
  102. private:
  103. Image texture1, texture2;
  104. float rotation, delta;
  105. // Functions to create a couple of images to use as textures..
  106. static Image createImage1()
  107. {
  108. Image image (new OpenGLFrameBufferImage (Image::ARGB, 256, 256));
  109. Graphics g (image);
  110. g.fillAll (Colours::white.withAlpha (0.7f));
  111. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  112. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  113. drawRandomStars (g, image.getWidth(), image.getHeight());
  114. return image;
  115. }
  116. static Image createImage2()
  117. {
  118. Image image (new OpenGLFrameBufferImage (Image::ARGB, 128, 128));
  119. Graphics g (image);
  120. g.fillAll (Colours::darkred.withAlpha (0.7f));
  121. Path p;
  122. p.addStar (image.getBounds().getCentre().toFloat(), 11, image.getWidth() * 0.3f, image.getWidth() * 0.5f);
  123. g.setGradientFill (ColourGradient (Colours::blue, image.getWidth() * 0.5f, image.getHeight() * 0.5f,
  124. Colours::green, image.getWidth() * 0.2f, image.getHeight() * 0.2f,
  125. true));
  126. g.fillPath (p);
  127. drawRandomStars (g, image.getWidth(), image.getHeight());
  128. return image;
  129. }
  130. static void drawRandomStars (Graphics& g, int w, int h)
  131. {
  132. Random r;
  133. for (int i = 10; --i >= 0;)
  134. {
  135. Path pp;
  136. pp.addStar (Point<float> (r.nextFloat() * w, r.nextFloat() * h), r.nextInt (8) + 3, 10.0f, 20.0f, 0.0f);
  137. g.setColour (Colours::pink.withAlpha (0.4f));
  138. g.fillPath (pp);
  139. }
  140. }
  141. };
  142. //==============================================================================
  143. class OpenGLDemo : public Component
  144. {
  145. public:
  146. OpenGLDemo()
  147. : Component ("OpenGL")
  148. {
  149. addAndMakeVisible (&canvas);
  150. }
  151. void resized()
  152. {
  153. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  154. }
  155. private:
  156. DemoOpenGLCanvas canvas;
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo);
  158. };
  159. //==============================================================================
  160. Component* createOpenGLDemo()
  161. {
  162. return new OpenGLDemo();
  163. }
  164. #endif