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