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.

300 lines
10KB

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