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> |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 13 | #include <string.h> |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 14 | |
| 15 | #ifdef WIN32 |
| 16 | #include <windows.h> |
| 17 | #include <mmsystem.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <GL/glut.h> |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 21 | #include "readtex.h" |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 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.1415926535 |
| 30 | #endif |
| 31 | |
| 32 | #define vinit(a,i,j,k) {\ |
| 33 | (a)[0]=i;\ |
| 34 | (a)[1]=j;\ |
| 35 | (a)[2]=k;\ |
| 36 | } |
| 37 | |
| 38 | #define vinit4(a,i,j,k,w) {\ |
| 39 | (a)[0]=i;\ |
| 40 | (a)[1]=j;\ |
| 41 | (a)[2]=k;\ |
| 42 | (a)[3]=w;\ |
| 43 | } |
| 44 | |
| 45 | |
| 46 | #define vadds(a,dt,b) {\ |
| 47 | (a)[0]+=(dt)*(b)[0];\ |
| 48 | (a)[1]+=(dt)*(b)[1];\ |
| 49 | (a)[2]+=(dt)*(b)[2];\ |
| 50 | } |
| 51 | |
| 52 | #define vequ(a,b) {\ |
| 53 | (a)[0]=(b)[0];\ |
| 54 | (a)[1]=(b)[1];\ |
| 55 | (a)[2]=(b)[2];\ |
| 56 | } |
| 57 | |
| 58 | #define vinter(a,dt,b,c) {\ |
| 59 | (a)[0]=(dt)*(b)[0]+(1.0-dt)*(c)[0];\ |
| 60 | (a)[1]=(dt)*(b)[1]+(1.0-dt)*(c)[1];\ |
| 61 | (a)[2]=(dt)*(b)[2]+(1.0-dt)*(c)[2];\ |
| 62 | } |
| 63 | |
| 64 | #define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0)) |
| 65 | |
| 66 | #define vclamp(v) {\ |
| 67 | (v)[0]=clamp((v)[0]);\ |
| 68 | (v)[1]=clamp((v)[1]);\ |
| 69 | (v)[2]=clamp((v)[2]);\ |
| 70 | } |
| 71 | |
| 72 | static int WIDTH = 640; |
| 73 | static int HEIGHT = 480; |
| 74 | |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 75 | static GLint T0 = 0; |
| 76 | static GLint Frames = 0; |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 77 | static GLint NiceFog = 1; |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 78 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 79 | #define DIMP 20.0 |
| 80 | #define DIMTP 16.0 |
| 81 | |
| 82 | #define RIDCOL 0.4 |
| 83 | |
| 84 | #define NUMTREE 50 |
| 85 | #define TREEINR 2.5 |
| 86 | #define TREEOUTR 8.0 |
| 87 | |
| 88 | #define AGRAV -9.8 |
| 89 | |
| 90 | typedef struct |
| 91 | { |
| 92 | int age; |
| 93 | float p[3][3]; |
| 94 | float v[3]; |
| 95 | float c[3][4]; |
| 96 | } |
| 97 | part; |
| 98 | |
| 99 | static float treepos[NUMTREE][3]; |
| 100 | |
| 101 | static float black[3] = { 0.0, 0.0, 0.0 }; |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 102 | static float blu[3] = { 1.0, 0.2, 0.0 }; |
| 103 | static float blu2[3] = { 1.0, 1.0, 0.0 }; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 104 | |
| 105 | static float fogcolor[4] = { 1.0, 1.0, 1.0, 1.0 }; |
| 106 | |
| 107 | static float q[4][3] = { |
| 108 | {-DIMP, 0.0, -DIMP}, |
| 109 | {DIMP, 0.0, -DIMP}, |
| 110 | {DIMP, 0.0, DIMP}, |
| 111 | {-DIMP, 0.0, DIMP} |
| 112 | }; |
| 113 | |
| 114 | static float qt[4][2] = { |
| 115 | {-DIMTP, -DIMTP}, |
| 116 | {DIMTP, -DIMTP}, |
| 117 | {DIMTP, DIMTP}, |
| 118 | {-DIMTP, DIMTP} |
| 119 | }; |
| 120 | |
| 121 | static int win = 0; |
| 122 | |
| 123 | static int np; |
| 124 | static float eject_r, dt, maxage, eject_vy, eject_vl; |
| 125 | static short shadows; |
| 126 | static float ridtri; |
| 127 | static int fog = 1; |
| 128 | static int help = 1; |
| 129 | static int joyavailable = 0; |
| 130 | static int joyactive = 0; |
| 131 | |
| 132 | static part *p; |
| 133 | |
| 134 | static GLuint groundid; |
| 135 | static GLuint treeid; |
| 136 | |
| 137 | static float obs[3] = { 2.0, 1.0, 0.0 }; |
| 138 | static float dir[3]; |
| 139 | static float v = 0.0; |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 140 | static float alpha = -84.0; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 141 | static float beta = 90.0; |
| 142 | |
| 143 | static float |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 144 | vrnd(void) |
| 145 | { |
| 146 | return (((float) rand()) / RAND_MAX); |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | setnewpart(part * p) |
| 151 | { |
| 152 | float a, v[3], *c; |
| 153 | |
| 154 | p->age = 0; |
| 155 | |
| 156 | a = vrnd() * 3.14159265359 * 2.0; |
| 157 | |
| 158 | vinit(v, sin(a) * eject_r * vrnd(), 0.15, cos(a) * eject_r * vrnd()); |
| 159 | vinit(p->p[0], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, |
| 160 | v[2] + vrnd() * ridtri); |
| 161 | vinit(p->p[1], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, |
| 162 | v[2] + vrnd() * ridtri); |
| 163 | vinit(p->p[2], v[0] + vrnd() * ridtri, v[1] + vrnd() * ridtri, |
| 164 | v[2] + vrnd() * ridtri); |
| 165 | |
| 166 | vinit(p->v, v[0] * eject_vl / (eject_r / 2), |
| 167 | vrnd() * eject_vy + eject_vy / 2, v[2] * eject_vl / (eject_r / 2)); |
| 168 | |
| 169 | c = blu; |
| 170 | |
| 171 | vinit4(p->c[0], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 172 | c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 173 | c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0); |
| 174 | vinit4(p->c[1], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 175 | c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 176 | c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0); |
| 177 | vinit4(p->c[2], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 178 | c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), |
| 179 | c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0); |
| 180 | } |
| 181 | |
| 182 | static void |
| 183 | setpart(part * p) |
| 184 | { |
| 185 | float fact; |
| 186 | |
| 187 | if (p->p[0][1] < 0.1) { |
| 188 | setnewpart(p); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | p->v[1] += AGRAV * dt; |
| 193 | |
| 194 | vadds(p->p[0], dt, p->v); |
| 195 | vadds(p->p[1], dt, p->v); |
| 196 | vadds(p->p[2], dt, p->v); |
| 197 | |
| 198 | p->age++; |
| 199 | |
| 200 | if ((p->age) > maxage) { |
| 201 | vequ(p->c[0], blu2); |
| 202 | vequ(p->c[1], blu2); |
| 203 | vequ(p->c[2], blu2); |
| 204 | } |
| 205 | else { |
| 206 | fact = 1.0 / maxage; |
| 207 | vadds(p->c[0], fact, blu2); |
| 208 | vclamp(p->c[0]); |
| 209 | p->c[0][3] = fact * (maxage - p->age); |
| 210 | |
| 211 | vadds(p->c[1], fact, blu2); |
| 212 | vclamp(p->c[1]); |
| 213 | p->c[1][3] = fact * (maxage - p->age); |
| 214 | |
| 215 | vadds(p->c[2], fact, blu2); |
| 216 | vclamp(p->c[2]); |
| 217 | p->c[2][3] = fact * (maxage - p->age); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | drawtree(float x, float y, float z) |
| 223 | { |
| 224 | glBegin(GL_QUADS); |
| 225 | glTexCoord2f(0.0, 0.0); |
| 226 | glVertex3f(x - 1.5, y + 0.0, z); |
| 227 | |
| 228 | glTexCoord2f(1.0, 0.0); |
| 229 | glVertex3f(x + 1.5, y + 0.0, z); |
| 230 | |
| 231 | glTexCoord2f(1.0, 1.0); |
| 232 | glVertex3f(x + 1.5, y + 3.0, z); |
| 233 | |
| 234 | glTexCoord2f(0.0, 1.0); |
| 235 | glVertex3f(x - 1.5, y + 3.0, z); |
| 236 | |
| 237 | |
| 238 | glTexCoord2f(0.0, 0.0); |
| 239 | glVertex3f(x, y + 0.0, z - 1.5); |
| 240 | |
| 241 | glTexCoord2f(1.0, 0.0); |
| 242 | glVertex3f(x, y + 0.0, z + 1.5); |
| 243 | |
| 244 | glTexCoord2f(1.0, 1.0); |
| 245 | glVertex3f(x, y + 3.0, z + 1.5); |
| 246 | |
| 247 | glTexCoord2f(0.0, 1.0); |
| 248 | glVertex3f(x, y + 3.0, z - 1.5); |
| 249 | |
| 250 | glEnd(); |
| 251 | |
| 252 | } |
| 253 | |
| 254 | static void |
| 255 | calcposobs(void) |
| 256 | { |
| 257 | dir[0] = sin(alpha * M_PI / 180.0); |
| 258 | dir[2] = cos(alpha * M_PI / 180.0) * sin(beta * M_PI / 180.0); |
| 259 | dir[1] = cos(beta * M_PI / 180.0); |
| 260 | |
Brian Paul | a41edc3 | 2001-05-09 20:02:28 +0000 | [diff] [blame] | 261 | if (dir[0] < 1.0e-5 && dir[0] > -1.0e-5) |
| 262 | dir[0] = 0; |
| 263 | if (dir[1] < 1.0e-5 && dir[1] > -1.0e-5) |
| 264 | dir[1] = 0; |
| 265 | if (dir[2] < 1.0e-5 && dir[2] > -1.0e-5) |
| 266 | dir[2] = 0; |
| 267 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 268 | obs[0] += v * dir[0]; |
| 269 | obs[1] += v * dir[1]; |
| 270 | obs[2] += v * dir[2]; |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | printstring(void *font, char *string) |
| 275 | { |
| 276 | int len, i; |
| 277 | |
| 278 | len = (int) strlen(string); |
| 279 | for (i = 0; i < len; i++) |
| 280 | glutBitmapCharacter(font, string[i]); |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | reshape(int width, int height) |
| 285 | { |
| 286 | WIDTH = width; |
| 287 | HEIGHT = height; |
| 288 | glViewport(0, 0, (GLint) width, (GLint) height); |
| 289 | glMatrixMode(GL_PROJECTION); |
| 290 | glLoadIdentity(); |
| 291 | gluPerspective(70.0, width / (float) height, 0.1, 30.0); |
| 292 | |
| 293 | glMatrixMode(GL_MODELVIEW); |
| 294 | } |
| 295 | |
| 296 | static void |
| 297 | printhelp(void) |
| 298 | { |
| 299 | glColor4f(0.0, 0.0, 0.0, 0.5); |
| 300 | glRecti(40, 40, 600, 440); |
| 301 | |
| 302 | glColor3f(1.0, 0.0, 0.0); |
| 303 | glRasterPos2i(300, 420); |
| 304 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Help"); |
| 305 | |
| 306 | glRasterPos2i(60, 390); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 307 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "h - Toggle Help"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 308 | |
| 309 | glRasterPos2i(60, 360); |
| 310 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "t - Increase particle size"); |
| 311 | glRasterPos2i(60, 330); |
| 312 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "T - Decrease particle size"); |
| 313 | |
| 314 | glRasterPos2i(60, 300); |
| 315 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "r - Increase emission radius"); |
| 316 | glRasterPos2i(60, 270); |
| 317 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "R - Decrease emission radius"); |
| 318 | |
| 319 | glRasterPos2i(60, 240); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 320 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "f - Toggle Fog"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 321 | glRasterPos2i(60, 210); |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 322 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "s - Toggle shadows"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 323 | glRasterPos2i(60, 180); |
| 324 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "Arrow Keys - Rotate"); |
| 325 | glRasterPos2i(60, 150); |
| 326 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "a - Increase velocity"); |
| 327 | glRasterPos2i(60, 120); |
| 328 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, "z - Decrease velocity"); |
| 329 | |
| 330 | glRasterPos2i(60, 90); |
| 331 | if (joyavailable) |
| 332 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
Brian Paul | c39a4bc | 2005-04-04 20:06:40 +0000 | [diff] [blame] | 333 | "j - Toggle jostick control (Joystick control available)"); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 334 | else |
| 335 | printstring(GLUT_BITMAP_TIMES_ROMAN_24, |
| 336 | "(No Joystick control available)"); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | dojoy(void) |
| 341 | { |
| 342 | #ifdef WIN32 |
| 343 | static UINT max[2] = { 0, 0 }; |
| 344 | static UINT min[2] = { 0xffffffff, 0xffffffff }, center[2]; |
| 345 | MMRESULT res; |
| 346 | JOYINFO joy; |
| 347 | |
| 348 | res = joyGetPos(JOYSTICKID1, &joy); |
| 349 | |
| 350 | if (res == JOYERR_NOERROR) { |
| 351 | joyavailable = 1; |
| 352 | |
| 353 | if (max[0] < joy.wXpos) |
| 354 | max[0] = joy.wXpos; |
| 355 | if (min[0] > joy.wXpos) |
| 356 | min[0] = joy.wXpos; |
| 357 | center[0] = (max[0] + min[0]) / 2; |
| 358 | |
| 359 | if (max[1] < joy.wYpos) |
| 360 | max[1] = joy.wYpos; |
| 361 | if (min[1] > joy.wYpos) |
| 362 | min[1] = joy.wYpos; |
| 363 | center[1] = (max[1] + min[1]) / 2; |
| 364 | |
| 365 | if (joyactive) { |
| 366 | if (fabs(center[0] - (float) joy.wXpos) > 0.1 * (max[0] - min[0])) |
| 367 | alpha += |
| 368 | 2.5 * (center[0] - (float) joy.wXpos) / (max[0] - min[0]); |
| 369 | if (fabs(center[1] - (float) joy.wYpos) > 0.1 * (max[1] - min[1])) |
| 370 | beta += 2.5 * (center[1] - (float) joy.wYpos) / (max[1] - min[1]); |
| 371 | |
| 372 | if (joy.wButtons & JOY_BUTTON1) |
| 373 | v += 0.01; |
| 374 | if (joy.wButtons & JOY_BUTTON2) |
| 375 | v -= 0.01; |
| 376 | } |
| 377 | } |
| 378 | else |
| 379 | joyavailable = 0; |
| 380 | #endif |
| 381 | } |
| 382 | |
| 383 | static void |
| 384 | drawfire(void) |
| 385 | { |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 386 | static char frbuf[80] = ""; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 387 | int j; |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 388 | static double t0 = -1.; |
| 389 | double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; |
| 390 | if (t0 < 0.0) |
| 391 | t0 = t; |
| 392 | dt = (t - t0) * 1.0; |
| 393 | t0 = t; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 394 | |
| 395 | dojoy(); |
| 396 | |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 397 | if (NiceFog) |
| 398 | glHint(GL_FOG_HINT, GL_NICEST); |
| 399 | else |
| 400 | glHint(GL_FOG_HINT, GL_DONT_CARE); |
| 401 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 402 | glEnable(GL_DEPTH_TEST); |
| 403 | |
| 404 | if (fog) |
| 405 | glEnable(GL_FOG); |
| 406 | else |
| 407 | glDisable(GL_FOG); |
| 408 | |
| 409 | glDepthMask(GL_TRUE); |
| 410 | glClearColor(1.0, 1.0, 1.0, 1.0); |
| 411 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 412 | |
| 413 | glPushMatrix(); |
| 414 | calcposobs(); |
| 415 | gluLookAt(obs[0], obs[1], obs[2], |
| 416 | obs[0] + dir[0], obs[1] + dir[1], obs[2] + dir[2], |
| 417 | 0.0, 1.0, 0.0); |
| 418 | |
| 419 | glColor4f(1.0, 1.0, 1.0, 1.0); |
| 420 | |
| 421 | glEnable(GL_TEXTURE_2D); |
| 422 | |
| 423 | glBindTexture(GL_TEXTURE_2D, groundid); |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 424 | #if 1 |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 425 | glBegin(GL_QUADS); |
| 426 | glTexCoord2fv(qt[0]); |
| 427 | glVertex3fv(q[0]); |
| 428 | glTexCoord2fv(qt[1]); |
| 429 | glVertex3fv(q[1]); |
| 430 | glTexCoord2fv(qt[2]); |
| 431 | glVertex3fv(q[2]); |
| 432 | glTexCoord2fv(qt[3]); |
| 433 | glVertex3fv(q[3]); |
| 434 | glEnd(); |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 435 | #else |
| 436 | /* Subdivide the ground into a bunch of quads. This improves fog |
| 437 | * if GL_FOG_HINT != GL_NICEST |
| 438 | */ |
| 439 | { |
| 440 | float x, y; |
| 441 | float dx = 1.0, dy = 1.0; |
| 442 | glBegin(GL_QUADS); |
| 443 | for (y = -DIMP; y < DIMP; y += 1.0) { |
| 444 | for (x = -DIMP; x < DIMP; x += 1.0) { |
| 445 | glTexCoord2f(0, 0); glVertex3f(x, 0, y); |
| 446 | glTexCoord2f(1, 0); glVertex3f(x+dx, 0, y); |
| 447 | glTexCoord2f(1, 1); glVertex3f(x+dx, 0, y+dy); |
| 448 | glTexCoord2f(0, 1); glVertex3f(x, 0, y+dy); |
| 449 | } |
| 450 | } |
| 451 | glEnd(); |
| 452 | } |
| 453 | #endif |
| 454 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 455 | |
| 456 | glEnable(GL_ALPHA_TEST); |
| 457 | glAlphaFunc(GL_GEQUAL, 0.9); |
| 458 | |
| 459 | glBindTexture(GL_TEXTURE_2D, treeid); |
| 460 | for (j = 0; j < NUMTREE; j++) |
| 461 | drawtree(treepos[j][0], treepos[j][1], treepos[j][2]); |
| 462 | |
| 463 | glDisable(GL_TEXTURE_2D); |
| 464 | glDepthMask(GL_FALSE); |
| 465 | glDisable(GL_ALPHA_TEST); |
| 466 | |
| 467 | if (shadows) { |
| 468 | glBegin(GL_TRIANGLES); |
| 469 | for (j = 0; j < np; j++) { |
| 470 | glColor4f(black[0], black[1], black[2], p[j].c[0][3]); |
| 471 | glVertex3f(p[j].p[0][0], 0.1, p[j].p[0][2]); |
| 472 | |
| 473 | glColor4f(black[0], black[1], black[2], p[j].c[1][3]); |
| 474 | glVertex3f(p[j].p[1][0], 0.1, p[j].p[1][2]); |
| 475 | |
| 476 | glColor4f(black[0], black[1], black[2], p[j].c[2][3]); |
| 477 | glVertex3f(p[j].p[2][0], 0.1, p[j].p[2][2]); |
| 478 | } |
| 479 | glEnd(); |
| 480 | } |
| 481 | |
| 482 | glBegin(GL_TRIANGLES); |
| 483 | for (j = 0; j < np; j++) { |
| 484 | glColor4fv(p[j].c[0]); |
| 485 | glVertex3fv(p[j].p[0]); |
| 486 | |
| 487 | glColor4fv(p[j].c[1]); |
| 488 | glVertex3fv(p[j].p[1]); |
| 489 | |
| 490 | glColor4fv(p[j].c[2]); |
| 491 | glVertex3fv(p[j].p[2]); |
| 492 | |
| 493 | setpart(&p[j]); |
| 494 | } |
| 495 | glEnd(); |
| 496 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 497 | glDisable(GL_TEXTURE_2D); |
| 498 | glDisable(GL_ALPHA_TEST); |
| 499 | glDisable(GL_DEPTH_TEST); |
| 500 | glDisable(GL_FOG); |
| 501 | |
| 502 | glMatrixMode(GL_PROJECTION); |
| 503 | glLoadIdentity(); |
| 504 | glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0); |
| 505 | glMatrixMode(GL_MODELVIEW); |
| 506 | glLoadIdentity(); |
| 507 | |
| 508 | glColor3f(1.0, 0.0, 0.0); |
| 509 | glRasterPos2i(10, 10); |
| 510 | printstring(GLUT_BITMAP_HELVETICA_18, frbuf); |
| 511 | glRasterPos2i(370, 470); |
| 512 | printstring(GLUT_BITMAP_HELVETICA_10, |
| 513 | "Fire V1.5 Written by David Bucciarelli (tech.hmw@plus.it)"); |
| 514 | |
| 515 | if (help) |
| 516 | printhelp(); |
| 517 | |
| 518 | reshape(WIDTH, HEIGHT); |
| 519 | glPopMatrix(); |
| 520 | |
| 521 | glutSwapBuffers(); |
| 522 | |
Brian Paul | d49b34a | 2000-09-12 18:44:45 +0000 | [diff] [blame] | 523 | Frames++; |
| 524 | { |
| 525 | GLint t = glutGet(GLUT_ELAPSED_TIME); |
| 526 | if (t - T0 >= 2000) { |
| 527 | GLfloat seconds = (t - T0) / 1000.0; |
| 528 | GLfloat fps = Frames / seconds; |
| 529 | sprintf(frbuf, "Frame rate: %f", fps); |
| 530 | T0 = t; |
| 531 | Frames = 0; |
| 532 | } |
| 533 | } |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | |
| 537 | static void |
Brian Paul | e188b6e | 2000-10-21 04:29:02 +0000 | [diff] [blame] | 538 | idle(void) |
| 539 | { |
| 540 | glutPostRedisplay(); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | static void |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 545 | special(int key, int x, int y) |
| 546 | { |
| 547 | switch (key) { |
| 548 | case GLUT_KEY_LEFT: |
| 549 | alpha += 2.0; |
| 550 | break; |
| 551 | case GLUT_KEY_RIGHT: |
| 552 | alpha -= 2.0; |
| 553 | break; |
| 554 | case GLUT_KEY_DOWN: |
| 555 | beta -= 2.0; |
| 556 | break; |
| 557 | case GLUT_KEY_UP: |
| 558 | beta += 2.0; |
| 559 | break; |
| 560 | } |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 561 | glutPostRedisplay(); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static void |
| 565 | key(unsigned char key, int x, int y) |
| 566 | { |
| 567 | switch (key) { |
| 568 | case 27: |
| 569 | exit(0); |
| 570 | break; |
| 571 | |
| 572 | case 'a': |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 573 | v += 0.0005; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 574 | break; |
| 575 | case 'z': |
Brian Paul | 7a663b3 | 2005-01-09 17:52:48 +0000 | [diff] [blame] | 576 | v -= 0.0005; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 577 | break; |
| 578 | |
| 579 | case 'j': |
| 580 | joyactive = (!joyactive); |
| 581 | break; |
| 582 | case 'h': |
| 583 | help = (!help); |
| 584 | break; |
| 585 | case 'f': |
| 586 | fog = (!fog); |
| 587 | break; |
| 588 | case 's': |
| 589 | shadows = !shadows; |
| 590 | break; |
| 591 | case 'R': |
| 592 | eject_r -= 0.03; |
| 593 | break; |
| 594 | case 'r': |
| 595 | eject_r += 0.03; |
| 596 | break; |
| 597 | case 't': |
| 598 | ridtri += 0.005; |
| 599 | break; |
| 600 | case 'T': |
| 601 | ridtri -= 0.005; |
| 602 | break; |
| 603 | #ifdef XMESA |
| 604 | case ' ': |
| 605 | XMesaSetFXmode(fullscreen ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW); |
| 606 | fullscreen = (!fullscreen); |
| 607 | break; |
| 608 | #endif |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 609 | case 'n': |
| 610 | NiceFog = !NiceFog; |
| 611 | printf("NiceFog %d\n", NiceFog); |
| 612 | break; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 613 | } |
Brian Paul | 8a42f6b | 2001-02-16 21:48:16 +0000 | [diff] [blame] | 614 | glutPostRedisplay(); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void |
| 618 | inittextures(void) |
| 619 | { |
| 620 | GLenum gluerr; |
| 621 | GLubyte tex[128][128][4]; |
| 622 | |
| 623 | glGenTextures(1, &groundid); |
| 624 | glBindTexture(GL_TEXTURE_2D, groundid); |
| 625 | |
| 626 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 627 | if (!LoadRGBMipmaps("../images/s128.rgb", GL_RGB)) { |
| 628 | fprintf(stderr, "Error reading a texture.\n"); |
| 629 | exit(-1); |
| 630 | } |
| 631 | |
| 632 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 633 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 634 | |
| 635 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 636 | GL_LINEAR_MIPMAP_LINEAR); |
| 637 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 638 | |
| 639 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); |
| 640 | |
| 641 | glGenTextures(1, &treeid); |
| 642 | glBindTexture(GL_TEXTURE_2D, treeid); |
| 643 | |
Brian Paul | f768817 | 2000-06-27 17:12:10 +0000 | [diff] [blame] | 644 | if (1) |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 645 | { |
| 646 | int w, h; |
| 647 | GLenum format; |
| 648 | int x, y; |
Brian Paul | f768817 | 2000-06-27 17:12:10 +0000 | [diff] [blame] | 649 | GLubyte *image = LoadRGBImage("../images/tree3.rgb", &w, &h, &format); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 650 | |
| 651 | if (!image) { |
| 652 | fprintf(stderr, "Error reading a texture.\n"); |
| 653 | exit(-1); |
| 654 | } |
| 655 | |
| 656 | for (y = 0; y < 128; y++) |
| 657 | for (x = 0; x < 128; x++) { |
| 658 | tex[x][y][0] = image[(y + x * 128) * 3]; |
| 659 | tex[x][y][1] = image[(y + x * 128) * 3 + 1]; |
| 660 | tex[x][y][2] = image[(y + x * 128) * 3 + 2]; |
| 661 | if ((tex[x][y][0] == tex[x][y][1]) && |
| 662 | (tex[x][y][1] == tex[x][y][2]) && (tex[x][y][2] == 255)) |
| 663 | tex[x][y][3] = 0; |
| 664 | else |
| 665 | tex[x][y][3] = 255; |
| 666 | } |
| 667 | |
| 668 | if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 128, 128, GL_RGBA, |
| 669 | GL_UNSIGNED_BYTE, (GLvoid *) (tex)))) { |
Brian Paul | 1972ac6 | 2002-08-08 17:12:38 +0000 | [diff] [blame] | 670 | fprintf(stderr, "GLULib%s\n", (char *) gluErrorString(gluerr)); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 671 | exit(-1); |
| 672 | } |
| 673 | } |
Brian Paul | f768817 | 2000-06-27 17:12:10 +0000 | [diff] [blame] | 674 | else { |
| 675 | if (!LoadRGBMipmaps("../images/tree2.rgba", GL_RGBA)) { |
| 676 | fprintf(stderr, "Error reading a texture.\n"); |
| 677 | exit(-1); |
| 678 | } |
| 679 | } |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 680 | |
| 681 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 682 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 683 | |
| 684 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 685 | GL_LINEAR_MIPMAP_LINEAR); |
| 686 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 687 | |
| 688 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | inittree(void) |
| 693 | { |
| 694 | int i; |
| 695 | float dist; |
| 696 | |
| 697 | for (i = 0; i < NUMTREE; i++) |
| 698 | do { |
| 699 | treepos[i][0] = vrnd() * TREEOUTR * 2.0 - TREEOUTR; |
| 700 | treepos[i][1] = 0.0; |
| 701 | treepos[i][2] = vrnd() * TREEOUTR * 2.0 - TREEOUTR; |
| 702 | dist = |
| 703 | sqrt(treepos[i][0] * treepos[i][0] + |
| 704 | treepos[i][2] * treepos[i][2]); |
| 705 | } while ((dist < TREEINR) || (dist > TREEOUTR)); |
| 706 | } |
| 707 | |
| 708 | int |
| 709 | main(int ac, char **av) |
| 710 | { |
| 711 | int i; |
| 712 | |
| 713 | fprintf(stderr, |
| 714 | "Fire V1.5\nWritten by David Bucciarelli (tech.hmw@plus.it)\n"); |
| 715 | |
| 716 | /* Default settings */ |
| 717 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 718 | np = 800; |
Daniel Borca | 6a432a7 | 2005-01-17 07:48:53 +0000 | [diff] [blame] | 719 | eject_r = -0.65; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 720 | dt = 0.015; |
| 721 | eject_vy = 4; |
| 722 | eject_vl = 1; |
| 723 | shadows = 1; |
Daniel Borca | 6a432a7 | 2005-01-17 07:48:53 +0000 | [diff] [blame] | 724 | ridtri = 0.25; |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 725 | |
| 726 | maxage = 1.0 / dt; |
| 727 | |
| 728 | if (ac == 2) |
| 729 | np = atoi(av[1]); |
| 730 | |
| 731 | if (ac == 4) { |
| 732 | WIDTH = atoi(av[2]); |
| 733 | HEIGHT = atoi(av[3]); |
| 734 | } |
| 735 | |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 736 | glutInitWindowSize(WIDTH, HEIGHT); |
| 737 | glutInit(&ac, av); |
| 738 | |
| 739 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
| 740 | |
| 741 | if (!(win = glutCreateWindow("Fire"))) { |
| 742 | fprintf(stderr, "Error opening a window.\n"); |
| 743 | exit(-1); |
| 744 | } |
| 745 | |
| 746 | reshape(WIDTH, HEIGHT); |
| 747 | |
| 748 | inittextures(); |
| 749 | |
| 750 | glShadeModel(GL_FLAT); |
| 751 | glEnable(GL_DEPTH_TEST); |
| 752 | |
| 753 | glEnable(GL_BLEND); |
| 754 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 755 | |
| 756 | glEnable(GL_FOG); |
| 757 | glFogi(GL_FOG_MODE, GL_EXP); |
| 758 | glFogfv(GL_FOG_COLOR, fogcolor); |
| 759 | glFogf(GL_FOG_DENSITY, 0.1); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 760 | |
Brian Paul | f02a5f6 | 2002-07-12 15:54:01 +0000 | [diff] [blame] | 761 | p = (part *) malloc(sizeof(part) * np); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 762 | |
| 763 | for (i = 0; i < np; i++) |
| 764 | setnewpart(&p[i]); |
| 765 | |
| 766 | inittree(); |
| 767 | |
| 768 | glutKeyboardFunc(key); |
| 769 | glutSpecialFunc(special); |
| 770 | glutDisplayFunc(drawfire); |
Brian Paul | e188b6e | 2000-10-21 04:29:02 +0000 | [diff] [blame] | 771 | glutIdleFunc(idle); |
Brian Paul | 5b0a7f3 | 2000-06-27 16:52:38 +0000 | [diff] [blame] | 772 | glutReshapeFunc(reshape); |
| 773 | glutMainLoop(); |
| 774 | |
| 775 | return (0); |
| 776 | } |