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