1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* galaxy --- spinning galaxies */
4 #if !defined( lint ) && !defined( SABER )
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 */
60 #define DEF_TRACKS "True"
62 static XrmOptionDescRec opts[] =
64 {"-tracks", ".galaxy.tracks", XrmoptionNoArg, (caddr_t) "on"},
65 {"+tracks", ".galaxy.tracks", XrmoptionNoArg, (caddr_t) "off"}
68 static argtype vars[] =
70 {(caddr_t *) & tracks, "tracks", "Tracks", DEF_TRACKS, t_Bool}
73 static OptionStruct desc[] =
75 {"-/+tracks", "turn on/off star tracks"}
78 ModeSpecOpt galaxy_opts = { 2, opts, 1, vars, desc };
81 #define FLOATRAND ((double) LRAND() / ((double) MAXRAND))
84 #define WRAP 1 /* Warp around edges */
85 #define BOUNCE 1 /* Bounce from borders */
90 #define MAX_STARS 3000
91 #define MAX_IDELTAT 50
92 /* These come originally from the Cluster-version */
93 #define DEFAULT_GALAXIES 3
94 #define DEFAULT_STARS 1000
95 #define DEFAULT_HITITERATIONS 7500
96 #define DEFAULT_IDELTAT 200 /* 0.02 */
97 #define EPSILON 0.00000001
99 #define sqrt_EPSILON 0.0001
101 #define DELTAT (MAX_IDELTAT * 0.0001)
103 #define GALAXYRANGESIZE 0.1
104 #define GALAXYMINSIZE 0.15
109 /* Colors for stars start here */
110 #define COLORSTEP (NUMCOLORS/COLORBASE) /* NUMCOLORS / COLORBASE colors
115 double pos[3], vel[3];
125 double pos[3], vel[3];
130 double mat[3][3]; /* Movement of stars(?) */
131 double scale; /* Scale */
132 int midx; /* Middle of screen, x */
133 int midy; /* Middle of screen, y */
135 double diff[3]; /* */
136 Galaxy *galaxies; /* the Whole Universe */
137 int ngalaxies; /* # galaxies */
138 int f_hititerations; /* # iterations before restart */
140 double rot_y; /* rotation of eye around center of universe, around
142 double rot_x; /* rotation of eye around center of universe, around
146 static unistruct *universes = NULL;
149 free_galaxies(unistruct * gp)
151 if (gp->galaxies != NULL) {
154 for (i = 0; i < gp->ngalaxies; i++) {
155 Galaxy *gt = &gp->galaxies[i];
157 if (gt->stars != NULL)
158 (void) free((void *) gt->stars);
159 if (gt->oldpoints != NULL)
160 (void) free((void *) gt->oldpoints);
161 if (gt->newpoints != NULL)
162 (void) free((void *) gt->newpoints);
164 (void) free((void *) gp->galaxies);
170 startover(ModeInfo * mi)
172 unistruct *gp = &universes[MI_SCREEN(mi)];
173 int i, j; /* more tmp */
174 double w1, w2; /* more tmp */
175 double d, v, w, h; /* yet more tmp */
181 if (MI_BATCHCOUNT(mi) < -MINGALAXIES)
183 gp->ngalaxies = MI_BATCHCOUNT(mi);
184 if (gp->ngalaxies < -MINGALAXIES)
185 gp->ngalaxies = NRAND(-gp->ngalaxies - MINGALAXIES + 1) + MINGALAXIES;
187 else if (gp->ngalaxies < MINGALAXIES)
188 gp->ngalaxies = MINGALAXIES;
189 if (gp->galaxies == NULL)
190 gp->galaxies = (Galaxy *) calloc(gp->ngalaxies, sizeof (Galaxy));
192 for (i = 0; i < gp->ngalaxies; ++i) {
193 Galaxy *gt = &gp->galaxies[i];
194 double sinw1, sinw2, cosw1, cosw2;
196 gt->galcol = NRAND(COLORBASE - 2);
198 gt->galcol += 2; /* Mult 8; 16..31 no green stars */
199 /* Galaxies still may have some green stars but are not all green. */
201 if (gt->stars != NULL) {
202 (void) free((void *) gt->stars);
205 gt->nstars = (NRAND(MAX_STARS / 2)) + MAX_STARS / 2;
206 gt->stars = (Star *) malloc(gt->nstars * sizeof (Star));
207 gt->oldpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
208 gt->newpoints = (XPoint *) malloc(gt->nstars * sizeof (XPoint));
210 w1 = 2.0 * M_PI * FLOATRAND;
211 w2 = 2.0 * M_PI * FLOATRAND;
217 gp->mat[0][0] = cosw2;
218 gp->mat[0][1] = -sinw1 * sinw2;
219 gp->mat[0][2] = cosw1 * sinw2;
221 gp->mat[1][1] = cosw1;
222 gp->mat[1][2] = sinw1;
223 gp->mat[2][0] = -sinw2;
224 gp->mat[2][1] = -sinw1 * cosw2;
225 gp->mat[2][2] = cosw1 * cosw2;
227 gt->vel[0] = FLOATRAND * 2.0 - 1.0;
228 gt->vel[1] = FLOATRAND * 2.0 - 1.0;
229 gt->vel[2] = FLOATRAND * 2.0 - 1.0;
230 gt->pos[0] = -gt->vel[0] * DELTAT * gp->f_hititerations + FLOATRAND -
232 gt->pos[1] = -gt->vel[1] * DELTAT * gp->f_hititerations + FLOATRAND -
234 gt->pos[2] = -gt->vel[2] * DELTAT * gp->f_hititerations + FLOATRAND -
237 gt->mass = (int) (FLOATRAND * 1000.0) + 1;
239 gp->size = GALAXYRANGESIZE * FLOATRAND + GALAXYMINSIZE;
241 for (j = 0; j < gt->nstars; ++j) {
242 Star *st = >->stars[j];
243 XPoint *oldp = >->oldpoints[j];
244 XPoint *newp = >->newpoints[j];
248 w = 2.0 * M_PI * FLOATRAND;
251 d = FLOATRAND * gp->size;
252 h = FLOATRAND * exp(-2.0 * (d / gp->size)) / 5.0 * gp->size;
255 st->pos[0] = gp->mat[0][0] * d * cosw + gp->mat[1][0] * d * sinw +
256 gp->mat[2][0] * h + gt->pos[0];
257 st->pos[1] = gp->mat[0][1] * d * cosw + gp->mat[1][1] * d * sinw +
258 gp->mat[2][1] * h + gt->pos[1];
259 st->pos[2] = gp->mat[0][2] * d * cosw + gp->mat[1][2] * d * sinw +
260 gp->mat[2][2] * h + gt->pos[2];
262 v = sqrt(gt->mass * QCONS / sqrt(d * d + h * h));
263 st->vel[0] = -gp->mat[0][0] * v * sinw + gp->mat[1][0] * v * cosw +
265 st->vel[1] = -gp->mat[0][1] * v * sinw + gp->mat[1][1] * v * cosw +
267 st->vel[2] = -gp->mat[0][2] * v * sinw + gp->mat[1][2] * v * cosw +
270 st->vel[0] *= DELTAT;
271 st->vel[1] *= DELTAT;
272 st->vel[2] *= DELTAT;
282 XClearWindow(MI_DISPLAY(mi), MI_WINDOW(mi));
285 (void) printf("ngalaxies=%d, f_hititerations=%d\n", gp->ngalaxies,
286 gp->f_hititerations);
287 (void) printf("f_deltat=%g\n", DELTAT);
288 (void) printf("Screen: ");
293 init_galaxy(ModeInfo * mi)
297 if (universes == NULL) {
298 if ((universes = (unistruct *) calloc(MI_NUM_SCREENS(mi),
299 sizeof (unistruct))) == NULL)
302 gp = &universes[MI_SCREEN(mi)];
304 gp->f_hititerations = MI_CYCLES(mi);
306 gp->scale = (double) (MI_WIN_WIDTH(mi) + MI_WIN_HEIGHT(mi)) / 8.0;
307 gp->midx = MI_WIN_WIDTH(mi) / 2;
308 gp->midy = MI_WIN_HEIGHT(mi) / 2;
313 draw_galaxy(ModeInfo * mi)
315 Display *display = MI_DISPLAY(mi);
316 Window window = MI_WINDOW(mi);
318 unistruct *gp = &universes[MI_SCREEN(mi)];
319 double d, eps, cox, six, cor, sir; /* tmp */
320 int i, j, k; /* more tmp */
321 XPoint *dummy = NULL;
326 cox = COSF(gp->rot_y);
327 six = SINF(gp->rot_y);
328 cor = COSF(gp->rot_x);
329 sir = SINF(gp->rot_x);
331 eps = 1/(EPSILON * sqrt_EPSILON * DELTAT * DELTAT * QCONS);
333 for (i = 0; i < gp->ngalaxies; ++i) {
334 Galaxy *gt = &gp->galaxies[i];
336 for (j = 0; j < gp->galaxies[i].nstars; ++j) {
337 Star *st = >->stars[j];
338 XPoint *newp = >->newpoints[j];
339 double v0 = st->vel[0];
340 double v1 = st->vel[1];
341 double v2 = st->vel[2];
343 for (k = 0; k < gp->ngalaxies; ++k) {
344 Galaxy *gtk = &gp->galaxies[k];
345 double d0 = gtk->pos[0] - st->pos[0];
346 double d1 = gtk->pos[1] - st->pos[1];
347 double d2 = gtk->pos[2] - st->pos[2];
349 d = d0 * d0 + d1 * d1 + d2 * d2;
351 d = gt->mass / (d * sqrt(d)) * DELTAT * DELTAT * QCONS;
367 newp->x = (short) (((cox * st->pos[0]) - (six * st->pos[2])) *
368 gp->scale) + gp->midx;
369 newp->y = (short) (((cor * st->pos[1]) - (sir * ((six * st->pos[0]) +
370 (cox * st->pos[2])))) * gp->scale) + gp->midy;
374 for (k = i + 1; k < gp->ngalaxies; ++k) {
375 Galaxy *gtk = &gp->galaxies[k];
376 double d0 = gtk->pos[0] - gt->pos[0];
377 double d1 = gtk->pos[1] - gt->pos[1];
378 double d2 = gtk->pos[2] - gt->pos[2];
380 d = d0 * d0 + d1 * d1 + d2 * d2;
382 d = gt->mass * gt->mass / (d * sqrt(d)) * DELTAT * QCONS;
384 d = gt->mass * gt->mass / (EPSILON * sqrt_EPSILON) * DELTAT * QCONS;
389 gt->vel[0] += d0 / gt->mass;
390 gt->vel[1] += d1 / gt->mass;
391 gt->vel[2] += d2 / gt->mass;
392 gtk->vel[0] -= d0 / gtk->mass;
393 gtk->vel[1] -= d1 / gtk->mass;
394 gtk->vel[2] -= d2 / gtk->mass;
397 gt->pos[0] += gt->vel[0] * DELTAT;
398 gt->pos[1] += gt->vel[1] * DELTAT;
399 gt->pos[2] += gt->vel[2] * DELTAT;
401 XSetForeground(display, gc, MI_WIN_BLACK_PIXEL(mi));
402 XDrawPoints(display, window, gc, gt->oldpoints, gt->nstars,
404 XSetForeground(display, gc, MI_PIXEL(mi, COLORSTEP * gt->galcol));
405 XDrawPoints(display, window, gc, gt->newpoints, gt->nstars,
408 dummy = gt->oldpoints;
409 gt->oldpoints = gt->newpoints;
410 gt->newpoints = dummy;
414 if (gp->step > gp->f_hititerations * 4)
419 release_galaxy(ModeInfo * mi)
421 if (universes != NULL) {
424 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
425 free_galaxies(&universes[screen]);
426 (void) free((void *) universes);
432 refresh_galaxy(ModeInfo * mi)
434 /* Do nothing, it will refresh by itself */