http://slackware.bholcomb.com/slackware/slackware-11.0/source/xap/xscreensaver/xscree...
[xscreensaver] / hacks / galaxy.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* galaxy --- spinning galaxies */
3 /* #include<math.h>*/
4 #if 0
5 static const char sccsid[] = "@(#)galaxy.c 4.04 97/07/28 xlockmore";
6 #endif
7
8 /* Originally done by Uli Siegmund <uli@wombat.okapi.sub.org> on Amiga
9  *   for EGS in Cluster
10  * Port from Cluster/EGS to C/Intuition by Harald Backert
11  * Port to X11 and incorporation into xlockmore by Hubert Feyrer
12  *   <hubert.feyrer@rz.uni-regensburg.de>
13  *
14  * Permission to use, copy, modify, and distribute this software and its
15  * documentation for any purpose and without fee is hereby granted,
16  * provided that the above copyright notice appear in all copies and that
17  * both that copyright notice and this permission notice appear in
18  * supporting documentation.
19  *
20  * This file is provided AS IS with no warranties of any kind.  The author
21  * shall have no liability with respect to the infringement of copyrights,
22  * trade secrets or any patents by this file or any part thereof.  In no
23  * event will the author be liable for any lost revenue or profits or
24  * other special, indirect and consequential damages.
25  *
26  * Revision History:
27  * 26-Aug-00: robert.nagtegaal@phil.uu.nl and roland@tschai.demon.nl:
28  *            various improvements
29  * 10-May-97: jwz@jwz.org: turned into a standalone program.
30  * 18-Apr-97: Memory leak fixed by Tom Schmidt <tschmidt@micron.com>
31  * 07-Apr-97: Modified by Dave Mitchell <davem@magnet.com>
32  * 23-Oct-94: Modified by David Bagley <bagleyd@bigfoot.com>
33  *  random star sizes
34  *  colors change depending on velocity
35  * 10-Oct-94: Add colors by Hubert Feyer
36  * 30-Sep-94: Initial port by Hubert Feyer
37  * 09-Mar-94: VMS can generate a random number 0.0 which results in a
38  *            division by zero, corrected by Jouk Jansen
39  *            <joukj@crys.chem.uva.nl>
40  */
41
42 #ifdef STANDALONE
43 # define DEFAULTS       "*delay:  20000  \n"   \
44                                         "*count:  -5     \n"   \
45                                         "*cycles:  250   \n"   \
46                                         "*ncolors:  64   \n"
47 # define UNIFORM_COLORS
48 # define reshape_galaxy 0
49 # define galaxy_handle_event 0
50 # include "xlockmore.h"    /* from the xscreensaver distribution */
51 #else  /* !STANDALONE */
52 # include "xlock.h"     /* from the xlockmore distribution */
53 #endif /* !STANDALONE */
54
55 static Bool tracks;
56 static Bool spin;
57 static Bool dbufp;
58
59 #define DEF_TRACKS "True"
60 #define DEF_SPIN   "True"
61 #define DEF_DBUF   "True"
62
63 static XrmOptionDescRec opts[] =
64 {
65  {"-tracks", ".galaxy.tracks", XrmoptionNoArg, "on"},
66  {"+tracks", ".galaxy.tracks", XrmoptionNoArg, "off"},
67  {"-spin",   ".galaxy.spin",   XrmoptionNoArg, "on"},
68  {"+spin",   ".galaxy.spin",   XrmoptionNoArg, "off"},
69  {"-dbuf",   ".galaxy.dbuf",   XrmoptionNoArg, "on"},
70  {"+dbuf",   ".galaxy.dbuf",   XrmoptionNoArg, "off"},
71 };
72
73 static argtype vars[] =
74 {
75  {&tracks, "tracks", "Tracks", DEF_TRACKS, t_Bool},
76  {&spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
77  {&dbufp,  "dbuf",   "Dbuf",   DEF_DBUF,   t_Bool}, 
78 };
79
80 static OptionStruct desc[] =
81 {
82  {"-/+tracks", "turn on/off star tracks"},
83  {"-/+spin",   "do/don't spin viewpoint"},
84  {"-/+dbuf",   "turn on/off double buffering."},
85 };
86
87 ENTRYPOINT ModeSpecOpt galaxy_opts =
88 {sizeof opts / sizeof opts[0], opts,
89  sizeof vars / sizeof vars[0], vars, desc};
90
91
92 #define FLOATRAND ((double) LRAND() / ((double) MAXRAND))
93
94 #if 0
95 #define WRAP       1  /* Warp around edges */
96 #define BOUNCE     1  /* Bounce from borders */
97 #endif
98
99 #define MINSIZE       1
100 #define MINGALAXIES    2
101 #define MAX_STARS    3000
102 #define MAX_IDELTAT    50
103 /* These come originally from the Cluster-version */
104 #define DEFAULT_GALAXIES  3
105 #define DEFAULT_STARS    1000
106 #define DEFAULT_HITITERATIONS  7500
107 #define DEFAULT_IDELTAT    200 /* 0.02 */
108 #define EPSILON 0.00000001
109
110 #define sqrt_EPSILON 0.0001
111
112 #define DELTAT (MAX_IDELTAT * 0.0001)
113
114 #define GALAXYRANGESIZE  0.1
115 #define GALAXYMINSIZE  0.15
116 #define QCONS    0.001
117
118
119 #define COLORBASE  16
120 /* colors per galaxy */
121 /* #define COLORSTEP  (NUMCOLORS/COLORBASE) */
122 # define COLORSTEP (MI_NCOLORS(mi)/COLORBASE)
123
124
125 typedef struct {
126  double      pos[3], vel[3];
127 } Star;
128
129
130 typedef struct {
131  int         mass;
132  int         nstars;
133  Star       *stars;
134  XPoint     *oldpoints;
135  XPoint     *newpoints;
136  double      pos[3], vel[3];
137  int         galcol;
138 } Galaxy;
139
140 typedef struct {
141  double      mat[3][3]; /* Movement of stars(?) */
142  double      scale; /* Scale */
143  int         midx; /* Middle of screen, x */
144  int         midy; /* Middle of screen, y */
145  double      size; /* */
146  double      diff[3]; /* */
147  Galaxy     *galaxies; /* the Whole Universe */
148  int         ngalaxies; /* # galaxies */
149  int         f_hititerations; /* # iterations before restart */
150  int         step; /* */
151  double      rot_y; /* rotation of eye around center of universe, around
152 y-axis*/
153  double      rot_x; /* rotation of eye around center of universe, around
154 x-axis */
155 } unistruct;
156
157 static unistruct *universes = NULL;
158
159 static void
160 free_galaxies(unistruct * gp)
161 {
162  if (gp->galaxies != NULL) {
163   int         i;
164
165   for (i = 0; i < gp->ngalaxies; i++) {
166    Galaxy     *gt = &gp->galaxies[i];
167
168    if (gt->stars != NULL)
169     (void) free((void *) gt->stars);
170    if (gt->oldpoints != NULL)
171        (void) free((void *) gt->oldpoints);
172    if (gt->newpoints != NULL)
173        (void) free((void *) gt->newpoints);
174   }
175   (void) free((void *) gp->galaxies);
176   gp->galaxies = NULL;
177  }
178 }
179
180 static void
181 startover(ModeInfo * mi)
182 {
183  unistruct  *gp = &universes[MI_SCREEN(mi)];
184  int         i, j; /* more tmp */
185  double      w1, w2; /* more tmp */
186  double      d, v, w, h; /* yet more tmp */
187
188  gp->step = 0;
189  gp->rot_y = 0;
190  gp->rot_x = 0;
191
192  if (MI_BATCHCOUNT(mi) < -MINGALAXIES)
193   free_galaxies(gp);
194  gp->ngalaxies = MI_BATCHCOUNT(mi);
195  if (gp->ngalaxies < -MINGALAXIES)
196   gp->ngalaxies = NRAND(-gp->ngalaxies - MINGALAXIES + 1) + MINGALAXIES;
197
198  else if (gp->ngalaxies < MINGALAXIES)
199   gp->ngalaxies = MINGALAXIES;
200  if (gp->galaxies == NULL)
201   gp->galaxies = (Galaxy *) calloc(gp->ngalaxies, sizeof (Galaxy));
202
203  for (i = 0; i < gp->ngalaxies; ++i) {
204   Galaxy     *gt = &gp->galaxies[i];
205   double      sinw1, sinw2, cosw1, cosw2;
206
207   gt->galcol = NRAND(COLORBASE - 2);
208   if (gt->galcol > 1)
209    gt->galcol += 2; /* Mult 8; 16..31 no green stars */
210   /* Galaxies still may have some green stars but are not all green. */
211
212   if (gt->stars != NULL) {
213    (void) free((void *) gt->stars);
214    gt->stars = NULL;
215   }
216   gt->nstars = (NRAND(MAX_STARS / 2)) + MAX_STARS / 2;
217   gt->stars = (Star *) malloc(gt->nstars * sizeof (Star));
218   gt->oldpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
219   gt->newpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
220
221   w1 = 2.0 * M_PI * FLOATRAND;
222   w2 = 2.0 * M_PI * FLOATRAND;
223   sinw1 = SINF(w1);
224   sinw2 = SINF(w2);
225   cosw1 = COSF(w1);
226   cosw2 = COSF(w2);
227
228   gp->mat[0][0] = cosw2;
229   gp->mat[0][1] = -sinw1 * sinw2;
230   gp->mat[0][2] = cosw1 * sinw2;
231   gp->mat[1][0] = 0.0;
232   gp->mat[1][1] = cosw1;
233   gp->mat[1][2] = sinw1;
234   gp->mat[2][0] = -sinw2;
235   gp->mat[2][1] = -sinw1 * cosw2;
236   gp->mat[2][2] = cosw1 * cosw2;
237
238   gt->vel[0] = FLOATRAND * 2.0 - 1.0;
239   gt->vel[1] = FLOATRAND * 2.0 - 1.0;
240   gt->vel[2] = FLOATRAND * 2.0 - 1.0;
241   gt->pos[0] = -gt->vel[0] * DELTAT * gp->f_hititerations + FLOATRAND -
242 0.5;
243   gt->pos[1] = -gt->vel[1] * DELTAT * gp->f_hititerations + FLOATRAND -
244 0.5;
245   gt->pos[2] = -gt->vel[2] * DELTAT * gp->f_hititerations + FLOATRAND -
246 0.5;
247
248   gt->mass = (int) (FLOATRAND * 1000.0) + 1;
249
250   gp->size = GALAXYRANGESIZE * FLOATRAND + GALAXYMINSIZE;
251
252   for (j = 0; j < gt->nstars; ++j) {
253    Star       *st = &gt->stars[j];
254    XPoint     *oldp = &gt->oldpoints[j];
255    XPoint     *newp = &gt->newpoints[j];
256
257    double      sinw, cosw;
258
259    w = 2.0 * M_PI * FLOATRAND;
260    sinw = SINF(w);
261    cosw = COSF(w);
262    d = FLOATRAND * gp->size;
263    h = FLOATRAND * exp(-2.0 * (d / gp->size)) / 5.0 * gp->size;
264    if (FLOATRAND < 0.5)
265     h = -h;
266    st->pos[0] = gp->mat[0][0] * d * cosw + gp->mat[1][0] * d * sinw +
267 gp->mat[2][0] * h + gt->pos[0];
268    st->pos[1] = gp->mat[0][1] * d * cosw + gp->mat[1][1] * d * sinw +
269 gp->mat[2][1] * h + gt->pos[1];
270    st->pos[2] = gp->mat[0][2] * d * cosw + gp->mat[1][2] * d * sinw +
271 gp->mat[2][2] * h + gt->pos[2];
272
273    v = sqrt(gt->mass * QCONS / sqrt(d * d + h * h));
274    st->vel[0] = -gp->mat[0][0] * v * sinw + gp->mat[1][0] * v * cosw +
275 gt->vel[0];
276    st->vel[1] = -gp->mat[0][1] * v * sinw + gp->mat[1][1] * v * cosw +
277 gt->vel[1];
278    st->vel[2] = -gp->mat[0][2] * v * sinw + gp->mat[1][2] * v * cosw +
279 gt->vel[2];
280
281    st->vel[0] *= DELTAT;
282    st->vel[1] *= DELTAT;
283    st->vel[2] *= DELTAT;
284
285    oldp->x = 0;
286    oldp->y = 0;
287    newp->x = 0;
288    newp->y = 0;
289   }
290
291  }
292
293  XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
294
295 #if 0
296  (void) printf("ngalaxies=%d, f_hititerations=%d\n", gp->ngalaxies,
297 gp->f_hititerations);
298  (void) printf("f_deltat=%g\n", DELTAT);
299  (void) printf("Screen: ");
300 #endif /*0 */
301 }
302
303 ENTRYPOINT void
304 init_galaxy(ModeInfo * mi)
305 {
306  unistruct  *gp;
307
308  if (universes == NULL) {
309   if ((universes = (unistruct *) calloc(MI_NUM_SCREENS(mi),
310       sizeof (unistruct))) == NULL)
311    return;
312  }
313  gp = &universes[MI_SCREEN(mi)];
314
315 # ifdef HAVE_COCOA      /* Don't second-guess Quartz's double-buffering */
316   dbufp = False;
317 # endif
318
319  gp->f_hititerations = MI_CYCLES(mi);
320
321  gp->scale = (double) (MI_WIN_WIDTH(mi) + MI_WIN_HEIGHT(mi)) / 8.0;
322  gp->midx =  MI_WIN_WIDTH(mi)  / 2;
323  gp->midy =  MI_WIN_HEIGHT(mi) / 2;
324  startover(mi);
325 }
326
327 ENTRYPOINT void
328 draw_galaxy(ModeInfo * mi)
329 {
330   Display    *display = MI_DISPLAY(mi);
331   Window      window = MI_WINDOW(mi);
332   GC          gc = MI_GC(mi);
333   unistruct  *gp = &universes[MI_SCREEN(mi)];
334   double      d, eps, cox, six, cor, sir;  /* tmp */
335   int         i, j, k; /* more tmp */
336   XPoint    *dummy = NULL;
337
338   if (! dbufp)
339     XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
340
341   if(spin){
342     gp->rot_y += 0.01;
343     gp->rot_x += 0.004;
344   }
345
346   cox = COSF(gp->rot_y);
347   six = SINF(gp->rot_y);
348   cor = COSF(gp->rot_x);
349   sir = SINF(gp->rot_x);
350
351   eps = 1/(EPSILON * sqrt_EPSILON * DELTAT * DELTAT * QCONS);
352
353   for (i = 0; i < gp->ngalaxies; ++i) {
354     Galaxy     *gt = &gp->galaxies[i];
355
356     for (j = 0; j < gp->galaxies[i].nstars; ++j) {
357       Star       *st = &gt->stars[j];
358       XPoint     *newp = &gt->newpoints[j];
359       double      v0 = st->vel[0];
360       double      v1 = st->vel[1];
361       double      v2 = st->vel[2];
362
363       for (k = 0; k < gp->ngalaxies; ++k) {
364         Galaxy     *gtk = &gp->galaxies[k];
365         double      d0 = gtk->pos[0] - st->pos[0];
366         double      d1 = gtk->pos[1] - st->pos[1];
367         double      d2 = gtk->pos[2] - st->pos[2];
368
369         d = d0 * d0 + d1 * d1 + d2 * d2;
370         if (d > EPSILON)
371           d = gt->mass / (d * sqrt(d)) * DELTAT * DELTAT * QCONS;
372         else
373           d = gt->mass * eps;
374         v0 += d0 * d;
375         v1 += d1 * d;
376         v2 += d2 * d;
377       }
378
379       st->vel[0] = v0;
380       st->vel[1] = v1;
381       st->vel[2] = v2;
382
383       st->pos[0] += v0;
384       st->pos[1] += v1;
385       st->pos[2] += v2;
386
387       newp->x = (short) (((cox * st->pos[0]) - (six * st->pos[2])) *
388                          gp->scale) + gp->midx;
389       newp->y = (short) (((cor * st->pos[1]) - (sir * ((six * st->pos[0]) +
390                                                        (cox * st->pos[2]))))
391                          * gp->scale) + gp->midy;
392
393     }
394
395     for (k = i + 1; k < gp->ngalaxies; ++k) {
396       Galaxy     *gtk = &gp->galaxies[k];
397       double      d0 = gtk->pos[0] - gt->pos[0];
398       double      d1 = gtk->pos[1] - gt->pos[1];
399       double      d2 = gtk->pos[2] - gt->pos[2];
400
401       d = d0 * d0 + d1 * d1 + d2 * d2;
402       if (d > EPSILON)
403         d = gt->mass * gt->mass / (d * sqrt(d)) * DELTAT * QCONS;
404       else
405         d = gt->mass * gt->mass / (EPSILON * sqrt_EPSILON) * DELTAT * QCONS;
406
407       d0 *= d;
408       d1 *= d;
409       d2 *= d;
410       gt->vel[0] += d0 / gt->mass;
411       gt->vel[1] += d1 / gt->mass;
412       gt->vel[2] += d2 / gt->mass;
413       gtk->vel[0] -= d0 / gtk->mass;
414       gtk->vel[1] -= d1 / gtk->mass;
415       gtk->vel[2] -= d2 / gtk->mass;
416     }
417
418     gt->pos[0] += gt->vel[0] * DELTAT;
419     gt->pos[1] += gt->vel[1] * DELTAT;
420     gt->pos[2] += gt->vel[2] * DELTAT;
421
422     if (dbufp) {
423       XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
424       XDrawPoints(display, window, gc, gt->oldpoints, gt->nstars,
425                   CoordModeOrigin);
426     }
427     XSetForeground(display, gc, MI_PIXEL(mi, COLORSTEP * gt->galcol));
428     XDrawPoints(display, window, gc, gt->newpoints, gt->nstars,
429                 CoordModeOrigin);
430
431     dummy = gt->oldpoints;
432     gt->oldpoints = gt->newpoints;
433     gt->newpoints = dummy;
434   }
435
436   gp->step++;
437   if (gp->step > gp->f_hititerations * 4)
438     startover(mi);
439 }
440
441 ENTRYPOINT void
442 release_galaxy(ModeInfo * mi)
443 {
444  if (universes != NULL) {
445   int         screen;
446
447   for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
448    free_galaxies(&universes[screen]);
449   (void) free((void *) universes);
450   universes = NULL;
451  }
452 }
453
454 ENTRYPOINT void
455 refresh_galaxy(ModeInfo * mi)
456 {
457  /* Do nothing, it will refresh by itself */
458 }
459
460 XSCREENSAVER_MODULE ("Galaxy", galaxy)