Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fire.c 16KB

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