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.

299 lines
10.0KB

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