2
* (C) Copyright IBM Corporation 2006
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* on the rights to use, copy, modify, merge, publish, distribute, sub
9
* license, and/or sell copies of the Software, and to permit persons to whom
10
* the Software is furnished to do so, subject to the following conditions:
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19
* IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
* DEALINGS IN THE SOFTWARE.
31
#include <GLUT/glut.h>
33
typedef void (* PFNGLBINDVERTEXARRAYAPPLEPROC) (GLuint array);
34
typedef void (* PFNGLDELETEVERTEXARRAYSAPPLEPROC) (GLsizei n, const GLuint *arrays);
35
typedef void (* PFNGLGENVERTEXARRAYSAPPLEPROC) (GLsizei n, const GLuint *arrays);
36
typedef GLboolean (* PFNGLISVERTEXARRAYAPPLEPROC) (GLuint array);
42
static PFNGLBINDVERTEXARRAYAPPLEPROC bind_vertex_array = NULL;
43
static PFNGLGENVERTEXARRAYSAPPLEPROC gen_vertex_arrays = NULL;
44
static PFNGLDELETEVERTEXARRAYSAPPLEPROC delete_vertex_arrays = NULL;
45
static PFNGLISVERTEXARRAYAPPLEPROC is_vertex_array = NULL;
47
static int Width = 400;
48
static int Height = 200;
50
static const GLfloat Near = 5.0, Far = 25.0;
51
static GLfloat angle = 0.0;
53
static GLuint cube_array_obj = 0;
54
static GLuint oct_array_obj = 0;
56
static const GLfloat cube_vert[] = {
57
-0.5, -0.5, -0.5, 1.0,
72
-0.5, -0.5, -0.5, 1.0,
82
-0.5, -0.5, -0.5, 1.0,
89
static const GLfloat cube_color[] = {
121
static const GLfloat oct_vert[] = {
122
0.0, 0.0, 0.7071, 1.0,
126
0.0, 0.0, 0.7071, 1.0,
128
-0.5, -0.5, 0.0, 1.0,
130
0.0, 0.0, 0.7071, 1.0,
131
-0.5, -0.5, 0.0, 1.0,
134
0.0, 0.0, 0.7071, 1.0,
139
0.0, 0.0, -0.7071, 1.0,
143
0.0, 0.0, -0.7071, 1.0,
145
-0.5, -0.5, 0.0, 1.0,
147
0.0, 0.0, -0.7071, 1.0,
148
-0.5, -0.5, 0.0, 1.0,
151
0.0, 0.0, -0.7071, 1.0,
156
static const GLfloat oct_color[] = {
190
static void Display( void )
192
glClearColor(0.1, 0.1, 0.4, 0);
193
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
195
glMatrixMode( GL_MODELVIEW );
197
glTranslatef( 0.0, 0.0, -15.0 );
198
glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 );
201
(*bind_vertex_array)( cube_array_obj );
203
glTranslatef(-1.5, 0, 0);
204
glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
205
glDrawArrays( GL_QUADS, 0, 4 * 6 );
209
(*bind_vertex_array)( oct_array_obj );
211
glTranslatef(1.5, 0, 0);
212
glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
213
glDrawArrays( GL_TRIANGLES, 0, 3 * 8 );
220
static void Idle( void )
222
static double t0 = -1.;
223
double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
229
angle += 70.0 * dt; /* 70 degrees per second */
230
angle = fmod(angle, 360.0); /* prevents eventual overflow */
236
static void Visible( int vis )
238
if ( vis == GLUT_VISIBLE ) {
239
glutIdleFunc( Idle );
242
glutIdleFunc( NULL );
245
static void Reshape( int width, int height )
247
GLfloat ar = (float) width / (float) height;
250
glViewport( 0, 0, width, height );
251
glMatrixMode( GL_PROJECTION );
253
glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
257
static void Key( unsigned char key, int x, int y )
263
(*delete_vertex_arrays)( 1, & cube_array_obj );
264
(*delete_vertex_arrays)( 1, & oct_array_obj );
265
glutDestroyWindow(Win);
273
static void Init( void )
275
const char * const ver_string = (const char * const)
276
glGetString( GL_VERSION );
278
printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
279
printf("GL_VERSION = %s\n", ver_string);
281
if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
282
printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
286
bind_vertex_array = (PFNGLBINDVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glBindVertexArrayAPPLE" );
287
gen_vertex_arrays = (PFNGLGENVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glGenVertexArraysAPPLE" );
288
delete_vertex_arrays = (PFNGLDELETEVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
289
is_vertex_array = (PFNGLISVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glIsVertexArrayAPPLE" );
291
assert(bind_vertex_array);
292
assert(gen_vertex_arrays);
293
assert(delete_vertex_arrays);
294
assert(is_vertex_array);
296
glEnable( GL_DEPTH_TEST );
298
(*gen_vertex_arrays)( 1, & cube_array_obj );
299
(*bind_vertex_array)( cube_array_obj );
300
glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert);
301
glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color);
302
glEnableClientState( GL_VERTEX_ARRAY );
303
glEnableClientState( GL_COLOR_ARRAY );
305
(*gen_vertex_arrays)( 1, & oct_array_obj );
306
(*bind_vertex_array)( oct_array_obj );
307
glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert);
308
glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color);
309
glEnableClientState( GL_VERTEX_ARRAY );
310
glEnableClientState( GL_COLOR_ARRAY );
312
(*bind_vertex_array)( 0 );
313
glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF );
314
glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE );
318
int main( int argc, char *argv[] )
320
glutInitWindowSize( Width, Height );
321
glutInit( &argc, argv );
322
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
323
Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
324
glutReshapeFunc( Reshape );
325
glutKeyboardFunc( Key );
326
glutDisplayFunc( Display );
327
glutVisibilityFunc( Visible );