Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright IBM Corporation 2006 |
| 3 | * All Rights Reserved. |
| 4 | * |
| 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: |
| 11 | * |
| 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 |
| 14 | * Software. |
| 15 | * |
| 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. |
| 23 | */ |
| 24 | |
Brian Paul | cc738e0 | 2006-08-01 20:03:29 +0000 | [diff] [blame] | 25 | #include <assert.h> |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <math.h> |
| 29 | |
| 30 | #ifdef __darwin__ |
| 31 | #include <GLUT/glut.h> |
| 32 | |
| 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); |
| 37 | |
| 38 | #else |
| 39 | #include <GL/glut.h> |
| 40 | #endif |
| 41 | |
| 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; |
| 46 | |
| 47 | static int Width = 400; |
| 48 | static int Height = 200; |
Brian Paul | d8bb5ea | 2006-07-12 02:32:21 +0000 | [diff] [blame] | 49 | static int Win = 0; |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 50 | static const GLfloat Near = 5.0, Far = 25.0; |
| 51 | static GLfloat angle = 0.0; |
| 52 | |
| 53 | static GLuint cube_array_obj = 0; |
| 54 | static GLuint oct_array_obj = 0; |
| 55 | |
| 56 | static const GLfloat cube_vert[] = { |
| 57 | -0.5, -0.5, -0.5, 1.0, |
| 58 | 0.5, -0.5, -0.5, 1.0, |
| 59 | 0.5, 0.5, -0.5, 1.0, |
| 60 | -0.5, 0.5, -0.5, 1.0, |
| 61 | |
| 62 | -0.5, -0.5, 0.5, 1.0, |
| 63 | 0.5, -0.5, 0.5, 1.0, |
| 64 | 0.5, 0.5, 0.5, 1.0, |
| 65 | -0.5, 0.5, 0.5, 1.0, |
| 66 | |
| 67 | -0.5, 0.5, -0.5, 1.0, |
| 68 | 0.5, 0.5, -0.5, 1.0, |
| 69 | 0.5, 0.5, 0.5, 1.0, |
| 70 | -0.5, 0.5, 0.5, 1.0, |
| 71 | |
| 72 | -0.5, -0.5, -0.5, 1.0, |
| 73 | 0.5, -0.5, -0.5, 1.0, |
| 74 | 0.5, -0.5, 0.5, 1.0, |
| 75 | -0.5, -0.5, 0.5, 1.0, |
| 76 | |
| 77 | 0.5, -0.5, -0.5, 1.0, |
| 78 | 0.5, -0.5, 0.5, 1.0, |
| 79 | 0.5, 0.5, 0.5, 1.0, |
| 80 | 0.5, 0.5, -0.5, 1.0, |
| 81 | |
| 82 | -0.5, -0.5, -0.5, 1.0, |
| 83 | -0.5, -0.5, 0.5, 1.0, |
| 84 | -0.5, 0.5, 0.5, 1.0, |
| 85 | -0.5, 0.5, -0.5, 1.0, |
| 86 | |
| 87 | }; |
| 88 | |
| 89 | static const GLfloat cube_color[] = { |
| 90 | 1.0, 0.0, 0.0, 1.0, |
| 91 | 1.0, 0.0, 0.0, 1.0, |
| 92 | 1.0, 0.0, 0.0, 1.0, |
| 93 | 1.0, 0.0, 0.0, 1.0, |
| 94 | |
| 95 | 0.0, 1.0, 0.0, 1.0, |
| 96 | 0.0, 1.0, 0.0, 1.0, |
| 97 | 0.0, 1.0, 0.0, 1.0, |
| 98 | 0.0, 1.0, 0.0, 1.0, |
| 99 | |
| 100 | 0.0, 0.0, 1.0, 1.0, |
| 101 | 0.0, 0.0, 1.0, 1.0, |
| 102 | 0.0, 0.0, 1.0, 1.0, |
| 103 | 0.0, 0.0, 1.0, 1.0, |
| 104 | |
| 105 | 1.0, 0.0, 1.0, 1.0, |
| 106 | 1.0, 0.0, 1.0, 1.0, |
| 107 | 1.0, 0.0, 1.0, 1.0, |
| 108 | 1.0, 0.0, 1.0, 1.0, |
| 109 | |
| 110 | 1.0, 1.0, 1.0, 1.0, |
| 111 | 1.0, 1.0, 1.0, 1.0, |
| 112 | 1.0, 1.0, 1.0, 1.0, |
| 113 | 1.0, 1.0, 1.0, 1.0, |
| 114 | |
| 115 | 0.5, 0.5, 0.5, 1.0, |
| 116 | 0.5, 0.5, 0.5, 1.0, |
| 117 | 0.5, 0.5, 0.5, 1.0, |
| 118 | 0.5, 0.5, 0.5, 1.0, |
| 119 | }; |
| 120 | |
| 121 | static const GLfloat oct_vert[] = { |
| 122 | 0.0, 0.0, 0.7071, 1.0, |
| 123 | 0.5, 0.5, 0.0, 1.0, |
| 124 | -0.5, 0.5, 0.0, 1.0, |
| 125 | |
| 126 | 0.0, 0.0, 0.7071, 1.0, |
| 127 | 0.5, -0.5, 0.0, 1.0, |
| 128 | -0.5, -0.5, 0.0, 1.0, |
| 129 | |
| 130 | 0.0, 0.0, 0.7071, 1.0, |
| 131 | -0.5, -0.5, 0.0, 1.0, |
| 132 | -0.5, 0.5, 0.0, 1.0, |
| 133 | |
| 134 | 0.0, 0.0, 0.7071, 1.0, |
| 135 | 0.5, 0.5, 0.0, 1.0, |
| 136 | 0.5, -0.5, 0.0, 1.0, |
| 137 | |
| 138 | |
| 139 | 0.0, 0.0, -0.7071, 1.0, |
| 140 | 0.5, 0.5, 0.0, 1.0, |
| 141 | -0.5, 0.5, 0.0, 1.0, |
| 142 | |
| 143 | 0.0, 0.0, -0.7071, 1.0, |
| 144 | 0.5, -0.5, 0.0, 1.0, |
| 145 | -0.5, -0.5, 0.0, 1.0, |
| 146 | |
| 147 | 0.0, 0.0, -0.7071, 1.0, |
| 148 | -0.5, -0.5, 0.0, 1.0, |
| 149 | -0.5, 0.5, 0.0, 1.0, |
| 150 | |
| 151 | 0.0, 0.0, -0.7071, 1.0, |
| 152 | 0.5, 0.5, 0.0, 1.0, |
| 153 | 0.5, -0.5, 0.0, 1.0, |
| 154 | }; |
| 155 | |
| 156 | static const GLfloat oct_color[] = { |
| 157 | 1.0, 0.64, 0.0, 1.0, |
| 158 | 1.0, 0.64, 0.0, 1.0, |
| 159 | 1.0, 0.64, 0.0, 1.0, |
| 160 | |
| 161 | 0.8, 0.51, 0.0, 1.0, |
| 162 | 0.8, 0.51, 0.0, 1.0, |
| 163 | 0.8, 0.51, 0.0, 1.0, |
| 164 | |
| 165 | 0.5, 0.32, 0.0, 1.0, |
| 166 | 0.5, 0.32, 0.0, 1.0, |
| 167 | 0.5, 0.32, 0.0, 1.0, |
| 168 | |
| 169 | 0.2, 0.13, 0.0, 1.0, |
| 170 | 0.2, 0.13, 0.0, 1.0, |
| 171 | 0.2, 0.13, 0.0, 1.0, |
| 172 | |
| 173 | 0.2, 0.13, 0.0, 1.0, |
| 174 | 0.2, 0.13, 0.0, 1.0, |
| 175 | 0.2, 0.13, 0.0, 1.0, |
| 176 | |
| 177 | 0.5, 0.32, 0.0, 1.0, |
| 178 | 0.5, 0.32, 0.0, 1.0, |
| 179 | 0.5, 0.32, 0.0, 1.0, |
| 180 | |
| 181 | 0.8, 0.51, 0.0, 1.0, |
| 182 | 0.8, 0.51, 0.0, 1.0, |
| 183 | 0.8, 0.51, 0.0, 1.0, |
| 184 | |
| 185 | 1.0, 0.64, 0.0, 1.0, |
| 186 | 1.0, 0.64, 0.0, 1.0, |
| 187 | 1.0, 0.64, 0.0, 1.0, |
| 188 | }; |
| 189 | |
| 190 | static void Display( void ) |
| 191 | { |
| 192 | glClearColor(0.1, 0.1, 0.4, 0); |
| 193 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 194 | |
| 195 | glMatrixMode( GL_MODELVIEW ); |
| 196 | glLoadIdentity(); |
| 197 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 198 | glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 ); |
| 199 | |
| 200 | |
| 201 | (*bind_vertex_array)( cube_array_obj ); |
| 202 | glPushMatrix(); |
| 203 | glTranslatef(-1.5, 0, 0); |
| 204 | glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 ); |
| 205 | glDrawArrays( GL_QUADS, 0, 4 * 6 ); |
| 206 | glPopMatrix(); |
| 207 | |
| 208 | |
| 209 | (*bind_vertex_array)( oct_array_obj ); |
| 210 | glPushMatrix(); |
| 211 | glTranslatef(1.5, 0, 0); |
| 212 | glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 ); |
| 213 | glDrawArrays( GL_TRIANGLES, 0, 3 * 8 ); |
| 214 | glPopMatrix(); |
| 215 | |
| 216 | glutSwapBuffers(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void Idle( void ) |
| 221 | { |
| 222 | static double t0 = -1.; |
| 223 | double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 224 | if (t0 < 0.0) |
| 225 | t0 = t; |
| 226 | dt = t - t0; |
| 227 | t0 = t; |
| 228 | |
| 229 | angle += 70.0 * dt; /* 70 degrees per second */ |
| 230 | angle = fmod(angle, 360.0); /* prevents eventual overflow */ |
| 231 | |
| 232 | glutPostRedisplay(); |
| 233 | } |
| 234 | |
| 235 | |
| 236 | static void Visible( int vis ) |
| 237 | { |
| 238 | if ( vis == GLUT_VISIBLE ) { |
| 239 | glutIdleFunc( Idle ); |
| 240 | } |
| 241 | else { |
| 242 | glutIdleFunc( NULL ); |
| 243 | } |
| 244 | } |
| 245 | static void Reshape( int width, int height ) |
| 246 | { |
| 247 | GLfloat ar = (float) width / (float) height; |
| 248 | Width = width; |
| 249 | Height = height; |
| 250 | glViewport( 0, 0, width, height ); |
| 251 | glMatrixMode( GL_PROJECTION ); |
| 252 | glLoadIdentity(); |
| 253 | glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | static void Key( unsigned char key, int x, int y ) |
| 258 | { |
| 259 | (void) x; |
| 260 | (void) y; |
| 261 | switch (key) { |
| 262 | case 27: |
Brian Paul | 56cfa4d | 2009-05-07 14:15:24 -0600 | [diff] [blame] | 263 | (*delete_vertex_arrays)( 1, & cube_array_obj ); |
| 264 | (*delete_vertex_arrays)( 1, & oct_array_obj ); |
Brian Paul | d8bb5ea | 2006-07-12 02:32:21 +0000 | [diff] [blame] | 265 | glutDestroyWindow(Win); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 266 | exit(0); |
| 267 | break; |
| 268 | } |
| 269 | glutPostRedisplay(); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | static void Init( void ) |
| 274 | { |
| 275 | const char * const ver_string = (const char * const) |
| 276 | glGetString( GL_VERSION ); |
| 277 | |
| 278 | printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); |
| 279 | printf("GL_VERSION = %s\n", ver_string); |
| 280 | |
| 281 | if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) { |
| 282 | printf("Sorry, this program requires GL_APPLE_vertex_array_object\n"); |
| 283 | exit(1); |
| 284 | } |
| 285 | |
Brian Paul | cc738e0 | 2006-08-01 20:03:29 +0000 | [diff] [blame] | 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" ); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 290 | |
Brian Paul | cc738e0 | 2006-08-01 20:03:29 +0000 | [diff] [blame] | 291 | assert(bind_vertex_array); |
| 292 | assert(gen_vertex_arrays); |
| 293 | assert(delete_vertex_arrays); |
| 294 | assert(is_vertex_array); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 295 | |
| 296 | glEnable( GL_DEPTH_TEST ); |
| 297 | |
| 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 ); |
| 304 | |
| 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 ); |
| 311 | |
| 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 ); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | int main( int argc, char *argv[] ) |
| 319 | { |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 320 | glutInitWindowSize( Width, Height ); |
Brian Paul | 263f432 | 2009-12-18 08:12:55 -0700 | [diff] [blame] | 321 | glutInit( &argc, argv ); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 322 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); |
Brian Paul | d8bb5ea | 2006-07-12 02:32:21 +0000 | [diff] [blame] | 323 | Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" ); |
Ian Romanick | ee34e6e | 2006-06-12 16:26:29 +0000 | [diff] [blame] | 324 | glutReshapeFunc( Reshape ); |
| 325 | glutKeyboardFunc( Key ); |
| 326 | glutDisplayFunc( Display ); |
| 327 | glutVisibilityFunc( Visible ); |
| 328 | Init(); |
| 329 | glutMainLoop(); |
| 330 | return 0; |
| 331 | } |