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 PROGCLASS "Fire"
79
#define HACK_INIT init_fire
80
#define HACK_DRAW draw_fire
81
#define HACK_RESHAPE reshape_fire
82
#define fire_opts xlockmore_opts
83
#define DEFAULTS "*delay: 10000 \n" \
86
"*showFPS: False \n" \
87
"*wireframe: False \n" \
90
#include "xlockmore.h" /* from the xscreensaver distribution */
91
#else /* !STANDALONE */
92
#include "xlock.h" /* from the xlockmore distribution */
94
#endif /* !STANDALONE */
104
#if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( HAVE_XPM )
105
/* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
106
#include "xpm-ximage.h"
110
#include "../images/ground.xpm"
111
#include "../images/tree.xpm"
112
#else /* !STANDALONE */
113
#include "pixmaps/ground.xpm"
114
#include "pixmaps/tree.xpm"
115
#endif /* !STANDALONE */
116
#endif /* HAVE_XPM */
118
/* vector utility macros */
119
#define vinit(a,i,j,k) {\
125
#define vinit4(a,i,j,k,w) {\
132
#define vadds(a,dt,b) {\
133
(a)[0]+=(dt)*(b)[0];\
134
(a)[1]+=(dt)*(b)[1];\
135
(a)[2]+=(dt)*(b)[2];\
144
#define vinter(a,dt,b,c) {\
145
(a)[0]=(dt)*(b)[0]+(1.0-dt)*(c)[0];\
146
(a)[1]=(dt)*(b)[1]+(1.0-dt)*(c)[1];\
147
(a)[2]=(dt)*(b)[2]+(1.0-dt)*(c)[2];\
150
#define clamp(a) ((a) < 0.0 ? 0.0 : ((a) < 1.0 ? (a) : 1.0))
153
(v)[0]=clamp((v)[0]);\
154
(v)[1]=clamp((v)[1]);\
155
(v)[2]=clamp((v)[2]);\
158
/* Manage option vars */
159
#define DEF_TEXTURE "True"
160
#define DEF_FOG "False"
161
#define DEF_SHADOWS "True"
162
#define DEF_FRAMERATE "False"
163
#define DEF_TRACKMOUSE "False"
164
#define DEF_WANDER "True"
165
#define DEF_TREES "5"
167
static Bool do_texture;
169
static Bool do_shadows;
170
static Bool do_trackmouse;
171
static Bool do_wander;
172
static int num_trees;
173
static int frame = 0;
174
static XFontStruct *mode_font = None;
176
static XrmOptionDescRec opts[] = {
177
{"-texture", ".fire.texture", XrmoptionNoArg, "on"},
178
{"+texture", ".fire.texture", XrmoptionNoArg, "off"},
179
{"-fog", ".fire.fog", XrmoptionNoArg, "on"},
180
{"+fog", ".fire.fog", XrmoptionNoArg, "off"},
181
{"-shadows", ".fire.shadows", XrmoptionNoArg, "on"},
182
{"+shadows", ".fire.shadows", XrmoptionNoArg, "off"},
183
{"-trackmouse", ".fire.trackmouse", XrmoptionNoArg, "on"},
184
{"+trackmouse", ".fire.trackmouse", XrmoptionNoArg, "off"},
185
{"-wander", ".fire.wander", XrmoptionNoArg, "on"},
186
{"+wander", ".fire.wander", XrmoptionNoArg, "off"},
187
{"-trees", ".fire.trees", XrmoptionSepArg, 0},
188
{"-rain", ".fire.count", XrmoptionNoArg, "0"},
192
static argtype vars[] = {
193
{&do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
194
{&do_fog, "fog", "Fog", DEF_FOG, t_Bool},
195
{&do_shadows, "shadows", "Shadows", DEF_SHADOWS, t_Bool},
196
{&do_trackmouse, "trackmouse", "TrackMouse", DEF_TRACKMOUSE, t_Bool},
197
{&do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
198
{&num_trees, "trees", "Trees", DEF_TREES, t_Int},
201
static OptionStruct desc[] = {
202
{"-/+texture", "turn on/off texturing"},
203
{"-/+fog", "turn on/off fog"},
204
{"-/+shadows", "turn on/off shadows"},
205
{"-/+trackmouse", "turn on/off the tracking of the mouse"},
206
{"-/+wander", "turn on/off wandering"},
207
{"-trees num", "number of trees (0 disables)"},
210
ModeSpecOpt fire_opts =
211
{ sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
214
ModStruct fire_description =
215
{ "fire", "init_fire", "draw_fire", "release_fire",
216
"draw_fire", "change_fire", (char *) NULL, &fire_opts,
217
10000, 800, 1, 400, 64, 1.0, "",
218
"Shows a 3D fire-like image", 0, NULL
220
#endif /* USE_MODULES */
223
#define TREEINR 2.5 /* tree min distance */
224
#define TREEOUTR 8.0 /* tree max distance */
225
#define FRAME 50 /* frame count interval */
226
#define DIMP 20.0 /* dimension of ground */
227
#define DIMTP 16.0 /* dimension of ground texture */
229
#define RIDCOL 0.4 /* factor for color blending */
231
#define AGRAV -9.8 /* gravity */
233
#define NUMPART 7500 /* rain particles */
235
/* fire particle struct */
243
/* rain particle struct */
254
static float black[3] = { 0.0, 0.0, 0.0 }; /* shadow color */
255
static float partcol1[3] = { 1.0, 0.2, 0.0 }; /* initial color: red-ish */
256
static float partcol2[3] = { 1.0, 1.0, 0.0 }; /* blending color: yellow-ish */
257
static float fogcolor[4] = { 0.9, 0.9, 1.0, 1.0 };
260
static float q[4][3] = {
268
static float qt[4][2] = {
275
/* default values for observer */
276
static const float DEF_OBS[3] = { 2.0f, 1.0f, 0.0f };
278
#define DEF_ALPHA -90.0
279
#define DEF_BETA 90.0
286
/* the mode struct, contains all per screen variables */
288
GLint WIDTH, HEIGHT; /* display dimensions */
289
GLXContext *glx_context;
291
int np; /* number of fire particles : set it through 'count' resource */
292
float eject_r; /* emission radius */
293
float dt, maxage, eject_vy, eject_vl;
294
float ridtri; /* fire particle size */
295
Bool shadows; /* misc booleans: set them through specific resources */
298
part *p; /* fire particles array */
299
rain *r; /* rain particles array */
301
XImage *gtexture; /* ground texture image bits */
302
XImage *ttexture; /* tree texture image bits */
303
GLuint groundid; /* ground texture id: GL world */
304
GLuint treeid; /* tree texture id: GL world */
305
GLuint fontbase; /* fontbase id: GL world */
307
int num_trees; /* number of trees: set it through 'trees' resource */
308
treestruct *treepos; /* trees positions: float treepos[num_trees][3] */
310
float min[3]; /* raining area */
313
float obs[3]; /* observer coordinates */
314
float dir[3]; /* view direction */
315
float v; /* observer velocity */
316
float alpha; /* observer angles */
320
/* array of firestruct indexed by screen number */
321
static firestruct *fire = (firestruct *) NULL;
324
*-----------------------------------------------------------------------------
325
*-----------------------------------------------------------------------------
327
*-----------------------------------------------------------------------------
328
*-----------------------------------------------------------------------------
331
/* utility function for the rain particles */
332
static float gettimerain(void)
335
/* Oh yeah, *that's* portable! WTF. */
337
* I really thought clock() was standard ... EL
338
* I found this on the net:
339
* The clock() function conforms to ISO/IEC 9899:1990 (``ISO C89'')
342
static clock_t told= (clock_t)0;
351
return (0.0125 + ris/(float)CLOCKS_PER_SEC);
358
static float vrnd(void)
360
return ((float) LRAND() / (float) MAXRAND);
363
/* initialise new fire particle */
364
static void setnewpart(firestruct * fs, part * p)
370
a = vrnd() * M_PI * 2.0;
372
vinit(vi, sin(a) * fs->eject_r * vrnd(), 0.15, cos(a) * fs->eject_r * vrnd());
373
vinit(p->p[0], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
374
vinit(p->p[1], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
375
vinit(p->p[2], vi[0] + vrnd() * fs->ridtri, vi[1] + vrnd() * fs->ridtri, vi[2] + vrnd() * fs->ridtri);
377
vinit(p->v, vi[0] * fs->eject_vl / (fs->eject_r / 2),
378
vrnd() * fs->eject_vy + fs->eject_vy / 2,
379
vi[2] * fs->eject_vl / (fs->eject_r / 2));
383
vinit4(p->c[0], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
384
c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
385
c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
386
vinit4(p->c[1], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
387
c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
388
c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
389
vinit4(p->c[2], c[0] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
390
c[1] * ((1.0 - RIDCOL) + vrnd() * RIDCOL),
391
c[2] * ((1.0 - RIDCOL) + vrnd() * RIDCOL), 1.0);
394
/* initialise new rain particle */
395
static void setnewrain(firestruct * fs, rain * r)
399
vinit(r->acc,0.0f,-0.98f,0.0f);
400
vinit(r->vel,0.0f,0.0f,0.0f);
404
vinit(r->oldpos,fs->min[0]+(fs->max[0]-fs->min[0])*vrnd(),
405
fs->max[1]+0.2f*fs->max[1]*vrnd(),
406
fs->min[2]+(fs->max[2]-fs->min[2])*vrnd());
407
vequ(r->pos,r->oldpos);
408
vadds(r->oldpos,-(r->partLength),r->vel);
410
r->pos[1]=(fs->max[1]-fs->min[1])*vrnd()+fs->min[1];
411
r->oldpos[1]=r->pos[1]-r->partLength*r->vel[1];
414
/* set fire particle values */
415
static void setpart(firestruct * fs, part * p)
419
if (p->p[0][1] < 0.1) {
424
p->v[1] += AGRAV * fs->dt;
426
vadds(p->p[0], fs->dt, p->v);
427
vadds(p->p[1], fs->dt, p->v);
428
vadds(p->p[2], fs->dt, p->v);
432
if ((p->age) > fs->maxage) {
433
vequ(p->c[0], partcol2);
434
vequ(p->c[1], partcol2);
435
vequ(p->c[2], partcol2);
437
fact = 1.0 / fs->maxage;
438
vadds(p->c[0], fact, partcol2);
440
p->c[0][3] = fact * (fs->maxage - p->age);
442
vadds(p->c[1], fact, partcol2);
444
p->c[1][3] = fact * (fs->maxage - p->age);
446
vadds(p->c[2], fact, partcol2);
448
p->c[2][3] = fact * (fs->maxage - p->age);
452
/* set rain particle values */
453
static void setpartrain(firestruct * fs, rain * r, float dt)
457
vadds(r->vel,dt,r->acc);
458
vadds(r->pos,dt,r->vel);
460
if(r->pos[0]<fs->min[0])
461
r->pos[0]=fs->max[0]-(fs->min[0]-r->pos[0]);
462
if(r->pos[2]<fs->min[2])
463
r->pos[2]=fs->max[2]-(fs->min[2]-r->pos[2]);
465
if(r->pos[0]>fs->max[0])
466
r->pos[0]=fs->min[0]+(r->pos[0]-fs->max[0]);
467
if(r->pos[2]>fs->max[2])
468
r->pos[2]=fs->min[2]+(r->pos[2]-fs->max[2]);
470
vequ(r->oldpos,r->pos);
471
vadds(r->oldpos,-(r->partLength),r->vel);
472
if(r->pos[1]<fs->min[1])
477
static void drawtree(float x, float y, float z)
480
glTexCoord2f(0.0,0.0);
481
glVertex3f(x-1.5,y+0.0,z);
483
glTexCoord2f(1.0,0.0);
484
glVertex3f(x+1.5,y+0.0,z);
486
glTexCoord2f(1.0,1.0);
487
glVertex3f(x+1.5,y+3.0,z);
489
glTexCoord2f(0.0,1.0);
490
glVertex3f(x-1.5,y+3.0,z);
493
glTexCoord2f(0.0,0.0);
494
glVertex3f(x,y+0.0,z-1.5);
496
glTexCoord2f(1.0,0.0);
497
glVertex3f(x,y+0.0,z+1.5);
499
glTexCoord2f(1.0,1.0);
500
glVertex3f(x,y+3.0,z+1.5);
502
glTexCoord2f(0.0,1.0);
503
glVertex3f(x,y+3.0,z-1.5);
509
/* calculate observer position : modified only if trackmouse is used */
510
static void calcposobs(firestruct * fs)
512
fs->dir[0] = sin(fs->alpha * M_PI / 180.0);
514
cos(fs->alpha * M_PI / 180.0) * sin(fs->beta * M_PI / 180.0);
515
fs->dir[1] = cos(fs->beta * M_PI / 180.0);
517
fs->obs[0] += fs->v * fs->dir[0];
518
fs->obs[1] += fs->v * fs->dir[1];
519
fs->obs[2] += fs->v * fs->dir[2];
523
vinit(fs->min,fs->obs[0]-7.0f,-0.2f,fs->obs[2]-7.0f);
524
vinit(fs->max,fs->obs[0]+7.0f,8.0f,fs->obs[2]+7.0f);
528
/* track the mouse in a joystick manner : not perfect but it works */
529
static void trackmouse(ModeInfo * mi)
531
firestruct *fs = &fire[MI_SCREEN(mi)];
532
/* we keep static values (not per screen) for the mouse stuff: in general you have only one mouse :-> */
533
static int max[2] = { 0, 0 };
534
static int min[2] = { 0x7fffffff, 0x7fffffff }, center[2];
539
(void) XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
540
&r, &c, &rx, &ry, &cx, &cy, &m);
546
center[0] = (max[0] + min[0]) / 2;
552
center[1] = (max[1] + min[1]) / 2;
554
if (fabs(center[0] - (float) cx) > 0.1 * (max[0] - min[0]))
555
fs->alpha += 2.5 * (center[0] - (float) cx) / (max[0] - min[0]);
556
if (fabs(center[1] - (float) cy) > 0.1 * (max[1] - min[1]))
557
fs->beta += 2.5 * (center[1] - (float) cy) / (max[1] - min[1]);
559
/* oops: can't get those buttons */
567
/* initialise textures */
568
static void inittextures(ModeInfo * mi)
570
firestruct *fs = &fire[MI_SCREEN(mi)];
572
#if defined( I_HAVE_XPM )
575
glGenTextures(1, &fs->groundid);
576
#ifdef HAVE_GLBINDTEXTURE
577
glBindTexture(GL_TEXTURE_2D, fs->groundid);
578
#endif /* HAVE_GLBINDTEXTURE */
580
if ((fs->gtexture = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
581
MI_COLORMAP(mi), ground)) == None) {
582
(void) fprintf(stderr, "Error reading the ground texture.\n");
583
glDeleteTextures(1, &fs->groundid);
590
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
592
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
593
fs->gtexture->width, fs->gtexture->height, 0,
595
/* GL_UNSIGNED_BYTE, */
596
GL_UNSIGNED_INT_8_8_8_8_REV,
598
check_gl_error("texture");
600
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
601
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
603
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
604
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
606
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
610
glGenTextures(1, &fs->treeid);
611
#ifdef HAVE_GLBINDTEXTURE
612
glBindTexture(GL_TEXTURE_2D,fs->treeid);
613
#endif /* HAVE_GLBINDTEXTURE */
614
if ((fs->ttexture = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
615
MI_COLORMAP(mi), tree)) == None) {
616
(void)fprintf(stderr,"Error reading tree texture.\n");
617
glDeleteTextures(1, &fs->treeid);
624
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
625
fs->ttexture->width, fs->ttexture->height, 0,
627
/* GL_UNSIGNED_BYTE, */
628
GL_UNSIGNED_INT_8_8_8_8_REV,
630
check_gl_error("texture");
632
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
633
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
635
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
636
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
638
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
643
fs->groundid = 0; /* default textures */
646
#else /* !I_HAVE_XPM */
648
fs->groundid = 0; /* default textures */
650
#endif /* !I_HAVE_XPM */
653
/* init tree array and positions */
654
static Bool inittree(ModeInfo * mi)
656
firestruct *fs = &fire[MI_SCREEN(mi)];
660
/* allocate treepos array */
661
if ((fs->treepos = (treestruct *) malloc(fs->num_trees *
662
sizeof(treestruct))) == NULL) {
665
/* initialise positions */
666
for(i=0;i<fs->num_trees;i++)
668
fs->treepos[i].x =vrnd()*TREEOUTR*2.0-TREEOUTR;
669
fs->treepos[i].y =0.0;
670
fs->treepos[i].z =vrnd()*TREEOUTR*2.0-TREEOUTR;
671
dist=sqrt(fs->treepos[i].x *fs->treepos[i].x +fs->treepos[i].z *fs->treepos[i].z );
672
} while((dist<TREEINR) || (dist>TREEOUTR));
677
*-----------------------------------------------------------------------------
678
*-----------------------------------------------------------------------------
680
*-----------------------------------------------------------------------------
681
*-----------------------------------------------------------------------------
685
static void Reshape(ModeInfo * mi)
687
void reshape_fire(ModeInfo * mi, int width, int height)
691
firestruct *fs = &fire[MI_SCREEN(mi)];
692
int size = MI_SIZE(mi);
694
/* Viewport is specified size if size >= MINSIZE && size < screensize */
696
fs->WIDTH = MI_WIDTH(mi);
697
fs->HEIGHT = MI_HEIGHT(mi);
698
} else if (size < MINSIZE) {
700
fs->HEIGHT = MINSIZE;
702
fs->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
703
fs->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
705
glViewport((MI_WIDTH(mi) - fs->WIDTH) / 2, (MI_HEIGHT(mi) - fs->HEIGHT) / 2, fs->WIDTH, fs->HEIGHT);
706
glMatrixMode(GL_PROJECTION);
708
gluPerspective(70.0, fs->WIDTH / (float) fs->HEIGHT, 0.1, 30.0);
710
glMatrixMode(GL_MODELVIEW);
714
static void DrawFire(ModeInfo * mi)
717
firestruct *fs = &fire[MI_SCREEN(mi)];
718
Bool wire = MI_IS_WIREFRAME(mi);
720
if (do_trackmouse && !MI_IS_ICONIC(mi))
727
# define SINOID(SCALE,SIZE) \
728
((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
730
x = SINOID(0.031, 0.85);
731
y = SINOID(0.017, 0.25);
732
z = SINOID(0.023, 0.85);
734
fs->obs[0] = x + DEF_OBS[0];
735
fs->obs[1] = y + DEF_OBS[1];
736
fs->obs[2] = z + DEF_OBS[2];
741
glEnable(GL_DEPTH_TEST);
748
glDepthMask(GL_TRUE);
749
glClearColor(0.5, 0.5, 0.8, 1.0); /* sky in the distance */
750
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
754
gluLookAt(fs->obs[0], fs->obs[1], fs->obs[2],
755
fs->obs[0] + fs->dir[0], fs->obs[1] + fs->dir[1],
756
fs->obs[2] + fs->dir[2], 0.0, 1.0, 0.0);
759
glEnable(GL_TEXTURE_2D);
761
/* draw ground using the computed texture */
763
glColor4f(1.0,1.0,1.0,1.0); /* white to get texture in it's true color */
764
#ifdef HAVE_GLBINDTEXTURE
765
glBindTexture(GL_TEXTURE_2D, fs->groundid);
766
#endif /* HAVE_GLBINDTEXTURE */
769
glColor4f(0.54, 0.27, 0.07, 1.0); /* untextured ground color */
771
glTexCoord2fv(qt[0]);
773
glTexCoord2fv(qt[1]);
775
glTexCoord2fv(qt[2]);
777
glTexCoord2fv(qt[3]);
781
glAlphaFunc(GL_GEQUAL, 0.9);
784
/* here do_texture IS True - and color used is white */
785
glEnable(GL_ALPHA_TEST);
786
#ifdef HAVE_GLBINDTEXTURE
787
glBindTexture(GL_TEXTURE_2D,fs->treeid);
788
#endif /* HAVE_GLBINDTEXTURE */
789
for(j=0;j<fs->num_trees;j++)
790
drawtree(fs->treepos[j].x ,fs->treepos[j].y ,fs->treepos[j].z );
791
glDisable(GL_ALPHA_TEST);
793
glDisable(GL_TEXTURE_2D);
794
glDepthMask(GL_FALSE);
797
/* draw shadows with black color */
798
glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
799
for (j = 0; j < fs->np; j++) {
800
glColor4f(black[0], black[1], black[2], fs->p[j].c[0][3]);
801
glVertex3f(fs->p[j].p[0][0], 0.1, fs->p[j].p[0][2]);
803
glColor4f(black[0], black[1], black[2], fs->p[j].c[1][3]);
804
glVertex3f(fs->p[j].p[1][0], 0.1, fs->p[j].p[1][2]);
806
glColor4f(black[0], black[1], black[2], fs->p[j].c[2][3]);
807
glVertex3f(fs->p[j].p[2][0], 0.1, fs->p[j].p[2][2]);
812
glBegin(wire ? GL_LINE_STRIP : GL_TRIANGLES);
813
for (j = 0; j < fs->np; j++) {
814
/* draw particles: colors are computed in setpart */
815
glColor4fv(fs->p[j].c[0]);
816
glVertex3fv(fs->p[j].p[0]);
818
glColor4fv(fs->p[j].c[1]);
819
glVertex3fv(fs->p[j].p[1]);
821
glColor4fv(fs->p[j].c[2]);
822
glVertex3fv(fs->p[j].p[2]);
824
setpart(fs, &fs->p[j]);
828
/* draw rain particles if no fire particles */
831
float timeused = gettimerain();
832
glDisable(GL_TEXTURE_2D);
833
glShadeModel(GL_SMOOTH);
835
for (j = 0; j < NUMPART; j++) {
836
glColor4f(0.7f,0.95f,1.0f,0.0f);
837
glVertex3fv(fs->r[j].oldpos);
838
glColor4f(0.3f,0.7f,1.0f,1.0f);
839
glVertex3fv(fs->r[j].pos);
840
setpartrain(fs, &fs->r[j],timeused);
843
glShadeModel(GL_FLAT);
846
glDisable(GL_TEXTURE_2D);
847
glDisable(GL_ALPHA_TEST);
848
glDisable(GL_DEPTH_TEST);
851
/* manage framerate display */
852
if (MI_IS_FPS(mi)) do_fps (mi);
857
static Bool Init(ModeInfo * mi)
860
firestruct *fs = &fire[MI_SCREEN(mi)];
862
/* default settings */
863
fs->eject_r = 0.1 + NRAND(10) * 0.03;
867
fs->ridtri = 0.1 + NRAND(10) * 0.005;
868
fs->maxage = 1.0 / fs->dt;
869
vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
871
fs->alpha = DEF_ALPHA;
874
/* initialise texture stuff */
879
fs->ttexture = (XImage*) NULL;
880
fs->gtexture = (XImage*) NULL;
883
if (MI_IS_DEBUG(mi)) {
884
(void) fprintf(stderr,
885
"%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
889
fs->fog ? "on" : "off",
890
fs->shadows ? "on" : "off",
891
fs->eject_r, fs->ridtri);
894
glShadeModel(GL_FLAT);
895
glEnable(GL_DEPTH_TEST);
897
/* makes particles blend with background */
898
if (!MI_IS_WIREFRAME(mi)||(!fs->np))
901
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
906
glFogi(GL_FOG_MODE, GL_EXP);
907
glFogfv(GL_FOG_COLOR, fogcolor);
908
glFogf(GL_FOG_DENSITY, 0.03);
909
glHint(GL_FOG_HINT, GL_NICEST);
911
/* initialise particles and trees */
912
for (i = 0; i < fs->np; i++) {
913
setnewpart(fs, &(fs->p[i]));
921
/* if no fire particles then initialise rain particles */
924
vinit(fs->min,-7.0f,-0.2f,-7.0f);
925
vinit(fs->max,7.0f,8.0f,7.0f);
926
for (i = 0; i < NUMPART; i++) {
927
setnewrain(fs, &(fs->r[i]));
935
*-----------------------------------------------------------------------------
936
*-----------------------------------------------------------------------------
938
*-----------------------------------------------------------------------------
939
*-----------------------------------------------------------------------------
944
free_fire(firestruct *fs)
946
if (mode_font != None && fs->fontbase != None) {
947
glDeleteLists(fs->fontbase, mode_font->max_char_or_byte2 -
948
mode_font->min_char_or_byte2 + 1);
953
(void) free((void *) fs->p);
954
fs->p = (part *) NULL;
957
(void) free((void *) fs->r);
958
fs->r = (rain *) NULL;
960
if (fs->treepos != NULL) {
961
(void) free((void *) fs->treepos);
962
fs->treepos = (treestruct *) NULL;
964
if (fs->ttexture != None) {
965
glDeleteTextures(1, &fs->treeid);
966
XDestroyImage(fs->ttexture);
969
if (fs->gtexture != None) {
970
glDeleteTextures(1, &fs->groundid);
971
XDestroyImage(fs->gtexture);
977
*-----------------------------------------------------------------------------
978
* Initialize fire. Called each time the window changes.
979
*-----------------------------------------------------------------------------
983
init_fire(ModeInfo * mi)
987
/* allocate the main fire table if needed */
989
if ((fire = (firestruct *) calloc(MI_NUM_SCREENS(mi),
990
sizeof(firestruct))) == NULL)
994
/* initialise the per screen fire structure */
995
fs = &fire[MI_SCREEN(mi)];
996
fs->np = MI_COUNT(mi);
998
fs->shadows = do_shadows;
999
/* initialise fire particles if any */
1000
if ((fs->np)&&(fs->p == NULL)) {
1001
if ((fs->p = (part *) calloc(fs->np, sizeof(part))) == NULL) {
1006
else if (fs->r == NULL) {
1007
/* initialise rain particles if no fire particles */
1008
if ((fs->r = (rain *) calloc(NUMPART, sizeof(part))) == NULL) {
1014
/* check tree number */
1016
fs->num_trees = (num_trees<MAX_TREES)?num_trees:MAX_TREES;
1020
/* check wander/trackmouse */
1021
if (do_trackmouse && do_wander) do_wander = 0;
1023
/* xlock GL stuff */
1024
if ((fs->glx_context = init_GL(mi)) != NULL) {
1027
Reshape(mi); /* xlock mode */
1029
reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
1031
glDrawBuffer(GL_BACK);
1042
*-----------------------------------------------------------------------------
1043
* Called by the mainline code periodically to update the display.
1044
*-----------------------------------------------------------------------------
1046
void draw_fire(ModeInfo * mi)
1048
firestruct *fs = &fire[MI_SCREEN(mi)];
1050
Display *display = MI_DISPLAY(mi);
1051
Window window = MI_WINDOW(mi);
1053
MI_IS_DRAWN(mi) = True;
1055
if (!fs->glx_context)
1058
glXMakeCurrent(display, window, *(fs->glx_context));
1061
Reshape(mi); /* xlock mode */
1063
reshape_fire(mi,MI_WIDTH(mi),MI_HEIGHT(mi)); /* xscreensaver mode */
1067
glXSwapBuffers(display, window);
1072
*-----------------------------------------------------------------------------
1073
* The display is being taken away from us. Free up malloc'ed
1074
* memory and X resources that we've alloc'ed. Only called
1075
* once, we must zap everything for every screen.
1076
*-----------------------------------------------------------------------------
1079
void release_fire(ModeInfo * mi)
1083
for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
1084
free_fire(&fire[screen]);
1085
(void) free((void *) fire);
1086
fire = (firestruct *) NULL;
1088
if (mode_font != None)
1090
/* only free-ed when there are no more screens used */
1091
XFreeFont(MI_DISPLAY(mi), mode_font);
1097
void change_fire(ModeInfo * mi)
1099
firestruct *fs = &fire[MI_SCREEN(mi)];
1101
if (!fs->glx_context)
1104
glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(fs->glx_context));
1106
/* if available, randomly change some values */
1108
fs->fog = LRAND() & 1;
1110
fs->shadows = LRAND() & 1;
1111
/* reset observer position */
1113
vinit(fs->obs, DEF_OBS[0], DEF_OBS[1], DEF_OBS[2]);
1115
/* particle randomisation */
1116
fs->eject_r = 0.1 + NRAND(10) * 0.03;
1117
fs->ridtri = 0.1 + NRAND(10) * 0.005;
1119
if (MI_IS_DEBUG(mi)) {
1120
(void) fprintf(stderr,
1121
"%s:\n\tnum_part=%d\n\ttrees=%d\n\tfog=%s\n\tshadows=%s\n\teject_r=%.3f\n\tridtri=%.3f\n",
1125
fs->fog ? "on" : "off",
1126
fs->shadows ? "on" : "off",
1127
fs->eject_r, fs->ridtri);
1130
#endif /* MODE_fire */