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