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.

204 lines
7.2KB

  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. // (no need to call makeCurrentContextActive(), as that will have
  55. // been done for us before the method call).
  56. glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  57. #if ! JUCE_OPENGL_ES
  58. glClearDepth (1.0);
  59. #endif
  60. glDepthFunc (GL_LESS);
  61. glEnable (GL_DEPTH_TEST);
  62. glEnable (GL_TEXTURE_2D);
  63. glEnable (GL_BLEND);
  64. glShadeModel (GL_SMOOTH);
  65. glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
  66. glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
  67. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  68. texture1.load (createImage1());
  69. texture2.load (createImage2());
  70. }
  71. void mouseDrag (const MouseEvent& e)
  72. {
  73. delta = e.getDistanceFromDragStartX() / 100.0f;
  74. repaint();
  75. }
  76. void renderOpenGL()
  77. {
  78. OpenGLHelpers::clear (Colours::darkgrey.withAlpha (0.0f));
  79. OpenGLHelpers::clear (Colours::darkblue);
  80. OpenGLHelpers::prepareFor2D (getWidth(), getHeight());
  81. texture1.draw2D (50.0f, getHeight() - 50.0f,
  82. getWidth() - 50.0f, getHeight() - 50.0f,
  83. getWidth() - 50.0f, 50.0f,
  84. 50.0f, 50.0f,
  85. Colours::white.withAlpha (fabsf (::sinf (rotation / 100.0f))));
  86. glLoadIdentity();
  87. glClear (GL_DEPTH_BUFFER_BIT);
  88. OpenGLHelpers::setPerspective (45.0, getWidth() / (double) getHeight(), 0.1, 100.0);
  89. glMatrixMode (GL_MODELVIEW);
  90. glPushMatrix();
  91. glTranslatef (0.0f, 0.0f, -5.0f);
  92. glRotatef (rotation, 0.5f, 1.0f, 0.0f);
  93. // this draws the sides of our spinning cube..
  94. texture1.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. texture1.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. texture1.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. texture2.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. texture2.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. texture2.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. glPopMatrix();
  101. }
  102. void timerCallback()
  103. {
  104. rotation += delta;
  105. repaint();
  106. }
  107. private:
  108. OpenGLTexture texture1, texture2;
  109. float rotation, delta;
  110. // Functions to create a couple of images to use as textures..
  111. static Image createImage1()
  112. {
  113. Image image (Image::ARGB, 256, 256, true, Image::SoftwareImage);
  114. Graphics g (image);
  115. g.fillAll (Colours::white.withAlpha (0.7f));
  116. g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
  117. 0, 0, image.getWidth(), image.getHeight(), RectanglePlacement::stretchToFit);
  118. return image;
  119. }
  120. static Image createImage2()
  121. {
  122. Image image (Image::ARGB, 128, 128, true, Image::SoftwareImage);
  123. Graphics g (image);
  124. g.fillAll (Colours::darkred.withAlpha (0.7f));
  125. Path p;
  126. p.addStar (image.getBounds().getCentre().toFloat(), 11, image.getWidth() * 0.3f, image.getWidth() * 0.5f);
  127. g.setGradientFill (ColourGradient (Colours::blue, image.getWidth() * 0.5f, image.getHeight() * 0.5f,
  128. Colours::green, image.getWidth() * 0.2f, image.getHeight() * 0.2f,
  129. true));
  130. g.fillPath (p);
  131. return image;
  132. }
  133. };
  134. //==============================================================================
  135. class OpenGLDemo : public Component
  136. {
  137. public:
  138. OpenGLDemo()
  139. : Component ("OpenGL")
  140. {
  141. addAndMakeVisible (&canvas);
  142. }
  143. void resized()
  144. {
  145. canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  146. }
  147. private:
  148. DemoOpenGLCanvas canvas;
  149. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo);
  150. };
  151. //==============================================================================
  152. Component* createOpenGLDemo()
  153. {
  154. return new OpenGLDemo();
  155. }
  156. #endif