132001f49Smrg/*
232001f49Smrg * (C) Copyright IBM Corporation 2006
332001f49Smrg * All Rights Reserved.
432001f49Smrg *
532001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a
632001f49Smrg * copy of this software and associated documentation files (the "Software"),
732001f49Smrg * to deal in the Software without restriction, including without limitation
832001f49Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
932001f49Smrg * license, and/or sell copies of the Software, and to permit persons to whom
1032001f49Smrg * the Software is furnished to do so, subject to the following conditions:
1132001f49Smrg *
1232001f49Smrg * The above copyright notice and this permission notice (including the next
1332001f49Smrg * paragraph) shall be included in all copies or substantial portions of the
1432001f49Smrg * Software.
1532001f49Smrg *
1632001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1732001f49Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1832001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
1932001f49Smrg * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2032001f49Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2132001f49Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2232001f49Smrg * DEALINGS IN THE SOFTWARE.
2332001f49Smrg */
2432001f49Smrg
2532001f49Smrg#include <assert.h>
2632001f49Smrg#include <stdio.h>
2732001f49Smrg#include <stdlib.h>
2832001f49Smrg#include <math.h>
2932001f49Smrg#include <GL/glew.h>
3032001f49Smrg#include "glut_wrap.h"
3132001f49Smrg
3232001f49Smrg
3332001f49Smrgstatic int Width = 400;
3432001f49Smrgstatic int Height = 200;
3532001f49Smrgstatic int Win = 0;
3632001f49Smrgstatic const GLfloat Near = 5.0, Far = 25.0;
3732001f49Smrgstatic GLfloat angle = 0.0;
3832001f49Smrg
3932001f49Smrgstatic GLuint cube_array_obj = 0;
4032001f49Smrgstatic GLuint oct_array_obj = 0;
4132001f49Smrg
4232001f49Smrgstatic const GLfloat cube_vert[] = {
4332001f49Smrg    -0.5, -0.5, -0.5,  1.0,
4432001f49Smrg     0.5, -0.5, -0.5,  1.0,
4532001f49Smrg     0.5,  0.5, -0.5,  1.0,
4632001f49Smrg    -0.5,  0.5, -0.5,  1.0,
4732001f49Smrg
4832001f49Smrg    -0.5, -0.5,  0.5,  1.0,
4932001f49Smrg     0.5, -0.5,  0.5,  1.0,
5032001f49Smrg     0.5,  0.5,  0.5,  1.0,
5132001f49Smrg    -0.5,  0.5,  0.5,  1.0,
5232001f49Smrg
5332001f49Smrg    -0.5,  0.5, -0.5,  1.0,
5432001f49Smrg     0.5,  0.5, -0.5,  1.0,
5532001f49Smrg     0.5,  0.5,  0.5,  1.0,
5632001f49Smrg    -0.5,  0.5,  0.5,  1.0,
5732001f49Smrg
5832001f49Smrg    -0.5, -0.5, -0.5,  1.0,
5932001f49Smrg     0.5, -0.5, -0.5,  1.0,
6032001f49Smrg     0.5, -0.5,  0.5,  1.0,
6132001f49Smrg    -0.5, -0.5,  0.5,  1.0,
6232001f49Smrg
6332001f49Smrg     0.5, -0.5, -0.5,  1.0,
6432001f49Smrg     0.5, -0.5,  0.5,  1.0,
6532001f49Smrg     0.5,  0.5,  0.5,  1.0,
6632001f49Smrg     0.5,  0.5, -0.5,  1.0,
6732001f49Smrg
6832001f49Smrg    -0.5, -0.5, -0.5,  1.0,
6932001f49Smrg    -0.5, -0.5,  0.5,  1.0,
7032001f49Smrg    -0.5,  0.5,  0.5,  1.0,
7132001f49Smrg    -0.5,  0.5, -0.5,  1.0,
7232001f49Smrg
7332001f49Smrg};
7432001f49Smrg
7532001f49Smrgstatic const GLfloat cube_color[] = {
7632001f49Smrg     1.0, 0.0, 0.0, 1.0,
7732001f49Smrg     1.0, 0.0, 0.0, 1.0,
7832001f49Smrg     1.0, 0.0, 0.0, 1.0,
7932001f49Smrg     1.0, 0.0, 0.0, 1.0,
8032001f49Smrg
8132001f49Smrg     0.0, 1.0, 0.0, 1.0,
8232001f49Smrg     0.0, 1.0, 0.0, 1.0,
8332001f49Smrg     0.0, 1.0, 0.0, 1.0,
8432001f49Smrg     0.0, 1.0, 0.0, 1.0,
8532001f49Smrg
8632001f49Smrg     0.0, 0.0, 1.0, 1.0,
8732001f49Smrg     0.0, 0.0, 1.0, 1.0,
8832001f49Smrg     0.0, 0.0, 1.0, 1.0,
8932001f49Smrg     0.0, 0.0, 1.0, 1.0,
9032001f49Smrg
9132001f49Smrg     1.0, 0.0, 1.0, 1.0,
9232001f49Smrg     1.0, 0.0, 1.0, 1.0,
9332001f49Smrg     1.0, 0.0, 1.0, 1.0,
9432001f49Smrg     1.0, 0.0, 1.0, 1.0,
9532001f49Smrg
9632001f49Smrg     1.0, 1.0, 1.0, 1.0,
9732001f49Smrg     1.0, 1.0, 1.0, 1.0,
9832001f49Smrg     1.0, 1.0, 1.0, 1.0,
9932001f49Smrg     1.0, 1.0, 1.0, 1.0,
10032001f49Smrg
10132001f49Smrg     0.5, 0.5, 0.5, 1.0,
10232001f49Smrg     0.5, 0.5, 0.5, 1.0,
10332001f49Smrg     0.5, 0.5, 0.5, 1.0,
10432001f49Smrg     0.5, 0.5, 0.5, 1.0,
10532001f49Smrg};
10632001f49Smrg
10732001f49Smrgstatic const GLfloat oct_vert[] = {
10832001f49Smrg    0.0,  0.0,  0.7071, 1.0,
10932001f49Smrg    0.5,  0.5,  0.0,  1.0,
11032001f49Smrg   -0.5,  0.5,  0.0,  1.0,
11132001f49Smrg
11232001f49Smrg    0.0,  0.0,  0.7071, 1.0,
11332001f49Smrg    0.5, -0.5,  0.0,  1.0,
11432001f49Smrg   -0.5, -0.5,  0.0,  1.0,
11532001f49Smrg
11632001f49Smrg    0.0,  0.0,  0.7071, 1.0,
11732001f49Smrg   -0.5, -0.5,  0.0,  1.0,
11832001f49Smrg   -0.5,  0.5,  0.0,  1.0,
11932001f49Smrg
12032001f49Smrg    0.0,  0.0,  0.7071, 1.0,
12132001f49Smrg    0.5,  0.5,  0.0,  1.0,
12232001f49Smrg    0.5, -0.5,  0.0,  1.0,
12332001f49Smrg
12432001f49Smrg
12532001f49Smrg    0.0,  0.0, -0.7071, 1.0,
12632001f49Smrg    0.5,  0.5,  0.0,  1.0,
12732001f49Smrg   -0.5,  0.5,  0.0,  1.0,
12832001f49Smrg
12932001f49Smrg    0.0,  0.0, -0.7071, 1.0,
13032001f49Smrg    0.5, -0.5,  0.0,  1.0,
13132001f49Smrg   -0.5, -0.5,  0.0,  1.0,
13232001f49Smrg
13332001f49Smrg    0.0,  0.0, -0.7071, 1.0,
13432001f49Smrg   -0.5, -0.5,  0.0,  1.0,
13532001f49Smrg   -0.5,  0.5,  0.0,  1.0,
13632001f49Smrg
13732001f49Smrg    0.0,  0.0, -0.7071, 1.0,
13832001f49Smrg    0.5,  0.5,  0.0,  1.0,
13932001f49Smrg    0.5, -0.5,  0.0,  1.0,
14032001f49Smrg};
14132001f49Smrg
14232001f49Smrgstatic const GLfloat oct_color[] = {
14332001f49Smrg     1.0, 0.64, 0.0, 1.0,
14432001f49Smrg     1.0, 0.64, 0.0, 1.0,
14532001f49Smrg     1.0, 0.64, 0.0, 1.0,
14632001f49Smrg
14732001f49Smrg     0.8, 0.51, 0.0, 1.0,
14832001f49Smrg     0.8, 0.51, 0.0, 1.0,
14932001f49Smrg     0.8, 0.51, 0.0, 1.0,
15032001f49Smrg
15132001f49Smrg     0.5, 0.32, 0.0, 1.0,
15232001f49Smrg     0.5, 0.32, 0.0, 1.0,
15332001f49Smrg     0.5, 0.32, 0.0, 1.0,
15432001f49Smrg
15532001f49Smrg     0.2, 0.13, 0.0, 1.0,
15632001f49Smrg     0.2, 0.13, 0.0, 1.0,
15732001f49Smrg     0.2, 0.13, 0.0, 1.0,
15832001f49Smrg
15932001f49Smrg     0.2, 0.13, 0.0, 1.0,
16032001f49Smrg     0.2, 0.13, 0.0, 1.0,
16132001f49Smrg     0.2, 0.13, 0.0, 1.0,
16232001f49Smrg
16332001f49Smrg     0.5, 0.32, 0.0, 1.0,
16432001f49Smrg     0.5, 0.32, 0.0, 1.0,
16532001f49Smrg     0.5, 0.32, 0.0, 1.0,
16632001f49Smrg
16732001f49Smrg     0.8, 0.51, 0.0, 1.0,
16832001f49Smrg     0.8, 0.51, 0.0, 1.0,
16932001f49Smrg     0.8, 0.51, 0.0, 1.0,
17032001f49Smrg
17132001f49Smrg     1.0, 0.64, 0.0, 1.0,
17232001f49Smrg     1.0, 0.64, 0.0, 1.0,
17332001f49Smrg     1.0, 0.64, 0.0, 1.0,
17432001f49Smrg};
17532001f49Smrg
17632001f49Smrgstatic void Display( void )
17732001f49Smrg{
17832001f49Smrg   glClearColor(0.1, 0.1, 0.4, 0);
17932001f49Smrg   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
18032001f49Smrg
18132001f49Smrg   glMatrixMode( GL_MODELVIEW );
18232001f49Smrg   glLoadIdentity();
18332001f49Smrg   glTranslatef( 0.0, 0.0, -15.0 );
18432001f49Smrg   glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 );
18532001f49Smrg
18632001f49Smrg
18732001f49Smrg   glBindVertexArrayAPPLE( cube_array_obj );
18832001f49Smrg   glPushMatrix();
18932001f49Smrg   glTranslatef(-1.5, 0, 0);
19032001f49Smrg   glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
19132001f49Smrg   glDrawArrays( GL_QUADS, 0, 4 * 6 );
19232001f49Smrg   glPopMatrix();
19332001f49Smrg
19432001f49Smrg
19532001f49Smrg   glBindVertexArrayAPPLE( oct_array_obj );
19632001f49Smrg   glPushMatrix();
19732001f49Smrg   glTranslatef(1.5, 0, 0);
19832001f49Smrg   glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
19932001f49Smrg   glDrawArrays( GL_TRIANGLES, 0, 3 * 8 );
20032001f49Smrg   glPopMatrix();
20132001f49Smrg
20232001f49Smrg   glutSwapBuffers();
20332001f49Smrg}
20432001f49Smrg
20532001f49Smrg
20632001f49Smrgstatic void Idle( void )
20732001f49Smrg{
20832001f49Smrg   static double t0 = -1.;
20932001f49Smrg   double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
21032001f49Smrg   if (t0 < 0.0)
21132001f49Smrg      t0 = t;
21232001f49Smrg   dt = t - t0;
21332001f49Smrg   t0 = t;
21432001f49Smrg
21532001f49Smrg   angle += 70.0 * dt;  /* 70 degrees per second */
21632001f49Smrg   angle = fmod(angle, 360.0); /* prevents eventual overflow */
21732001f49Smrg
21832001f49Smrg   glutPostRedisplay();
21932001f49Smrg}
22032001f49Smrg
22132001f49Smrg
22232001f49Smrgstatic void Visible( int vis )
22332001f49Smrg{
22432001f49Smrg   if ( vis == GLUT_VISIBLE ) {
22532001f49Smrg      glutIdleFunc( Idle );
22632001f49Smrg   }
22732001f49Smrg   else {
22832001f49Smrg      glutIdleFunc( NULL );
22932001f49Smrg   }
23032001f49Smrg}
23132001f49Smrgstatic void Reshape( int width, int height )
23232001f49Smrg{
23332001f49Smrg   GLfloat ar = (float) width / (float) height;
23432001f49Smrg   Width = width;
23532001f49Smrg   Height = height;
23632001f49Smrg   glViewport( 0, 0, width, height );
23732001f49Smrg   glMatrixMode( GL_PROJECTION );
23832001f49Smrg   glLoadIdentity();
23932001f49Smrg   glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
24032001f49Smrg}
24132001f49Smrg
24232001f49Smrg
24332001f49Smrgstatic void Key( unsigned char key, int x, int y )
24432001f49Smrg{
24532001f49Smrg   (void) x;
24632001f49Smrg   (void) y;
24732001f49Smrg   switch (key) {
24832001f49Smrg      case 27:
24932001f49Smrg         glDeleteVertexArraysAPPLE( 1, & cube_array_obj );
25032001f49Smrg         glDeleteVertexArraysAPPLE( 1, & oct_array_obj );
25132001f49Smrg         glutDestroyWindow(Win);
25232001f49Smrg         exit(0);
25332001f49Smrg         break;
25432001f49Smrg   }
25532001f49Smrg   glutPostRedisplay();
25632001f49Smrg}
25732001f49Smrg
25832001f49Smrg
25932001f49Smrgstatic void Init( void )
26032001f49Smrg{
26132001f49Smrg   const char * const ver_string = (const char *)
26232001f49Smrg       glGetString( GL_VERSION );
26332001f49Smrg
26432001f49Smrg   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
26532001f49Smrg   printf("GL_VERSION = %s\n", ver_string);
26632001f49Smrg
26732001f49Smrg   if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
26832001f49Smrg      printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
26932001f49Smrg      exit(1);
27032001f49Smrg   }
27132001f49Smrg
27232001f49Smrg   glEnable( GL_DEPTH_TEST );
27332001f49Smrg
27432001f49Smrg   glGenVertexArraysAPPLE( 1, & cube_array_obj );
27532001f49Smrg   glBindVertexArrayAPPLE( cube_array_obj );
27632001f49Smrg   glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert);
27732001f49Smrg   glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color);
27832001f49Smrg   glEnableClientState( GL_VERTEX_ARRAY );
27932001f49Smrg   glEnableClientState( GL_COLOR_ARRAY );
28032001f49Smrg
28132001f49Smrg   glGenVertexArraysAPPLE( 1, & oct_array_obj );
28232001f49Smrg   glBindVertexArrayAPPLE( oct_array_obj );
28332001f49Smrg   glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert);
28432001f49Smrg   glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color);
28532001f49Smrg   glEnableClientState( GL_VERTEX_ARRAY );
28632001f49Smrg   glEnableClientState( GL_COLOR_ARRAY );
28732001f49Smrg
28832001f49Smrg   glBindVertexArrayAPPLE( 0 );
28932001f49Smrg   glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF );
29032001f49Smrg   glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE );
29132001f49Smrg}
29232001f49Smrg
29332001f49Smrg
29432001f49Smrgint main( int argc, char *argv[] )
29532001f49Smrg{
29632001f49Smrg   glutInitWindowSize( Width, Height );
29732001f49Smrg   glutInit( &argc, argv );
29832001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
29932001f49Smrg   Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
30032001f49Smrg   glutReshapeFunc( Reshape );
30132001f49Smrg   glutKeyboardFunc( Key );
30232001f49Smrg   glutDisplayFunc( Display );
30332001f49Smrg   glutVisibilityFunc( Visible );
30432001f49Smrg   glewInit();
30532001f49Smrg   Init();
30632001f49Smrg   glutMainLoop();
30732001f49Smrg   return 0;
30832001f49Smrg}
309