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;
49 static const GLfloat Near
= 5.0, Far
= 25.0;
50 static GLfloat angle
= 0.0;
52 static GLuint cube_array_obj
= 0;
53 static GLuint oct_array_obj
= 0;
55 static const GLfloat cube_vert
[] = {
56 -0.5, -0.5, -0.5, 1.0,
71 -0.5, -0.5, -0.5, 1.0,
81 -0.5, -0.5, -0.5, 1.0,
88 static const GLfloat cube_color
[] = {
120 static const GLfloat oct_vert
[] = {
121 0.0, 0.0, 0.7071, 1.0,
125 0.0, 0.0, 0.7071, 1.0,
127 -0.5, -0.5, 0.0, 1.0,
129 0.0, 0.0, 0.7071, 1.0,
130 -0.5, -0.5, 0.0, 1.0,
133 0.0, 0.0, 0.7071, 1.0,
138 0.0, 0.0, -0.7071, 1.0,
142 0.0, 0.0, -0.7071, 1.0,
144 -0.5, -0.5, 0.0, 1.0,
146 0.0, 0.0, -0.7071, 1.0,
147 -0.5, -0.5, 0.0, 1.0,
150 0.0, 0.0, -0.7071, 1.0,
155 static const GLfloat oct_color
[] = {
189 static void Display( void )
191 glClearColor(0.1, 0.1, 0.4, 0);
192 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
194 glMatrixMode( GL_MODELVIEW
);
196 glTranslatef( 0.0, 0.0, -15.0 );
197 glRotatef( angle
, 0.0 * angle
, 0.0 * angle
, 1.0 );
200 (*bind_vertex_array
)( cube_array_obj
);
202 glTranslatef(-1.5, 0, 0);
203 glRotatef( angle
, 0.3 * angle
, 0.8 * angle
, 1.0 );
204 glDrawArrays( GL_QUADS
, 0, 4 * 6 );
208 (*bind_vertex_array
)( oct_array_obj
);
210 glTranslatef(1.5, 0, 0);
211 glRotatef( angle
, 0.3 * angle
, 0.8 * angle
, 1.0 );
212 glDrawArrays( GL_TRIANGLES
, 0, 3 * 8 );
219 static void Idle( void )
221 static double t0
= -1.;
222 double dt
, t
= glutGet(GLUT_ELAPSED_TIME
) / 1000.0;
228 angle
+= 70.0 * dt
; /* 70 degrees per second */
229 angle
= fmod(angle
, 360.0); /* prevents eventual overflow */
235 static void Visible( int vis
)
237 if ( vis
== GLUT_VISIBLE
) {
238 glutIdleFunc( Idle
);
241 glutIdleFunc( NULL
);
244 static void Reshape( int width
, int height
)
246 GLfloat ar
= (float) width
/ (float) height
;
249 glViewport( 0, 0, width
, height
);
250 glMatrixMode( GL_PROJECTION
);
252 glFrustum( -ar
, ar
, -1.0, 1.0, Near
, Far
);
256 static void Key( unsigned char key
, int x
, int y
)
262 glutDestroyWindow(Win
);
270 static void Init( void )
272 const char * const ver_string
= (const char * const)
273 glGetString( GL_VERSION
);
275 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
276 printf("GL_VERSION = %s\n", ver_string
);
278 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
279 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
283 bind_vertex_array
= glutGetProcAddress( "glBindVertexArrayAPPLE" );
284 gen_vertex_arrays
= glutGetProcAddress( "glGenVertexArraysAPPLE" );
285 delete_vertex_arrays
= glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
286 is_vertex_array
= glutGetProcAddress( "glIsVertexArrayAPPLE" );
289 glEnable( GL_DEPTH_TEST
);
291 (*gen_vertex_arrays
)( 1, & cube_array_obj
);
292 (*bind_vertex_array
)( cube_array_obj
);
293 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_vert
);
294 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_color
);
295 glEnableClientState( GL_VERTEX_ARRAY
);
296 glEnableClientState( GL_COLOR_ARRAY
);
298 (*gen_vertex_arrays
)( 1, & oct_array_obj
);
299 (*bind_vertex_array
)( oct_array_obj
);
300 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_vert
);
301 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_color
);
302 glEnableClientState( GL_VERTEX_ARRAY
);
303 glEnableClientState( GL_COLOR_ARRAY
);
305 (*bind_vertex_array
)( 0 );
306 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xDEADBEEF );
307 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xBADDC0DE );
311 int main( int argc
, char *argv
[] )
313 glutInit( &argc
, argv
);
314 glutInitWindowPosition( 0, 0 );
315 glutInitWindowSize( Width
, Height
);
316 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
317 Win
= glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
318 glutReshapeFunc( Reshape
);
319 glutKeyboardFunc( Key
);
320 glutDisplayFunc( Display
);
321 glutVisibilityFunc( Visible
);