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.

270 lines
9.1KB

  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. #ifdef _WIN32
  19. #include <windows.h>
  20. #endif
  21. #include "../jucedemo_headers.h"
  22. #if JUCE_OPENGL && ! JUCE_IPHONE
  23. #if JUCE_WINDOWS
  24. #include <gl/gl.h>
  25. #include <gl/glu.h>
  26. #elif JUCE_LINUX
  27. #include <GL/gl.h>
  28. #include <GL/glut.h>
  29. #undef KeyPress
  30. #elif JUCE_MAC
  31. #include <GLUT/glut.h>
  32. #elif JUCE_IPHONE
  33. //#include <GL/glut.h>
  34. #endif
  35. #ifndef GL_BGRA_EXT
  36. #define GL_BGRA_EXT 0x80e1
  37. #endif
  38. //==============================================================================
  39. class DemoOpenGLCanvas : public OpenGLComponent,
  40. public Timer
  41. {
  42. float rotation, delta;
  43. Image* image;
  44. public:
  45. DemoOpenGLCanvas()
  46. {
  47. rotation = 0.0f;
  48. delta = 1.0f;
  49. Image* im = ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize);
  50. image = new Image (Image::RGB, 512, 512, true);
  51. Graphics g (*image);
  52. g.fillAll (Colours::white);
  53. g.drawImage (im, 0, 0, 512, 512, 0, 0, im->getWidth(), im->getHeight());
  54. delete im;
  55. startTimer (20);
  56. // Just for demo purposes, let's dump a list of all the available pixel formats..
  57. OwnedArray <OpenGLPixelFormat> availablePixelFormats;
  58. OpenGLPixelFormat::getAvailablePixelFormats (this, availablePixelFormats);
  59. for (int i = 0; i < availablePixelFormats.size(); ++i)
  60. {
  61. const OpenGLPixelFormat* const pixFormat = availablePixelFormats[i];
  62. String formatDescription;
  63. formatDescription
  64. << i << ": RGBA=(" << pixFormat->redBits
  65. << ", " << pixFormat->greenBits
  66. << ", " << pixFormat->blueBits
  67. << ", " << pixFormat->alphaBits
  68. << "), depth=" << pixFormat->depthBufferBits
  69. << ", stencil=" << pixFormat->stencilBufferBits
  70. << ", accum RGBA=(" << pixFormat->accumulationBufferRedBits
  71. << ", " << pixFormat->accumulationBufferGreenBits
  72. << ", " << pixFormat->accumulationBufferBlueBits
  73. << ", " << pixFormat->accumulationBufferAlphaBits
  74. << "), full-scene AA="
  75. << pixFormat->fullSceneAntiAliasingNumSamples;
  76. Logger::outputDebugString (formatDescription);
  77. }
  78. }
  79. ~DemoOpenGLCanvas()
  80. {
  81. delete image;
  82. }
  83. // when the component creates a new internal context, this is called, and
  84. // we'll use the opportunity to create the textures needed.
  85. void newOpenGLContextCreated()
  86. {
  87. // (no need to call makeCurrentContextActive(), as that will have
  88. // been done for us before the method call).
  89. glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  90. glClearDepth (1.0);
  91. glDepthFunc (GL_LESS);
  92. glEnable (GL_DEPTH_TEST);
  93. glEnable (GL_TEXTURE_2D);
  94. glEnable (GL_BLEND);
  95. glShadeModel (GL_SMOOTH);
  96. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  97. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  98. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  99. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  100. glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
  101. Image::BitmapData srcData (*image, 0, 0, image->getWidth(), image->getHeight());
  102. glTexImage2D (GL_TEXTURE_2D, 0, 4, image->getWidth(), image->getHeight(),
  103. 0, GL_RGB,
  104. GL_UNSIGNED_BYTE, srcData.data);
  105. glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
  106. glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
  107. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  108. }
  109. void mouseDrag (const MouseEvent& e)
  110. {
  111. delta = e.getDistanceFromDragStartX() / 100.0f;
  112. repaint();
  113. }
  114. void renderOpenGL()
  115. {
  116. glClearColor (0.8f, 0.0f, 0.4f, 0.0f);
  117. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  118. glMatrixMode(GL_PROJECTION);
  119. glLoadIdentity();
  120. glOrtho (0.0, getWidth(), 0.0, getHeight(), 0, 1);
  121. glColor4f (1.0f, 1.0f, 1.0f, fabsf (::sinf (rotation / 100.0f)));
  122. glBegin(GL_QUADS);
  123. glTexCoord2i (0, 0); glVertex2f (50.0f, getHeight() - 50.0f);
  124. glTexCoord2i (1, 0); glVertex2f (getWidth() - 50.0f, getHeight() - 50.0f);
  125. glTexCoord2i (1, 1); glVertex2f (getWidth() - 50.0f, 50.0f);
  126. glTexCoord2i (0, 1); glVertex2f (50.0f, 50.0f);
  127. glEnd();
  128. glMatrixMode (GL_PROJECTION);
  129. glLoadIdentity();
  130. glClear (GL_DEPTH_BUFFER_BIT);
  131. gluPerspective (45.0f,
  132. getWidth() / (GLfloat) getHeight(),
  133. 0.1f,
  134. 100.0f);
  135. glMatrixMode (GL_MODELVIEW);
  136. glLoadIdentity();
  137. glPushMatrix();
  138. glTranslatef (0.0f, 0.0f, -5.0f);
  139. glRotatef (rotation, 0.5f, 1.0f, 0.0f);
  140. glBegin (GL_QUADS);
  141. glColor3f (0.0f, 1.0f, 0.0f);
  142. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
  143. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
  144. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
  145. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
  146. glColor3f (1.0f, 0.0f, 0.0f);
  147. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  148. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
  149. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
  150. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  151. glColor3f (0.0f, 0.0f, 1.0f);
  152. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
  153. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
  154. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
  155. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
  156. glColor3f (1.0f, 1.0f, 0.0f);
  157. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  158. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  159. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
  160. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
  161. glColor3f (0.0f, 1.0f, 1.0f);
  162. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
  163. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
  164. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
  165. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
  166. glColor3f (1.0f, 0.0f, 1.0f);
  167. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
  168. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
  169. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
  170. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
  171. glEnd();
  172. glPopMatrix();
  173. }
  174. void timerCallback()
  175. {
  176. rotation += delta;
  177. repaint();
  178. }
  179. };
  180. //==============================================================================
  181. class OpenGLDemo : public Component
  182. {
  183. //==============================================================================
  184. DemoOpenGLCanvas* canvas;
  185. public:
  186. //==============================================================================
  187. OpenGLDemo()
  188. {
  189. setName (T("OpenGL"));
  190. canvas = new DemoOpenGLCanvas();
  191. addAndMakeVisible (canvas);
  192. }
  193. ~OpenGLDemo()
  194. {
  195. deleteAllChildren();
  196. }
  197. void resized()
  198. {
  199. canvas->setBounds (10, 10, getWidth() - 20, getHeight() - 50);
  200. }
  201. };
  202. //==============================================================================
  203. Component* createOpenGLDemo()
  204. {
  205. return new OpenGLDemo();
  206. }
  207. #endif