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 glutDestroyWindow(Win
);
271 static void Init( void )
273 const char * const ver_string
= (const char * const)
274 glGetString( GL_VERSION
);
276 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
277 printf("GL_VERSION = %s\n", ver_string
);
279 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
280 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
284 bind_vertex_array
= (PFNGLBINDVERTEXARRAYAPPLEPROC
) glutGetProcAddress( "glBindVertexArrayAPPLE" );
285 gen_vertex_arrays
= (PFNGLGENVERTEXARRAYSAPPLEPROC
) glutGetProcAddress( "glGenVertexArraysAPPLE" );
286 delete_vertex_arrays
= (PFNGLDELETEVERTEXARRAYSAPPLEPROC
) glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
287 is_vertex_array
= (PFNGLISVERTEXARRAYAPPLEPROC
) glutGetProcAddress( "glIsVertexArrayAPPLE" );
289 assert(bind_vertex_array
);
290 assert(gen_vertex_arrays
);
291 assert(delete_vertex_arrays
);
292 assert(is_vertex_array
);
294 glEnable( GL_DEPTH_TEST
);
296 (*gen_vertex_arrays
)( 1, & cube_array_obj
);
297 (*bind_vertex_array
)( cube_array_obj
);
298 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_vert
);
299 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, cube_color
);
300 glEnableClientState( GL_VERTEX_ARRAY
);
301 glEnableClientState( GL_COLOR_ARRAY
);
303 (*gen_vertex_arrays
)( 1, & oct_array_obj
);
304 (*bind_vertex_array
)( oct_array_obj
);
305 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_vert
);
306 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, oct_color
);
307 glEnableClientState( GL_VERTEX_ARRAY
);
308 glEnableClientState( GL_COLOR_ARRAY
);
310 (*bind_vertex_array
)( 0 );
311 glVertexPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xDEADBEEF );
312 glColorPointer( 4, GL_FLOAT
, sizeof(GLfloat
) * 4, (void *) 0xBADDC0DE );
316 int main( int argc
, char *argv
[] )
318 glutInit( &argc
, argv
);
319 glutInitWindowPosition( 0, 0 );
320 glutInitWindowSize( Width
, Height
);
321 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
322 Win
= glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
323 glutReshapeFunc( Reshape
);
324 glutKeyboardFunc( Key
);
325 glutDisplayFunc( Display
);
326 glutVisibilityFunc( Visible
);