blob: f4171a8346d60c5b9414e7c3806389e6d3298dfe [file] [log] [blame]
Brian Paul5b0a7f32000-06-27 16:52:38 +00001/*
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>
Brian Paul92eddb02005-01-09 17:37:50 +000015#include <string.h>
Brian Paul5b0a7f32000-06-27 16:52:38 +000016
17#ifdef WIN32
18#include <windows.h>
19#endif
20
21#include <GL/glut.h>
Brian Paul92eddb02005-01-09 17:37:50 +000022#include "readtex.h"
Brian Paul0a355dc2001-03-27 17:30:51 +000023#include "tunneldat.h"
Brian Paul5b0a7f32000-06-27 16:52:38 +000024
25#ifdef FX
26#endif
27
28#ifdef XMESA
29#include "GL/xmesa.h"
30static int fullscreen = 1;
31#endif
32
33#ifdef FX
Daniel Borcae9e98402003-12-19 11:26:46 +000034GLint fxMesaSelectCurrentBoard(int);
Brian Paul5b0a7f32000-06-27 16:52:38 +000035#endif
36
37static int WIDTHC0 = 640;
38static int HEIGHTC0 = 480;
39
40static int WIDTHC1 = 640;
41static int HEIGHTC1 = 480;
42
Brian Pauld49b34a2000-09-12 18:44:45 +000043static GLint T0 = 0;
44static GLint Frames = 0;
Brian Paul5b0a7f32000-06-27 16:52:38 +000045
46#define NUMBLOC 5
47
48#ifndef M_PI
49#define M_PI 3.1415926535
50#endif
51
Brian Paul5b0a7f32000-06-27 16:52:38 +000052static float obs[3] = { 1000.0, 0.0, 2.0 };
53static float dir[3];
Brian Paul92eddb02005-01-09 17:37:50 +000054static float v = 30.;
Brian Paul5b0a7f32000-06-27 16:52:38 +000055static float alpha = 90.0;
56static float beta = 90.0;
57
Brian Paul92eddb02005-01-09 17:37:50 +000058static int fog = 1;
Brian Paul5b0a7f32000-06-27 16:52:38 +000059static int bfcull = 1;
60static int usetex = 1;
61static int cstrip = 0;
62static int help = 1;
63static int joyavailable = 0;
64static int joyactive = 0;
65
66static int channel[2];
67
68static GLuint t1id, t2id;
69
70static void
71inittextures(void)
72{
Brian Paul5b0a7f32000-06-27 16:52:38 +000073 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_NEAREST);
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,
Brian Paul92eddb02005-01-09 17:37:50 +0000100 GL_LINEAR_MIPMAP_LINEAR);
Brian Paul5b0a7f32000-06-27 16:52:38 +0000101 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102
103 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
104}
105
106static void
Brian Paul0a355dc2001-03-27 17:30:51 +0000107drawobjs(const int *l, const float *f)
Brian Paul5b0a7f32000-06-27 16:52:38 +0000108{
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
Brian Paul5b0a7f32000-06-27 16:52:38 +0000158static void
159calcposobs(void)
160{
Brian Paul92eddb02005-01-09 17:37:50 +0000161 static double t0 = -1.;
162 double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
163 if (t0 < 0.0)
164 t0 = t;
165 dt = t - t0;
166 t0 = t;
167
Brian Paul5b0a7f32000-06-27 16:52:38 +0000168 dir[0] = sin(alpha * M_PI / 180.0);
169 dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
170 dir[2] = cos(beta * M_PI / 180.0);
171
Brian Paula41edc32001-05-09 20:02:28 +0000172 if (dir[0] < 1.0e-5 && dir[0] > -1.0e-5)
173 dir[0] = 0;
174 if (dir[1] < 1.0e-5 && dir[1] > -1.0e-5)
175 dir[1] = 0;
176 if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5)
177 dir[2] = 0;
178
Brian Paul92eddb02005-01-09 17:37:50 +0000179 obs[0] += v * dir[0] * dt;
180 obs[1] += v * dir[1] * dt;
181 obs[2] += v * dir[2] * dt;
Brian Paul5b0a7f32000-06-27 16:52:38 +0000182}
183
184static void
185special(int k, int x, int y)
186{
187 switch (k) {
188 case GLUT_KEY_LEFT:
189 alpha -= 2.0;
190 break;
191 case GLUT_KEY_RIGHT:
192 alpha += 2.0;
193 break;
194 case GLUT_KEY_DOWN:
195 beta -= 2.0;
196 break;
197 case GLUT_KEY_UP:
198 beta += 2.0;
199 break;
200 }
201}
202
203static void
204key(unsigned char k, int x, int y)
205{
206 switch (k) {
207 case 27:
Brian Paul547e4872008-07-09 15:52:04 -0600208 glutDestroyWindow(channel[0]);
209 glutDestroyWindow(channel[1]);
Brian Paul5b0a7f32000-06-27 16:52:38 +0000210 exit(0);
211 break;
212
213 case 'a':
Brian Paul92eddb02005-01-09 17:37:50 +0000214 v += 5.;
Brian Paul5b0a7f32000-06-27 16:52:38 +0000215 break;
216 case 'z':
Brian Paul92eddb02005-01-09 17:37:50 +0000217 v -= 5.;
Brian Paul5b0a7f32000-06-27 16:52:38 +0000218 break;
219
220#ifdef XMESA
221 case ' ':
222 fullscreen = (!fullscreen);
223
224 glutSetWindow(channel[0]);
225 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
226
227 glutSetWindow(channel[1]);
228 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
229 break;
230#endif
231
232 case 'j':
233 joyactive = (!joyactive);
234 break;
235 case 'h':
236 help = (!help);
237 break;
238 case 'f':
239 fog = (!fog);
240 break;
241 case 't':
242 usetex = (!usetex);
243 break;
244 case 'b':
245 if (bfcull) {
246 glDisable(GL_CULL_FACE);
247 bfcull = 0;
248 }
249 else {
250 glEnable(GL_CULL_FACE);
251 bfcull = 1;
252 }
253 break;
254 case 'm':
255 cstrip = (!cstrip);
256 break;
257
258 case 'd':
259 fprintf(stderr, "Deleting textures...\n");
260 glDeleteTextures(1, &t1id);
261 glDeleteTextures(1, &t2id);
262 fprintf(stderr, "Loading textures...\n");
263 inittextures();
264 fprintf(stderr, "Done.\n");
265 break;
266 }
267}
268
269static void
270reshapechannel0(int w, int h)
271{
272 float ratio;
273
274 WIDTHC0 = w;
275 HEIGHTC0 = h;
276 glMatrixMode(GL_PROJECTION);
277 glLoadIdentity();
278
279 ratio = 0.5f * w / (float) h;
280
281 glFrustum(-2.0, 0.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
282
283 glMatrixMode(GL_MODELVIEW);
284 glLoadIdentity();
285 glViewport(0, 0, w, h);
286}
287
288static void
289reshapechannel1(int w, int h)
290{
291 float ratio;
292
293 WIDTHC1 = w;
294 HEIGHTC1 = h;
295 glMatrixMode(GL_PROJECTION);
296 glLoadIdentity();
297
298 ratio = 0.5f * w / (float) h;
299
300 glFrustum(0.0, 2.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
301
302 glMatrixMode(GL_MODELVIEW);
303 glLoadIdentity();
304 glViewport(0, 0, w, h);
305}
306
307static void
308printstring(void *font, char *string)
309{
310 int len, i;
311
312 len = (int) strlen(string);
313 for (i = 0; i < len; i++)
314 glutBitmapCharacter(font, string[i]);
315}
316
317static void
318printhelp(void)
319{
320 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
321 glColor4f(0.0, 0.0, 0.0, 0.5);
322 glRecti(40, 40, 600, 440);
323
324 glColor3f(1.0, 0.0, 0.0);
325 glRasterPos2i(300, 420);
326 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
327
328 glRasterPos2i(60, 390);
Brian Paulc39a4bc2005-04-04 20:06:40 +0000329 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Toggle Help");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000330 glRasterPos2i(60, 360);
Brian Paulc39a4bc2005-04-04 20:06:40 +0000331 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Toggle Textures");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000332 glRasterPos2i(60, 330);
Brian Paulc39a4bc2005-04-04 20:06:40 +0000333 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000334 glRasterPos2i(60, 300);
Brian Paulc39a4bc2005-04-04 20:06:40 +0000335 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Toggle strips");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000336 glRasterPos2i(60, 270);
Brian Paulc39a4bc2005-04-04 20:06:40 +0000337 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Toggle Back face culling");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000338 glRasterPos2i(60, 240);
339 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
340 glRasterPos2i(60, 210);
341 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
342 glRasterPos2i(60, 180);
343 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
344
345 glRasterPos2i(60, 150);
346 if (joyavailable)
347 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
Brian Paulc39a4bc2005-04-04 20:06:40 +0000348 "j - Toggle jostick control (Joystick control available)");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000349 else
350 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
351 "(No Joystick control available)");
352}
353
354static void
355dojoy(void)
356{
357#ifdef WIN32
358 static UINT max[2] = { 0, 0 };
359 static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
360 MMRESULT res;
361 JOYINFO joy;
362
363 res = joyGetPos(JOYSTICKID1, &joy);
364
365 if (res == JOYERR_NOERROR) {
366 joyavailable = 1;
367
368 if (max[0] < joy.wXpos)
369 max[0] = joy.wXpos;
370 if (min[0] > joy.wXpos)
371 min[0] = joy.wXpos;
372 center[0] = (max[0] + min[0]) / 2;
373
374 if (max[1] < joy.wYpos)
375 max[1] = joy.wYpos;
376 if (min[1] > joy.wYpos)
377 min[1] = joy.wYpos;
378 center[1] = (max[1] + min[1]) / 2;
379
380 if (joyactive) {
381 if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
382 alpha -=
383 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
384 if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
385 beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
386
387 if (joy.wButtons & JOY_BUTTON1)
388 v += 0.01;
389 if (joy.wButtons & JOY_BUTTON2)
390 v -= 0.01;
391 }
392 }
393 else
394 joyavailable = 0;
395#endif
396}
397
398static void
399draw(void)
400{
Brian Pauld49b34a2000-09-12 18:44:45 +0000401 static char frbuf[80] = "";
Brian Paul5b0a7f32000-06-27 16:52:38 +0000402 int i;
Brian Pauld49b34a2000-09-12 18:44:45 +0000403 float base, offset;
Brian Paul5b0a7f32000-06-27 16:52:38 +0000404
405 dojoy();
406
407 glClear(GL_COLOR_BUFFER_BIT);
408
409 glClear(GL_COLOR_BUFFER_BIT);
410
411 if (usetex)
412 glEnable(GL_TEXTURE_2D);
413 else
414 glDisable(GL_TEXTURE_2D);
415
416 if (fog)
417 glEnable(GL_FOG);
418 else
419 glDisable(GL_FOG);
420
421 glShadeModel(GL_SMOOTH);
422
423 glPushMatrix();
424 calcposobs();
425 gluLookAt(obs[0], obs[1], obs[2],
426 obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
427 0.0, 0.0, 1.0);
428
429 if (dir[0] > 0) {
430 offset = 8.0;
431 base = obs[0] - fmod(obs[0], 8.0);
432 }
433 else {
434 offset = -8.0;
435 base = obs[0] + (8.0 - fmod(obs[0], 8.0));
436 }
437
438 glPushMatrix();
439 glTranslatef(base - offset / 2.0, 0.0, 0.0);
440 for (i = 0; i < NUMBLOC; i++) {
441 glTranslatef(offset, 0.0, 0.0);
442 glBindTexture(GL_TEXTURE_2D, t1id);
443 drawobjs(striplength_skin_11, stripdata_skin_11);
444 glBindTexture(GL_TEXTURE_2D, t2id);
445 drawobjs(striplength_skin_12, stripdata_skin_12);
446 drawobjs(striplength_skin_9, stripdata_skin_9);
447 drawobjs(striplength_skin_13, stripdata_skin_13);
448 }
449 glPopMatrix();
450 glPopMatrix();
451
Brian Paul5b0a7f32000-06-27 16:52:38 +0000452 glDisable(GL_TEXTURE_2D);
453 glDisable(GL_FOG);
454 glShadeModel(GL_FLAT);
455
456 glMatrixMode(GL_PROJECTION);
457 glPushMatrix();
458 glLoadIdentity();
459 glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
460
461 glMatrixMode(GL_MODELVIEW);
462 glLoadIdentity();
463
464 glColor3f(1.0, 0.0, 0.0);
465 glRasterPos2i(10, 10);
466 printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
467 glRasterPos2i(350, 470);
468 printstring(GLUT_BITMAP_HELVETICA_10,
469 "Tunnel2 V1.0 Written by David Bucciarelli (tech.hmw@plus.it)");
470
471 if (help)
472 printhelp();
473
474 glMatrixMode(GL_PROJECTION);
475 glPopMatrix();
476 glMatrixMode(GL_MODELVIEW);
477
Brian Pauld49b34a2000-09-12 18:44:45 +0000478 Frames++;
479 {
480 GLint t = glutGet(GLUT_ELAPSED_TIME);
481 if (t - T0 >= 2000) {
482 GLfloat seconds = (t - T0) / 1000.0;
483 GLfloat fps = Frames / seconds;
484 sprintf(frbuf, "Frame rate: %f", fps);
485 T0 = t;
486 Frames = 0;
487 }
488 }
Brian Paul5b0a7f32000-06-27 16:52:38 +0000489}
490
491static void
492drawchannel0(void)
493{
494 glutSetWindow(channel[0]);
495 draw();
496 glutSwapBuffers();
497}
498
499static void
500drawchannel1(void)
501{
502 glutSetWindow(channel[1]);
503 draw();
504 glutSwapBuffers();
505}
506
507static void
508drawall(void)
509{
510 glutSetWindow(channel[0]);
511 draw();
512 glutSetWindow(channel[1]);
513 draw();
514
515 glutSetWindow(channel[0]);
516 glutSwapBuffers();
517 glutSetWindow(channel[1]);
518 glutSwapBuffers();
519}
520
521static void
522init(void)
523{
524 float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
525
526 glShadeModel(GL_SMOOTH);
527 glDisable(GL_DEPTH_TEST);
528 glEnable(GL_CULL_FACE);
529 glEnable(GL_TEXTURE_2D);
530
531 glEnable(GL_FOG);
532 glFogi(GL_FOG_MODE, GL_EXP2);
533 glFogfv(GL_FOG_COLOR, fogcolor);
534
535 glFogf(GL_FOG_DENSITY, 0.06);
536 glHint(GL_FOG_HINT, GL_NICEST);
537
538 glEnable(GL_BLEND);
539 /*
540 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
541 glEnable(GL_POLYGON_SMOOTH);
542 */
543
544 glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
545 glClear(GL_COLOR_BUFFER_BIT);
546}
547
548int
549main(int ac, char **av)
550{
551 fprintf(stderr,
552 "Tunnel2 V1.0\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000553
554 glutInitWindowPosition(0, 0);
555 glutInitWindowSize(WIDTHC0, HEIGHTC0);
556 glutInit(&ac, av);
557
Brian Pauld49b34a2000-09-12 18:44:45 +0000558 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Brian Paul5b0a7f32000-06-27 16:52:38 +0000559
560#ifdef FX
Daniel Borcae9e98402003-12-19 11:26:46 +0000561 if (fxMesaSelectCurrentBoard(0) < 0) {
Brian Paul5b0a7f32000-06-27 16:52:38 +0000562 fprintf(stderr, "The first Voodoo Graphics board is missing !?!?\n");
563 return -1;
564 }
565#endif
566 if (!(channel[0] = glutCreateWindow("Channel 0"))) {
567 fprintf(stderr, "Error, couldn't open window\n");
568 return -1;
569 }
570
571 reshapechannel0(WIDTHC0, HEIGHTC0);
572 init();
573 inittextures();
574 glutDisplayFunc(drawchannel0);
575 glutReshapeFunc(reshapechannel0);
576 glutKeyboardFunc(key);
577 glutSpecialFunc(special);
578
579#ifdef FX
Daniel Borcae9e98402003-12-19 11:26:46 +0000580 if (fxMesaSelectCurrentBoard(1) < 0) {
Brian Paul5b0a7f32000-06-27 16:52:38 +0000581 fprintf(stderr, "The second Voodoo Graphics board is missing !\n");
582 exit(-1);
583 }
584#endif
585 glutInitWindowPosition(WIDTHC0, 0);
586 glutInitWindowSize(WIDTHC1, HEIGHTC1);
587 if (!(channel[1] = glutCreateWindow("Channel 1"))) {
588 fprintf(stderr, "Error, couldn't open window\n");
589 exit(-1);
590 }
591
592 reshapechannel1(WIDTHC1, HEIGHTC1);
593 init();
594 inittextures();
595 glutDisplayFunc(drawchannel1);
596 glutReshapeFunc(reshapechannel1);
597 glutKeyboardFunc(key);
598 glutSpecialFunc(special);
599
600 glutIdleFunc(drawall);
601
602 calcposobs();
603
604 glutMainLoop();
605
606 return 0;
607}