Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / extras / JuceDemo / Source / demos / OpenGLDemo.cpp
blobb9abd5486463cefcd492bc21c50560e16ed8397a
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-9 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifdef _WIN32
27 #include <windows.h>
28 #endif
30 #include "../jucedemo_headers.h"
32 #if JUCE_OPENGL
34 #if JUCE_WINDOWS
35 #include <gl/gl.h>
36 #include <gl/glu.h>
37 #elif JUCE_LINUX
38 #include <GL/gl.h>
39 #include <GL/glut.h>
40 #undef KeyPress
41 #elif JUCE_IOS
42 #include <OpenGLES/ES1/gl.h>
43 #include <OpenGLES/ES1/glext.h>
44 #elif JUCE_MAC
45 #include <GLUT/glut.h>
46 #elif JUCE_IOS
47 //#include <GL/glut.h>
48 #endif
50 #ifndef GL_BGRA_EXT
51 #define GL_BGRA_EXT 0x80e1
52 #endif
54 //==============================================================================
55 class DemoOpenGLCanvas : public OpenGLComponent,
56 public Timer
58 public:
59 DemoOpenGLCanvas()
60 : rotation (0.0f),
61 delta (1.0f)
63 #if JUCE_IOS
64 // (On the iPhone, choose a format without a depth buffer)
65 setPixelFormat (OpenGLPixelFormat (8, 8, 0, 0));
66 #endif
68 image = Image (Image::RGB, 512, 512, true, Image::SoftwareImage);
69 Graphics g (image);
71 g.fillAll (Colours::white);
72 g.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png, BinaryData::juce_pngSize),
73 0, 0, 512, 512, RectanglePlacement::stretchToFit);
75 startTimer (20);
77 // Just for demo purposes, let's dump a list of all the available pixel formats..
78 OwnedArray <OpenGLPixelFormat> availablePixelFormats;
79 OpenGLPixelFormat::getAvailablePixelFormats (this, availablePixelFormats);
81 for (int i = 0; i < availablePixelFormats.size(); ++i)
83 const OpenGLPixelFormat* const pixFormat = availablePixelFormats[i];
85 String formatDescription;
86 formatDescription
87 << i << ": RGBA=(" << pixFormat->redBits
88 << ", " << pixFormat->greenBits
89 << ", " << pixFormat->blueBits
90 << ", " << pixFormat->alphaBits
91 << "), depth=" << pixFormat->depthBufferBits
92 << ", stencil=" << pixFormat->stencilBufferBits
93 << ", accum RGBA=(" << pixFormat->accumulationBufferRedBits
94 << ", " << pixFormat->accumulationBufferGreenBits
95 << ", " << pixFormat->accumulationBufferBlueBits
96 << ", " << pixFormat->accumulationBufferAlphaBits
97 << "), full-scene AA="
98 << (int) pixFormat->fullSceneAntiAliasingNumSamples;
100 Logger::outputDebugString (formatDescription);
104 ~DemoOpenGLCanvas()
108 // when the component creates a new internal context, this is called, and
109 // we'll use the opportunity to create the textures needed.
110 void newOpenGLContextCreated()
112 #if ! JUCE_IOS
113 // (no need to call makeCurrentContextActive(), as that will have
114 // been done for us before the method call).
115 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
116 glClearDepth (1.0);
118 glDepthFunc (GL_LESS);
119 glEnable (GL_DEPTH_TEST);
120 glEnable (GL_TEXTURE_2D);
121 glEnable (GL_BLEND);
122 glShadeModel (GL_SMOOTH);
124 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
125 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
126 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
129 glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
131 Image::BitmapData srcData (image, Image::BitmapData::readOnly);
133 glTexImage2D (GL_TEXTURE_2D, 0, 4, image.getWidth(), image.getHeight(),
134 0, GL_RGB,
135 GL_UNSIGNED_BYTE, srcData.data);
137 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
138 glHint (GL_POINT_SMOOTH_HINT, GL_NICEST);
139 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 #endif
143 void mouseDrag (const MouseEvent& e)
145 delta = e.getDistanceFromDragStartX() / 100.0f;
146 repaint();
149 void renderOpenGL()
151 glClearColor (0.25f, 0.25f, 0.25f, 0.0f);
152 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154 glMatrixMode (GL_PROJECTION);
155 glLoadIdentity();
157 #if JUCE_IOS
158 const GLfloat vertices[] = { -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f };
159 const GLubyte colours[] = { 255, 255, 0, 255, 0, 255, 255, 255, 0, 0, 0, 0, 255, 0, 255, 255 };
161 glOrthof (-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
162 glMatrixMode (GL_MODELVIEW);
163 glPushMatrix();
164 glRotatef (rotation, 0.0f, 0.0f, 1.0f);
166 glVertexPointer (2, GL_FLOAT, 0, vertices);
167 glEnableClientState (GL_VERTEX_ARRAY);
168 glColorPointer (4, GL_UNSIGNED_BYTE, 0, colours);
169 glEnableClientState (GL_COLOR_ARRAY);
171 glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
172 glPopMatrix();
173 #else
175 glOrtho (0.0, getWidth(), 0.0, getHeight(), 0, 1);
177 glColor4f (1.0f, 1.0f, 1.0f, fabsf (::sinf (rotation / 100.0f)));
178 glBegin (GL_QUADS);
179 glTexCoord2i (0, 0); glVertex2f (50.0f, getHeight() - 50.0f);
180 glTexCoord2i (1, 0); glVertex2f (getWidth() - 50.0f, getHeight() - 50.0f);
181 glTexCoord2i (1, 1); glVertex2f (getWidth() - 50.0f, 50.0f);
182 glTexCoord2i (0, 1); glVertex2f (50.0f, 50.0f);
183 glEnd();
185 glMatrixMode (GL_PROJECTION);
186 glLoadIdentity();
188 glClear (GL_DEPTH_BUFFER_BIT);
189 gluPerspective (45.0f,
190 getWidth() / (GLfloat) getHeight(),
191 0.1f,
192 100.0f);
194 glMatrixMode (GL_MODELVIEW);
196 glLoadIdentity();
197 glPushMatrix();
199 glTranslatef (0.0f, 0.0f, -5.0f);
200 glRotatef (rotation, 0.5f, 1.0f, 0.0f);
202 glBegin (GL_QUADS);
204 glColor3f (0.0f, 1.0f, 0.0f);
206 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
207 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
208 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
209 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
211 glColor3f (1.0f, 0.0f, 0.0f);
213 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
214 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
215 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
216 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
218 glColor3f (0.0f, 0.0f, 1.0f);
220 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
221 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
222 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
223 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
225 glColor3f (1.0f, 1.0f, 0.0f);
227 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
228 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
229 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
230 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
232 glColor3f (0.0f, 1.0f, 1.0f);
234 glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
235 glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
236 glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
237 glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
239 glColor3f (1.0f, 0.0f, 1.0f);
241 glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
242 glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
243 glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
244 glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
246 glEnd();
248 glPopMatrix();
249 #endif
252 void timerCallback()
254 rotation += delta;
256 repaint();
259 private:
260 float rotation, delta;
261 Image image;
265 //==============================================================================
266 class OpenGLDemo : public Component
268 public:
269 //==============================================================================
270 OpenGLDemo()
272 setName ("OpenGL");
274 addAndMakeVisible (&canvas);
277 void resized()
279 canvas.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
282 private:
283 DemoOpenGLCanvas canvas;
285 OpenGLDemo (const OpenGLDemo&);
286 OpenGLDemo& operator= (const OpenGLDemo&);
290 //==============================================================================
291 Component* createOpenGLDemo()
293 return new OpenGLDemo();
296 #endif