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 25#include <assert.h> 26#include <stdio.h> 27#include <stdlib.h> 28#include <math.h> 29#include <GL/glew.h> 30#include "glut_wrap.h" 31 32 33static int Width = 400; 34static int Height = 200; 35static int Win = 0; 36static const GLfloat Near = 5.0, Far = 25.0; 37static GLfloat angle = 0.0; 38 39static GLuint cube_array_obj = 0; 40static GLuint oct_array_obj = 0; 41 42static const GLfloat cube_vert[] = { 43 -0.5, -0.5, -0.5, 1.0, 44 0.5, -0.5, -0.5, 1.0, 45 0.5, 0.5, -0.5, 1.0, 46 -0.5, 0.5, -0.5, 1.0, 47 48 -0.5, -0.5, 0.5, 1.0, 49 0.5, -0.5, 0.5, 1.0, 50 0.5, 0.5, 0.5, 1.0, 51 -0.5, 0.5, 0.5, 1.0, 52 53 -0.5, 0.5, -0.5, 1.0, 54 0.5, 0.5, -0.5, 1.0, 55 0.5, 0.5, 0.5, 1.0, 56 -0.5, 0.5, 0.5, 1.0, 57 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 -0.5, -0.5, 0.5, 1.0, 62 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 0.5, 0.5, -0.5, 1.0, 67 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 -0.5, 0.5, -0.5, 1.0, 72 73}; 74 75static const GLfloat cube_color[] = { 76 1.0, 0.0, 0.0, 1.0, 77 1.0, 0.0, 0.0, 1.0, 78 1.0, 0.0, 0.0, 1.0, 79 1.0, 0.0, 0.0, 1.0, 80 81 0.0, 1.0, 0.0, 1.0, 82 0.0, 1.0, 0.0, 1.0, 83 0.0, 1.0, 0.0, 1.0, 84 0.0, 1.0, 0.0, 1.0, 85 86 0.0, 0.0, 1.0, 1.0, 87 0.0, 0.0, 1.0, 1.0, 88 0.0, 0.0, 1.0, 1.0, 89 0.0, 0.0, 1.0, 1.0, 90 91 1.0, 0.0, 1.0, 1.0, 92 1.0, 0.0, 1.0, 1.0, 93 1.0, 0.0, 1.0, 1.0, 94 1.0, 0.0, 1.0, 1.0, 95 96 1.0, 1.0, 1.0, 1.0, 97 1.0, 1.0, 1.0, 1.0, 98 1.0, 1.0, 1.0, 1.0, 99 1.0, 1.0, 1.0, 1.0, 100 101 0.5, 0.5, 0.5, 1.0, 102 0.5, 0.5, 0.5, 1.0, 103 0.5, 0.5, 0.5, 1.0, 104 0.5, 0.5, 0.5, 1.0, 105}; 106 107static const GLfloat oct_vert[] = { 108 0.0, 0.0, 0.7071, 1.0, 109 0.5, 0.5, 0.0, 1.0, 110 -0.5, 0.5, 0.0, 1.0, 111 112 0.0, 0.0, 0.7071, 1.0, 113 0.5, -0.5, 0.0, 1.0, 114 -0.5, -0.5, 0.0, 1.0, 115 116 0.0, 0.0, 0.7071, 1.0, 117 -0.5, -0.5, 0.0, 1.0, 118 -0.5, 0.5, 0.0, 1.0, 119 120 0.0, 0.0, 0.7071, 1.0, 121 0.5, 0.5, 0.0, 1.0, 122 0.5, -0.5, 0.0, 1.0, 123 124 125 0.0, 0.0, -0.7071, 1.0, 126 0.5, 0.5, 0.0, 1.0, 127 -0.5, 0.5, 0.0, 1.0, 128 129 0.0, 0.0, -0.7071, 1.0, 130 0.5, -0.5, 0.0, 1.0, 131 -0.5, -0.5, 0.0, 1.0, 132 133 0.0, 0.0, -0.7071, 1.0, 134 -0.5, -0.5, 0.0, 1.0, 135 -0.5, 0.5, 0.0, 1.0, 136 137 0.0, 0.0, -0.7071, 1.0, 138 0.5, 0.5, 0.0, 1.0, 139 0.5, -0.5, 0.0, 1.0, 140}; 141 142static const GLfloat oct_color[] = { 143 1.0, 0.64, 0.0, 1.0, 144 1.0, 0.64, 0.0, 1.0, 145 1.0, 0.64, 0.0, 1.0, 146 147 0.8, 0.51, 0.0, 1.0, 148 0.8, 0.51, 0.0, 1.0, 149 0.8, 0.51, 0.0, 1.0, 150 151 0.5, 0.32, 0.0, 1.0, 152 0.5, 0.32, 0.0, 1.0, 153 0.5, 0.32, 0.0, 1.0, 154 155 0.2, 0.13, 0.0, 1.0, 156 0.2, 0.13, 0.0, 1.0, 157 0.2, 0.13, 0.0, 1.0, 158 159 0.2, 0.13, 0.0, 1.0, 160 0.2, 0.13, 0.0, 1.0, 161 0.2, 0.13, 0.0, 1.0, 162 163 0.5, 0.32, 0.0, 1.0, 164 0.5, 0.32, 0.0, 1.0, 165 0.5, 0.32, 0.0, 1.0, 166 167 0.8, 0.51, 0.0, 1.0, 168 0.8, 0.51, 0.0, 1.0, 169 0.8, 0.51, 0.0, 1.0, 170 171 1.0, 0.64, 0.0, 1.0, 172 1.0, 0.64, 0.0, 1.0, 173 1.0, 0.64, 0.0, 1.0, 174}; 175 176static void Display( void ) 177{ 178 glClearColor(0.1, 0.1, 0.4, 0); 179 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 180 181 glMatrixMode( GL_MODELVIEW ); 182 glLoadIdentity(); 183 glTranslatef( 0.0, 0.0, -15.0 ); 184 glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 ); 185 186 187 glBindVertexArrayAPPLE( cube_array_obj ); 188 glPushMatrix(); 189 glTranslatef(-1.5, 0, 0); 190 glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 ); 191 glDrawArrays( GL_QUADS, 0, 4 * 6 ); 192 glPopMatrix(); 193 194 195 glBindVertexArrayAPPLE( oct_array_obj ); 196 glPushMatrix(); 197 glTranslatef(1.5, 0, 0); 198 glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 ); 199 glDrawArrays( GL_TRIANGLES, 0, 3 * 8 ); 200 glPopMatrix(); 201 202 glutSwapBuffers(); 203} 204 205 206static void Idle( void ) 207{ 208 static double t0 = -1.; 209 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; 210 if (t0 < 0.0) 211 t0 = t; 212 dt = t - t0; 213 t0 = t; 214 215 angle += 70.0 * dt; /* 70 degrees per second */ 216 angle = fmod(angle, 360.0); /* prevents eventual overflow */ 217 218 glutPostRedisplay(); 219} 220 221 222static void Visible( int vis ) 223{ 224 if ( vis == GLUT_VISIBLE ) { 225 glutIdleFunc( Idle ); 226 } 227 else { 228 glutIdleFunc( NULL ); 229 } 230} 231static void Reshape( int width, int height ) 232{ 233 GLfloat ar = (float) width / (float) height; 234 Width = width; 235 Height = height; 236 glViewport( 0, 0, width, height ); 237 glMatrixMode( GL_PROJECTION ); 238 glLoadIdentity(); 239 glFrustum( -ar, ar, -1.0, 1.0, Near, Far ); 240} 241 242 243static void Key( unsigned char key, int x, int y ) 244{ 245 (void) x; 246 (void) y; 247 switch (key) { 248 case 27: 249 glDeleteVertexArraysAPPLE( 1, & cube_array_obj ); 250 glDeleteVertexArraysAPPLE( 1, & oct_array_obj ); 251 glutDestroyWindow(Win); 252 exit(0); 253 break; 254 } 255 glutPostRedisplay(); 256} 257 258 259static void Init( void ) 260{ 261 const char * const ver_string = (const char *) 262 glGetString( GL_VERSION ); 263 264 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); 265 printf("GL_VERSION = %s\n", ver_string); 266 267 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) { 268 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n"); 269 exit(1); 270 } 271 272 glEnable( GL_DEPTH_TEST ); 273 274 glGenVertexArraysAPPLE( 1, & cube_array_obj ); 275 glBindVertexArrayAPPLE( cube_array_obj ); 276 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert); 277 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color); 278 glEnableClientState( GL_VERTEX_ARRAY ); 279 glEnableClientState( GL_COLOR_ARRAY ); 280 281 glGenVertexArraysAPPLE( 1, & oct_array_obj ); 282 glBindVertexArrayAPPLE( oct_array_obj ); 283 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert); 284 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color); 285 glEnableClientState( GL_VERTEX_ARRAY ); 286 glEnableClientState( GL_COLOR_ARRAY ); 287 288 glBindVertexArrayAPPLE( 0 ); 289 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF ); 290 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE ); 291} 292 293 294int main( int argc, char *argv[] ) 295{ 296 glutInitWindowSize( Width, Height ); 297 glutInit( &argc, argv ); 298 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 299 Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" ); 300 glutReshapeFunc( Reshape ); 301 glutKeyboardFunc( Key ); 302 glutDisplayFunc( Display ); 303 glutVisibilityFunc( Visible ); 304 glewInit(); 305 Init(); 306 glutMainLoop(); 307 return 0; 308} 309