use gray background, memset TempImage to white to help with debugging
[mesa.git] / progs / demos / tunnel.c
1 /*
2 * This program is under the GNU GPL.
3 * Use at your own risk.
4 *
5 * written by David Bucciarelli (tech.hmw@plus.it)
6 * Humanware s.r.l.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <time.h>
13
14 #ifdef WIN32
15 #include <windows.h>
16 #endif
17
18 #include <GL/glut.h>
19 #include "../util/readtex.c"
20 #include "tunneldat.c"
21
22 #ifdef XMESA
23 #include "GL/xmesa.h"
24 static int fullscreen = 1;
25 #endif
26
27 static int WIDTH = 640;
28 static int HEIGHT = 480;
29
30 static GLint T0 = 0;
31 static GLint Frames = 0;
32
33 #define NUMBLOC 5
34
35 #ifndef M_PI
36 #define M_PI 3.1415926535
37 #endif
38
39 extern int striplength_skin_13[];
40 extern float stripdata_skin_13[];
41
42 extern int striplength_skin_12[];
43 extern float stripdata_skin_12[];
44
45 extern int striplength_skin_11[];
46 extern float stripdata_skin_11[];
47
48 extern int striplength_skin_9[];
49 extern float stripdata_skin_9[];
50
51
52 static int win = 0;
53
54 static float obs[3] = { 1000.0, 0.0, 2.0 };
55 static float dir[3];
56 static float v = 0.5;
57 static float alpha = 90.0;
58 static float beta = 90.0;
59
60 static int fog = 0;
61 static int bfcull = 1;
62 static int usetex = 1;
63 static int cstrip = 0;
64 static int help = 1;
65 static int joyavailable = 0;
66 static int joyactive = 0;
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_LINEAR);
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(int *l, 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 obs[0] += v * dir[0];
166 obs[1] += v * dir[1];
167 obs[2] += v * dir[2];
168 }
169
170 static void
171 special(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
189 static void
190 key(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 XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW);
208 break;
209 #endif
210
211 case 'j':
212 joyactive = (!joyactive);
213 break;
214 case 'h':
215 help = (!help);
216 break;
217 case 'f':
218 fog = (!fog);
219 break;
220 case 't':
221 usetex = (!usetex);
222 break;
223 case 'b':
224 if (bfcull) {
225 glDisable(GL_CULL_FACE);
226 bfcull = 0;
227 }
228 else {
229 glEnable(GL_CULL_FACE);
230 bfcull = 1;
231 }
232 break;
233 case 'm':
234 cstrip = (!cstrip);
235 break;
236
237 case 'd':
238 fprintf(stderr, "Deleting textures...\n");
239 glDeleteTextures(1, &t1id);
240 glDeleteTextures(1, &t2id);
241 fprintf(stderr, "Loading textures...\n");
242 inittextures();
243 fprintf(stderr, "Done.\n");
244 break;
245 }
246 }
247
248 static void
249 reshape(int w, int h)
250 {
251 WIDTH = w;
252 HEIGHT = h;
253 glMatrixMode(GL_PROJECTION);
254 glLoadIdentity();
255 gluPerspective(80.0, w / (float) h, 1.0, 50.0);
256 glMatrixMode(GL_MODELVIEW);
257 glLoadIdentity();
258 glViewport(0, 0, w, h);
259 }
260
261 static void
262 printstring(void *font, char *string)
263 {
264 int len, i;
265
266 len = (int) strlen(string);
267 for (i = 0; i < len; i++)
268 glutBitmapCharacter(font, string[i]);
269 }
270
271 static void
272 printhelp(void)
273 {
274 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
275 glColor4f(0.0, 0.0, 0.0, 0.5);
276 glRecti(40, 40, 600, 440);
277
278 glColor3f(1.0, 0.0, 0.0);
279 glRasterPos2i(300, 420);
280 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help");
281
282 glRasterPos2i(60, 390);
283 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help");
284 glRasterPos2i(60, 360);
285 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures");
286 glRasterPos2i(60, 330);
287 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog");
288 glRasterPos2i(60, 300);
289 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Togle strips");
290 glRasterPos2i(60, 270);
291 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling");
292 glRasterPos2i(60, 240);
293 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate");
294 glRasterPos2i(60, 210);
295 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity");
296 glRasterPos2i(60, 180);
297 printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity");
298
299 glRasterPos2i(60, 150);
300 if (joyavailable)
301 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
302 "j - Togle jostick control (Joystick control available)");
303 else
304 printstring(GLUT_BITMAP_TIMES_ROMAN_24,
305 "(No Joystick control available)");
306 }
307
308 static void
309 dojoy(void)
310 {
311 #ifdef WIN32
312 static UINT max[2] = { 0, 0 };
313 static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2];
314 MMRESULT res;
315 JOYINFO joy;
316
317 res = joyGetPos(JOYSTICKID1, &joy);
318
319 if (res == JOYERR_NOERROR) {
320 joyavailable = 1;
321
322 if (max[0] < joy.wXpos)
323 max[0] = joy.wXpos;
324 if (min[0] > joy.wXpos)
325 min[0] = joy.wXpos;
326 center[0] = (max[0] + min[0]) / 2;
327
328 if (max[1] < joy.wYpos)
329 max[1] = joy.wYpos;
330 if (min[1] > joy.wYpos)
331 min[1] = joy.wYpos;
332 center[1] = (max[1] + min[1]) / 2;
333
334 if (joyactive) {
335 if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0]))
336 alpha -=
337 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]);
338 if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1]))
339 beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]);
340
341 if (joy.wButtons & JOY_BUTTON1)
342 v += 0.01;
343 if (joy.wButtons & JOY_BUTTON2)
344 v -= 0.01;
345 }
346 }
347 else
348 joyavailable = 0;
349 #endif
350 }
351
352 static void
353 draw(void)
354 {
355 static char frbuf[80] = "";
356 int i;
357 float base, offset;
358
359 dojoy();
360
361 glClear(GL_COLOR_BUFFER_BIT);
362
363 if (usetex)
364 glEnable(GL_TEXTURE_2D);
365 else
366 glDisable(GL_TEXTURE_2D);
367
368 if (fog)
369 glEnable(GL_FOG);
370 else
371 glDisable(GL_FOG);
372
373 glShadeModel(GL_SMOOTH);
374
375 glPushMatrix();
376 calcposobs();
377 gluLookAt(obs[0], obs[1], obs[2],
378 obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2],
379 0.0, 0.0, 1.0);
380
381 if (dir[0] > 0) {
382 offset = 8.0;
383 base = obs[0] - fmod(obs[0], 8.0);
384 }
385 else {
386 offset = -8.0;
387 base = obs[0] + (8.0 - fmod(obs[0], 8.0));
388 }
389
390 glPushMatrix();
391 glTranslatef(base - offset / 2.0, 0.0, 0.0);
392 for (i = 0; i < NUMBLOC; i++) {
393 glTranslatef(offset, 0.0, 0.0);
394 glBindTexture(GL_TEXTURE_2D, t1id);
395 drawobjs(striplength_skin_11, stripdata_skin_11);
396 glBindTexture(GL_TEXTURE_2D, t2id);
397 drawobjs(striplength_skin_12, stripdata_skin_12);
398 drawobjs(striplength_skin_9, stripdata_skin_9);
399 drawobjs(striplength_skin_13, stripdata_skin_13);
400 }
401 glPopMatrix();
402 glPopMatrix();
403
404 glDisable(GL_TEXTURE_2D);
405 glDisable(GL_FOG);
406 glShadeModel(GL_FLAT);
407
408 glMatrixMode(GL_PROJECTION);
409 glPushMatrix();
410 glLoadIdentity();
411 glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
412
413 glMatrixMode(GL_MODELVIEW);
414 glLoadIdentity();
415
416 glColor3f(1.0, 0.0, 0.0);
417 glRasterPos2i(10, 10);
418 printstring(GLUT_BITMAP_HELVETICA_18, frbuf);
419 glRasterPos2i(350, 470);
420 printstring(GLUT_BITMAP_HELVETICA_10,
421 "Tunnel V1.5 Written by David Bucciarelli (tech.hmw@plus.it)");
422
423 if (help)
424 printhelp();
425
426 glMatrixMode(GL_PROJECTION);
427 glPopMatrix();
428 glMatrixMode(GL_MODELVIEW);
429
430 glutSwapBuffers();
431
432 Frames++;
433 {
434 GLint t = glutGet(GLUT_ELAPSED_TIME);
435 if (t - T0 >= 2000) {
436 GLfloat seconds = (t - T0) / 1000.0;
437 GLfloat fps = Frames / seconds;
438 sprintf(frbuf, "Frame rate: %f", fps);
439 T0 = t;
440 Frames = 0;
441 }
442 }
443 }
444
445 int
446 main(int ac, char **av)
447 {
448 float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 };
449
450 fprintf(stderr,
451 "Tunnel V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n");
452
453 glutInitWindowPosition(0, 0);
454 glutInitWindowSize(WIDTH, HEIGHT);
455 glutInit(&ac, av);
456
457 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
458
459 if (!(win = glutCreateWindow("Tunnel"))) {
460 fprintf(stderr, "Error, couldn't open window\n");
461 return -1;
462 }
463
464 glMatrixMode(GL_PROJECTION);
465 glLoadIdentity();
466 gluPerspective(80.0, WIDTH / (float) HEIGHT, 1.0, 50.0);
467
468 glMatrixMode(GL_MODELVIEW);
469
470 glShadeModel(GL_SMOOTH);
471 glDisable(GL_DEPTH_TEST);
472 glEnable(GL_CULL_FACE);
473 glEnable(GL_TEXTURE_2D);
474
475 glEnable(GL_FOG);
476 glFogi(GL_FOG_MODE, GL_EXP2);
477 glFogfv(GL_FOG_COLOR, fogcolor);
478
479 glFogf(GL_FOG_DENSITY, 0.06);
480 glHint(GL_FOG_HINT, GL_NICEST);
481
482 inittextures();
483
484 glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]);
485 glClear(GL_COLOR_BUFFER_BIT);
486
487 calcposobs();
488
489 glutReshapeFunc(reshape);
490 glutDisplayFunc(draw);
491 glutKeyboardFunc(key);
492 glutSpecialFunc(special);
493 glutIdleFunc(draw);
494
495 glEnable(GL_BLEND);
496 /*glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); */
497 /*glEnable(GL_POLYGON_SMOOTH); */
498
499 glutMainLoop();
500
501 return 0;
502 }