b03781622199f3954cc0945c91972ae56ccf7324
[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 int Win = 0;
49 static const GLfloat Near = 5.0, Far = 25.0;
50 static GLfloat angle = 0.0;
51
52 static GLuint cube_array_obj = 0;
53 static GLuint oct_array_obj = 0;
54
55 static const GLfloat cube_vert[] = {
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 -0.5, 0.5, -0.5, 1.0,
60
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 -0.5, 0.5, 0.5, 1.0,
65
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 -0.5, 0.5, 0.5, 1.0,
70
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 -0.5, -0.5, 0.5, 1.0,
75
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 0.5, 0.5, -0.5, 1.0,
80
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 -0.5, 0.5, -0.5, 1.0,
85
86 };
87
88 static const GLfloat cube_color[] = {
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 1.0, 0.0, 0.0, 1.0,
93
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 0.0, 1.0, 0.0, 1.0,
98
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 0.0, 0.0, 1.0, 1.0,
103
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 1.0, 0.0, 1.0, 1.0,
108
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 1.0, 1.0, 1.0, 1.0,
113
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 0.5, 0.5, 0.5, 1.0,
118 };
119
120 static const GLfloat oct_vert[] = {
121 0.0, 0.0, 0.7071, 1.0,
122 0.5, 0.5, 0.0, 1.0,
123 -0.5, 0.5, 0.0, 1.0,
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
138 0.0, 0.0, -0.7071, 1.0,
139 0.5, 0.5, 0.0, 1.0,
140 -0.5, 0.5, 0.0, 1.0,
141
142 0.0, 0.0, -0.7071, 1.0,
143 0.5, -0.5, 0.0, 1.0,
144 -0.5, -0.5, 0.0, 1.0,
145
146 0.0, 0.0, -0.7071, 1.0,
147 -0.5, -0.5, 0.0, 1.0,
148 -0.5, 0.5, 0.0, 1.0,
149
150 0.0, 0.0, -0.7071, 1.0,
151 0.5, 0.5, 0.0, 1.0,
152 0.5, -0.5, 0.0, 1.0,
153 };
154
155 static const GLfloat oct_color[] = {
156 1.0, 0.64, 0.0, 1.0,
157 1.0, 0.64, 0.0, 1.0,
158 1.0, 0.64, 0.0, 1.0,
159
160 0.8, 0.51, 0.0, 1.0,
161 0.8, 0.51, 0.0, 1.0,
162 0.8, 0.51, 0.0, 1.0,
163
164 0.5, 0.32, 0.0, 1.0,
165 0.5, 0.32, 0.0, 1.0,
166 0.5, 0.32, 0.0, 1.0,
167
168 0.2, 0.13, 0.0, 1.0,
169 0.2, 0.13, 0.0, 1.0,
170 0.2, 0.13, 0.0, 1.0,
171
172 0.2, 0.13, 0.0, 1.0,
173 0.2, 0.13, 0.0, 1.0,
174 0.2, 0.13, 0.0, 1.0,
175
176 0.5, 0.32, 0.0, 1.0,
177 0.5, 0.32, 0.0, 1.0,
178 0.5, 0.32, 0.0, 1.0,
179
180 0.8, 0.51, 0.0, 1.0,
181 0.8, 0.51, 0.0, 1.0,
182 0.8, 0.51, 0.0, 1.0,
183
184 1.0, 0.64, 0.0, 1.0,
185 1.0, 0.64, 0.0, 1.0,
186 1.0, 0.64, 0.0, 1.0,
187 };
188
189 static void Display( void )
190 {
191 glClearColor(0.1, 0.1, 0.4, 0);
192 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
193
194 glMatrixMode( GL_MODELVIEW );
195 glLoadIdentity();
196 glTranslatef( 0.0, 0.0, -15.0 );
197 glRotatef( angle, 0.0 * angle , 0.0 * angle, 1.0 );
198
199
200 (*bind_vertex_array)( cube_array_obj );
201 glPushMatrix();
202 glTranslatef(-1.5, 0, 0);
203 glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
204 glDrawArrays( GL_QUADS, 0, 4 * 6 );
205 glPopMatrix();
206
207
208 (*bind_vertex_array)( oct_array_obj );
209 glPushMatrix();
210 glTranslatef(1.5, 0, 0);
211 glRotatef( angle, 0.3 * angle , 0.8 * angle, 1.0 );
212 glDrawArrays( GL_TRIANGLES, 0, 3 * 8 );
213 glPopMatrix();
214
215 glutSwapBuffers();
216 }
217
218
219 static void Idle( void )
220 {
221 static double t0 = -1.;
222 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
223 if (t0 < 0.0)
224 t0 = t;
225 dt = t - t0;
226 t0 = t;
227
228 angle += 70.0 * dt; /* 70 degrees per second */
229 angle = fmod(angle, 360.0); /* prevents eventual overflow */
230
231 glutPostRedisplay();
232 }
233
234
235 static void Visible( int vis )
236 {
237 if ( vis == GLUT_VISIBLE ) {
238 glutIdleFunc( Idle );
239 }
240 else {
241 glutIdleFunc( NULL );
242 }
243 }
244 static void Reshape( int width, int height )
245 {
246 GLfloat ar = (float) width / (float) height;
247 Width = width;
248 Height = height;
249 glViewport( 0, 0, width, height );
250 glMatrixMode( GL_PROJECTION );
251 glLoadIdentity();
252 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
253 }
254
255
256 static void Key( unsigned char key, int x, int y )
257 {
258 (void) x;
259 (void) y;
260 switch (key) {
261 case 27:
262 glutDestroyWindow(Win);
263 exit(0);
264 break;
265 }
266 glutPostRedisplay();
267 }
268
269
270 static void Init( void )
271 {
272 const char * const ver_string = (const char * const)
273 glGetString( GL_VERSION );
274
275 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
276 printf("GL_VERSION = %s\n", ver_string);
277
278 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
279 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
280 exit(1);
281 }
282
283 bind_vertex_array = glutGetProcAddress( "glBindVertexArrayAPPLE" );
284 gen_vertex_arrays = glutGetProcAddress( "glGenVertexArraysAPPLE" );
285 delete_vertex_arrays = glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
286 is_vertex_array = glutGetProcAddress( "glIsVertexArrayAPPLE" );
287
288
289 glEnable( GL_DEPTH_TEST );
290
291 (*gen_vertex_arrays)( 1, & cube_array_obj );
292 (*bind_vertex_array)( cube_array_obj );
293 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert);
294 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color);
295 glEnableClientState( GL_VERTEX_ARRAY );
296 glEnableClientState( GL_COLOR_ARRAY );
297
298 (*gen_vertex_arrays)( 1, & oct_array_obj );
299 (*bind_vertex_array)( oct_array_obj );
300 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert);
301 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color);
302 glEnableClientState( GL_VERTEX_ARRAY );
303 glEnableClientState( GL_COLOR_ARRAY );
304
305 (*bind_vertex_array)( 0 );
306 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF );
307 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE );
308 }
309
310
311 int main( int argc, char *argv[] )
312 {
313 glutInit( &argc, argv );
314 glutInitWindowPosition( 0, 0 );
315 glutInitWindowSize( Width, Height );
316 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
317 Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
318 glutReshapeFunc( Reshape );
319 glutKeyboardFunc( Key );
320 glutDisplayFunc( Display );
321 glutVisibilityFunc( Visible );
322 Init();
323 glutMainLoop();
324 return 0;
325 }