1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* fire --- 3D fire or rain landscape */
5 static const char sccsid[] = "@(#)fire.c 5.02 2001/09/26 xlockmore";
8 /* Copyright (c) E. Lassauge, 2001. */
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * The original code for this mode was written by David Bucciarelli
24 * (tech.hmw@plus.it) and could be found in the demo package
25 * of Mesa (Mesa-3.2/3Dfx/demos/). This mode is the result of the merge of
26 * two of the David's demos (fire and rain).
28 * Eric Lassauge (October-10-2000) <lassauge@users.sourceforge.net>
29 * http://lassauge.free.fr/linux.html
33 * E.Lassauge - 26-Sep-2001:
34 * - add wander option and code
35 * - cleanups for xscreensaver
37 * E.Lassauge - 09-Mar-2001:
38 * - get rid of my framerate options to use showfps
40 * E.Lassauge - 12-Jan-2001:
41 * - add rain particules, selected if count=0 (no fire means rain !)
43 * E.Lassauge - 28-Nov-2000:
44 * - modified release part to add freeing of GL objects
46 * E.Lassauge - 14-Nov-2000:
47 * - use new common xpm_to_ximage function
49 * E.Lassauge - 25-Oct-2000:
50 * - add the trees (with a new resource '-trees')
51 * - corrected handling of color (textured vs untextured)
52 * - corrected handling of endiannes for the xpm files
53 * - inverted ground pixmap file
54 * - use malloc-ed tree array
56 * TSchmidt - 23-Oct-2000:
57 * - added size option like used in sproingies mode
59 * E.Lassauge - 13-Oct-2000:
60 * - when trackmouse and window is iconified (login screen): stop tracking
61 * - add pure GLX handling of framerate display (erased GLUT stuff)
62 * - made count a per screen variable and update it only if framemode
63 * - changes for no_texture an wireframe modes
64 * - change no_texture color for the ground
65 * - add freeing of texture image
66 * - misc comments and little tweakings
69 * - perhaps use a user supplied xpm for ground image (or a whatever image
70 * file using ImageMagick ?)
71 * - random number of trees ? change trees at change_fire ?
72 * - fix wireframe mode: it's too CPU intensive.
73 * - look how we can get the Wheel events (Button4&5).
77 #ifdef STANDALONE /* xscreensaver mode */
78 #define DEFAULTS "*delay: 10000 \n" \
81 "*showFPS: False \n" \
82 "*wireframe: False \n" \
85 #include "xlockmore.h" /* from the xscreensaver distribution */
86 #include "gltrackball.h"
87 #else /* !STANDALONE */
88 #include "xlock.h" /* from the xlockmore distribution */
90 #endif /* !STANDALONE */
96 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
97 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
98 #include "ximage-loader.h"
101 #include "images/gen/ground_png.h"
102 #include "images/gen/tree_png.h"
103 #endif /* HAVE_XPM */
105 /* vector utility macros */
106 #define vinit(a,i,j,k) {\
112 #define vinit4(a,i,j,k,w) {\
119 #define vadds(a,dt,b) {\
120 (a)[0]+=(dt)*(b)[0];\
121 (a)[1]+=(dt)*(b)[1];\
122 (a)[2]+=(dt)*(b)[2];\
131 #define vinter(a,dt,b,c) {\
132 (a)[0]=(dt)*(b)[0]+(1.0-dt)*(c)[0];\
133 (a)[1]=(dt)*(b)[1]+(1.0-dt)*(c)[1];\
134 (a)[2]=(dt)*(b)[2]+(1.0-dt)*(c)[2];\
137 #define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0))
140 (v)[0]=clamp((v)[0]);\
141 (v)[1]=clamp((v)[1]);\
142 (v)[2]=clamp((v)[2]);\
145 /* Manage option vars */
146 #define DEF_TEXTURE "True"
147 #define DEF_FOG "False"
148 #define DEF_SHADOWS "True"
149 #define DEF_FRAMERATE "False"
150 #define DEF_WANDER "True"
151 #define DEF_TREES "5"
153 static Bool do_texture;
155 static Bool do_shadows;
156 static Bool do_wander;
157 static int num_trees;
158 static XFontStruct *mode_font = None;
160 static XrmOptionDescRec opts[] = {
161 {"-texture", ".fire.texture", XrmoptionNoArg, "on"},
162 {"+texture", ".fire.texture", XrmoptionNoArg, "off"},
163 {"-fog", ".fire.fog", XrmoptionNoArg, "on"},
164 {"+fog", ".fire.fog", XrmoptionNoArg, "off"},
165 {"-shadows", ".fire.shadows", XrmoptionNoArg, "on"},
166 {"+shadows", ".fire.shadows", XrmoptionNoArg, "off"},
167 {"-wander", ".fire.wander", XrmoptionNoArg, "on"},
168 {"+wander", ".fire.wander", XrmoptionNoArg, "off"},
169 {"-trees", ".fire.trees", XrmoptionSepArg, 0},
170 {"-rain", ".fire.count", XrmoptionNoArg, "0"},
174 static argtype vars[] = {
175 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
176 {&do_fog, "fog", "Fog", DEF_FOG, t_Bool},
177 {&do_shadows, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
178 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
179 {&num_trees, "trees", "Trees", DEF_TREES, t_Int},
182 static OptionStruct desc[] = {
183 {"-/+texture", "turn on/off texturing"},
184 {"-/+fog", "turn on/off fog"},
185 {"-/+shadows", "turn on/off shadows"},
186 {"-/+wander", "turn on/off wandering"},
187 {"-trees num", "number of trees (0 disables)"},
190 ENTRYPOINT ModeSpecOpt fire_opts =
191 { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
194 ModStruct fire_description =
195 { "fire", "init_fire", "draw_fire", "release_fire",
196 "draw_fire", "change_fire", (char *) NULL, &fire_opts,
197 10000, 800, 1, 400, 64, 1.0, "",
198 "Shows a 3D fire-like image", 0, NULL
200 #endif /* USE_MODULES */
203 #define TREEINR 2.5 /* tree min distance */
204 #define TREEOUTR 8.0 /* tree max distance */
205 #define FRAME 50 /* frame count interval */
206 #define DIMP 20.0 /* dimension of ground */
207 #define DIMTP 16.0 /* dimension of ground texture */
209 #define RIDCOL 0.4 /* factor for color blending */
211 #define AGRAV -9.8 /* gravity */
213 #define NUMPART 7500 /* rain particles */
215 /* fire particle struct */
223 /* rain particle struct */
234 static const GLfloat black[3] = { 0.0, 0.0, 0.0 }; /* shadow color */
235 static const GLfloat partcol1[3] = { 1.0, 0.2, 0.0 }; /* initial color: red-ish */
236 static const GLfloat partcol2[3] = { 1.0, 1.0, 0.0 }; /* blending color: yellow-ish */
237 static const GLfloat fogcolor[4] = { 0.9, 0.9, 1.0, 1.0 };
240 static const float q[4][3] = {
248 static const float qt[4][2] = {
255 /* default values for observer */
256 static const float DEF_OBS[3] = { 2.0f, 1.0f, 0.0f };
258 #define DEF_ALPHA -90.0
259 #define DEF_BETA 90.0
266 /* the mode struct, contains all per screen variables */
268 GLint WIDTH, HEIGHT; /* display dimensions */
269 GLXContext *glx_context;
271 int np; /* number of fire particles : set it through 'count' resource */
272 float eject_r; /* emission radius */
273 float dt, maxage, eject_vy, eject_vl;
274 float ridtri; /* fire particle size */
275 Bool shadows; /* misc booleans: set them through specific resources */
278 part *p; /* fire particles array */
279 rain *r; /* rain particles array */
281 XImage *gtexture; /* ground texture image bits */
282 XImage *ttexture; /* tree texture image bits */
283 GLuint groundid; /* ground texture id: GL world */
284 GLuint treeid; /* tree texture id: GL world */
285 GLuint fontbase; /* fontbase id: GL world */
287 int num_trees; /* number of trees: set it through 'trees' resource */
288 treestruct *treepos; /* trees positions: float treepos[num_trees][3] */
290 float min[3]; /* raining area */
293 float obs[3]; /* observer coordinates */
294 float dir[3]; /* view direction */
295 float v; /* observer velocity */
296 float alpha; /* observer angles */
299 trackball_state *trackball;
305 /* array of firestruct indexed by screen number */
306 static firestruct *fire = (firestruct *) NULL;
309 *-----------------------------------------------------------------------------
310 *-----------------------------------------------------------------------------
312 *-----------------------------------------------------------------------------
313 *-----------------------------------------------------------------------------
316 /* utility function for the rain particles */
317 static float gettimerain(void)
320 /* Oh yeah, *that's* portable! WTF. */
322 * I really thought clock() was standard ... EL
323 * I found this on the net:
324 * The clock() function conforms to ISO/IEC 9899:1990 (``ISO C89'')
327 static clock_t told= (clock_t)0;
336 return (0.0125 + ris/(float)CLOCKS_PER_SEC);
343 static float vrnd(void)
345 return ((float) LRAND() / (float) MAXRAND);
348 /* initialise new fire particle */
349 static void setnewpart(firestruct * fs, part * p)
356 a = vrnd() * M_PI * 2.0;
358 vinit(vi, sin(a) * fs->eject_r * vrnd(), 0.15, cos(a) * fs->eject_r * vrnd());
359 vinit(p->p[0], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
360 vinit(p->p[1], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
361 vinit(p->p[2], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
363 vinit(p->v, vi[0] * fs->eject_vl / (fs->eject_r / 2),
364 vrnd() * fs->eject_vy + fs->eject_vy / 2,
365 vi[2] * fs->eject_vl / (fs->eject_r / 2));
369 vinit4(p->c[0], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
370 c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
371 c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
372 vinit4(p->c[1], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
373 c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
374 c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
375 vinit4(p->c[2], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
376 c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
377 c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
380 /* initialise new rain particle */
381 static void setnewrain(firestruct * fs, rain * r)
385 vinit(r->acc,0.0f,-0.98f,0.0f);
386 vinit(r->vel,0.0f,0.0f,0.0f);
390 vinit(r->oldpos,fs->min[0]+(fs->max[0]-fs->min[0])*vrnd(),
391 fs->max[1]+0.2f*fs->max[1]*vrnd(),
392 fs->min[2]+(fs->max[2]-fs->min[2])*vrnd());
393 vequ(r->pos,r->oldpos);
394 vadds(r->oldpos,-(r->partLength),r->vel);
396 r->pos[1]=(fs->max[1]-fs->min[1])*vrnd()+fs->min[1];
397 r->oldpos[1]=r->pos[1]-r->partLength*r->vel[1];
400 /* set fire particle values */
401 static void setpart(firestruct * fs, part * p)
405 if (p->p[0][1] < 0.1) {
410 p->v[1] += AGRAV * fs->dt;
412 vadds(p->p[0], fs->dt, p->v);
413 vadds(p->p[1], fs->dt, p->v);
414 vadds(p->p[2], fs->dt, p->v);
418 if ((p->age) > fs->maxage) {
419 vequ(p->c[0], partcol2);
420 vequ(p->c[1], partcol2);
421 vequ(p->c[2], partcol2);
423 fact = 1.0 / fs->maxage;
424 vadds(p->c[0], fact, partcol2);
426 p->c[0][3] = fact * (fs->maxage - p->age);
428 vadds(p->c[1], fact, partcol2);
430 p->c[1][3] = fact * (fs->maxage - p->age);
432 vadds(p->c[2], fact, partcol2);
434 p->c[2][3] = fact * (fs->maxage - p->age);
438 /* set rain particle values */
439 static void setpartrain(firestruct * fs, rain * r, float dt)
443 vadds(r->vel,dt,r->acc);
444 vadds(r->pos,dt,r->vel);
446 if(r->pos[0]<fs->min[0])
447 r->pos[0]=fs->max[0]-(fs->min[0]-r->pos[0]);
448 if(r->pos[2]<fs->min[2])
449 r->pos[2]=fs->max[2]-(fs->min[2]-r->pos[2]);
451 if(r->pos[0]>fs->max[0])
452 r->pos[0]=fs->min[0]+(r->pos[0]-fs->max[0]);
453 if(r->pos[2]>fs->max[2])
454 r->pos[2]=fs->min[2]+(r->pos[2]-fs->max[2]);
456 vequ(r->oldpos,r->pos);
457 vadds(r->oldpos,-(r->partLength),r->vel);
458 if(r->pos[1]<fs->min[1])
463 static int drawtree(float x, float y, float z)
467 glTexCoord2f(0.0,0.0);
468 glVertex3f(x-1.5,y+0.0,z);
470 glTexCoord2f(1.0,0.0);
471 glVertex3f(x+1.5,y+0.0,z);
473 glTexCoord2f(1.0,1.0);
474 glVertex3f(x+1.5,y+3.0,z);
476 glTexCoord2f(0.0,1.0);
477 glVertex3f(x-1.5,y+3.0,z);
481 glTexCoord2f(0.0,0.0);
482 glVertex3f(x,y+0.0,z-1.5);
484 glTexCoord2f(1.0,0.0);
485 glVertex3f(x,y+0.0,z+1.5);
487 glTexCoord2f(1.0,1.0);
488 glVertex3f(x,y+3.0,z+1.5);
490 glTexCoord2f(0.0,1.0);
491 glVertex3f(x,y+3.0,z-1.5);
499 /* calculate observer position : modified only if trackmouse is used */
500 static void calcposobs(firestruct * fs)
502 fs->dir[0] = sin(fs->alpha * M_PI / 180.0);
504 cos(fs->alpha * M_PI / 180.0) * sin(fs->beta * M_PI / 180.0);
505 fs->dir[1] = cos(fs->beta * M_PI / 180.0);
507 fs->obs[0] += fs->v * fs->dir[0];
508 fs->obs[1] += fs->v * fs->dir[1];
509 fs->obs[2] += fs->v * fs->dir[2];
513 vinit(fs->min,fs->obs[0]-7.0f,-0.2f,fs->obs[2]-7.0f);
514 vinit(fs->max,fs->obs[0]+7.0f,8.0f,fs->obs[2]+7.0f);
519 /* initialise textures */
520 static void inittextures(ModeInfo * mi)
522 firestruct *fs = &fire[MI_SCREEN(mi)];
524 #if defined( I_HAVE_XPM )
527 glGenTextures(1, &fs->groundid);
528 #ifdef HAVE_GLBINDTEXTURE
529 glBindTexture(GL_TEXTURE_2D, fs->groundid);
530 #endif /* HAVE_GLBINDTEXTURE */
532 if ((fs->gtexture = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
536 (void) fprintf(stderr, "Error reading the ground texture.\n");
537 glDeleteTextures(1, &fs->groundid);
544 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
546 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
547 fs->gtexture->width, fs->gtexture->height, 0,
548 GL_RGBA, GL_UNSIGNED_BYTE, fs->gtexture->data);
549 check_gl_error("texture");
551 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
552 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
555 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
557 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
561 glGenTextures(1, &fs->treeid);
562 #ifdef HAVE_GLBINDTEXTURE
563 glBindTexture(GL_TEXTURE_2D,fs->treeid);
564 #endif /* HAVE_GLBINDTEXTURE */
565 if ((fs->ttexture = image_data_to_ximage(MI_DISPLAY(mi),
570 (void)fprintf(stderr,"Error reading tree texture.\n");
571 glDeleteTextures(1, &fs->treeid);
578 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
579 fs->ttexture->width, fs->ttexture->height, 0,
580 GL_RGBA, GL_UNSIGNED_BYTE, fs->ttexture->data);
581 check_gl_error("texture");
583 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
584 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
586 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
587 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
589 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
594 fs->groundid = 0; /* default textures */
597 #else /* !I_HAVE_XPM */
599 fs->groundid = 0; /* default textures */
601 #endif /* !I_HAVE_XPM */
604 /* init tree array and positions */
605 static Bool inittree(ModeInfo * mi)
607 firestruct *fs = &fire[MI_SCREEN(mi)];
611 /* allocate treepos array */
612 if ((fs->treepos = (treestruct *) malloc(fs->num_trees *
613 sizeof(treestruct))) == NULL) {
616 /* initialise positions */
617 for(i=0;i<fs->num_trees;i++) {
619 fs->treepos[i].x =vrnd()*TREEOUTR*2.0-TREEOUTR;
620 fs->treepos[i].y =0.0;
621 fs->treepos[i].z =vrnd()*TREEOUTR*2.0-TREEOUTR;
622 dist = sqrt(fs->treepos[i].x * fs->treepos[i].x +
623 fs->treepos[i].z * fs->treepos[i].z);
624 } while((dist<TREEINR) || (dist>TREEOUTR));
630 *-----------------------------------------------------------------------------
631 *-----------------------------------------------------------------------------
633 *-----------------------------------------------------------------------------
634 *-----------------------------------------------------------------------------
637 ENTRYPOINT void reshape_fire(ModeInfo * mi, int width, int height)
640 firestruct *fs = &fire[MI_SCREEN(mi)];
641 int size = MI_SIZE(mi);
643 /* Viewport is specified size if size >= MINSIZE && size < screensize */
645 fs->WIDTH = MI_WIDTH(mi);
646 fs->HEIGHT = MI_HEIGHT(mi);
647 } else if (size < MINSIZE) {
649 fs->HEIGHT = MINSIZE;
651 fs->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
652 fs->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
654 glViewport((MI_WIDTH(mi) - fs->WIDTH) / 2, (MI_HEIGHT(mi) - fs->HEIGHT) / 2, fs->WIDTH, fs->HEIGHT);
655 glMatrixMode(GL_PROJECTION);
657 gluPerspective(70.0, fs->WIDTH / (float) fs->HEIGHT, 0.1, 30.0);
659 glMatrixMode(GL_MODELVIEW);
663 static void DrawFire(ModeInfo * mi)
666 firestruct *fs = &fire[MI_SCREEN(mi)];
667 Bool wire = MI_IS_WIREFRAME(mi);
669 mi->polygon_count = 0;
671 if (do_wander && !fs->button_down_p)
675 # define SINOID(SCALE,SIZE) \
676 ((((1 + sin((fs->frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
678 x = SINOID(0.031, 0.85);
679 y = SINOID(0.017, 0.25);
680 z = SINOID(0.023, 0.85);
682 fs->obs[0] = x + DEF_OBS[0];
683 fs->obs[1] = y + DEF_OBS[1];
684 fs->obs[2] = z + DEF_OBS[2];
689 glEnable(GL_DEPTH_TEST);
696 glDepthMask(GL_TRUE);
697 glClearColor(0.5, 0.5, 0.8, 1.0); /* sky in the distance */
698 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
704 gltrackball_rotate (fs->trackball);
706 gluLookAt(fs->obs[0], fs->obs[1], fs->obs[2],
707 fs->obs[0] + fs->dir[0],
708 fs->obs[1] + fs->dir[1],
709 fs->obs[2] + fs->dir[2],
712 glEnable(GL_TEXTURE_2D);
714 /* draw ground using the computed texture */
716 glColor4f(1.0,1.0,1.0,1.0); /* white to get texture in it's true color */
717 #ifdef HAVE_GLBINDTEXTURE
718 glBindTexture(GL_TEXTURE_2D, fs->groundid);
719 #endif /* HAVE_GLBINDTEXTURE */
722 glColor4f(0.54, 0.27, 0.07, 1.0); /* untextured ground color */
724 glTexCoord2fv(qt[0]);
726 glTexCoord2fv(qt[1]);
728 glTexCoord2fv(qt[2]);
730 glTexCoord2fv(qt[3]);
735 glAlphaFunc(GL_GEQUAL, 0.9);
738 /* here do_texture IS True - and color used is white */
739 glEnable(GL_ALPHA_TEST);
740 #ifdef HAVE_GLBINDTEXTURE
741 glBindTexture(GL_TEXTURE_2D,fs->treeid);
742 #endif /* HAVE_GLBINDTEXTURE */
743 for(j=0;j<fs->num_trees;j++)
744 mi->polygon_count += drawtree(fs->treepos[j].x ,fs->treepos[j].y ,fs->treepos[j].z );
745 glDisable(GL_ALPHA_TEST);
747 glDisable(GL_TEXTURE_2D);
748 glDepthMask(GL_FALSE);
751 /* draw shadows with black color */
752 glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
753 for (j = 0; j < fs->np; j++) {
754 glColor4f(black[0], black[1], black[2], fs->p[j].c[0][3]);
755 glVertex3f(fs->p[j].p[0][0], 0.1, fs->p[j].p[0][2]);
757 glColor4f(black[0], black[1], black[2], fs->p[j].c[1][3]);
758 glVertex3f(fs->p[j].p[1][0], 0.1, fs->p[j].p[1][2]);
760 glColor4f(black[0], black[1], black[2], fs->p[j].c[2][3]);
761 glVertex3f(fs->p[j].p[2][0], 0.1, fs->p[j].p[2][2]);
767 glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
768 for (j = 0; j < fs->np; j++) {
769 /* draw particles: colors are computed in setpart */
770 glColor4fv(fs->p[j].c[0]);
771 glVertex3fv(fs->p[j].p[0]);
773 glColor4fv(fs->p[j].c[1]);
774 glVertex3fv(fs->p[j].p[1]);
776 glColor4fv(fs->p[j].c[2]);
777 glVertex3fv(fs->p[j].p[2]);
780 setpart(fs, &fs->p[j]);
784 /* draw rain particles if no fire particles */
787 float timeused = gettimerain();
788 glDisable(GL_TEXTURE_2D);
789 glShadeModel(GL_SMOOTH);
791 for (j = 0; j < NUMPART; j++) {
792 glColor4f(0.7f,0.95f,1.0f,0.0f);
793 glVertex3fv(fs->r[j].oldpos);
794 glColor4f(0.3f,0.7f,1.0f,1.0f);
795 glVertex3fv(fs->r[j].pos);
796 setpartrain(fs, &fs->r[j],timeused);
800 glShadeModel(GL_FLAT);
803 glDisable(GL_TEXTURE_2D);
804 glDisable(GL_ALPHA_TEST);
805 glDisable(GL_DEPTH_TEST);
808 /* manage framerate display */
809 if (MI_IS_FPS(mi)) do_fps (mi);
814 static Bool Init(ModeInfo * mi)
817 firestruct *fs = &fire[MI_SCREEN(mi)];
819 /* default settings */
820 fs->eject_r = 0.1 + NRAND(10) * 0.03;
824 fs->ridtri = 0.1 + NRAND(10) * 0.005;
825 fs->maxage = 1.0 / fs->dt;
826 vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
828 fs->alpha = DEF_ALPHA;
831 /* initialise texture stuff */
836 fs->ttexture = (XImage*) NULL;
837 fs->gtexture = (XImage*) NULL;
840 if (MI_IS_DEBUG(mi)) {
841 (void) fprintf(stderr,
842 "%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
846 fs->fog ? "on" : "off",
847 fs->shadows ? "on" : "off",
848 fs->eject_r, fs->ridtri);
851 /* initialise particles and trees */
852 for (i = 0; i < fs->np; i++) {
853 setnewpart(fs, &(fs->p[i]));
861 /* if no fire particles then initialise rain particles */
864 vinit(fs->min,-7.0f,-0.2f,-7.0f);
865 vinit(fs->max,7.0f,8.0f,7.0f);
866 for (i = 0; i < NUMPART; i++) {
867 setnewrain(fs, &(fs->r[i]));
875 *-----------------------------------------------------------------------------
876 *-----------------------------------------------------------------------------
878 *-----------------------------------------------------------------------------
879 *-----------------------------------------------------------------------------
884 free_fire(ModeInfo * mi)
886 firestruct *fs = &fire[MI_SCREEN(mi)];
888 if (mode_font != None && fs->fontbase != None) {
889 glDeleteLists(fs->fontbase, mode_font->max_char_or_byte2 -
890 mode_font->min_char_or_byte2 + 1);
895 (void) free((void *) fs->p);
896 fs->p = (part *) NULL;
899 (void) free((void *) fs->r);
900 fs->r = (rain *) NULL;
902 if (fs->treepos != NULL) {
903 (void) free((void *) fs->treepos);
904 fs->treepos = (treestruct *) NULL;
906 if (fs->ttexture != None) {
907 glDeleteTextures(1, &fs->treeid);
908 XDestroyImage(fs->ttexture);
911 if (fs->gtexture != None) {
912 glDeleteTextures(1, &fs->groundid);
913 XDestroyImage(fs->gtexture);
919 *-----------------------------------------------------------------------------
920 * Initialize fire. Called each time the window changes.
921 *-----------------------------------------------------------------------------
925 init_fire(ModeInfo * mi)
930 fs = &fire[MI_SCREEN(mi)];
931 fs->np = MI_COUNT(mi);
933 fs->shadows = do_shadows;
934 /* initialise fire particles if any */
935 if ((fs->np)&&(fs->p == NULL)) {
936 if ((fs->p = (part *) calloc(fs->np, sizeof(part))) == NULL) {
941 else if (fs->r == NULL) {
942 /* initialise rain particles if no fire particles */
943 if ((fs->r = (rain *) calloc(NUMPART, sizeof(part))) == NULL) {
949 /* check tree number */
951 fs->num_trees = (num_trees<MAX_TREES)?num_trees:MAX_TREES;
955 fs->trackball = gltrackball_init (False);
958 if ((fs->glx_context = init_GL(mi)) != NULL) {
961 Reshape(mi); /* xlock mode */
963 reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
965 glDrawBuffer(GL_BACK);
976 *-----------------------------------------------------------------------------
977 * Called by the mainline code periodically to update the display.
978 *-----------------------------------------------------------------------------
980 ENTRYPOINT void draw_fire(ModeInfo * mi)
982 firestruct *fs = &fire[MI_SCREEN(mi)];
984 Display *display = MI_DISPLAY(mi);
985 Window window = MI_WINDOW(mi);
987 MI_IS_DRAWN(mi) = True;
989 if (!fs->glx_context)
992 glXMakeCurrent(display, window, *(fs->glx_context));
994 glShadeModel(GL_FLAT);
995 glEnable(GL_DEPTH_TEST);
997 /* makes particles blend with background */
998 if (!MI_IS_WIREFRAME(mi)||(!fs->np))
1001 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1006 glFogi(GL_FOG_MODE, GL_EXP);
1007 glFogfv(GL_FOG_COLOR, fogcolor);
1008 glFogf(GL_FOG_DENSITY, 0.03);
1009 glHint(GL_FOG_HINT, GL_NICEST);
1012 glRotatef(current_device_rotation(), 0, 0, 1);
1016 Reshape(mi); /* xlock mode */
1018 reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
1022 glXSwapBuffers(display, window);
1027 *-----------------------------------------------------------------------------
1028 * The display is being taken away from us. Free up malloc'ed
1029 * memory and X resources that we've alloc'ed. Only called
1030 * once, we must zap everything for every screen.
1031 *-----------------------------------------------------------------------------
1034 ENTRYPOINT void release_fire(ModeInfo * mi)
1036 if (mode_font != None)
1038 /* only free-ed when there are no more screens used */
1039 XFreeFont(MI_DISPLAY(mi), mode_font);
1046 fire_handle_event (ModeInfo *mi, XEvent *event)
1048 firestruct *fs = &fire[MI_SCREEN(mi)];
1050 if (gltrackball_event_handler (event, fs->trackball,
1051 MI_WIDTH (mi), MI_HEIGHT (mi),
1052 &fs->button_down_p))
1060 ENTRYPOINT void change_fire(ModeInfo * mi)
1062 firestruct *fs = &fire[MI_SCREEN(mi)];
1064 if (!fs->glx_context)
1067 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(fs->glx_context));
1069 /* if available, randomly change some values */
1071 fs->fog = LRAND() & 1;
1073 fs->shadows = LRAND() & 1;
1074 /* reset observer position */
1076 vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
1078 /* particle randomisation */
1079 fs->eject_r = 0.1 + NRAND(10) * 0.03;
1080 fs->ridtri = 0.1 + NRAND(10) * 0.005;
1082 if (MI_IS_DEBUG(mi)) {
1083 (void) fprintf(stderr,
1084 "%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
1088 fs->fog ? "on" : "off",
1089 fs->shadows ? "on" : "off",
1090 fs->eject_r, fs->ridtri);
1093 #endif /* !STANDALONE */
1095 XSCREENSAVER_MODULE_2 ("GLForestFire", glforestfire, fire)
1097 #endif /* MODE_fire */