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