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