Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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 | #define FRAME 50 |
| 31 | |
| 32 | #define NUMBLOC 5 |
| 33 | |
| 34 | #ifndef M_PI |
| 35 | #define M_PI 3.1415926535 |
| 36 | #endif |
| 37 | |
| 38 | extern int striplength_skin_13[]; |
| 39 | extern float stripdata_skin_13[]; |
| 40 | |
| 41 | extern int striplength_skin_12[]; |
| 42 | extern float stripdata_skin_12[]; |
| 43 | |
| 44 | extern int striplength_skin_11[]; |
| 45 | extern float stripdata_skin_11[]; |
| 46 | |
| 47 | extern int striplength_skin_9[]; |
| 48 | extern float stripdata_skin_9[]; |
| 49 | |
| 50 | |
| 51 | static int win = 0; |
| 52 | |
| 53 | static float obs[3] = { 1000.0, 0.0, 2.0 }; |
| 54 | static float dir[3]; |
| 55 | static float v = 0.5; |
| 56 | static float alpha = 90.0; |
| 57 | static float beta = 90.0; |
| 58 | |
| 59 | static int fog = 0; |
| 60 | static int bfcull = 1; |
| 61 | static int usetex = 1; |
| 62 | static int cstrip = 0; |
| 63 | static int help = 1; |
| 64 | static int joyavailable = 0; |
| 65 | static int joyactive = 0; |
| 66 | |
| 67 | static GLuint t1id, t2id; |
| 68 | |
| 69 | static void |
| 70 | inittextures(void) |
| 71 | { |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 72 | glGenTextures(1, &t1id); |
| 73 | glBindTexture(GL_TEXTURE_2D, t1id); |
| 74 | |
| 75 | if (!LoadRGBMipmaps("../images/tile.rgb", GL_RGB)) { |
| 76 | fprintf(stderr, "Error reading a texture.\n"); |
| 77 | exit(-1); |
| 78 | } |
| 79 | |
| 80 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 81 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 82 | |
| 83 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 84 | GL_LINEAR_MIPMAP_LINEAR); |
| 85 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 86 | |
| 87 | glGenTextures(1, &t2id); |
| 88 | glBindTexture(GL_TEXTURE_2D, t2id); |
| 89 | |
| 90 | if (!LoadRGBMipmaps("../images/bw.rgb", GL_RGB)) { |
| 91 | fprintf(stderr, "Error reading a texture.\n"); |
| 92 | exit(-1); |
| 93 | } |
| 94 | |
| 95 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 96 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 97 | |
| 98 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 99 | GL_LINEAR_MIPMAP_LINEAR); |
| 100 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 101 | |
| 102 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | drawobjs(int *l, float *f) |
| 107 | { |
| 108 | int mend, j; |
| 109 | |
| 110 | if (cstrip) { |
| 111 | float r = 0.33, g = 0.33, b = 0.33; |
| 112 | |
| 113 | for (; (*l) != 0;) { |
| 114 | mend = *l++; |
| 115 | |
| 116 | r += 0.33; |
| 117 | if (r > 1.0) { |
| 118 | r = 0.33; |
| 119 | g += 0.33; |
| 120 | if (g > 1.0) { |
| 121 | g = 0.33; |
| 122 | b += 0.33; |
| 123 | if (b > 1.0) |
| 124 | b = 0.33; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | glColor3f(r, g, b); |
| 129 | glBegin(GL_TRIANGLE_STRIP); |
| 130 | for (j = 0; j < mend; j++) { |
| 131 | f += 4; |
| 132 | glTexCoord2fv(f); |
| 133 | f += 2; |
| 134 | glVertex3fv(f); |
| 135 | f += 3; |
| 136 | } |
| 137 | glEnd(); |
| 138 | } |
| 139 | } |
| 140 | else |
| 141 | for (; (*l) != 0;) { |
| 142 | mend = *l++; |
| 143 | |
| 144 | glBegin(GL_TRIANGLE_STRIP); |
| 145 | for (j = 0; j < mend; j++) { |
| 146 | glColor4fv(f); |
| 147 | f += 4; |
| 148 | glTexCoord2fv(f); |
| 149 | f += 2; |
| 150 | glVertex3fv(f); |
| 151 | f += 3; |
| 152 | } |
| 153 | glEnd(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static float |
| 158 | gettime(void) |
| 159 | { |
| 160 | static clock_t told = 0; |
| 161 | clock_t tnew, ris; |
| 162 | |
| 163 | tnew = clock(); |
| 164 | |
| 165 | ris = tnew - told; |
| 166 | |
| 167 | told = tnew; |
| 168 | |
| 169 | return (ris / (float) CLOCKS_PER_SEC); |
| 170 | } |
| 171 | |
| 172 | static void |
| 173 | calcposobs(void) |
| 174 | { |
| 175 | dir[0] = sin(alpha * M_PI / 180.0); |
| 176 | dir[1] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0); |
| 177 | dir[2] = cos(beta * M_PI / 180.0); |
| 178 | |
| 179 | obs[0] += v * dir[0]; |
| 180 | obs[1] += v * dir[1]; |
| 181 | obs[2] += v * dir[2]; |
| 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 += 0.01; |
| 213 | break; |
| 214 | case 'z': |
| 215 | v -= 0.01; |
| 216 | break; |
| 217 | |
| 218 | #ifdef XMESA |
| 219 | case ' ': |
| 220 | fullscreen = (!fullscreen); |
| 221 | XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW); |
| 222 | break; |
| 223 | #endif |
| 224 | |
| 225 | case 'j': |
| 226 | joyactive = (!joyactive); |
| 227 | break; |
| 228 | case 'h': |
| 229 | help = (!help); |
| 230 | break; |
| 231 | case 'f': |
| 232 | fog = (!fog); |
| 233 | break; |
| 234 | case 't': |
| 235 | usetex = (!usetex); |
| 236 | break; |
| 237 | case 'b': |
| 238 | if (bfcull) { |
| 239 | glDisable(GL_CULL_FACE); |
| 240 | bfcull = 0; |
| 241 | } |
| 242 | else { |
| 243 | glEnable(GL_CULL_FACE); |
| 244 | bfcull = 1; |
| 245 | } |
| 246 | break; |
| 247 | case 'm': |
| 248 | cstrip = (!cstrip); |
| 249 | break; |
| 250 | |
| 251 | case 'd': |
| 252 | fprintf(stderr, "Deleting textures...\n"); |
| 253 | glDeleteTextures(1, &t1id); |
| 254 | glDeleteTextures(1, &t2id); |
| 255 | fprintf(stderr, "Loading textures...\n"); |
| 256 | inittextures(); |
| 257 | fprintf(stderr, "Done.\n"); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | reshape(int w, int h) |
| 264 | { |
| 265 | WIDTH = w; |
| 266 | HEIGHT = h; |
| 267 | glMatrixMode(GL_PROJECTION); |
| 268 | glLoadIdentity(); |
| 269 | gluPerspective(80.0, w / (float) h, 1.0, 50.0); |
| 270 | glMatrixMode(GL_MODELVIEW); |
| 271 | glLoadIdentity(); |
| 272 | glViewport(0, 0, w, h); |
| 273 | } |
| 274 | |
| 275 | static void |
| 276 | printstring(void *font, char *string) |
| 277 | { |
| 278 | int len, i; |
| 279 | |
| 280 | len = (int) strlen(string); |
| 281 | for (i = 0; i < len; i++) |
| 282 | glutBitmapCharacter(font, string[i]); |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | printhelp(void) |
| 287 | { |
| 288 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 289 | glColor4f(0.0, 0.0, 0.0, 0.5); |
| 290 | glRecti(40, 40, 600, 440); |
| 291 | |
| 292 | glColor3f(1.0, 0.0, 0.0); |
| 293 | glRasterPos2i(300, 420); |
| 294 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help"); |
| 295 | |
| 296 | glRasterPos2i(60, 390); |
| 297 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help"); |
| 298 | glRasterPos2i(60, 360); |
| 299 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures"); |
| 300 | glRasterPos2i(60, 330); |
| 301 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog"); |
| 302 | glRasterPos2i(60, 300); |
| 303 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "m - Togle strips"); |
| 304 | glRasterPos2i(60, 270); |
| 305 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling"); |
| 306 | glRasterPos2i(60, 240); |
| 307 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate"); |
| 308 | glRasterPos2i(60, 210); |
| 309 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity"); |
| 310 | glRasterPos2i(60, 180); |
| 311 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity"); |
| 312 | |
| 313 | glRasterPos2i(60, 150); |
| 314 | if (joyavailable) |
| 315 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
| 316 | "j - Togle jostick control (Joystick control available)"); |
| 317 | else |
| 318 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
| 319 | "(No Joystick control available)"); |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | dojoy(void) |
| 324 | { |
| 325 | #ifdef WIN32 |
| 326 | static UINT max[2] = { 0, 0 }; |
| 327 | static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2]; |
| 328 | MMRESULT res; |
| 329 | JOYINFO joy; |
| 330 | |
| 331 | res = joyGetPos(JOYSTICKID1, &joy); |
| 332 | |
| 333 | if (res == JOYERR_NOERROR) { |
| 334 | joyavailable = 1; |
| 335 | |
| 336 | if (max[0] < joy.wXpos) |
| 337 | max[0] = joy.wXpos; |
| 338 | if (min[0] > joy.wXpos) |
| 339 | min[0] = joy.wXpos; |
| 340 | center[0] = (max[0] + min[0]) / 2; |
| 341 | |
| 342 | if (max[1] < joy.wYpos) |
| 343 | max[1] = joy.wYpos; |
| 344 | if (min[1] > joy.wYpos) |
| 345 | min[1] = joy.wYpos; |
| 346 | center[1] = (max[1] + min[1]) / 2; |
| 347 | |
| 348 | if (joyactive) { |
| 349 | if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0])) |
| 350 | alpha -= |
| 351 | 2.0 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]); |
| 352 | if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1])) |
| 353 | beta += 2.0 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]); |
| 354 | |
| 355 | if (joy.wButtons & JOY_BUTTON1) |
| 356 | v += 0.01; |
| 357 | if (joy.wButtons & JOY_BUTTON2) |
| 358 | v -= 0.01; |
| 359 | } |
| 360 | } |
| 361 | else |
| 362 | joyavailable = 0; |
| 363 | #endif |
| 364 | } |
| 365 | |
| 366 | static void |
| 367 | draw(void) |
| 368 | { |
| 369 | static int count = 0; |
| 370 | static char frbuf[80]; |
| 371 | int i; |
| 372 | float fr, base, offset; |
| 373 | |
| 374 | dojoy(); |
| 375 | |
| 376 | glClear(GL_COLOR_BUFFER_BIT); |
| 377 | |
| 378 | if (usetex) |
| 379 | glEnable(GL_TEXTURE_2D); |
| 380 | else |
| 381 | glDisable(GL_TEXTURE_2D); |
| 382 | |
| 383 | if (fog) |
| 384 | glEnable(GL_FOG); |
| 385 | else |
| 386 | glDisable(GL_FOG); |
| 387 | |
| 388 | glShadeModel(GL_SMOOTH); |
| 389 | |
| 390 | glPushMatrix(); |
| 391 | calcposobs(); |
| 392 | gluLookAt(obs[0], obs[1], obs[2], |
| 393 | obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2], |
| 394 | 0.0, 0.0, 1.0); |
| 395 | |
| 396 | if (dir[0] > 0) { |
| 397 | offset = 8.0; |
| 398 | base = obs[0] - fmod(obs[0], 8.0); |
| 399 | } |
| 400 | else { |
| 401 | offset = -8.0; |
| 402 | base = obs[0] + (8.0 - fmod(obs[0], 8.0)); |
| 403 | } |
| 404 | |
| 405 | glPushMatrix(); |
| 406 | glTranslatef(base - offset / 2.0, 0.0, 0.0); |
| 407 | for (i = 0; i < NUMBLOC; i++) { |
| 408 | glTranslatef(offset, 0.0, 0.0); |
| 409 | glBindTexture(GL_TEXTURE_2D, t1id); |
| 410 | drawobjs(striplength_skin_11, stripdata_skin_11); |
| 411 | glBindTexture(GL_TEXTURE_2D, t2id); |
| 412 | drawobjs(striplength_skin_12, stripdata_skin_12); |
| 413 | drawobjs(striplength_skin_9, stripdata_skin_9); |
| 414 | drawobjs(striplength_skin_13, stripdata_skin_13); |
| 415 | } |
| 416 | glPopMatrix(); |
| 417 | glPopMatrix(); |
| 418 | |
| 419 | if ((count % FRAME) == 0) { |
| 420 | fr = gettime(); |
| 421 | sprintf(frbuf, "Frame rate: %f", FRAME / fr); |
| 422 | } |
| 423 | |
| 424 | glDisable(GL_TEXTURE_2D); |
| 425 | glDisable(GL_FOG); |
| 426 | glShadeModel(GL_FLAT); |
| 427 | |
| 428 | glMatrixMode(GL_PROJECTION); |
| 429 | glPushMatrix(); |
| 430 | glLoadIdentity(); |
| 431 | glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0); |
| 432 | |
| 433 | glMatrixMode(GL_MODELVIEW); |
| 434 | glLoadIdentity(); |
| 435 | |
| 436 | glColor3f(1.0, 0.0, 0.0); |
| 437 | glRasterPos2i(10, 10); |
| 438 | printstring(GLUT_BITMAP_HELVETICA_18, frbuf); |
| 439 | glRasterPos2i(350, 470); |
| 440 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 441 | "Tunnel V1.5 Written by David Bucciarelli (tech.hmw@plus.it)"); |
| 442 | |
| 443 | if (help) |
| 444 | printhelp(); |
| 445 | |
| 446 | glMatrixMode(GL_PROJECTION); |
| 447 | glPopMatrix(); |
| 448 | glMatrixMode(GL_MODELVIEW); |
| 449 | |
| 450 | glutSwapBuffers(); |
| 451 | |
| 452 | count++; |
| 453 | } |
| 454 | |
| 455 | int |
| 456 | main(int ac, char **av) |
| 457 | { |
| 458 | float fogcolor[4] = { 0.7, 0.7, 0.7, 1.0 }; |
| 459 | |
| 460 | fprintf(stderr, |
| 461 | "Tunnel V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n"); |
| 462 | |
| 463 | glutInitWindowPosition(0, 0); |
| 464 | glutInitWindowSize(WIDTH, HEIGHT); |
| 465 | glutInit(&ac, av); |
| 466 | |
| 467 | glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); |
| 468 | |
| 469 | if (!(win = glutCreateWindow("Tunnel"))) { |
| 470 | fprintf(stderr, "Error, couldn't open window\n"); |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | glMatrixMode(GL_PROJECTION); |
| 475 | glLoadIdentity(); |
| 476 | gluPerspective(80.0, WIDTH / (float) HEIGHT, 1.0, 50.0); |
| 477 | |
| 478 | glMatrixMode(GL_MODELVIEW); |
| 479 | |
| 480 | glShadeModel(GL_SMOOTH); |
| 481 | glDisable(GL_DEPTH_TEST); |
| 482 | glEnable(GL_CULL_FACE); |
| 483 | glEnable(GL_TEXTURE_2D); |
| 484 | |
| 485 | glEnable(GL_FOG); |
| 486 | glFogi(GL_FOG_MODE, GL_EXP2); |
| 487 | glFogfv(GL_FOG_COLOR, fogcolor); |
| 488 | |
| 489 | glFogf(GL_FOG_DENSITY, 0.06); |
| 490 | glHint(GL_FOG_HINT, GL_NICEST); |
| 491 | |
| 492 | inittextures(); |
| 493 | |
| 494 | glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]); |
| 495 | glClear(GL_COLOR_BUFFER_BIT); |
| 496 | |
| 497 | calcposobs(); |
| 498 | |
| 499 | glutReshapeFunc(reshape); |
| 500 | glutDisplayFunc(draw); |
| 501 | glutKeyboardFunc(key); |
| 502 | glutSpecialFunc(special); |
| 503 | glutIdleFunc(draw); |
| 504 | |
| 505 | glEnable(GL_BLEND); |
| 506 | /*glBlendFunc(GL_SRC_ALPHA_SATURATE,GL_ONE); */ |
| 507 | /*glEnable(GL_POLYGON_SMOOTH); */ |
| 508 | |
| 509 | glutMainLoop(); |
| 510 | |
| 511 | return 0; |
| 512 | } |