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.
30 #include <GLUT/glut.h>
32 typedef void (* PFNGLBINDVERTEXARRAYAPPLEPROC
) (GLuint array
);
33 typedef void (* PFNGLDELETEVERTEXARRAYSAPPLEPROC
) (GLsizei n
, const GLuint
*arrays
);
34 typedef void (* PFNGLGENVERTEXARRAYSAPPLEPROC
) (GLsizei n
, const GLuint
*arrays
);
35 typedef GLboolean (* PFNGLISVERTEXARRAYAPPLEPROC
) (GLuint array
);
41 static PFNGLBINDVERTEXARRAYAPPLEPROC bind_vertex_array
= NULL
;
42 static PFNGLGENVERTEXARRAYSAPPLEPROC gen_vertex_arrays
= NULL
;
43 static PFNGLDELETEVERTEXARRAYSAPPLEPROC delete_vertex_arrays
= NULL
;
44 static PFNGLISVERTEXARRAYAPPLEPROC is_vertex_array
= NULL
;
46 static int Width
= 400;
47 static int Height
= 200;
48 static const GLfloat Near
= 5.0, Far
= 25.0;
49 static GLfloat angle
= 0.0;
51 static GLuint cube_array_obj
= 0;
52 static GLuint oct_array_obj
= 0;
54 static const GLfloat cube_vert
[] = {
55 -0.5, -0.5, -0.5, 1.0,
70 -0.5, -0.5, -0.5, 1.0,
80 -0.5, -0.5, -0.5, 1.0,
87 static const GLfloat cube_color
[] = {
119 static const GLfloat oct_vert
[] = {
120 0.0, 0.0, 0.7071, 1.0,
124 0.0, 0.0, 0.7071, 1.0,
126 -0.5, -0.5, 0.0, 1.0,
128 0.0, 0.0, 0.7071, 1.0,
129 -0.5, -0.5, 0.0, 1.0,
132 0.0, 0.0, 0.7071, 1.0,
137 0.0, 0.0, -0.7071, 1.0,
141 0.0, 0.0, -0.7071, 1.0,
143 -0.5, -0.5, 0.0, 1.0,
145 0.0, 0.0, -0.7071, 1.0,
146 -0.5, -0.5, 0.0, 1.0,
149 0.0, 0.0, -0.7071, 1.0,
154 static const GLfloat oct_color
[] = {
188 static void Display( void )
190 glClearColor(0.1, 0.1, 0.4, 0);
191 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
193 glMatrixMode( GL_MODELVIEW
);
195 glTranslatef( 0.0, 0.0, -15.0 );
196 glRotatef( angle
, 0.0 * angle
, 0.0 * angle
, 1.0 );
199 (*bind_vertex_array
)( cube_array_obj
);
201 glTranslatef(-1.5, 0, 0);
202 glRotatef( angle
, 0.3 * angle
, 0.8 * angle
, 1.0 );
203 glDrawArrays( GL_QUADS
, 0, 4 * 6 );
207 (*bind_vertex_array
)( oct_array_obj
);
209 glTranslatef(1.5, 0, 0);
210 glRotatef( angle
, 0.3 * angle
, 0.8 * angle
, 1.0 );
211 glDrawArrays( GL_TRIANGLES
, 0, 3 * 8 );
218 static void Idle( void )
220 static double t0
= -1.;
221 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
227 angle
+= 70.0 * dt
; /* 70 degrees per second */
228 angle
= fmod(angle
, 360.0); /* prevents eventual overflow */
234 static void Visible( int vis
)
236 if ( vis
== GLUT_VISIBLE
) {
237 glutIdleFunc( Idle
);
240 glutIdleFunc( NULL
);
243 static void Reshape( int width
, int height
)
245 GLfloat ar
= (float) width
/ (float) height
;
248 glViewport( 0, 0, width
, height
);
249 glMatrixMode( GL_PROJECTION
);
251 glFrustum( -ar
, ar
, -1.0, 1.0, Near
, Far
);
255 static void Key( unsigned char key
, int x
, int y
)
268 static void Init( void )
270 const char * const ver_string
= (const char * const)
271 glGetString( GL_VERSION
);
273 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
274 printf("GL_VERSION = %s\n", ver_string
);
276 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
277 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
281 bind_vertex_array
= glutGetProcAddress( "glBindVertexArrayAPPLE" );
282 gen_vertex_arrays
= glutGetProcAddress( "glGenVertexArraysAPPLE" );
283 delete_vertex_arrays
= glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
284 is_vertex_array
= glutGetProcAddress( "glIsVertexArrayAPPLE" );
287 glEnable( GL_DEPTH_TEST
);
289 (*gen_vertex_arrays
)( 1, & cube_array_obj
);
290 (*bind_vertex_array
)( cube_array_obj
);
291 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_vert
);
292 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_color
);
293 glEnableClientState( GL_VERTEX_ARRAY
);
294 glEnableClientState( GL_COLOR_ARRAY
);
296 (*gen_vertex_arrays
)( 1, & oct_array_obj
);
297 (*bind_vertex_array
)( oct_array_obj
);
298 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_vert
);
299 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_color
);
300 glEnableClientState( GL_VERTEX_ARRAY
);
301 glEnableClientState( GL_COLOR_ARRAY
);
303 (*bind_vertex_array
)( 0 );
304 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xDEADBEEF );
305 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xBADDC0DE );
309 int main( int argc
, char *argv
[] )
311 glutInit( &argc
, argv
);
312 glutInitWindowPosition( 0, 0 );
313 glutInitWindowSize( Width
, Height
);
314 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
315 glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
316 glutReshapeFunc( Reshape
);
317 glutKeyboardFunc( Key
);
318 glutDisplayFunc( Display
);
319 glutVisibilityFunc( Visible
);