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 | * based on a Mikael SkiZoWalker's (MoDEL) / France (Skizo@Hol.Fr) demo |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <math.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <time.h> |
| 15 | |
| 16 | #ifdef WIN32 |
| 17 | #include <windows.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <GL/glut.h> |
| 21 | |
| 22 | #ifdef XMESA |
| 23 | #include "GL/xmesa.h" |
| 24 | static int fullscreen = 1; |
| 25 | #endif |
| 26 | |
| 27 | #ifndef M_PI |
| 28 | #define M_PI 3.14159265 |
| 29 | #endif |
| 30 | |
| 31 | #define heightMnt 450 |
| 32 | #define lenghtXmnt 62 |
| 33 | #define lenghtYmnt 62 |
| 34 | |
| 35 | #define stepXmnt 96.0 |
| 36 | #define stepYmnt 96.0 |
| 37 | |
| 38 | #define WIDTH 640 |
| 39 | #define HEIGHT 480 |
| 40 | |
| 41 | #define TSCALE 4 |
| 42 | |
| 43 | #define FRAME 50 |
| 44 | |
| 45 | #define FOV 85 |
| 46 | |
| 47 | static GLfloat terrain[256 * 256]; |
| 48 | static GLfloat terraincolor[256 * 256][3]; |
| 49 | |
| 50 | static int win = 0; |
| 51 | |
| 52 | static int fog = 1; |
| 53 | static int bfcull = 1; |
| 54 | static int usetex = 1; |
| 55 | static int poutline = 0; |
| 56 | static int help = 1; |
| 57 | static int joyavailable = 0; |
| 58 | static int joyactive = 0; |
| 59 | static float ModZMnt; |
| 60 | static long GlobalMnt = 0; |
| 61 | |
| 62 | static int scrwidth = WIDTH; |
| 63 | static int scrheight = HEIGHT; |
| 64 | |
| 65 | #define OBSSTARTX 992.0 |
| 66 | #define OBSSTARTY 103.0 |
| 67 | |
| 68 | static float obs[3] = { OBSSTARTX, heightMnt * 1.3, OBSSTARTY }; |
| 69 | static float dir[3], v1[2], v2[2]; |
| 70 | static float v = 15.0; |
| 71 | static float alpha = 75.0; |
| 72 | static float beta = 90.0; |
| 73 | |
| 74 | static float |
| 75 | gettime(void) |
| 76 | { |
| 77 | static clock_t told = 0; |
| 78 | clock_t tnew, ris; |
| 79 | |
| 80 | tnew = clock(); |
| 81 | |
| 82 | ris = tnew - told; |
| 83 | |
| 84 | told = tnew; |
| 85 | |
| 86 | return (ris / (float) CLOCKS_PER_SEC); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | calcposobs(void) |
| 91 | { |
| 92 | float alpha1, alpha2; |
| 93 | |
| 94 | dir[0] = sin(alpha * M_PI / 180.0); |
| 95 | dir[2] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0); |
| 96 | dir[1] = cos(beta * M_PI / 180.0); |
| 97 | |
| 98 | alpha1 = alpha + FOV / 2.0; |
| 99 | v1[0] = sin(alpha1 * M_PI / 180.0); |
| 100 | v1[1] = cos(alpha1 * M_PI / 180.0); |
| 101 | |
| 102 | alpha2 = alpha - FOV / 2.0; |
| 103 | v2[0] = sin(alpha2 * M_PI / 180.0); |
| 104 | v2[1] = cos(alpha2 * M_PI / 180.0); |
| 105 | |
| 106 | obs[0] += v * dir[0]; |
| 107 | obs[1] += v * dir[1]; |
| 108 | obs[2] += v * dir[2]; |
| 109 | |
| 110 | if (obs[1] < 0.0) |
| 111 | obs[1] = 0.0; |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | reshape(int width, int height) |
| 116 | { |
| 117 | scrwidth = width; |
| 118 | scrheight = height; |
| 119 | glViewport(0, 0, (GLint) width, (GLint) height); |
| 120 | glMatrixMode(GL_PROJECTION); |
| 121 | glLoadIdentity(); |
| 122 | gluPerspective(50.0, ((GLfloat) width / (GLfloat) height), |
| 123 | lenghtXmnt * stepYmnt * 0.01, lenghtXmnt * stepYmnt * 0.7); |
| 124 | glMatrixMode(GL_MODELVIEW); |
| 125 | glLoadIdentity(); |
| 126 | } |
| 127 | |
Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame^] | 128 | static int |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 129 | clipstrip(float y, float *start, float *end) |
| 130 | { |
| 131 | float x1, x2, t1, t2, tmp; |
| 132 | |
| 133 | if (v1[1] == 0.0) { |
| 134 | t1 = 0.0; |
| 135 | x1 = -HUGE_VAL; |
| 136 | } |
| 137 | else { |
| 138 | t1 = y / v1[1]; |
| 139 | x1 = t1 * v1[0]; |
| 140 | } |
| 141 | |
| 142 | if (v2[1] == 0.0) { |
| 143 | t2 = 0.0; |
| 144 | x2 = HUGE_VAL; |
| 145 | } |
| 146 | else { |
| 147 | t2 = y / v2[1]; |
| 148 | x2 = t2 * v2[0]; |
| 149 | } |
| 150 | |
| 151 | if (((x1 < -(lenghtXmnt * stepXmnt) / 2) && (t2 <= 0.0)) || |
| 152 | ((t1 <= 0.0) && (x2 > (lenghtXmnt * stepXmnt) / 2)) || |
| 153 | ((t1 < 0.0) && (t2 < 0.0))) |
| 154 | return 0; |
| 155 | |
| 156 | if ((t1 == 0.0) && (t2 == 0.0)) { |
| 157 | if ((v1[0] < 0.0) && (v1[1] > 0.0) && (v2[0] < 0.0) && (v2[1] < 0.0)) { |
| 158 | *start = -(lenghtXmnt * stepXmnt) / 2; |
| 159 | *end = stepXmnt; |
| 160 | return 1; |
| 161 | } |
| 162 | else { |
| 163 | if ((v1[0] > 0.0) && (v1[1] < 0.0) && (v2[0] > 0.0) && (v2[1] > 0.0)) { |
| 164 | *start = -stepXmnt; |
| 165 | *end = (lenghtXmnt * stepXmnt) / 2; |
| 166 | return 1; |
| 167 | } |
| 168 | else |
| 169 | return 0; |
| 170 | } |
| 171 | } |
| 172 | else { |
| 173 | if (t2 < 0.0) { |
| 174 | if (x1 < 0.0) |
| 175 | x2 = -(lenghtXmnt * stepXmnt) / 2; |
| 176 | else |
| 177 | x2 = (lenghtXmnt * stepXmnt) / 2; |
| 178 | } |
| 179 | |
| 180 | if (t1 < 0.0) { |
| 181 | if (x2 < 0.0) |
| 182 | x1 = -(lenghtXmnt * stepXmnt) / 2; |
| 183 | else |
| 184 | x1 = (lenghtXmnt * stepXmnt) / 2; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (x1 > x2) { |
| 189 | tmp = x1; |
| 190 | x1 = x2; |
| 191 | x2 = tmp; |
| 192 | } |
| 193 | |
| 194 | x1 -= stepXmnt; |
| 195 | if (x1 < -(lenghtXmnt * stepXmnt) / 2) |
| 196 | x1 = -(lenghtXmnt * stepXmnt) / 2; |
| 197 | |
| 198 | x2 += stepXmnt; |
| 199 | if (x2 > (lenghtXmnt * stepXmnt) / 2) |
| 200 | x2 = (lenghtXmnt * stepXmnt) / 2; |
| 201 | |
| 202 | *start = ((int) (x1 / stepXmnt)) * stepXmnt; |
| 203 | *end = ((int) (x2 / stepXmnt)) * stepXmnt; |
| 204 | |
| 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | printstring(void *font, char *string) |
| 210 | { |
| 211 | int len, i; |
| 212 | |
| 213 | len = (int) strlen(string); |
| 214 | for (i = 0; i < len; i++) |
| 215 | glutBitmapCharacter(font, string[i]); |
| 216 | } |
| 217 | |
| 218 | static void |
| 219 | printhelp(void) |
| 220 | { |
| 221 | glEnable(GL_BLEND); |
| 222 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 223 | glColor4f(0.0, 0.0, 0.0, 0.5); |
| 224 | glRecti(40, 40, 600, 440); |
| 225 | glDisable(GL_BLEND); |
| 226 | |
| 227 | glColor3f(1.0, 0.0, 0.0); |
| 228 | glRasterPos2i(300, 420); |
| 229 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help"); |
| 230 | |
| 231 | glRasterPos2i(60, 390); |
| 232 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Togle Help"); |
| 233 | glRasterPos2i(60, 360); |
| 234 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Togle Textures"); |
| 235 | glRasterPos2i(60, 330); |
| 236 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Togle Fog"); |
| 237 | glRasterPos2i(60, 300); |
| 238 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "p - Wire frame"); |
| 239 | glRasterPos2i(60, 270); |
| 240 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Togle Back face culling"); |
| 241 | glRasterPos2i(60, 240); |
| 242 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate"); |
| 243 | glRasterPos2i(60, 210); |
| 244 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity"); |
| 245 | glRasterPos2i(60, 180); |
| 246 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity"); |
| 247 | |
| 248 | glRasterPos2i(60, 150); |
| 249 | if (joyavailable) |
| 250 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
| 251 | "j - Togle jostick control (Joystick control available)"); |
| 252 | else |
| 253 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
| 254 | "(No Joystick control available)"); |
| 255 | } |
| 256 | |
Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame^] | 257 | static void |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 258 | drawterrain(void) |
| 259 | { |
| 260 | int h, i, idx, ox, oy; |
| 261 | float j, k, start, end; |
| 262 | |
| 263 | ox = (int) (obs[0] / stepXmnt); |
| 264 | oy = (int) (obs[2] / stepYmnt); |
| 265 | GlobalMnt = ((ox * TSCALE) & 255) + ((oy * TSCALE) & 255) * 256; |
| 266 | |
| 267 | glPushMatrix(); |
| 268 | glTranslatef((float) ox * stepXmnt, 0, (float) oy * stepYmnt); |
| 269 | |
| 270 | for (h = 0, k = -(lenghtYmnt * stepYmnt) / 2; h < lenghtYmnt; |
| 271 | k += stepYmnt, h++) { |
| 272 | if (!clipstrip(k, &start, &end)) |
| 273 | continue; |
| 274 | |
| 275 | glBegin(GL_TRIANGLE_STRIP); /* I hope that the optimizer will be able to improve this code */ |
| 276 | for (i = (int) (lenghtXmnt / 2 + start / stepXmnt), j = start; j <= end; |
| 277 | j += stepXmnt, i++) { |
| 278 | idx = (i * TSCALE + h * 256 * TSCALE + GlobalMnt) & 65535; |
| 279 | glColor3fv(terraincolor[idx]); |
| 280 | glTexCoord2f((ox + i) / 8.0, (oy + h) / 8.0); |
| 281 | glVertex3f(j, terrain[idx], k); |
| 282 | |
| 283 | idx = |
| 284 | (i * TSCALE + h * 256 * TSCALE + 256 * TSCALE + |
| 285 | GlobalMnt) & 65535; |
| 286 | glColor3fv(terraincolor[idx]); |
| 287 | glTexCoord2f((ox + i) / 8.0, (oy + h + 1) / 8.0); |
| 288 | glVertex3f(j, terrain[idx], k + stepYmnt); |
| 289 | } |
| 290 | glEnd(); |
| 291 | } |
| 292 | |
| 293 | glDisable(GL_CULL_FACE); |
| 294 | glDisable(GL_TEXTURE_2D); |
| 295 | glEnable(GL_BLEND); |
| 296 | glBegin(GL_QUADS); |
| 297 | glColor4f(0.1, 0.7, 1.0, 0.4); |
| 298 | glVertex3f(-(lenghtXmnt * stepXmnt) / 2.0, heightMnt * 0.6, |
| 299 | -(lenghtYmnt * stepYmnt) / 2.0); |
| 300 | glVertex3f(-(lenghtXmnt * stepXmnt) / 2.0, heightMnt * 0.6, |
| 301 | (lenghtYmnt * stepYmnt) / 2.0); |
| 302 | glVertex3f((lenghtXmnt * stepXmnt) / 2.0, heightMnt * 0.6, |
| 303 | (lenghtYmnt * stepYmnt) / 2.0); |
| 304 | glVertex3f((lenghtXmnt * stepXmnt) / 2.0, heightMnt * 0.6, |
| 305 | -(lenghtYmnt * stepYmnt) / 2.0); |
| 306 | glEnd(); |
| 307 | glDisable(GL_BLEND); |
| 308 | if (bfcull) |
| 309 | glEnable(GL_CULL_FACE); |
| 310 | glEnable(GL_TEXTURE_2D); |
| 311 | |
| 312 | glPopMatrix(); |
| 313 | |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | dojoy(void) |
| 318 | { |
| 319 | #ifdef WIN32 |
| 320 | static UINT max[2] = { 0, 0 }; |
| 321 | static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2]; |
| 322 | MMRESULT res; |
| 323 | JOYINFO joy; |
| 324 | |
| 325 | res = joyGetPos(JOYSTICKID1, &joy); |
| 326 | |
| 327 | if (res == JOYERR_NOERROR) { |
| 328 | joyavailable = 1; |
| 329 | |
| 330 | if (max[0] < joy.wXpos) |
| 331 | max[0] = joy.wXpos; |
| 332 | if (min[0] > joy.wXpos) |
| 333 | min[0] = joy.wXpos; |
| 334 | center[0] = (max[0] + min[0]) / 2; |
| 335 | |
| 336 | if (max[1] < joy.wYpos) |
| 337 | max[1] = joy.wYpos; |
| 338 | if (min[1] > joy.wYpos) |
| 339 | min[1] = joy.wYpos; |
| 340 | center[1] = (max[1] + min[1]) / 2; |
| 341 | |
| 342 | if (joyactive) { |
| 343 | if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0])) |
| 344 | alpha += |
| 345 | 2.5 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]); |
| 346 | if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1])) |
| 347 | beta += 2.5 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]); |
| 348 | |
| 349 | if (joy.wButtons & JOY_BUTTON1) |
| 350 | v += 0.5; |
| 351 | if (joy.wButtons & JOY_BUTTON2) |
| 352 | v -= 0.5; |
| 353 | } |
| 354 | } |
| 355 | else |
| 356 | joyavailable = 0; |
| 357 | #endif |
| 358 | } |
| 359 | |
Brian Paul | 02e8a03 | 2000-06-27 17:04:43 +0000 | [diff] [blame^] | 360 | static void |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 361 | drawscene(void) |
| 362 | { |
| 363 | static int count = 0; |
| 364 | static char frbuf[80]; |
| 365 | float fr; |
| 366 | |
| 367 | dojoy(); |
| 368 | |
| 369 | glShadeModel(GL_SMOOTH); |
| 370 | glEnable(GL_DEPTH_TEST); |
| 371 | |
| 372 | if (usetex) |
| 373 | glEnable(GL_TEXTURE_2D); |
| 374 | else |
| 375 | glDisable(GL_TEXTURE_2D); |
| 376 | |
| 377 | if (fog) |
| 378 | glEnable(GL_FOG); |
| 379 | else |
| 380 | glDisable(GL_FOG); |
| 381 | |
| 382 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 383 | |
| 384 | glPushMatrix(); |
| 385 | |
| 386 | calcposobs(); |
| 387 | gluLookAt(obs[0], obs[1], obs[2], |
| 388 | obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2], |
| 389 | 0.0, 1.0, 0.0); |
| 390 | |
| 391 | drawterrain(); |
| 392 | glPopMatrix(); |
| 393 | |
| 394 | if ((count % FRAME) == 0) { |
| 395 | fr = gettime(); |
| 396 | sprintf(frbuf, "Frame rate: %.3f", FRAME / fr); |
| 397 | } |
| 398 | |
| 399 | glDisable(GL_TEXTURE_2D); |
| 400 | glDisable(GL_DEPTH_TEST); |
| 401 | glDisable(GL_FOG); |
| 402 | glShadeModel(GL_FLAT); |
| 403 | |
| 404 | glMatrixMode(GL_PROJECTION); |
| 405 | glLoadIdentity(); |
| 406 | glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0); |
| 407 | glMatrixMode(GL_MODELVIEW); |
| 408 | glLoadIdentity(); |
| 409 | |
| 410 | glColor3f(1.0, 0.0, 0.0); |
| 411 | glRasterPos2i(10, 10); |
| 412 | printstring(GLUT_BITMAP_HELVETICA_18, frbuf); |
| 413 | glRasterPos2i(350, 470); |
| 414 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 415 | "Terrain V1.2 Written by David Bucciarelli (tech.hmw@plus.it)"); |
| 416 | glRasterPos2i(434, 457); |
| 417 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 418 | "Based on a Mickael's demo (Skizo@Hol.Fr)"); |
| 419 | |
| 420 | if (help) |
| 421 | printhelp(); |
| 422 | |
| 423 | reshape(scrwidth, scrheight); |
| 424 | |
| 425 | glutSwapBuffers(); |
| 426 | |
| 427 | count++; |
| 428 | } |
| 429 | |
| 430 | static void |
| 431 | key(unsigned char k, int x, int y) |
| 432 | { |
| 433 | switch (k) { |
| 434 | case 27: |
| 435 | exit(0); |
| 436 | break; |
| 437 | case 'a': |
| 438 | v += 0.5; |
| 439 | break; |
| 440 | case 'z': |
| 441 | v -= 0.5; |
| 442 | break; |
| 443 | case 'p': |
| 444 | if (poutline) { |
| 445 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 446 | poutline = 0; |
| 447 | } |
| 448 | else { |
| 449 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 450 | poutline = 1; |
| 451 | } |
| 452 | break; |
| 453 | case 'j': |
| 454 | joyactive = (!joyactive); |
| 455 | break; |
| 456 | case 'h': |
| 457 | help = (!help); |
| 458 | break; |
| 459 | case 'f': |
| 460 | fog = (!fog); |
| 461 | break; |
| 462 | case 't': |
| 463 | usetex = (!usetex); |
| 464 | break; |
| 465 | case 'b': |
| 466 | if (bfcull) { |
| 467 | glDisable(GL_CULL_FACE); |
| 468 | bfcull = 0; |
| 469 | } |
| 470 | else { |
| 471 | glEnable(GL_CULL_FACE); |
| 472 | bfcull = 1; |
| 473 | } |
| 474 | break; |
| 475 | #ifdef XMESA |
| 476 | case ' ': |
| 477 | XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW); |
| 478 | fullscreen = (!fullscreen); |
| 479 | break; |
| 480 | #endif |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void |
| 485 | special(int k, int x, int y) |
| 486 | { |
| 487 | switch (k) { |
| 488 | case GLUT_KEY_LEFT: |
| 489 | alpha += 2.0; |
| 490 | break; |
| 491 | case GLUT_KEY_RIGHT: |
| 492 | alpha -= 2.0; |
| 493 | break; |
| 494 | case GLUT_KEY_DOWN: |
| 495 | beta -= 2.0; |
| 496 | break; |
| 497 | case GLUT_KEY_UP: |
| 498 | beta += 2.0; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | static void |
| 504 | calccolor(GLfloat height, GLfloat c[3]) |
| 505 | { |
| 506 | GLfloat color[4][3] = { |
| 507 | {1.0, 1.0, 1.0}, |
| 508 | {0.0, 0.8, 0.0}, |
| 509 | {1.0, 1.0, 0.3}, |
| 510 | {0.0, 0.0, 0.8} |
| 511 | }; |
| 512 | GLfloat fact; |
| 513 | |
| 514 | height = height * (1.0 / 255.0); |
| 515 | |
| 516 | if (height >= 0.9) { |
| 517 | c[0] = color[0][0]; |
| 518 | c[1] = color[0][1]; |
| 519 | c[2] = color[0][2]; |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | if ((height < 0.9) && (height >= 0.7)) { |
| 524 | fact = (height - 0.7) * 5.0; |
| 525 | c[0] = fact * color[0][0] + (1.0 - fact) * color[1][0]; |
| 526 | c[1] = fact * color[0][1] + (1.0 - fact) * color[1][1]; |
| 527 | c[2] = fact * color[0][2] + (1.0 - fact) * color[1][2]; |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | if ((height < 0.7) && (height >= 0.6)) { |
| 532 | fact = (height - 0.6) * 10.0; |
| 533 | c[0] = fact * color[1][0] + (1.0 - fact) * color[2][0]; |
| 534 | c[1] = fact * color[1][1] + (1.0 - fact) * color[2][1]; |
| 535 | c[2] = fact * color[1][2] + (1.0 - fact) * color[2][2]; |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | if ((height < 0.6) && (height >= 0.5)) { |
| 540 | fact = (height - 0.5) * 10.0; |
| 541 | c[0] = fact * color[2][0] + (1.0 - fact) * color[3][0]; |
| 542 | c[1] = fact * color[2][1] + (1.0 - fact) * color[3][1]; |
| 543 | c[2] = fact * color[2][2] + (1.0 - fact) * color[3][2]; |
| 544 | return; |
| 545 | } |
| 546 | |
| 547 | c[0] = color[3][0]; |
| 548 | c[1] = color[3][1]; |
| 549 | c[2] = color[3][2]; |
| 550 | } |
| 551 | |
| 552 | static void |
| 553 | loadpic(void) |
| 554 | { |
| 555 | GLubyte bufferter[256 * 256], terrainpic[256 * 256]; |
| 556 | FILE *FilePic; |
| 557 | int i, tmp; |
| 558 | GLenum gluerr; |
| 559 | |
| 560 | if ((FilePic = fopen("terrain.dat", "r")) == NULL) { |
| 561 | fprintf(stderr, "Error loading Mnt.bin\n"); |
| 562 | exit(-1); |
| 563 | } |
| 564 | fread(bufferter, 256 * 256, 1, FilePic); |
| 565 | fclose(FilePic); |
| 566 | |
| 567 | for (i = 0; i < (256 * 256); i++) { |
| 568 | terrain[i] = (bufferter[i] * (heightMnt / 255.0f)); |
| 569 | calccolor((GLfloat) bufferter[i], terraincolor[i]); |
| 570 | tmp = (((int) bufferter[i]) + 96); |
| 571 | terrainpic[i] = (tmp > 255) ? 255 : tmp; |
| 572 | } |
| 573 | |
| 574 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 575 | if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 256, 256, GL_LUMINANCE, |
| 576 | GL_UNSIGNED_BYTE, |
| 577 | (GLvoid *) (&terrainpic[0])))) { |
| 578 | fprintf(stderr, "GLULib%s\n", gluErrorString(gluerr)); |
| 579 | exit(-1); |
| 580 | } |
| 581 | |
| 582 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 583 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 584 | |
| 585 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 586 | GL_LINEAR_MIPMAP_LINEAR); |
| 587 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 588 | |
| 589 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 590 | glEnable(GL_TEXTURE_2D); |
| 591 | } |
| 592 | |
| 593 | static void |
| 594 | init(void) |
| 595 | { |
| 596 | float fogcolor[4] = { 0.6, 0.7, 0.7, 1.0 }; |
| 597 | |
| 598 | glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]); |
| 599 | glClearDepth(1.0); |
| 600 | glDepthFunc(GL_LEQUAL); |
| 601 | glShadeModel(GL_SMOOTH); |
| 602 | glEnable(GL_DEPTH_TEST); |
| 603 | glEnable(GL_CULL_FACE); |
| 604 | |
| 605 | glDisable(GL_BLEND); |
| 606 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 607 | |
| 608 | glEnable(GL_FOG); |
| 609 | glFogi(GL_FOG_MODE, GL_EXP2); |
| 610 | glFogfv(GL_FOG_COLOR, fogcolor); |
| 611 | glFogf(GL_FOG_DENSITY, 0.0007); |
| 612 | #ifdef FX |
| 613 | glHint(GL_FOG_HINT, GL_NICEST); |
| 614 | #endif |
| 615 | |
| 616 | reshape(scrwidth, scrheight); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | int |
| 621 | main(int ac, char **av) |
| 622 | { |
| 623 | glutInitWindowPosition(0, 0); |
| 624 | glutInitWindowSize(WIDTH, HEIGHT); |
| 625 | glutInit(&ac, av); |
| 626 | |
| 627 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
| 628 | |
| 629 | if (!(win = glutCreateWindow("Terrain"))) { |
| 630 | fprintf(stderr, "Error, couldn't open window\n"); |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | ModZMnt = 0.0f; |
| 635 | loadpic(); |
| 636 | |
| 637 | init(); |
| 638 | |
| 639 | #ifndef FX |
| 640 | glDisable(GL_TEXTURE_2D); |
| 641 | usetex = 0; |
| 642 | #endif |
| 643 | |
| 644 | glutReshapeFunc(reshape); |
| 645 | glutDisplayFunc(drawscene); |
| 646 | glutKeyboardFunc(key); |
| 647 | glutSpecialFunc(special); |
| 648 | glutIdleFunc(drawscene); |
| 649 | |
| 650 | glutMainLoop(); |
| 651 | |
| 652 | return 0; |
| 653 | } |