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.

297 lines
9.9KB

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