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