blob: 8d662484109f5a4d87f68b11b5ff454ea1339aa4 [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>
15#include <time.h>
16
17#ifdef WIN32
18#include <windows.h>
19#endif
20
21#include <GL/glut.h>
pescod1ff1f62000-12-24 22:53:54 +000022#include "readtex.c"
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
34GLboolean fxMesaSelectCurrentBoard(int);
35#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];
54static float v = 0.5;
55static float alpha = 90.0;
56static float beta = 90.0;
57
58static int fog = 0;
59static 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,
100 GL_LINEAR_MIPMAP_NEAREST);
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
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{
161 dir[0] = sin(alpha * M_PI / 180.0);
162 dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0);
163 dir[2] = cos(beta * M_PI / 180.0);
164
165 obs[0] += v * dir[0];
166 obs[1] += v * dir[1];
167 obs[2] += v * dir[2];
168}
169
170static void
171special(int k, int x, int y)
172{
173 switch (k) {
174 case GLUT_KEY_LEFT:
175 alpha -= 2.0;
176 break;
177 case GLUT_KEY_RIGHT:
178 alpha += 2.0;
179 break;
180 case GLUT_KEY_DOWN:
181 beta -= 2.0;
182 break;
183 case GLUT_KEY_UP:
184 beta += 2.0;
185 break;
186 }
187}
188
189static void
190key(unsigned char k, int x, int y)
191{
192 switch (k) {
193 case 27:
194 exit(0);
195 break;
196
197 case 'a':
198 v += 0.01;
199 break;
200 case 'z':
201 v -= 0.01;
202 break;
203
204#ifdef XMESA
205 case ' ':
206 fullscreen = (!fullscreen);
207
208 glutSetWindow(channel[0]);
209 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
210
211 glutSetWindow(channel[1]);
212 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
213 break;
214#endif
215
216 case 'j':
217 joyactive = (!joyactive);
218 break;
219 case 'h':
220 help = (!help);
221 break;
222 case 'f':
223 fog = (!fog);
224 break;
225 case 't':
226 usetex = (!usetex);
227 break;
228 case 'b':
229 if (bfcull) {
230 glDisable(GL_CULL_FACE);
231 bfcull = 0;
232 }
233 else {
234 glEnable(GL_CULL_FACE);
235 bfcull = 1;
236 }
237 break;
238 case 'm':
239 cstrip = (!cstrip);
240 break;
241
242 case 'd':
243 fprintf(stderr, "Deleting textures...\n");
244 glDeleteTextures(1, &t1id);
245 glDeleteTextures(1, &t2id);
246 fprintf(stderr, "Loading textures...\n");
247 inittextures();
248 fprintf(stderr, "Done.\n");
249 break;
250 }
251}
252
253static void
254reshapechannel0(int w, int h)
255{
256 float ratio;
257
258 WIDTHC0 = w;
259 HEIGHTC0 = h;
260 glMatrixMode(GL_PROJECTION);
261 glLoadIdentity();
262
263 ratio = 0.5f * w / (float) h;
264
265 glFrustum(-2.0, 0.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
266
267 glMatrixMode(GL_MODELVIEW);
268 glLoadIdentity();
269 glViewport(0, 0, w, h);
270}
271
272static void
273reshapechannel1(int w, int h)
274{
275 float ratio;
276
277 WIDTHC1 = w;
278 HEIGHTC1 = h;
279 glMatrixMode(GL_PROJECTION);
280 glLoadIdentity();
281
282 ratio = 0.5f * w / (float) h;
283
284 glFrustum(0.0, 2.0, -1.0 * ratio, 1.0 * ratio, 1.0, 60.0);
285
286 glMatrixMode(GL_MODELVIEW);
287 glLoadIdentity();
288 glViewport(0, 0, w, h);
289}
290
291static void
292printstring(void *font, char *string)
293{
294 int len, i;
295
296 len = (int) strlen(string);
297 for (i = 0; i < len; i++)
298 glutBitmapCharacter(font, string[i]);
299}
300
301static void
302printhelp(void)
303{
304 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
305 glColor4f(0.0, 0.0, 0.0, 0.5);
306 glRecti(40, 40, 600, 440);
307
308 glColor3f(1.0, 0.0, 0.0);
309 glRasterPos2i(300, 420);
310 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
311
312 glRasterPos2i(60, 390);
313 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help");
314 glRasterPos2i(60, 360);
315 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures");
316 glRasterPos2i(60, 330);
317 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog");
318 glRasterPos2i(60, 300);
319 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Togle strips");
320 glRasterPos2i(60, 270);
321 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling");
322 glRasterPos2i(60, 240);
323 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
324 glRasterPos2i(60, 210);
325 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
326 glRasterPos2i(60, 180);
327 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
328
329 glRasterPos2i(60, 150);
330 if (joyavailable)
331 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
332 "j - Togle jostick control (Joystick control available)");
333 else
334 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
335 "(No Joystick control available)");
336}
337
338static void
339dojoy(void)
340{
341#ifdef WIN32
342 static UINT max[2] = { 0, 0 };
343 static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
344 MMRESULT res;
345 JOYINFO joy;
346
347 res = joyGetPos(JOYSTICKID1, &joy);
348
349 if (res == JOYERR_NOERROR) {
350 joyavailable = 1;
351
352 if (max[0] < joy.wXpos)
353 max[0] = joy.wXpos;
354 if (min[0] > joy.wXpos)
355 min[0] = joy.wXpos;
356 center[0] = (max[0] + min[0]) / 2;
357
358 if (max[1] < joy.wYpos)
359 max[1] = joy.wYpos;
360 if (min[1] > joy.wYpos)
361 min[1] = joy.wYpos;
362 center[1] = (max[1] + min[1]) / 2;
363
364 if (joyactive) {
365 if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
366 alpha -=
367 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
368 if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
369 beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
370
371 if (joy.wButtons & JOY_BUTTON1)
372 v += 0.01;
373 if (joy.wButtons & JOY_BUTTON2)
374 v -= 0.01;
375 }
376 }
377 else
378 joyavailable = 0;
379#endif
380}
381
382static void
383draw(void)
384{
Brian Pauld49b34a2000-09-12 18:44:45 +0000385 static char frbuf[80] = "";
Brian Paul5b0a7f32000-06-27 16:52:38 +0000386 int i;
Brian Pauld49b34a2000-09-12 18:44:45 +0000387 float base, offset;
Brian Paul5b0a7f32000-06-27 16:52:38 +0000388
389 dojoy();
390
391 glClear(GL_COLOR_BUFFER_BIT);
392
393 glClear(GL_COLOR_BUFFER_BIT);
394
395 if (usetex)
396 glEnable(GL_TEXTURE_2D);
397 else
398 glDisable(GL_TEXTURE_2D);
399
400 if (fog)
401 glEnable(GL_FOG);
402 else
403 glDisable(GL_FOG);
404
405 glShadeModel(GL_SMOOTH);
406
407 glPushMatrix();
408 calcposobs();
409 gluLookAt(obs[0], obs[1], obs[2],
410 obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
411 0.0, 0.0, 1.0);
412
413 if (dir[0] > 0) {
414 offset = 8.0;
415 base = obs[0] - fmod(obs[0], 8.0);
416 }
417 else {
418 offset = -8.0;
419 base = obs[0] + (8.0 - fmod(obs[0], 8.0));
420 }
421
422 glPushMatrix();
423 glTranslatef(base - offset / 2.0, 0.0, 0.0);
424 for (i = 0; i < NUMBLOC; i++) {
425 glTranslatef(offset, 0.0, 0.0);
426 glBindTexture(GL_TEXTURE_2D, t1id);
427 drawobjs(striplength_skin_11, stripdata_skin_11);
428 glBindTexture(GL_TEXTURE_2D, t2id);
429 drawobjs(striplength_skin_12, stripdata_skin_12);
430 drawobjs(striplength_skin_9, stripdata_skin_9);
431 drawobjs(striplength_skin_13, stripdata_skin_13);
432 }
433 glPopMatrix();
434 glPopMatrix();
435
Brian Paul5b0a7f32000-06-27 16:52:38 +0000436 glDisable(GL_TEXTURE_2D);
437 glDisable(GL_FOG);
438 glShadeModel(GL_FLAT);
439
440 glMatrixMode(GL_PROJECTION);
441 glPushMatrix();
442 glLoadIdentity();
443 glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
444
445 glMatrixMode(GL_MODELVIEW);
446 glLoadIdentity();
447
448 glColor3f(1.0, 0.0, 0.0);
449 glRasterPos2i(10, 10);
450 printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
451 glRasterPos2i(350, 470);
452 printstring(GLUT_BITMAP_HELVETICA_10,
453 "Tunnel2 V1.0 Written by David Bucciarelli (tech.hmw@plus.it)");
454
455 if (help)
456 printhelp();
457
458 glMatrixMode(GL_PROJECTION);
459 glPopMatrix();
460 glMatrixMode(GL_MODELVIEW);
461
Brian Pauld49b34a2000-09-12 18:44:45 +0000462 Frames++;
463 {
464 GLint t = glutGet(GLUT_ELAPSED_TIME);
465 if (t - T0 >= 2000) {
466 GLfloat seconds = (t - T0) / 1000.0;
467 GLfloat fps = Frames / seconds;
468 sprintf(frbuf, "Frame rate: %f", fps);
469 T0 = t;
470 Frames = 0;
471 }
472 }
Brian Paul5b0a7f32000-06-27 16:52:38 +0000473}
474
475static void
476drawchannel0(void)
477{
478 glutSetWindow(channel[0]);
479 draw();
480 glutSwapBuffers();
481}
482
483static void
484drawchannel1(void)
485{
486 glutSetWindow(channel[1]);
487 draw();
488 glutSwapBuffers();
489}
490
491static void
492drawall(void)
493{
494 glutSetWindow(channel[0]);
495 draw();
496 glutSetWindow(channel[1]);
497 draw();
498
499 glutSetWindow(channel[0]);
500 glutSwapBuffers();
501 glutSetWindow(channel[1]);
502 glutSwapBuffers();
503}
504
505static void
506init(void)
507{
508 float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
509
510 glShadeModel(GL_SMOOTH);
511 glDisable(GL_DEPTH_TEST);
512 glEnable(GL_CULL_FACE);
513 glEnable(GL_TEXTURE_2D);
514
515 glEnable(GL_FOG);
516 glFogi(GL_FOG_MODE, GL_EXP2);
517 glFogfv(GL_FOG_COLOR, fogcolor);
518
519 glFogf(GL_FOG_DENSITY, 0.06);
520 glHint(GL_FOG_HINT, GL_NICEST);
521
522 glEnable(GL_BLEND);
523 /*
524 glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
525 glEnable(GL_POLYGON_SMOOTH);
526 */
527
528 glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
529 glClear(GL_COLOR_BUFFER_BIT);
530}
531
532int
533main(int ac, char **av)
534{
535 fprintf(stderr,
536 "Tunnel2 V1.0\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
Brian Paul5b0a7f32000-06-27 16:52:38 +0000537
538 glutInitWindowPosition(0, 0);
539 glutInitWindowSize(WIDTHC0, HEIGHTC0);
540 glutInit(&ac, av);
541
Brian Pauld49b34a2000-09-12 18:44:45 +0000542 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
Brian Paul5b0a7f32000-06-27 16:52:38 +0000543
544#ifdef FX
545 if (!fxMesaSelectCurrentBoard(0)) {
546 fprintf(stderr, "The first Voodoo Graphics board is missing !?!?\n");
547 return -1;
548 }
549#endif
550 if (!(channel[0] = glutCreateWindow("Channel 0"))) {
551 fprintf(stderr, "Error, couldn't open window\n");
552 return -1;
553 }
554
555 reshapechannel0(WIDTHC0, HEIGHTC0);
556 init();
557 inittextures();
558 glutDisplayFunc(drawchannel0);
559 glutReshapeFunc(reshapechannel0);
560 glutKeyboardFunc(key);
561 glutSpecialFunc(special);
562
563#ifdef FX
564 if (!fxMesaSelectCurrentBoard(1)) {
565 fprintf(stderr, "The second Voodoo Graphics board is missing !\n");
566 exit(-1);
567 }
568#endif
569 glutInitWindowPosition(WIDTHC0, 0);
570 glutInitWindowSize(WIDTHC1, HEIGHTC1);
571 if (!(channel[1] = glutCreateWindow("Channel 1"))) {
572 fprintf(stderr, "Error, couldn't open window\n");
573 exit(-1);
574 }
575
576 reshapechannel1(WIDTHC1, HEIGHTC1);
577 init();
578 inittextures();
579 glutDisplayFunc(drawchannel1);
580 glutReshapeFunc(reshapechannel1);
581 glutKeyboardFunc(key);
582 glutSpecialFunc(special);
583
584 glutIdleFunc(drawall);
585
586 calcposobs();
587
588 glutMainLoop();
589
590 return 0;
591}