From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[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                                         "*fpsSolid:  true   \n" \
48                                         "*ignoreRotation: True \n" \
49                                     "*lowrez: True \n" \
50
51 # define UNIFORM_COLORS
52 # define release_galaxy 0
53 # define reshape_galaxy 0
54 # define galaxy_handle_event 0
55 # include "xlockmore.h"    /* from the xscreensaver distribution */
56 #else  /* !STANDALONE */
57 # include "xlock.h"     /* from the xlockmore distribution */
58 #endif /* !STANDALONE */
59
60 static Bool tracks;
61 static Bool spin;
62 static Bool dbufp;
63
64 #define DEF_TRACKS "True"
65 #define DEF_SPIN   "True"
66 #define DEF_DBUF   "True"
67
68 static XrmOptionDescRec opts[] =
69 {
70  {"-tracks", ".galaxy.tracks", XrmoptionNoArg, "on"},
71  {"+tracks", ".galaxy.tracks", XrmoptionNoArg, "off"},
72  {"-spin",   ".galaxy.spin",   XrmoptionNoArg, "on"},
73  {"+spin",   ".galaxy.spin",   XrmoptionNoArg, "off"},
74  {"-dbuf",   ".galaxy.dbuf",   XrmoptionNoArg, "on"},
75  {"+dbuf",   ".galaxy.dbuf",   XrmoptionNoArg, "off"},
76 };
77
78 static argtype vars[] =
79 {
80  {&tracks, "tracks", "Tracks", DEF_TRACKS, t_Bool},
81  {&spin,   "spin",   "Spin",   DEF_SPIN,   t_Bool},
82  {&dbufp,  "dbuf",   "Dbuf",   DEF_DBUF,   t_Bool}, 
83 };
84
85 static OptionStruct desc[] =
86 {
87  {"-/+tracks", "turn on/off star tracks"},
88  {"-/+spin",   "do/don't spin viewpoint"},
89  {"-/+dbuf",   "turn on/off double buffering."},
90 };
91
92 ENTRYPOINT ModeSpecOpt galaxy_opts =
93 {sizeof opts / sizeof opts[0], opts,
94  sizeof vars / sizeof vars[0], vars, desc};
95
96
97 #define FLOATRAND ((double) LRAND() / ((double) MAXRAND))
98
99 #if 0
100 #define WRAP       1  /* Warp around edges */
101 #define BOUNCE     1  /* Bounce from borders */
102 #endif
103
104 #define MINSIZE       1
105 #define MINGALAXIES    2
106 #define MAX_STARS    3000
107 #define MAX_IDELTAT    50
108 /* These come originally from the Cluster-version */
109 #define DEFAULT_GALAXIES  3
110 #define DEFAULT_STARS    1000
111 #define DEFAULT_HITITERATIONS  7500
112 #define DEFAULT_IDELTAT    200 /* 0.02 */
113 #define EPSILON 0.00000001
114
115 #define sqrt_EPSILON 0.0001
116
117 #define DELTAT (MAX_IDELTAT * 0.0001)
118
119 #define GALAXYRANGESIZE  0.1
120 #define GALAXYMINSIZE  0.15
121 #define QCONS    0.001
122
123
124 #define COLORBASE  16
125 /* colors per galaxy */
126 /* #define COLORSTEP  (NUMCOLORS/COLORBASE) */
127 # define COLORSTEP (MI_NCOLORS(mi)/COLORBASE)
128
129
130 typedef struct {
131  double      pos[3], vel[3];
132 } Star;
133
134
135 typedef struct {
136  int         mass;
137  int         nstars;
138  Star       *stars;
139  XPoint     *oldpoints;
140  XPoint     *newpoints;
141  double      pos[3], vel[3];
142  int         galcol;
143 } Galaxy;
144
145 typedef struct {
146  double      mat[3][3]; /* Movement of stars(?) */
147  double      scale; /* Scale */
148  int         midx; /* Middle of screen, x */
149  int         midy; /* Middle of screen, y */
150  double      size; /* */
151  double      diff[3]; /* */
152  Galaxy     *galaxies; /* the Whole Universe */
153  int         ngalaxies; /* # galaxies */
154  int         f_hititerations; /* # iterations before restart */
155  int         step; /* */
156  double      rot_y; /* rotation of eye around center of universe, around
157 y-axis*/
158  double      rot_x; /* rotation of eye around center of universe, around
159 x-axis */
160 } unistruct;
161
162 static unistruct *universes = NULL;
163
164 ENTRYPOINT void
165 free_galaxy(ModeInfo * mi)
166 {
167  unistruct  *gp = &universes[MI_SCREEN(mi)];
168  if (gp->galaxies != NULL) {
169   int         i;
170
171   for (i = 0; i < gp->ngalaxies; i++) {
172    Galaxy     *gt = &gp->galaxies[i];
173
174    if (gt->stars != NULL)
175     (void) free((void *) gt->stars);
176    if (gt->oldpoints != NULL)
177        (void) free((void *) gt->oldpoints);
178    if (gt->newpoints != NULL)
179        (void) free((void *) gt->newpoints);
180   }
181   (void) free((void *) gp->galaxies);
182   gp->galaxies = NULL;
183  }
184 }
185
186 static void
187 startover(ModeInfo * mi)
188 {
189  unistruct  *gp = &universes[MI_SCREEN(mi)];
190  int         i, j; /* more tmp */
191  double      w1, w2; /* more tmp */
192  double      d, v, w, h; /* yet more tmp */
193
194  gp->step = 0;
195  gp->rot_y = 0;
196  gp->rot_x = 0;
197
198  if (MI_BATCHCOUNT(mi) < -MINGALAXIES)
199   free_galaxy(mi);
200  gp->ngalaxies = MI_BATCHCOUNT(mi);
201  if (gp->ngalaxies < -MINGALAXIES)
202   gp->ngalaxies = NRAND(-gp->ngalaxies - MINGALAXIES + 1) + MINGALAXIES;
203
204  else if (gp->ngalaxies < MINGALAXIES)
205   gp->ngalaxies = MINGALAXIES;
206  if (gp->galaxies == NULL)
207   gp->galaxies = (Galaxy *) calloc(gp->ngalaxies, sizeof (Galaxy));
208
209  for (i = 0; i < gp->ngalaxies; ++i) {
210   Galaxy     *gt = &gp->galaxies[i];
211   double      sinw1, sinw2, cosw1, cosw2;
212
213   gt->galcol = NRAND(COLORBASE - 2);
214   if (gt->galcol > 1)
215    gt->galcol += 2; /* Mult 8; 16..31 no green stars */
216   /* Galaxies still may have some green stars but are not all green. */
217
218   if (gt->stars != NULL) {
219    (void) free((void *) gt->stars);
220    gt->stars = NULL;
221   }
222   gt->nstars = (NRAND(MAX_STARS / 2)) + MAX_STARS / 2;
223   gt->stars = (Star *) malloc(gt->nstars * sizeof (Star));
224   gt->oldpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
225   gt->newpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
226
227   w1 = 2.0 * M_PI * FLOATRAND;
228   w2 = 2.0 * M_PI * FLOATRAND;
229   sinw1 = SINF(w1);
230   sinw2 = SINF(w2);
231   cosw1 = COSF(w1);
232   cosw2 = COSF(w2);
233
234   gp->mat[0][0] = cosw2;
235   gp->mat[0][1] = -sinw1 * sinw2;
236   gp->mat[0][2] = cosw1 * sinw2;
237   gp->mat[1][0] = 0.0;
238   gp->mat[1][1] = cosw1;
239   gp->mat[1][2] = sinw1;
240   gp->mat[2][0] = -sinw2;
241   gp->mat[2][1] = -sinw1 * cosw2;
242   gp->mat[2][2] = cosw1 * cosw2;
243
244   gt->vel[0] = FLOATRAND * 2.0 - 1.0;
245   gt->vel[1] = FLOATRAND * 2.0 - 1.0;
246   gt->vel[2] = FLOATRAND * 2.0 - 1.0;
247   gt->pos[0] = -gt->vel[0] * DELTAT * gp->f_hititerations + FLOATRAND -
248 0.5;
249   gt->pos[1] = -gt->vel[1] * DELTAT * gp->f_hititerations + FLOATRAND -
250 0.5;
251   gt->pos[2] = -gt->vel[2] * DELTAT * gp->f_hititerations + FLOATRAND -
252 0.5;
253
254   gt->mass = (int) (FLOATRAND * 1000.0) + 1;
255
256   gp->size = GALAXYRANGESIZE * FLOATRAND + GALAXYMINSIZE;
257
258   for (j = 0; j < gt->nstars; ++j) {
259    Star       *st = &gt->stars[j];
260    XPoint     *oldp = &gt->oldpoints[j];
261    XPoint     *newp = &gt->newpoints[j];
262
263    double      sinw, cosw;
264
265    w = 2.0 * M_PI * FLOATRAND;
266    sinw = SINF(w);
267    cosw = COSF(w);
268    d = FLOATRAND * gp->size;
269    h = FLOATRAND * exp(-2.0 * (d / gp->size)) / 5.0 * gp->size;
270    if (FLOATRAND < 0.5)
271     h = -h;
272    st->pos[0] = gp->mat[0][0] * d * cosw + gp->mat[1][0] * d * sinw +
273 gp->mat[2][0] * h + gt->pos[0];
274    st->pos[1] = gp->mat[0][1] * d * cosw + gp->mat[1][1] * d * sinw +
275 gp->mat[2][1] * h + gt->pos[1];
276    st->pos[2] = gp->mat[0][2] * d * cosw + gp->mat[1][2] * d * sinw +
277 gp->mat[2][2] * h + gt->pos[2];
278
279    v = sqrt(gt->mass * QCONS / sqrt(d * d + h * h));
280    st->vel[0] = -gp->mat[0][0] * v * sinw + gp->mat[1][0] * v * cosw +
281 gt->vel[0];
282    st->vel[1] = -gp->mat[0][1] * v * sinw + gp->mat[1][1] * v * cosw +
283 gt->vel[1];
284    st->vel[2] = -gp->mat[0][2] * v * sinw + gp->mat[1][2] * v * cosw +
285 gt->vel[2];
286
287    st->vel[0] *= DELTAT;
288    st->vel[1] *= DELTAT;
289    st->vel[2] *= DELTAT;
290
291    oldp->x = 0;
292    oldp->y = 0;
293    newp->x = 0;
294    newp->y = 0;
295   }
296
297  }
298
299  XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
300
301 #if 0
302  (void) printf("ngalaxies=%d, f_hititerations=%d\n", gp->ngalaxies,
303 gp->f_hititerations);
304  (void) printf("f_deltat=%g\n", DELTAT);
305  (void) printf("Screen: ");
306 #endif /*0 */
307 }
308
309 ENTRYPOINT void
310 init_galaxy(ModeInfo * mi)
311 {
312  unistruct  *gp;
313
314  MI_INIT (mi, universes);
315  gp = &universes[MI_SCREEN(mi)];
316
317 # ifdef HAVE_JWXYZ      /* Don't second-guess Quartz's double-buffering */
318   dbufp = False;
319 # endif
320
321  gp->f_hititerations = MI_CYCLES(mi);
322
323  gp->scale = (double) (MI_WIN_WIDTH(mi) + MI_WIN_HEIGHT(mi)) / 8.0;
324  gp->midx =  MI_WIN_WIDTH(mi)  / 2;
325  gp->midy =  MI_WIN_HEIGHT(mi) / 2;
326  startover(mi);
327 }
328
329 ENTRYPOINT void
330 draw_galaxy(ModeInfo * mi)
331 {
332   Display    *display = MI_DISPLAY(mi);
333   Window      window = MI_WINDOW(mi);
334   GC          gc = MI_GC(mi);
335   unistruct  *gp = &universes[MI_SCREEN(mi)];
336   double      d, eps, cox, six, cor, sir;  /* tmp */
337   int         i, j, k; /* more tmp */
338   XPoint    *dummy = NULL;
339
340   if (! dbufp)
341     XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
342
343   if(spin){
344     gp->rot_y += 0.01;
345     gp->rot_x += 0.004;
346   }
347
348   cox = COSF(gp->rot_y);
349   six = SINF(gp->rot_y);
350   cor = COSF(gp->rot_x);
351   sir = SINF(gp->rot_x);
352
353   eps = 1/(EPSILON * sqrt_EPSILON * DELTAT * DELTAT * QCONS);
354
355   for (i = 0; i < gp->ngalaxies; ++i) {
356     Galaxy     *gt = &gp->galaxies[i];
357
358     for (j = 0; j < gp->galaxies[i].nstars; ++j) {
359       Star       *st = &gt->stars[j];
360       XPoint     *newp = &gt->newpoints[j];
361       double      v0 = st->vel[0];
362       double      v1 = st->vel[1];
363       double      v2 = st->vel[2];
364
365       for (k = 0; k < gp->ngalaxies; ++k) {
366         Galaxy     *gtk = &gp->galaxies[k];
367         double      d0 = gtk->pos[0] - st->pos[0];
368         double      d1 = gtk->pos[1] - st->pos[1];
369         double      d2 = gtk->pos[2] - st->pos[2];
370
371         d = d0 * d0 + d1 * d1 + d2 * d2;
372         if (d > EPSILON)
373           d = gtk->mass / (d * sqrt(d)) * DELTAT * DELTAT * QCONS;
374         else
375           d = gtk->mass / (eps * sqrt(eps));
376         v0 += d0 * d;
377         v1 += d1 * d;
378         v2 += d2 * d;
379       }
380
381       st->vel[0] = v0;
382       st->vel[1] = v1;
383       st->vel[2] = v2;
384
385       st->pos[0] += v0;
386       st->pos[1] += v1;
387       st->pos[2] += v2;
388
389       newp->x = (short) (((cox * st->pos[0]) - (six * st->pos[2])) *
390                          gp->scale) + gp->midx;
391       newp->y = (short) (((cor * st->pos[1]) - (sir * ((six * st->pos[0]) +
392                                                        (cox * st->pos[2]))))
393                          * gp->scale) + gp->midy;
394
395     }
396
397     for (k = i + 1; k < gp->ngalaxies; ++k) {
398       Galaxy     *gtk = &gp->galaxies[k];
399       double      d0 = gtk->pos[0] - gt->pos[0];
400       double      d1 = gtk->pos[1] - gt->pos[1];
401       double      d2 = gtk->pos[2] - gt->pos[2];
402
403       d = d0 * d0 + d1 * d1 + d2 * d2;
404       if (d > EPSILON)
405         d = 1 / (d * sqrt(d)) * DELTAT * QCONS;
406       else
407         d = 1 / (EPSILON * sqrt_EPSILON) * DELTAT * QCONS;
408
409       d0 *= d;
410       d1 *= d;
411       d2 *= d;
412       gt->vel[0] += d0 * gtk->mass;
413       gt->vel[1] += d1 * gtk->mass;
414       gt->vel[2] += d2 * gtk->mass;
415       gtk->vel[0] -= d0 * gt->mass;
416       gtk->vel[1] -= d1 * gt->mass;
417       gtk->vel[2] -= d2 * gt->mass;
418     }
419
420     gt->pos[0] += gt->vel[0] * DELTAT;
421     gt->pos[1] += gt->vel[1] * DELTAT;
422     gt->pos[2] += gt->vel[2] * DELTAT;
423
424     if (dbufp) {
425       XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
426       XDrawPoints(display, window, gc, gt->oldpoints, gt->nstars,
427                   CoordModeOrigin);
428     }
429     XSetForeground(display, gc, MI_PIXEL(mi, COLORSTEP * gt->galcol));
430     XDrawPoints(display, window, gc, gt->newpoints, gt->nstars,
431                 CoordModeOrigin);
432
433     dummy = gt->oldpoints;
434     gt->oldpoints = gt->newpoints;
435     gt->newpoints = dummy;
436   }
437
438   gp->step++;
439   if (gp->step > gp->f_hititerations * 4)
440     startover(mi);
441 }
442
443 #ifndef STANDALONE
444 ENTRYPOINT void
445 refresh_galaxy(ModeInfo * mi)
446 {
447  /* Do nothing, it will refresh by itself */
448 }
449 #endif
450
451 XSCREENSAVER_MODULE ("Galaxy", galaxy)