WIP - port to Mali EGL
[mesa-demos/mali.git] / src / tests / quads.c
blob11d983d489b31bd199c2bb0ac5a78aabcf5d4875
1 /**
2 * Draw colored quads.
3 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <math.h>
9 #include <GL/glew.h>
10 #include "glut_wrap.h"
12 #define NUM_QUADS 20
15 static int Win;
16 static GLfloat Xrot = 40, Yrot = 0, Zrot = 0;
17 static GLboolean Anim = GL_TRUE;
18 static GLuint Vbuffer = 0;
20 #if 1
21 #else
22 static GLfloat buf[NUM_QUADS * 6 * 4];
23 #endif
25 static GLboolean doSwapBuffers = GL_TRUE;
27 static GLint Frames = 0, T0 = 0;
30 static void
31 Idle(void)
33 Xrot += 3.0;
34 Yrot += 4.0;
35 Zrot += 2.0;
36 glutPostRedisplay();
40 static void
41 Draw(void)
43 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45 glPushMatrix();
46 glRotatef(Xrot, 1, 0, 0);
47 glRotatef(Yrot, 0, 1, 0);
48 glRotatef(Zrot, 0, 0, 1);
50 glDrawArrays(GL_QUADS, 0, NUM_QUADS*4);
52 glPopMatrix();
54 if (doSwapBuffers)
55 glutSwapBuffers();
57 else
58 glFinish();
62 GLint t = glutGet(GLUT_ELAPSED_TIME);
63 Frames++;
64 if (t - T0 >= 5000) {
65 GLfloat seconds = (t - T0) / 1000.0;
66 GLfloat fps = Frames / seconds;
67 printf("%d frames in %6.3f seconds = %6.3f FPS\n",
68 Frames, seconds, fps);
69 T0 = t;
70 Frames = 0;
76 static void
77 Reshape(int width, int height)
79 glViewport(0, 0, width, height);
80 glMatrixMode(GL_PROJECTION);
81 glLoadIdentity();
82 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
85 glTranslatef(0.0, 0.0, -8.0);
89 static void
90 Key(unsigned char key, int x, int y)
92 const GLfloat step = 3.0;
93 (void) x;
94 (void) y;
95 switch (key) {
96 case 's':
97 doSwapBuffers = !doSwapBuffers;
98 break;
99 case 'a':
100 Anim = !Anim;
101 if (Anim)
102 glutIdleFunc(Idle);
103 else
104 glutIdleFunc(NULL);
105 break;
106 case 'z':
107 Zrot -= step;
108 break;
109 case 'Z':
110 Zrot += step;
111 break;
112 case 27:
113 glutDestroyWindow(Win);
114 exit(0);
115 break;
117 glutPostRedisplay();
121 static void
122 SpecialKey(int key, int x, int y)
124 const GLfloat step = 3.0;
125 (void) x;
126 (void) y;
127 switch (key) {
128 case GLUT_KEY_UP:
129 Xrot -= step;
130 break;
131 case GLUT_KEY_DOWN:
132 Xrot += step;
133 break;
134 case GLUT_KEY_LEFT:
135 Yrot -= step;
136 break;
137 case GLUT_KEY_RIGHT:
138 Yrot += step;
139 break;
141 glutPostRedisplay();
145 static void
146 quad(float x, float y, float z, float *v)
148 int k = 0;
150 /* color */
151 v[k++] = x * 0.5 + 0.5;
152 v[k++] = y * 0.5 + 0.5;
153 v[k++] = z * 0.5 + 0.5;
154 /* vert */
155 v[k++] = x;
156 v[k++] = y;
157 v[k++] = z;
159 /* color */
160 v[k++] = -x * 0.5 + 0.5;
161 v[k++] = -y * 0.5 + 0.5;
162 v[k++] = z * 0.5 + 0.5;
163 /* vert */
164 v[k++] = -x;
165 v[k++] = -y;
166 v[k++] = z;
168 /* color */
169 v[k++] = -x * 0.5 + 0.5;
170 v[k++] = -y * 0.5 + 0.5;
171 v[k++] = -z * 0.5 + 0.5;
172 /* vert */
173 v[k++] = -x;
174 v[k++] = -y;
175 v[k++] = -z;
177 /* color */
178 v[k++] = x * 0.5 + 0.5;
179 v[k++] = y * 0.5 + 0.5;
180 v[k++] = -z * 0.5 + 0.5;
181 /* vert */
182 v[k++] = x;
183 v[k++] = y;
184 v[k++] = -z;
187 static void
188 gen_quads(GLfloat *buf)
190 float *v = buf;
191 float r = 1.0;
192 int i;
194 for (i = 0; i < NUM_QUADS; i++) {
195 float angle = i / (float) NUM_QUADS * M_PI;
196 float x = r * cos(angle);
197 float y = r * sin(angle);
198 float z = 1.10;
199 quad(x, y, z, v);
200 v += 24;
203 if (0) {
204 float *p = buf;
205 for (i = 0; i < NUM_QUADS * 4 * 2; i++) {
206 printf("%d: %f %f %f\n", i, p[0], p[1], p[2]);
207 p += 3;
213 static void
214 Init(void)
216 int bytes = NUM_QUADS * 4 * 2 * 3 * sizeof(float);
217 GLfloat *f;
219 #if 1
220 glGenBuffers(1, &Vbuffer);
221 glBindBuffer(GL_ARRAY_BUFFER, Vbuffer);
222 glBufferData(GL_ARRAY_BUFFER_ARB, bytes, NULL, GL_STATIC_DRAW_ARB);
223 f = (float *) glMapBuffer(GL_ARRAY_BUFFER_ARB, GL_WRITE_ONLY_ARB);
224 gen_quads(f);
225 glUnmapBuffer(GL_ARRAY_BUFFER_ARB);
226 glColorPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 0);
227 glVertexPointer(3, GL_FLOAT, 6*sizeof(float), (void *) 12);
228 #else
229 f = buf;
230 gen_quads(f);
231 glColorPointer(3, GL_FLOAT, 6*sizeof(float), buf);
232 glVertexPointer(3, GL_FLOAT, 6*sizeof(float), buf + 3);
233 #endif
235 glEnableClientState(GL_COLOR_ARRAY);
236 glEnableClientState(GL_VERTEX_ARRAY);
238 glEnable(GL_DEPTH_TEST);
240 glClearColor(0.5, 0.5, 0.5, 0.0);
245 main(int argc, char *argv[])
247 glutInit(&argc, argv);
248 glutInitWindowPosition(0, 0);
249 glutInitWindowSize(600, 600);
250 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
251 Win = glutCreateWindow(argv[0]);
252 glewInit();
253 glutReshapeFunc(Reshape);
254 glutKeyboardFunc(Key);
255 glutSpecialFunc(SpecialKey);
256 glutDisplayFunc(Draw);
257 if (Anim)
258 glutIdleFunc(Idle);
259 Init();
260 glutMainLoop();
261 return 0;