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