better fps computation
[mesa.git] / progs / demos / tunnel.c
1 /*
2 * This program is under the GNU GPL.
3 * Use at your own risk.
4 *
5 * written by David Bucciarelli (tech.hmw@plus.it)
6 * Humanware s.r.l.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <time.h>
13
14 #ifdef WIN32
15 #include <windows.h>
16 #endif
17
18 #include <GL/glut.h>
19 #include "../util/readtex.c"
20 #include "tunneldat.c"
21
22 #ifdef XMESA
23 #include "GL/xmesa.h"
24 static int fullscreen = 1;
25 #endif
26
27 static int WIDTH = 640;
28 static int HEIGHT = 480;
29
30 static GLint T0 = 0;
31 static GLint Frames = 0;
32
33 #define NUMBLOC 5
34
35 #ifndef M_PI
36 #define M_PI 3.1415926535
37 #endif
38
39 extern int striplength_skin_13[];
40 extern float stripdata_skin_13[];
41
42 extern int striplength_skin_12[];
43 extern float stripdata_skin_12[];
44
45 extern int striplength_skin_11[];
46 extern float stripdata_skin_11[];
47
48 extern int striplength_skin_9[];
49 extern float stripdata_skin_9[];
50
51
52 static int win = 0;
53
54 static float obs[3] = { 1000.0, 0.0, 2.0 };
55 static float dir[3];
56 static float v = 0.5;
57 static float alpha = 90.0;
58 static float beta = 90.0;
59
60 static int fog = 0;
61 static int bfcull = 1;
62 static int usetex = 1;
63 static int cstrip = 0;
64 static int help = 1;
65 static int joyavailable = 0;
66 static int joyactive = 0;
67
68 static GLuint t1id, t2id;
69
70 static void
71 inittextures(void)
72 {
73 glGenTextures(1, &t1id);
74 glBindTexture(GL_TEXTURE_2D, t1id);
75
76 if (!LoadRGBMipmaps("../images/tile.rgb", GL_RGB)) {
77 fprintf(stderr, "Error reading a texture.\n");
78 exit(-1);
79 }
80
81 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
82 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
83
84 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
85 GL_LINEAR_MIPMAP_LINEAR);
86 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
87
88 glGenTextures(1, &t2id);
89 glBindTexture(GL_TEXTURE_2D, t2id);
90
91 if (!LoadRGBMipmaps("../images/bw.rgb", GL_RGB)) {
92 fprintf(stderr, "Error reading a texture.\n");
93 exit(-1);
94 }
95
96 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
97 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
98
99 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
100 GL_LINEAR_MIPMAP_LINEAR);
101 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102
103 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
104 }
105
106 static void
107 drawobjs(int *l, float *f)
108 {
109 int mend, j;
110
111 if (cstrip) {
112 float r = 0.33, g = 0.33, b = 0.33;
113
114 for (; (*l) != 0;) {
115 mend = *l++;
116
117 r += 0.33;
118 if (r > 1.0) {
119 r = 0.33;
120 g += 0.33;
121 if (g > 1.0) {
122 g = 0.33;
123 b += 0.33;
124 if (b > 1.0)
125 b = 0.33;
126 }
127 }
128
129 glColor3f(r, g, b);
130 glBegin(GL_TRIANGLE_STRIP);
131 for (j = 0; j < mend; j++) {
132 f += 4;
133 glTexCoord2fv(f);
134 f += 2;
135 glVertex3fv(f);
136 f += 3;
137 }
138 glEnd();
139 }
140 }
141 else
142 for (; (*l) != 0;) {
143 mend = *l++;
144
145 glBegin(GL_TRIANGLE_STRIP);
146 for (j = 0; j < mend; j++) {
147 glColor4fv(f);
148 f += 4;
149 glTexCoord2fv(f);
150 f += 2;
151 glVertex3fv(f);
152 f += 3;
153 }
154 glEnd();
155 }
156 }
157
158 static float
159 gettime(void)
160 {
161 static clock_t told = 0;
162 clock_t tnew, ris;
163
164 tnew = clock();
165
166 ris = tnew - told;
167
168 told = tnew;
169
170 return (ris / (float) CLOCKS_PER_SEC);
171 }
172
173 static void
174 calcposobs(void)
175 {
176 dir[0] = sin(alpha * M_PI / 180.0);
177 dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
178 dir[2] = cos(beta * M_PI / 180.0);
179
180 obs[0] += v * dir[0];
181 obs[1] += v * dir[1];
182 obs[2] += v * dir[2];
183 }
184
185 static void
186 special(int k, int x, int y)
187 {
188 switch (k) {
189 case GLUT_KEY_LEFT:
190 alpha -= 2.0;
191 break;
192 case GLUT_KEY_RIGHT:
193 alpha += 2.0;
194 break;
195 case GLUT_KEY_DOWN:
196 beta -= 2.0;
197 break;
198 case GLUT_KEY_UP:
199 beta += 2.0;
200 break;
201 }
202 }
203
204 static void
205 key(unsigned char k, int x, int y)
206 {
207 switch (k) {
208 case 27:
209 exit(0);
210 break;
211
212 case 'a':
213 v += 0.01;
214 break;
215 case 'z':
216 v -= 0.01;
217 break;
218
219 #ifdef XMESA
220 case ' ':
221 fullscreen = (!fullscreen);
222 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
223 break;
224 #endif
225
226 case 'j':
227 joyactive = (!joyactive);
228 break;
229 case 'h':
230 help = (!help);
231 break;
232 case 'f':
233 fog = (!fog);
234 break;
235 case 't':
236 usetex = (!usetex);
237 break;
238 case 'b':
239 if (bfcull) {
240 glDisable(GL_CULL_FACE);
241 bfcull = 0;
242 }
243 else {
244 glEnable(GL_CULL_FACE);
245 bfcull = 1;
246 }
247 break;
248 case 'm':
249 cstrip = (!cstrip);
250 break;
251
252 case 'd':
253 fprintf(stderr, "Deleting textures...\n");
254 glDeleteTextures(1, &t1id);
255 glDeleteTextures(1, &t2id);
256 fprintf(stderr, "Loading textures...\n");
257 inittextures();
258 fprintf(stderr, "Done.\n");
259 break;
260 }
261 }
262
263 static void
264 reshape(int w, int h)
265 {
266 WIDTH = w;
267 HEIGHT = h;
268 glMatrixMode(GL_PROJECTION);
269 glLoadIdentity();
270 gluPerspective(80.0, w / (float) h, 1.0, 50.0);
271 glMatrixMode(GL_MODELVIEW);
272 glLoadIdentity();
273 glViewport(0, 0, w, h);
274 }
275
276 static void
277 printstring(void *font, char *string)
278 {
279 int len, i;
280
281 len = (int) strlen(string);
282 for (i = 0; i < len; i++)
283 glutBitmapCharacter(font, string[i]);
284 }
285
286 static void
287 printhelp(void)
288 {
289 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
290 glColor4f(0.0, 0.0, 0.0, 0.5);
291 glRecti(40, 40, 600, 440);
292
293 glColor3f(1.0, 0.0, 0.0);
294 glRasterPos2i(300, 420);
295 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
296
297 glRasterPos2i(60, 390);
298 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help");
299 glRasterPos2i(60, 360);
300 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures");
301 glRasterPos2i(60, 330);
302 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog");
303 glRasterPos2i(60, 300);
304 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Togle strips");
305 glRasterPos2i(60, 270);
306 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling");
307 glRasterPos2i(60, 240);
308 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
309 glRasterPos2i(60, 210);
310 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
311 glRasterPos2i(60, 180);
312 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
313
314 glRasterPos2i(60, 150);
315 if (joyavailable)
316 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
317 "j - Togle jostick control (Joystick control available)");
318 else
319 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
320 "(No Joystick control available)");
321 }
322
323 static void
324 dojoy(void)
325 {
326 #ifdef WIN32
327 static UINT max[2] = { 0, 0 };
328 static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
329 MMRESULT res;
330 JOYINFO joy;
331
332 res = joyGetPos(JOYSTICKID1, &joy);
333
334 if (res == JOYERR_NOERROR) {
335 joyavailable = 1;
336
337 if (max[0] < joy.wXpos)
338 max[0] = joy.wXpos;
339 if (min[0] > joy.wXpos)
340 min[0] = joy.wXpos;
341 center[0] = (max[0] + min[0]) / 2;
342
343 if (max[1] < joy.wYpos)
344 max[1] = joy.wYpos;
345 if (min[1] > joy.wYpos)
346 min[1] = joy.wYpos;
347 center[1] = (max[1] + min[1]) / 2;
348
349 if (joyactive) {
350 if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
351 alpha -=
352 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
353 if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
354 beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
355
356 if (joy.wButtons & JOY_BUTTON1)
357 v += 0.01;
358 if (joy.wButtons & JOY_BUTTON2)
359 v -= 0.01;
360 }
361 }
362 else
363 joyavailable = 0;
364 #endif
365 }
366
367 static void
368 draw(void)
369 {
370 static char frbuf[80] = "";
371 int i;
372 float fr, base, offset;
373
374 dojoy();
375
376 glClear(GL_COLOR_BUFFER_BIT);
377
378 if (usetex)
379 glEnable(GL_TEXTURE_2D);
380 else
381 glDisable(GL_TEXTURE_2D);
382
383 if (fog)
384 glEnable(GL_FOG);
385 else
386 glDisable(GL_FOG);
387
388 glShadeModel(GL_SMOOTH);
389
390 glPushMatrix();
391 calcposobs();
392 gluLookAt(obs[0], obs[1], obs[2],
393 obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
394 0.0, 0.0, 1.0);
395
396 if (dir[0] > 0) {
397 offset = 8.0;
398 base = obs[0] - fmod(obs[0], 8.0);
399 }
400 else {
401 offset = -8.0;
402 base = obs[0] + (8.0 - fmod(obs[0], 8.0));
403 }
404
405 glPushMatrix();
406 glTranslatef(base - offset / 2.0, 0.0, 0.0);
407 for (i = 0; i < NUMBLOC; i++) {
408 glTranslatef(offset, 0.0, 0.0);
409 glBindTexture(GL_TEXTURE_2D, t1id);
410 drawobjs(striplength_skin_11, stripdata_skin_11);
411 glBindTexture(GL_TEXTURE_2D, t2id);
412 drawobjs(striplength_skin_12, stripdata_skin_12);
413 drawobjs(striplength_skin_9, stripdata_skin_9);
414 drawobjs(striplength_skin_13, stripdata_skin_13);
415 }
416 glPopMatrix();
417 glPopMatrix();
418
419 glDisable(GL_TEXTURE_2D);
420 glDisable(GL_FOG);
421 glShadeModel(GL_FLAT);
422
423 glMatrixMode(GL_PROJECTION);
424 glPushMatrix();
425 glLoadIdentity();
426 glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
427
428 glMatrixMode(GL_MODELVIEW);
429 glLoadIdentity();
430
431 glColor3f(1.0, 0.0, 0.0);
432 glRasterPos2i(10, 10);
433 printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
434 glRasterPos2i(350, 470);
435 printstring(GLUT_BITMAP_HELVETICA_10,
436 "Tunnel V1.5 Written by David Bucciarelli (tech.hmw@plus.it)");
437
438 if (help)
439 printhelp();
440
441 glMatrixMode(GL_PROJECTION);
442 glPopMatrix();
443 glMatrixMode(GL_MODELVIEW);
444
445 glutSwapBuffers();
446
447 Frames++;
448
449 {
450 GLint t = glutGet(GLUT_ELAPSED_TIME);
451 if (t - T0 >= 2000) {
452 GLfloat seconds = (t - T0) / 1000.0;
453 GLfloat fps = Frames / seconds;
454 sprintf(frbuf, "Frame rate: %f", fps);
455 T0 = t;
456 Frames = 0;
457 }
458 }
459 }
460
461 int
462 main(int ac, char **av)
463 {
464 float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
465
466 fprintf(stderr,
467 "Tunnel V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
468
469 glutInitWindowPosition(0, 0);
470 glutInitWindowSize(WIDTH, HEIGHT);
471 glutInit(&ac, av);
472
473 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
474
475 if (!(win = glutCreateWindow("Tunnel"))) {
476 fprintf(stderr, "Error, couldn't open window\n");
477 return -1;
478 }
479
480 glMatrixMode(GL_PROJECTION);
481 glLoadIdentity();
482 gluPerspective(80.0, WIDTH / (float) HEIGHT, 1.0, 50.0);
483
484 glMatrixMode(GL_MODELVIEW);
485
486 glShadeModel(GL_SMOOTH);
487 glDisable(GL_DEPTH_TEST);
488 glEnable(GL_CULL_FACE);
489 glEnable(GL_TEXTURE_2D);
490
491 glEnable(GL_FOG);
492 glFogi(GL_FOG_MODE, GL_EXP2);
493 glFogfv(GL_FOG_COLOR, fogcolor);
494
495 glFogf(GL_FOG_DENSITY, 0.06);
496 glHint(GL_FOG_HINT, GL_NICEST);
497
498 inittextures();
499
500 glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
501 glClear(GL_COLOR_BUFFER_BIT);
502
503 calcposobs();
504
505 glutReshapeFunc(reshape);
506 glutDisplayFunc(draw);
507 glutKeyboardFunc(key);
508 glutSpecialFunc(special);
509 glutIdleFunc(draw);
510
511 glEnable(GL_BLEND);
512 /*glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); */
513 /*glEnable(GL_POLYGON_SMOOTH); */
514
515 glutMainLoop();
516
517 return 0;
518 }