1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* galaxy --- spinning galaxies */
5 static const char sccsid[] = "@(#)galaxy.c 4.04 97/07/28 xlockmore";
8 /* Originally done by Uli Siegmund <uli@wombat.okapi.sub.org> on Amiga
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>
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.
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.
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>
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>
43 # define PROGCLASS "Galaxy"
44 # define HACK_INIT init_galaxy
45 # define HACK_DRAW draw_galaxy
46 # define galaxy_opts xlockmore_opts
47 # define DEFAULTS "*delay: 100 \n" \
52 # define UNIFORM_COLORS
53 # include "xlockmore.h" /* from the xscreensaver distribution */
54 #else /* !STANDALONE */
55 # include "xlock.h" /* from the xlockmore distribution */
56 #endif /* !STANDALONE */
61 #define DEF_TRACKS "True"
62 #define DEF_SPIN "True"
64 static XrmOptionDescRec opts[] =
66 {"-tracks", ".galaxy.tracks", XrmoptionNoArg, "on"},
67 {"+tracks", ".galaxy.tracks", XrmoptionNoArg, "off"},
68 {"-spin", ".galaxy.spin", XrmoptionNoArg, "on"},
69 {"+spin", ".galaxy.spin", XrmoptionNoArg, "off"}
72 static argtype vars[] =
74 {&tracks, "tracks", "Tracks", DEF_TRACKS, t_Bool},
75 {&spin, "spin", "Spin", DEF_SPIN, t_Bool}
78 static OptionStruct desc[] =
80 {"-/+tracks", "turn on/off star tracks"},
81 {"-/+spin", "do/don't spin viewpoint"}
84 ModeSpecOpt galaxy_opts = { 4, opts, 2, vars, desc };
87 #define FLOATRAND ((double) LRAND() / ((double) MAXRAND))
90 #define WRAP 1 /* Warp around edges */
91 #define BOUNCE 1 /* Bounce from borders */
96 #define MAX_STARS 3000
97 #define MAX_IDELTAT 50
98 /* These come originally from the Cluster-version */
99 #define DEFAULT_GALAXIES 3
100 #define DEFAULT_STARS 1000
101 #define DEFAULT_HITITERATIONS 7500
102 #define DEFAULT_IDELTAT 200 /* 0.02 */
103 #define EPSILON 0.00000001
105 #define sqrt_EPSILON 0.0001
107 #define DELTAT (MAX_IDELTAT * 0.0001)
109 #define GALAXYRANGESIZE 0.1
110 #define GALAXYMINSIZE 0.15
115 /* Colors for stars start here */
116 #define COLORSTEP (NUMCOLORS/COLORBASE) /* NUMCOLORS / COLORBASE colors
121 double pos[3], vel[3];
131 double pos[3], vel[3];
136 double mat[3][3]; /* Movement of stars(?) */
137 double scale; /* Scale */
138 int midx; /* Middle of screen, x */
139 int midy; /* Middle of screen, y */
141 double diff[3]; /* */
142 Galaxy *galaxies; /* the Whole Universe */
143 int ngalaxies; /* # galaxies */
144 int f_hititerations; /* # iterations before restart */
146 double rot_y; /* rotation of eye around center of universe, around
148 double rot_x; /* rotation of eye around center of universe, around
152 static unistruct *universes = NULL;
155 free_galaxies(unistruct * gp)
157 if (gp->galaxies != NULL) {
160 for (i = 0; i < gp->ngalaxies; i++) {
161 Galaxy *gt = &gp->galaxies[i];
163 if (gt->stars != NULL)
164 (void) free((void *) gt->stars);
165 if (gt->oldpoints != NULL)
166 (void) free((void *) gt->oldpoints);
167 if (gt->newpoints != NULL)
168 (void) free((void *) gt->newpoints);
170 (void) free((void *) gp->galaxies);
176 startover(ModeInfo * mi)
178 unistruct *gp = &universes[MI_SCREEN(mi)];
179 int i, j; /* more tmp */
180 double w1, w2; /* more tmp */
181 double d, v, w, h; /* yet more tmp */
187 if (MI_BATCHCOUNT(mi) < -MINGALAXIES)
189 gp->ngalaxies = MI_BATCHCOUNT(mi);
190 if (gp->ngalaxies < -MINGALAXIES)
191 gp->ngalaxies = NRAND(-gp->ngalaxies - MINGALAXIES + 1) + MINGALAXIES;
193 else if (gp->ngalaxies < MINGALAXIES)
194 gp->ngalaxies = MINGALAXIES;
195 if (gp->galaxies == NULL)
196 gp->galaxies = (Galaxy *) calloc(gp->ngalaxies, sizeof (Galaxy));
198 for (i = 0; i < gp->ngalaxies; ++i) {
199 Galaxy *gt = &gp->galaxies[i];
200 double sinw1, sinw2, cosw1, cosw2;
202 gt->galcol = NRAND(COLORBASE - 2);
204 gt->galcol += 2; /* Mult 8; 16..31 no green stars */
205 /* Galaxies still may have some green stars but are not all green. */
207 if (gt->stars != NULL) {
208 (void) free((void *) gt->stars);
211 gt->nstars = (NRAND(MAX_STARS / 2)) + MAX_STARS / 2;
212 gt->stars = (Star *) malloc(gt->nstars * sizeof (Star));
213 gt->oldpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
214 gt->newpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
216 w1 = 2.0 * M_PI * FLOATRAND;
217 w2 = 2.0 * M_PI * FLOATRAND;
223 gp->mat[0][0] = cosw2;
224 gp->mat[0][1] = -sinw1 * sinw2;
225 gp->mat[0][2] = cosw1 * sinw2;
227 gp->mat[1][1] = cosw1;
228 gp->mat[1][2] = sinw1;
229 gp->mat[2][0] = -sinw2;
230 gp->mat[2][1] = -sinw1 * cosw2;
231 gp->mat[2][2] = cosw1 * cosw2;
233 gt->vel[0] = FLOATRAND * 2.0 - 1.0;
234 gt->vel[1] = FLOATRAND * 2.0 - 1.0;
235 gt->vel[2] = FLOATRAND * 2.0 - 1.0;
236 gt->pos[0] = -gt->vel[0] * DELTAT * gp->f_hititerations + FLOATRAND -
238 gt->pos[1] = -gt->vel[1] * DELTAT * gp->f_hititerations + FLOATRAND -
240 gt->pos[2] = -gt->vel[2] * DELTAT * gp->f_hititerations + FLOATRAND -
243 gt->mass = (int) (FLOATRAND * 1000.0) + 1;
245 gp->size = GALAXYRANGESIZE * FLOATRAND + GALAXYMINSIZE;
247 for (j = 0; j < gt->nstars; ++j) {
248 Star *st = >->stars[j];
249 XPoint *oldp = >->oldpoints[j];
250 XPoint *newp = >->newpoints[j];
254 w = 2.0 * M_PI * FLOATRAND;
257 d = FLOATRAND * gp->size;
258 h = FLOATRAND * exp(-2.0 * (d / gp->size)) / 5.0 * gp->size;
261 st->pos[0] = gp->mat[0][0] * d * cosw + gp->mat[1][0] * d * sinw +
262 gp->mat[2][0] * h + gt->pos[0];
263 st->pos[1] = gp->mat[0][1] * d * cosw + gp->mat[1][1] * d * sinw +
264 gp->mat[2][1] * h + gt->pos[1];
265 st->pos[2] = gp->mat[0][2] * d * cosw + gp->mat[1][2] * d * sinw +
266 gp->mat[2][2] * h + gt->pos[2];
268 v = sqrt(gt->mass * QCONS / sqrt(d * d + h * h));
269 st->vel[0] = -gp->mat[0][0] * v * sinw + gp->mat[1][0] * v * cosw +
271 st->vel[1] = -gp->mat[0][1] * v * sinw + gp->mat[1][1] * v * cosw +
273 st->vel[2] = -gp->mat[0][2] * v * sinw + gp->mat[1][2] * v * cosw +
276 st->vel[0] *= DELTAT;
277 st->vel[1] *= DELTAT;
278 st->vel[2] *= DELTAT;
288 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
291 (void) printf("ngalaxies=%d, f_hititerations=%d\n", gp->ngalaxies,
292 gp->f_hititerations);
293 (void) printf("f_deltat=%g\n", DELTAT);
294 (void) printf("Screen: ");
299 init_galaxy(ModeInfo * mi)
303 if (universes == NULL) {
304 if ((universes = (unistruct *) calloc(MI_NUM_SCREENS(mi),
305 sizeof (unistruct))) == NULL)
308 gp = &universes[MI_SCREEN(mi)];
310 gp->f_hititerations = MI_CYCLES(mi);
312 gp->scale = (double) (MI_WIN_WIDTH(mi) + MI_WIN_HEIGHT(mi)) / 8.0;
313 gp->midx = MI_WIN_WIDTH(mi) / 2;
314 gp->midy = MI_WIN_HEIGHT(mi) / 2;
319 draw_galaxy(ModeInfo * mi)
321 Display *display = MI_DISPLAY(mi);
322 Window window = MI_WINDOW(mi);
324 unistruct *gp = &universes[MI_SCREEN(mi)];
325 double d, eps, cox, six, cor, sir; /* tmp */
326 int i, j, k; /* more tmp */
327 XPoint *dummy = NULL;
334 cox = COSF(gp->rot_y);
335 six = SINF(gp->rot_y);
336 cor = COSF(gp->rot_x);
337 sir = SINF(gp->rot_x);
339 eps = 1/(EPSILON * sqrt_EPSILON * DELTAT * DELTAT * QCONS);
341 for (i = 0; i < gp->ngalaxies; ++i) {
342 Galaxy *gt = &gp->galaxies[i];
344 for (j = 0; j < gp->galaxies[i].nstars; ++j) {
345 Star *st = >->stars[j];
346 XPoint *newp = >->newpoints[j];
347 double v0 = st->vel[0];
348 double v1 = st->vel[1];
349 double v2 = st->vel[2];
351 for (k = 0; k < gp->ngalaxies; ++k) {
352 Galaxy *gtk = &gp->galaxies[k];
353 double d0 = gtk->pos[0] - st->pos[0];
354 double d1 = gtk->pos[1] - st->pos[1];
355 double d2 = gtk->pos[2] - st->pos[2];
357 d = d0 * d0 + d1 * d1 + d2 * d2;
359 d = gt->mass / (d * sqrt(d)) * DELTAT * DELTAT * QCONS;
375 newp->x = (short) (((cox * st->pos[0]) - (six * st->pos[2])) *
376 gp->scale) + gp->midx;
377 newp->y = (short) (((cor * st->pos[1]) - (sir * ((six * st->pos[0]) +
378 (cox * st->pos[2])))) * gp->scale) + gp->midy;
382 for (k = i + 1; k < gp->ngalaxies; ++k) {
383 Galaxy *gtk = &gp->galaxies[k];
384 double d0 = gtk->pos[0] - gt->pos[0];
385 double d1 = gtk->pos[1] - gt->pos[1];
386 double d2 = gtk->pos[2] - gt->pos[2];
388 d = d0 * d0 + d1 * d1 + d2 * d2;
390 d = gt->mass * gt->mass / (d * sqrt(d)) * DELTAT * QCONS;
392 d = gt->mass * gt->mass / (EPSILON * sqrt_EPSILON) * DELTAT * QCONS;
397 gt->vel[0] += d0 / gt->mass;
398 gt->vel[1] += d1 / gt->mass;
399 gt->vel[2] += d2 / gt->mass;
400 gtk->vel[0] -= d0 / gtk->mass;
401 gtk->vel[1] -= d1 / gtk->mass;
402 gtk->vel[2] -= d2 / gtk->mass;
405 gt->pos[0] += gt->vel[0] * DELTAT;
406 gt->pos[1] += gt->vel[1] * DELTAT;
407 gt->pos[2] += gt->vel[2] * DELTAT;
409 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
410 XDrawPoints(display, window, gc, gt->oldpoints, gt->nstars,
412 XSetForeground(display, gc, MI_PIXEL(mi, COLORSTEP * gt->galcol));
413 XDrawPoints(display, window, gc, gt->newpoints, gt->nstars,
416 dummy = gt->oldpoints;
417 gt->oldpoints = gt->newpoints;
418 gt->newpoints = dummy;
422 if (gp->step > gp->f_hititerations * 4)
427 release_galaxy(ModeInfo * mi)
429 if (universes != NULL) {
432 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
433 free_galaxies(&universes[screen]);
434 (void) free((void *) universes);
440 refresh_galaxy(ModeInfo * mi)
442 /* Do nothing, it will refresh by itself */