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 ==============================================================================
30 #include "../jucedemo_headers.h"
42 #include <OpenGLES/ES1/gl.h>
43 #include <OpenGLES/ES1/glext.h>
45 #include <GLUT/glut.h>
47 //#include <GL/glut.h>
51 #define GL_BGRA_EXT 0x80e1
54 //==============================================================================
55 class DemoOpenGLCanvas
: public OpenGLComponent
,
64 // (On the iPhone, choose a format without a depth buffer)
65 setPixelFormat (OpenGLPixelFormat (8, 8, 0, 0));
68 image
= Image (Image::RGB
, 512, 512, true, Image::SoftwareImage
);
71 g
.fillAll (Colours::white
);
72 g
.drawImageWithin (ImageFileFormat::loadFrom (BinaryData::juce_png
, BinaryData::juce_pngSize
),
73 0, 0, 512, 512, RectanglePlacement::stretchToFit
);
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
;
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
);
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()
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
);
118 glDepthFunc (GL_LESS
);
119 glEnable (GL_DEPTH_TEST
);
120 glEnable (GL_TEXTURE_2D
);
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(),
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
);
143 void mouseDrag (const MouseEvent
& e
)
145 delta
= e
.getDistanceFromDragStartX() / 100.0f
;
151 glClearColor (0.25f
, 0.25f
, 0.25f
, 0.0f
);
152 glClear (GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
154 glMatrixMode (GL_PROJECTION
);
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
);
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);
175 glOrtho (0.0, getWidth(), 0.0, getHeight(), 0, 1);
177 glColor4f (1.0f
, 1.0f
, 1.0f
, fabsf (::sinf (rotation
/ 100.0f
)));
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
);
185 glMatrixMode (GL_PROJECTION
);
188 glClear (GL_DEPTH_BUFFER_BIT
);
189 gluPerspective (45.0f
,
190 getWidth() / (GLfloat
) getHeight(),
194 glMatrixMode (GL_MODELVIEW
);
199 glTranslatef (0.0f
, 0.0f
, -5.0f
);
200 glRotatef (rotation
, 0.5f
, 1.0f
, 0.0f
);
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
);
260 float rotation
, delta
;
265 //==============================================================================
266 class OpenGLDemo
: public Component
269 //==============================================================================
274 addAndMakeVisible (&canvas
);
279 canvas
.setBounds (10, 10, getWidth() - 20, getHeight() - 50);
283 DemoOpenGLCanvas canvas
;
285 OpenGLDemo (const OpenGLDemo
&);
286 OpenGLDemo
& operator= (const OpenGLDemo
&);
290 //==============================================================================
291 Component
* createOpenGLDemo()
293 return new OpenGLDemo();