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> |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame] | 14 | #include <string.h> |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
| 17 | #ifdef WIN32 |
| 18 | #include <windows.h> |
| 19 | #endif |
| 20 | |
| 21 | #include <GL/glut.h> |
| 22 | |
| 23 | #ifdef XMESA |
| 24 | #include "GL/xmesa.h" |
| 25 | static int fullscreen = 1; |
| 26 | #endif |
| 27 | |
| 28 | #ifndef M_PI |
| 29 | #define M_PI 3.14159265 |
| 30 | #endif |
| 31 | |
| 32 | #define heightMnt 450 |
| 33 | #define lenghtXmnt 62 |
| 34 | #define lenghtYmnt 62 |
| 35 | |
| 36 | #define stepXmnt 96.0 |
| 37 | #define stepYmnt 96.0 |
| 38 | |
| 39 | #define WIDTH 640 |
| 40 | #define HEIGHT 480 |
| 41 | |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 42 | static GLint T0 = 0; |
| 43 | static GLint Frames = 0; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 44 | |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 45 | #define TSCALE 4 |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 46 | |
| 47 | #define FOV 85 |
| 48 | |
| 49 | static GLfloat terrain[256 * 256]; |
| 50 | static GLfloat terraincolor[256 * 256][3]; |
| 51 | |
| 52 | static int win = 0; |
| 53 | |
| 54 | static int fog = 1; |
| 55 | static int bfcull = 1; |
| 56 | static int usetex = 1; |
| 57 | static int poutline = 0; |
| 58 | static int help = 1; |
| 59 | static int joyavailable = 0; |
| 60 | static int joyactive = 0; |
| 61 | static float ModZMnt; |
| 62 | static long GlobalMnt = 0; |
| 63 | |
| 64 | static int scrwidth = WIDTH; |
| 65 | static int scrheight = HEIGHT; |
| 66 | |
| 67 | #define OBSSTARTX 992.0 |
| 68 | #define OBSSTARTY 103.0 |
| 69 | |
| 70 | static float obs[3] = { OBSSTARTX, heightMnt * 1.3, OBSSTARTY }; |
| 71 | static float dir[3], v1[2], v2[2]; |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 72 | static float v = 900.0; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 73 | static float alpha = 75.0; |
| 74 | static float beta = 90.0; |
| 75 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 76 | static void |
| 77 | calcposobs(void) |
| 78 | { |
| 79 | float alpha1, alpha2; |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 80 | static double t0 = -1.; |
| 81 | double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 82 | if (t0 < 0.0) |
| 83 | t0 = t; |
| 84 | dt = t - t0; |
| 85 | t0 = t; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 86 | |
| 87 | dir[0] = sin(alpha * M_PI / 180.0); |
| 88 | dir[2] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0); |
| 89 | dir[1] = cos(beta * M_PI / 180.0); |
| 90 | |
Brian Paul | a41edc3 | 2001-05-09 20:02:28 +0000 | [diff] [blame] | 91 | if (dir[0] < 1.0e-5 && dir[0] > -1.0e-5) |
| 92 | dir[0] = 0; |
| 93 | if (dir[1] < 1.0e-5 && dir[1] > -1.0e-5) |
| 94 | dir[1] = 0; |
| 95 | if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5) |
| 96 | dir[2] = 0; |
| 97 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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 | |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 106 | obs[0] += v * dir[0] * dt; |
| 107 | obs[1] += v * dir[1] * dt; |
| 108 | obs[2] += v * dir[2] * dt; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 232 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Toggle Help"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 233 | glRasterPos2i(60, 360); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 234 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Toggle Textures"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 235 | glRasterPos2i(60, 330); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 236 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 237 | glRasterPos2i(60, 300); |
| 238 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "p - Wire frame"); |
| 239 | glRasterPos2i(60, 270); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 240 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "b - Toggle Back face culling"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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, |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 251 | "j - Toggle jostick control (Joystick control available)"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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 | { |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 363 | static char frbuf[80] = ""; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 364 | |
| 365 | dojoy(); |
| 366 | |
| 367 | glShadeModel(GL_SMOOTH); |
| 368 | glEnable(GL_DEPTH_TEST); |
| 369 | |
| 370 | if (usetex) |
| 371 | glEnable(GL_TEXTURE_2D); |
| 372 | else |
| 373 | glDisable(GL_TEXTURE_2D); |
| 374 | |
| 375 | if (fog) |
| 376 | glEnable(GL_FOG); |
| 377 | else |
| 378 | glDisable(GL_FOG); |
| 379 | |
| 380 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 381 | |
| 382 | glPushMatrix(); |
| 383 | |
| 384 | calcposobs(); |
| 385 | gluLookAt(obs[0], obs[1], obs[2], |
| 386 | obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2], |
| 387 | 0.0, 1.0, 0.0); |
| 388 | |
| 389 | drawterrain(); |
| 390 | glPopMatrix(); |
| 391 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 392 | glDisable(GL_TEXTURE_2D); |
| 393 | glDisable(GL_DEPTH_TEST); |
| 394 | glDisable(GL_FOG); |
| 395 | glShadeModel(GL_FLAT); |
| 396 | |
| 397 | glMatrixMode(GL_PROJECTION); |
| 398 | glLoadIdentity(); |
| 399 | glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0); |
| 400 | glMatrixMode(GL_MODELVIEW); |
| 401 | glLoadIdentity(); |
| 402 | |
| 403 | glColor3f(1.0, 0.0, 0.0); |
| 404 | glRasterPos2i(10, 10); |
| 405 | printstring(GLUT_BITMAP_HELVETICA_18, frbuf); |
| 406 | glRasterPos2i(350, 470); |
| 407 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 408 | "Terrain V1.2 Written by David Bucciarelli (tech.hmw@plus.it)"); |
| 409 | glRasterPos2i(434, 457); |
| 410 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 411 | "Based on a Mickael's demo (Skizo@Hol.Fr)"); |
| 412 | |
| 413 | if (help) |
| 414 | printhelp(); |
| 415 | |
| 416 | reshape(scrwidth, scrheight); |
| 417 | |
| 418 | glutSwapBuffers(); |
| 419 | |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 420 | Frames++; |
| 421 | { |
| 422 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 423 | if (t - T0 >= 2000) { |
| 424 | GLfloat seconds = (t - T0) / 1000.0; |
| 425 | GLfloat fps = Frames / seconds; |
| 426 | sprintf(frbuf, "Frame rate: %f", fps); |
| 427 | T0 = t; |
| 428 | Frames = 0; |
| 429 | } |
| 430 | } |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void |
| 434 | key(unsigned char k, int x, int y) |
| 435 | { |
| 436 | switch (k) { |
| 437 | case 27: |
| 438 | exit(0); |
| 439 | break; |
| 440 | case 'a': |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 441 | v += 50.; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 442 | break; |
| 443 | case 'z': |
Brian Paul | 92eddb0 | 2005-01-09 17:37:50 +0000 | [diff] [blame] | 444 | v -= 50.; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 445 | break; |
| 446 | case 'p': |
| 447 | if (poutline) { |
| 448 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 449 | poutline = 0; |
| 450 | } |
| 451 | else { |
| 452 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 453 | poutline = 1; |
| 454 | } |
| 455 | break; |
| 456 | case 'j': |
| 457 | joyactive = (!joyactive); |
| 458 | break; |
| 459 | case 'h': |
| 460 | help = (!help); |
| 461 | break; |
| 462 | case 'f': |
| 463 | fog = (!fog); |
| 464 | break; |
| 465 | case 't': |
| 466 | usetex = (!usetex); |
| 467 | break; |
| 468 | case 'b': |
| 469 | if (bfcull) { |
| 470 | glDisable(GL_CULL_FACE); |
| 471 | bfcull = 0; |
| 472 | } |
| 473 | else { |
| 474 | glEnable(GL_CULL_FACE); |
| 475 | bfcull = 1; |
| 476 | } |
| 477 | break; |
| 478 | #ifdef XMESA |
| 479 | case ' ': |
| 480 | XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW); |
| 481 | fullscreen = (!fullscreen); |
| 482 | break; |
| 483 | #endif |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | static void |
| 488 | special(int k, int x, int y) |
| 489 | { |
| 490 | switch (k) { |
| 491 | case GLUT_KEY_LEFT: |
| 492 | alpha += 2.0; |
| 493 | break; |
| 494 | case GLUT_KEY_RIGHT: |
| 495 | alpha -= 2.0; |
| 496 | break; |
| 497 | case GLUT_KEY_DOWN: |
| 498 | beta -= 2.0; |
| 499 | break; |
| 500 | case GLUT_KEY_UP: |
| 501 | beta += 2.0; |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | static void |
| 507 | calccolor(GLfloat height, GLfloat c[3]) |
| 508 | { |
| 509 | GLfloat color[4][3] = { |
| 510 | {1.0, 1.0, 1.0}, |
| 511 | {0.0, 0.8, 0.0}, |
| 512 | {1.0, 1.0, 0.3}, |
| 513 | {0.0, 0.0, 0.8} |
| 514 | }; |
| 515 | GLfloat fact; |
| 516 | |
| 517 | height = height * (1.0 / 255.0); |
| 518 | |
| 519 | if (height >= 0.9) { |
| 520 | c[0] = color[0][0]; |
| 521 | c[1] = color[0][1]; |
| 522 | c[2] = color[0][2]; |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | if ((height < 0.9) && (height >= 0.7)) { |
| 527 | fact = (height - 0.7) * 5.0; |
| 528 | c[0] = fact * color[0][0] + (1.0 - fact) * color[1][0]; |
| 529 | c[1] = fact * color[0][1] + (1.0 - fact) * color[1][1]; |
| 530 | c[2] = fact * color[0][2] + (1.0 - fact) * color[1][2]; |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | if ((height < 0.7) && (height >= 0.6)) { |
| 535 | fact = (height - 0.6) * 10.0; |
| 536 | c[0] = fact * color[1][0] + (1.0 - fact) * color[2][0]; |
| 537 | c[1] = fact * color[1][1] + (1.0 - fact) * color[2][1]; |
| 538 | c[2] = fact * color[1][2] + (1.0 - fact) * color[2][2]; |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | if ((height < 0.6) && (height >= 0.5)) { |
| 543 | fact = (height - 0.5) * 10.0; |
| 544 | c[0] = fact * color[2][0] + (1.0 - fact) * color[3][0]; |
| 545 | c[1] = fact * color[2][1] + (1.0 - fact) * color[3][1]; |
| 546 | c[2] = fact * color[2][2] + (1.0 - fact) * color[3][2]; |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | c[0] = color[3][0]; |
| 551 | c[1] = color[3][1]; |
| 552 | c[2] = color[3][2]; |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | loadpic(void) |
| 557 | { |
| 558 | GLubyte bufferter[256 * 256], terrainpic[256 * 256]; |
| 559 | FILE *FilePic; |
| 560 | int i, tmp; |
| 561 | GLenum gluerr; |
| 562 | |
| 563 | if ((FilePic = fopen("terrain.dat", "r")) == NULL) { |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 564 | fprintf(stderr, "Error loading terrain.dat\n"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 565 | exit(-1); |
| 566 | } |
| 567 | fread(bufferter, 256 * 256, 1, FilePic); |
| 568 | fclose(FilePic); |
| 569 | |
| 570 | for (i = 0; i < (256 * 256); i++) { |
| 571 | terrain[i] = (bufferter[i] * (heightMnt / 255.0f)); |
| 572 | calccolor((GLfloat) bufferter[i], terraincolor[i]); |
| 573 | tmp = (((int) bufferter[i]) + 96); |
| 574 | terrainpic[i] = (tmp > 255) ? 255 : tmp; |
| 575 | } |
| 576 | |
| 577 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 578 | if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 1, 256, 256, GL_LUMINANCE, |
| 579 | GL_UNSIGNED_BYTE, |
| 580 | (GLvoid *) (&terrainpic[0])))) { |
Brian Paul | 1972ac6 | 2002-08-08 17:12:38 +0000 | [diff] [blame] | 581 | fprintf(stderr, "GLULib%s\n", (char *) gluErrorString(gluerr)); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 582 | exit(-1); |
| 583 | } |
| 584 | |
| 585 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 586 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 587 | |
| 588 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 589 | GL_LINEAR_MIPMAP_LINEAR); |
| 590 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 591 | |
| 592 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 593 | glEnable(GL_TEXTURE_2D); |
| 594 | } |
| 595 | |
| 596 | static void |
| 597 | init(void) |
| 598 | { |
| 599 | float fogcolor[4] = { 0.6, 0.7, 0.7, 1.0 }; |
| 600 | |
| 601 | glClearColor(fogcolor[0], fogcolor[1], fogcolor[2], fogcolor[3]); |
| 602 | glClearDepth(1.0); |
| 603 | glDepthFunc(GL_LEQUAL); |
| 604 | glShadeModel(GL_SMOOTH); |
| 605 | glEnable(GL_DEPTH_TEST); |
| 606 | glEnable(GL_CULL_FACE); |
| 607 | |
| 608 | glDisable(GL_BLEND); |
| 609 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 610 | |
| 611 | glEnable(GL_FOG); |
| 612 | glFogi(GL_FOG_MODE, GL_EXP2); |
| 613 | glFogfv(GL_FOG_COLOR, fogcolor); |
| 614 | glFogf(GL_FOG_DENSITY, 0.0007); |
| 615 | #ifdef FX |
| 616 | glHint(GL_FOG_HINT, GL_NICEST); |
| 617 | #endif |
| 618 | |
| 619 | reshape(scrwidth, scrheight); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | int |
| 624 | main(int ac, char **av) |
| 625 | { |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 626 | glutInitWindowSize(WIDTH, HEIGHT); |
| 627 | glutInit(&ac, av); |
| 628 | |
| 629 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
| 630 | |
| 631 | if (!(win = glutCreateWindow("Terrain"))) { |
| 632 | fprintf(stderr, "Error, couldn't open window\n"); |
| 633 | return -1; |
| 634 | } |
| 635 | |
| 636 | ModZMnt = 0.0f; |
| 637 | loadpic(); |
| 638 | |
| 639 | init(); |
| 640 | |
| 641 | #ifndef FX |
| 642 | glDisable(GL_TEXTURE_2D); |
| 643 | usetex = 0; |
| 644 | #endif |
| 645 | |
| 646 | glutReshapeFunc(reshape); |
| 647 | glutDisplayFunc(drawscene); |
| 648 | glutKeyboardFunc(key); |
| 649 | glutSpecialFunc(special); |
| 650 | glutIdleFunc(drawscene); |
| 651 | |
| 652 | glutMainLoop(); |
| 653 | |
| 654 | return 0; |
| 655 | } |