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" \
84 # define refresh_fire 0
86 #include "xlockmore.h" /* from the xscreensaver distribution */
87 #include "gltrackball.h"
88 #else /* !STANDALONE */
89 #include "xlock.h" /* from the xlockmore distribution */
91 #endif /* !STANDALONE */
97 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
98 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
99 #include "xpm-ximage.h"
103 #include "../images/ground.xpm"
104 #include "../images/tree.xpm"
105 #else /* !STANDALONE */
106 #include "pixmaps/ground.xpm"
107 #include "pixmaps/tree.xpm"
108 #endif /* !STANDALONE */
109 #endif /* HAVE_XPM */
111 /* vector utility macros */
112 #define vinit(a,i,j,k) {\
118 #define vinit4(a,i,j,k,w) {\
125 #define vadds(a,dt,b) {\
126 (a)[0]+=(dt)*(b)[0];\
127 (a)[1]+=(dt)*(b)[1];\
128 (a)[2]+=(dt)*(b)[2];\
137 #define vinter(a,dt,b,c) {\
138 (a)[0]=(dt)*(b)[0]+(1.0-dt)*(c)[0];\
139 (a)[1]=(dt)*(b)[1]+(1.0-dt)*(c)[1];\
140 (a)[2]=(dt)*(b)[2]+(1.0-dt)*(c)[2];\
143 #define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0))
146 (v)[0]=clamp((v)[0]);\
147 (v)[1]=clamp((v)[1]);\
148 (v)[2]=clamp((v)[2]);\
151 /* Manage option vars */
152 #define DEF_TEXTURE "True"
153 #define DEF_FOG "False"
154 #define DEF_SHADOWS "True"
155 #define DEF_FRAMERATE "False"
156 #define DEF_WANDER "True"
157 #define DEF_TREES "5"
159 static Bool do_texture;
161 static Bool do_shadows;
162 static Bool do_wander;
163 static int num_trees;
164 static XFontStruct *mode_font = None;
166 static XrmOptionDescRec opts[] = {
167 {"-texture", ".fire.texture", XrmoptionNoArg, "on"},
168 {"+texture", ".fire.texture", XrmoptionNoArg, "off"},
169 {"-fog", ".fire.fog", XrmoptionNoArg, "on"},
170 {"+fog", ".fire.fog", XrmoptionNoArg, "off"},
171 {"-shadows", ".fire.shadows", XrmoptionNoArg, "on"},
172 {"+shadows", ".fire.shadows", XrmoptionNoArg, "off"},
173 {"-wander", ".fire.wander", XrmoptionNoArg, "on"},
174 {"+wander", ".fire.wander", XrmoptionNoArg, "off"},
175 {"-trees", ".fire.trees", XrmoptionSepArg, 0},
176 {"-rain", ".fire.count", XrmoptionNoArg, "0"},
180 static argtype vars[] = {
181 {&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
182 {&do_fog, "fog", "Fog", DEF_FOG, t_Bool},
183 {&do_shadows, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
184 {&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
185 {&num_trees, "trees", "Trees", DEF_TREES, t_Int},
188 static OptionStruct desc[] = {
189 {"-/+texture", "turn on/off texturing"},
190 {"-/+fog", "turn on/off fog"},
191 {"-/+shadows", "turn on/off shadows"},
192 {"-/+wander", "turn on/off wandering"},
193 {"-trees num", "number of trees (0 disables)"},
196 ENTRYPOINT ModeSpecOpt fire_opts =
197 { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
200 ModStruct fire_description =
201 { "fire", "init_fire", "draw_fire", "release_fire",
202 "draw_fire", "change_fire", (char *) NULL, &fire_opts,
203 10000, 800, 1, 400, 64, 1.0, "",
204 "Shows a 3D fire-like image", 0, NULL
206 #endif /* USE_MODULES */
209 #define TREEINR 2.5 /* tree min distance */
210 #define TREEOUTR 8.0 /* tree max distance */
211 #define FRAME 50 /* frame count interval */
212 #define DIMP 20.0 /* dimension of ground */
213 #define DIMTP 16.0 /* dimension of ground texture */
215 #define RIDCOL 0.4 /* factor for color blending */
217 #define AGRAV -9.8 /* gravity */
219 #define NUMPART 7500 /* rain particles */
221 /* fire particle struct */
229 /* rain particle struct */
240 static const GLfloat black[3] = { 0.0, 0.0, 0.0 }; /* shadow color */
241 static const GLfloat partcol1[3] = { 1.0, 0.2, 0.0 }; /* initial color: red-ish */
242 static const GLfloat partcol2[3] = { 1.0, 1.0, 0.0 }; /* blending color: yellow-ish */
243 static const GLfloat fogcolor[4] = { 0.9, 0.9, 1.0, 1.0 };
246 static const float q[4][3] = {
254 static const float qt[4][2] = {
261 /* default values for observer */
262 static const float DEF_OBS[3] = { 2.0f, 1.0f, 0.0f };
264 #define DEF_ALPHA -90.0
265 #define DEF_BETA 90.0
272 /* the mode struct, contains all per screen variables */
274 GLint WIDTH, HEIGHT; /* display dimensions */
275 GLXContext *glx_context;
277 int np; /* number of fire particles : set it through 'count' resource */
278 float eject_r; /* emission radius */
279 float dt, maxage, eject_vy, eject_vl;
280 float ridtri; /* fire particle size */
281 Bool shadows; /* misc booleans: set them through specific resources */
284 part *p; /* fire particles array */
285 rain *r; /* rain particles array */
287 XImage *gtexture; /* ground texture image bits */
288 XImage *ttexture; /* tree texture image bits */
289 GLuint groundid; /* ground texture id: GL world */
290 GLuint treeid; /* tree texture id: GL world */
291 GLuint fontbase; /* fontbase id: GL world */
293 int num_trees; /* number of trees: set it through 'trees' resource */
294 treestruct *treepos; /* trees positions: float treepos[num_trees][3] */
296 float min[3]; /* raining area */
299 float obs[3]; /* observer coordinates */
300 float dir[3]; /* view direction */
301 float v; /* observer velocity */
302 float alpha; /* observer angles */
305 trackball_state *trackball;
311 /* array of firestruct indexed by screen number */
312 static firestruct *fire = (firestruct *) NULL;
315 *-----------------------------------------------------------------------------
316 *-----------------------------------------------------------------------------
318 *-----------------------------------------------------------------------------
319 *-----------------------------------------------------------------------------
322 /* utility function for the rain particles */
323 static float gettimerain(void)
326 /* Oh yeah, *that's* portable! WTF. */
328 * I really thought clock() was standard ... EL
329 * I found this on the net:
330 * The clock() function conforms to ISO/IEC 9899:1990 (``ISO C89'')
333 static clock_t told= (clock_t)0;
342 return (0.0125 + ris/(float)CLOCKS_PER_SEC);
349 static float vrnd(void)
351 return ((float) LRAND() / (float) MAXRAND);
354 /* initialise new fire particle */
355 static void setnewpart(firestruct * fs, part * p)
362 a = vrnd() * M_PI * 2.0;
364 vinit(vi, sin(a) * fs->eject_r * vrnd(), 0.15, cos(a) * fs->eject_r * vrnd());
365 vinit(p->p[0], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
366 vinit(p->p[1], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
367 vinit(p->p[2], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
369 vinit(p->v, vi[0] * fs->eject_vl / (fs->eject_r / 2),
370 vrnd() * fs->eject_vy + fs->eject_vy / 2,
371 vi[2] * fs->eject_vl / (fs->eject_r / 2));
375 vinit4(p->c[0], 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);
378 vinit4(p->c[1], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
379 c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
380 c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
381 vinit4(p->c[2], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
382 c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
383 c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
386 /* initialise new rain particle */
387 static void setnewrain(firestruct * fs, rain * r)
391 vinit(r->acc,0.0f,-0.98f,0.0f);
392 vinit(r->vel,0.0f,0.0f,0.0f);
396 vinit(r->oldpos,fs->min[0]+(fs->max[0]-fs->min[0])*vrnd(),
397 fs->max[1]+0.2f*fs->max[1]*vrnd(),
398 fs->min[2]+(fs->max[2]-fs->min[2])*vrnd());
399 vequ(r->pos,r->oldpos);
400 vadds(r->oldpos,-(r->partLength),r->vel);
402 r->pos[1]=(fs->max[1]-fs->min[1])*vrnd()+fs->min[1];
403 r->oldpos[1]=r->pos[1]-r->partLength*r->vel[1];
406 /* set fire particle values */
407 static void setpart(firestruct * fs, part * p)
411 if (p->p[0][1] < 0.1) {
416 p->v[1] += AGRAV * fs->dt;
418 vadds(p->p[0], fs->dt, p->v);
419 vadds(p->p[1], fs->dt, p->v);
420 vadds(p->p[2], fs->dt, p->v);
424 if ((p->age) > fs->maxage) {
425 vequ(p->c[0], partcol2);
426 vequ(p->c[1], partcol2);
427 vequ(p->c[2], partcol2);
429 fact = 1.0 / fs->maxage;
430 vadds(p->c[0], fact, partcol2);
432 p->c[0][3] = fact * (fs->maxage - p->age);
434 vadds(p->c[1], fact, partcol2);
436 p->c[1][3] = fact * (fs->maxage - p->age);
438 vadds(p->c[2], fact, partcol2);
440 p->c[2][3] = fact * (fs->maxage - p->age);
444 /* set rain particle values */
445 static void setpartrain(firestruct * fs, rain * r, float dt)
449 vadds(r->vel,dt,r->acc);
450 vadds(r->pos,dt,r->vel);
452 if(r->pos[0]<fs->min[0])
453 r->pos[0]=fs->max[0]-(fs->min[0]-r->pos[0]);
454 if(r->pos[2]<fs->min[2])
455 r->pos[2]=fs->max[2]-(fs->min[2]-r->pos[2]);
457 if(r->pos[0]>fs->max[0])
458 r->pos[0]=fs->min[0]+(r->pos[0]-fs->max[0]);
459 if(r->pos[2]>fs->max[2])
460 r->pos[2]=fs->min[2]+(r->pos[2]-fs->max[2]);
462 vequ(r->oldpos,r->pos);
463 vadds(r->oldpos,-(r->partLength),r->vel);
464 if(r->pos[1]<fs->min[1])
469 static int drawtree(float x, float y, float z)
473 glTexCoord2f(0.0,0.0);
474 glVertex3f(x-1.5,y+0.0,z);
476 glTexCoord2f(1.0,0.0);
477 glVertex3f(x+1.5,y+0.0,z);
479 glTexCoord2f(1.0,1.0);
480 glVertex3f(x+1.5,y+3.0,z);
482 glTexCoord2f(0.0,1.0);
483 glVertex3f(x-1.5,y+3.0,z);
487 glTexCoord2f(0.0,0.0);
488 glVertex3f(x,y+0.0,z-1.5);
490 glTexCoord2f(1.0,0.0);
491 glVertex3f(x,y+0.0,z+1.5);
493 glTexCoord2f(1.0,1.0);
494 glVertex3f(x,y+3.0,z+1.5);
496 glTexCoord2f(0.0,1.0);
497 glVertex3f(x,y+3.0,z-1.5);
505 /* calculate observer position : modified only if trackmouse is used */
506 static void calcposobs(firestruct * fs)
508 fs->dir[0] = sin(fs->alpha * M_PI / 180.0);
510 cos(fs->alpha * M_PI / 180.0) * sin(fs->beta * M_PI / 180.0);
511 fs->dir[1] = cos(fs->beta * M_PI / 180.0);
513 fs->obs[0] += fs->v * fs->dir[0];
514 fs->obs[1] += fs->v * fs->dir[1];
515 fs->obs[2] += fs->v * fs->dir[2];
519 vinit(fs->min,fs->obs[0]-7.0f,-0.2f,fs->obs[2]-7.0f);
520 vinit(fs->max,fs->obs[0]+7.0f,8.0f,fs->obs[2]+7.0f);
525 /* initialise textures */
526 static void inittextures(ModeInfo * mi)
528 firestruct *fs = &fire[MI_SCREEN(mi)];
530 #if defined( I_HAVE_XPM )
533 glGenTextures(1, &fs->groundid);
534 #ifdef HAVE_GLBINDTEXTURE
535 glBindTexture(GL_TEXTURE_2D, fs->groundid);
536 #endif /* HAVE_GLBINDTEXTURE */
538 if ((fs->gtexture = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
539 MI_COLORMAP(mi), ground)) == None) {
540 (void) fprintf(stderr, "Error reading the ground texture.\n");
541 glDeleteTextures(1, &fs->groundid);
548 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
550 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
551 fs->gtexture->width, fs->gtexture->height, 0,
553 /* GL_UNSIGNED_BYTE, */
554 GL_UNSIGNED_INT_8_8_8_8_REV,
556 check_gl_error("texture");
558 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
559 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
561 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
562 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
564 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
568 glGenTextures(1, &fs->treeid);
569 #ifdef HAVE_GLBINDTEXTURE
570 glBindTexture(GL_TEXTURE_2D,fs->treeid);
571 #endif /* HAVE_GLBINDTEXTURE */
572 if ((fs->ttexture = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
573 MI_COLORMAP(mi), tree)) == None) {
574 (void)fprintf(stderr,"Error reading tree texture.\n");
575 glDeleteTextures(1, &fs->treeid);
582 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
583 fs->ttexture->width, fs->ttexture->height, 0,
585 /* GL_UNSIGNED_BYTE, */
586 GL_UNSIGNED_INT_8_8_8_8_REV,
588 check_gl_error("texture");
590 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
591 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
593 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
594 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
596 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
601 fs->groundid = 0; /* default textures */
604 #else /* !I_HAVE_XPM */
606 fs->groundid = 0; /* default textures */
608 #endif /* !I_HAVE_XPM */
611 /* init tree array and positions */
612 static Bool inittree(ModeInfo * mi)
614 firestruct *fs = &fire[MI_SCREEN(mi)];
618 /* allocate treepos array */
619 if ((fs->treepos = (treestruct *) malloc(fs->num_trees *
620 sizeof(treestruct))) == NULL) {
623 /* initialise positions */
624 for(i=0;i<fs->num_trees;i++)
626 fs->treepos[i].x =vrnd()*TREEOUTR*2.0-TREEOUTR;
627 fs->treepos[i].y =0.0;
628 fs->treepos[i].z =vrnd()*TREEOUTR*2.0-TREEOUTR;
629 dist=sqrt(fs->treepos[i].x *fs->treepos[i].x +fs->treepos[i].z *fs->treepos[i].z );
630 } while((dist<TREEINR) || (dist>TREEOUTR));
635 *-----------------------------------------------------------------------------
636 *-----------------------------------------------------------------------------
638 *-----------------------------------------------------------------------------
639 *-----------------------------------------------------------------------------
642 ENTRYPOINT void reshape_fire(ModeInfo * mi, int width, int height)
645 firestruct *fs = &fire[MI_SCREEN(mi)];
646 int size = MI_SIZE(mi);
648 /* Viewport is specified size if size >= MINSIZE && size < screensize */
650 fs->WIDTH = MI_WIDTH(mi);
651 fs->HEIGHT = MI_HEIGHT(mi);
652 } else if (size < MINSIZE) {
654 fs->HEIGHT = MINSIZE;
656 fs->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
657 fs->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
659 glViewport((MI_WIDTH(mi) - fs->WIDTH) / 2, (MI_HEIGHT(mi) - fs->HEIGHT) / 2, fs->WIDTH, fs->HEIGHT);
660 glMatrixMode(GL_PROJECTION);
662 gluPerspective(70.0, fs->WIDTH / (float) fs->HEIGHT, 0.1, 30.0);
664 glMatrixMode(GL_MODELVIEW);
668 static void DrawFire(ModeInfo * mi)
671 firestruct *fs = &fire[MI_SCREEN(mi)];
672 Bool wire = MI_IS_WIREFRAME(mi);
674 mi->polygon_count = 0;
676 if (do_wander && !fs->button_down_p)
680 # define SINOID(SCALE,SIZE) \
681 ((((1 + sin((fs->frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
683 x = SINOID(0.031, 0.85);
684 y = SINOID(0.017, 0.25);
685 z = SINOID(0.023, 0.85);
687 fs->obs[0] = x + DEF_OBS[0];
688 fs->obs[1] = y + DEF_OBS[1];
689 fs->obs[2] = z + DEF_OBS[2];
694 glEnable(GL_DEPTH_TEST);
701 glDepthMask(GL_TRUE);
702 glClearColor(0.5, 0.5, 0.8, 1.0); /* sky in the distance */
703 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
709 gltrackball_rotate (fs->trackball);
711 gluLookAt(fs->obs[0], fs->obs[1], fs->obs[2],
712 fs->obs[0] + fs->dir[0],
713 fs->obs[1] + fs->dir[1],
714 fs->obs[2] + fs->dir[2],
717 glEnable(GL_TEXTURE_2D);
719 /* draw ground using the computed texture */
721 glColor4f(1.0,1.0,1.0,1.0); /* white to get texture in it's true color */
722 #ifdef HAVE_GLBINDTEXTURE
723 glBindTexture(GL_TEXTURE_2D, fs->groundid);
724 #endif /* HAVE_GLBINDTEXTURE */
727 glColor4f(0.54, 0.27, 0.07, 1.0); /* untextured ground color */
729 glTexCoord2fv(qt[0]);
731 glTexCoord2fv(qt[1]);
733 glTexCoord2fv(qt[2]);
735 glTexCoord2fv(qt[3]);
740 glAlphaFunc(GL_GEQUAL, 0.9);
743 /* here do_texture IS True - and color used is white */
744 glEnable(GL_ALPHA_TEST);
745 #ifdef HAVE_GLBINDTEXTURE
746 glBindTexture(GL_TEXTURE_2D,fs->treeid);
747 #endif /* HAVE_GLBINDTEXTURE */
748 for(j=0;j<fs->num_trees;j++)
749 mi->polygon_count += drawtree(fs->treepos[j].x ,fs->treepos[j].y ,fs->treepos[j].z );
750 glDisable(GL_ALPHA_TEST);
752 glDisable(GL_TEXTURE_2D);
753 glDepthMask(GL_FALSE);
756 /* draw shadows with black color */
757 glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
758 for (j = 0; j < fs->np; j++) {
759 glColor4f(black[0], black[1], black[2], fs->p[j].c[0][3]);
760 glVertex3f(fs->p[j].p[0][0], 0.1, fs->p[j].p[0][2]);
762 glColor4f(black[0], black[1], black[2], fs->p[j].c[1][3]);
763 glVertex3f(fs->p[j].p[1][0], 0.1, fs->p[j].p[1][2]);
765 glColor4f(black[0], black[1], black[2], fs->p[j].c[2][3]);
766 glVertex3f(fs->p[j].p[2][0], 0.1, fs->p[j].p[2][2]);
772 glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
773 for (j = 0; j < fs->np; j++) {
774 /* draw particles: colors are computed in setpart */
775 glColor4fv(fs->p[j].c[0]);
776 glVertex3fv(fs->p[j].p[0]);
778 glColor4fv(fs->p[j].c[1]);
779 glVertex3fv(fs->p[j].p[1]);
781 glColor4fv(fs->p[j].c[2]);
782 glVertex3fv(fs->p[j].p[2]);
785 setpart(fs, &fs->p[j]);
789 /* draw rain particles if no fire particles */
792 float timeused = gettimerain();
793 glDisable(GL_TEXTURE_2D);
794 glShadeModel(GL_SMOOTH);
796 for (j = 0; j < NUMPART; j++) {
797 glColor4f(0.7f,0.95f,1.0f,0.0f);
798 glVertex3fv(fs->r[j].oldpos);
799 glColor4f(0.3f,0.7f,1.0f,1.0f);
800 glVertex3fv(fs->r[j].pos);
801 setpartrain(fs, &fs->r[j],timeused);
805 glShadeModel(GL_FLAT);
808 glDisable(GL_TEXTURE_2D);
809 glDisable(GL_ALPHA_TEST);
810 glDisable(GL_DEPTH_TEST);
813 /* manage framerate display */
814 if (MI_IS_FPS(mi)) do_fps (mi);
819 static Bool Init(ModeInfo * mi)
822 firestruct *fs = &fire[MI_SCREEN(mi)];
824 /* default settings */
825 fs->eject_r = 0.1 + NRAND(10) * 0.03;
829 fs->ridtri = 0.1 + NRAND(10) * 0.005;
830 fs->maxage = 1.0 / fs->dt;
831 vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
833 fs->alpha = DEF_ALPHA;
836 /* initialise texture stuff */
841 fs->ttexture = (XImage*) NULL;
842 fs->gtexture = (XImage*) NULL;
845 if (MI_IS_DEBUG(mi)) {
846 (void) fprintf(stderr,
847 "%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
851 fs->fog ? "on" : "off",
852 fs->shadows ? "on" : "off",
853 fs->eject_r, fs->ridtri);
856 glShadeModel(GL_FLAT);
857 glEnable(GL_DEPTH_TEST);
859 /* makes particles blend with background */
860 if (!MI_IS_WIREFRAME(mi)||(!fs->np))
863 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
868 glFogi(GL_FOG_MODE, GL_EXP);
869 glFogfv(GL_FOG_COLOR, fogcolor);
870 glFogf(GL_FOG_DENSITY, 0.03);
871 glHint(GL_FOG_HINT, GL_NICEST);
873 /* initialise particles and trees */
874 for (i = 0; i < fs->np; i++) {
875 setnewpart(fs, &(fs->p[i]));
883 /* if no fire particles then initialise rain particles */
886 vinit(fs->min,-7.0f,-0.2f,-7.0f);
887 vinit(fs->max,7.0f,8.0f,7.0f);
888 for (i = 0; i < NUMPART; i++) {
889 setnewrain(fs, &(fs->r[i]));
897 *-----------------------------------------------------------------------------
898 *-----------------------------------------------------------------------------
900 *-----------------------------------------------------------------------------
901 *-----------------------------------------------------------------------------
906 free_fire(firestruct *fs)
908 if (mode_font != None && fs->fontbase != None) {
909 glDeleteLists(fs->fontbase, mode_font->max_char_or_byte2 -
910 mode_font->min_char_or_byte2 + 1);
915 (void) free((void *) fs->p);
916 fs->p = (part *) NULL;
919 (void) free((void *) fs->r);
920 fs->r = (rain *) NULL;
922 if (fs->treepos != NULL) {
923 (void) free((void *) fs->treepos);
924 fs->treepos = (treestruct *) NULL;
926 if (fs->ttexture != None) {
927 glDeleteTextures(1, &fs->treeid);
928 XDestroyImage(fs->ttexture);
931 if (fs->gtexture != None) {
932 glDeleteTextures(1, &fs->groundid);
933 XDestroyImage(fs->gtexture);
939 *-----------------------------------------------------------------------------
940 * Initialize fire. Called each time the window changes.
941 *-----------------------------------------------------------------------------
945 init_fire(ModeInfo * mi)
949 /* allocate the main fire table if needed */
951 if ((fire = (firestruct *) calloc(MI_NUM_SCREENS(mi),
952 sizeof(firestruct))) == NULL)
956 /* initialise the per screen fire structure */
957 fs = &fire[MI_SCREEN(mi)];
958 fs->np = MI_COUNT(mi);
960 fs->shadows = do_shadows;
961 /* initialise fire particles if any */
962 if ((fs->np)&&(fs->p == NULL)) {
963 if ((fs->p = (part *) calloc(fs->np, sizeof(part))) == NULL) {
968 else if (fs->r == NULL) {
969 /* initialise rain particles if no fire particles */
970 if ((fs->r = (rain *) calloc(NUMPART, sizeof(part))) == NULL) {
976 /* check tree number */
978 fs->num_trees = (num_trees<MAX_TREES)?num_trees:MAX_TREES;
982 fs->trackball = gltrackball_init ();
985 if ((fs->glx_context = init_GL(mi)) != NULL) {
988 Reshape(mi); /* xlock mode */
990 reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
992 glDrawBuffer(GL_BACK);
1003 *-----------------------------------------------------------------------------
1004 * Called by the mainline code periodically to update the display.
1005 *-----------------------------------------------------------------------------
1007 ENTRYPOINT void draw_fire(ModeInfo * mi)
1009 firestruct *fs = &fire[MI_SCREEN(mi)];
1011 Display *display = MI_DISPLAY(mi);
1012 Window window = MI_WINDOW(mi);
1014 MI_IS_DRAWN(mi) = True;
1016 if (!fs->glx_context)
1019 glXMakeCurrent(display, window, *(fs->glx_context));
1022 Reshape(mi); /* xlock mode */
1024 reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
1028 glXSwapBuffers(display, window);
1033 *-----------------------------------------------------------------------------
1034 * The display is being taken away from us. Free up malloc'ed
1035 * memory and X resources that we've alloc'ed. Only called
1036 * once, we must zap everything for every screen.
1037 *-----------------------------------------------------------------------------
1040 ENTRYPOINT void release_fire(ModeInfo * mi)
1044 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
1045 free_fire(&fire[screen]);
1046 (void) free((void *) fire);
1047 fire = (firestruct *) NULL;
1049 if (mode_font != None)
1051 /* only free-ed when there are no more screens used */
1052 XFreeFont(MI_DISPLAY(mi), mode_font);
1059 fire_handle_event (ModeInfo *mi, XEvent *event)
1061 firestruct *fs = &fire[MI_SCREEN(mi)];
1063 if (event->xany.type == ButtonPress &&
1064 event->xbutton.button == Button1)
1066 fs->button_down_p = True;
1067 event->xbutton.x = MI_WIDTH(mi) - event->xbutton.x; /* kludge! */
1068 event->xbutton.y = MI_HEIGHT(mi) - event->xbutton.y;
1069 gltrackball_start (fs->trackball,
1070 event->xbutton.x, event->xbutton.y,
1071 MI_WIDTH (mi), MI_HEIGHT (mi));
1074 else if (event->xany.type == ButtonRelease &&
1075 event->xbutton.button == Button1)
1077 fs->button_down_p = False;
1080 else if (event->xany.type == ButtonPress &&
1081 (event->xbutton.button == Button4 ||
1082 event->xbutton.button == Button5 ||
1083 event->xbutton.button == Button6 ||
1084 event->xbutton.button == Button7))
1086 gltrackball_mousewheel (fs->trackball, event->xbutton.button, 5,
1087 !!event->xbutton.state);
1090 else if (event->xany.type == MotionNotify &&
1093 event->xmotion.x = MI_WIDTH(mi) - event->xmotion.x; /* kludge! */
1094 event->xmotion.y = MI_HEIGHT(mi) - event->xmotion.y;
1095 gltrackball_track (fs->trackball,
1096 event->xmotion.x, event->xmotion.y,
1097 MI_WIDTH (mi), MI_HEIGHT (mi));
1106 ENTRYPOINT void change_fire(ModeInfo * mi)
1108 firestruct *fs = &fire[MI_SCREEN(mi)];
1110 if (!fs->glx_context)
1113 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(fs->glx_context));
1115 /* if available, randomly change some values */
1117 fs->fog = LRAND() & 1;
1119 fs->shadows = LRAND() & 1;
1120 /* reset observer position */
1122 vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
1124 /* particle randomisation */
1125 fs->eject_r = 0.1 + NRAND(10) * 0.03;
1126 fs->ridtri = 0.1 + NRAND(10) * 0.005;
1128 if (MI_IS_DEBUG(mi)) {
1129 (void) fprintf(stderr,
1130 "%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
1134 fs->fog ? "on" : "off",
1135 fs->shadows ? "on" : "off",
1136 fs->eject_r, fs->ridtri);
1139 #endif /* !STANDALONE */
1141 XSCREENSAVER_MODULE_2 ("GLForestFire", glforestfire, fire)
1143 #endif /* MODE_fire */