i965: fix bugs in projective texture coordinates
[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 <assert.h>
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;
49 static int Win = 0;
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:
263 glutDestroyWindow(Win);
264 exit(0);
265 break;
266 }
267 glutPostRedisplay();
268 }
269
270
271 static void Init( void )
272 {
273 const char * const ver_string = (const char * const)
274 glGetString( GL_VERSION );
275
276 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
277 printf("GL_VERSION = %s\n", ver_string);
278
279 if ( !glutExtensionSupported("GL_APPLE_vertex_array_object") ) {
280 printf("Sorry, this program requires GL_APPLE_vertex_array_object\n");
281 exit(1);
282 }
283
284 bind_vertex_array = (PFNGLBINDVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glBindVertexArrayAPPLE" );
285 gen_vertex_arrays = (PFNGLGENVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glGenVertexArraysAPPLE" );
286 delete_vertex_arrays = (PFNGLDELETEVERTEXARRAYSAPPLEPROC) glutGetProcAddress( "glDeleteVertexArraysAPPLE" );
287 is_vertex_array = (PFNGLISVERTEXARRAYAPPLEPROC) glutGetProcAddress( "glIsVertexArrayAPPLE" );
288
289 assert(bind_vertex_array);
290 assert(gen_vertex_arrays);
291 assert(delete_vertex_arrays);
292 assert(is_vertex_array);
293
294 glEnable( GL_DEPTH_TEST );
295
296 (*gen_vertex_arrays)( 1, & cube_array_obj );
297 (*bind_vertex_array)( cube_array_obj );
298 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_vert);
299 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, cube_color);
300 glEnableClientState( GL_VERTEX_ARRAY );
301 glEnableClientState( GL_COLOR_ARRAY );
302
303 (*gen_vertex_arrays)( 1, & oct_array_obj );
304 (*bind_vertex_array)( oct_array_obj );
305 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_vert);
306 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, oct_color);
307 glEnableClientState( GL_VERTEX_ARRAY );
308 glEnableClientState( GL_COLOR_ARRAY );
309
310 (*bind_vertex_array)( 0 );
311 glVertexPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xDEADBEEF );
312 glColorPointer( 4, GL_FLOAT, sizeof(GLfloat) * 4, (void *) 0xBADDC0DE );
313 }
314
315
316 int main( int argc, char *argv[] )
317 {
318 glutInit( &argc, argv );
319 glutInitWindowPosition( 0, 0 );
320 glutInitWindowSize( Width, Height );
321 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
322 Win = glutCreateWindow( "GL_APPLE_vertex_array_object demo" );
323 glutReshapeFunc( Reshape );
324 glutKeyboardFunc( Key );
325 glutDisplayFunc( Display );
326 glutVisibilityFunc( Visible );
327 Init();
328 glutMainLoop();
329 return 0;
330 }