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.

186 lines
6.4KB

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