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