1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* glplanet --- 3D rotating planet, e.g., Earth.
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation for any purpose and without fee is hereby granted,
6 * provided that the above copyright notice appear in all copies and that
7 * both that copyright notice and this permission notice appear in
8 * supporting documentation.
10 * This file is provided AS IS with no warranties of any kind. The author
11 * shall have no liability with respect to the infringement of copyrights,
12 * trade secrets or any patents by this file or any part thereof. In no
13 * event will the author be liable for any lost revenue or profits or
14 * other special, indirect and consequential damages.
18 * 16-Jan-02: jwz@jwz.org gdk_pixbuf support.
19 * 21-Mar-01: jwz@jwz.org Broke sphere routine out into its own file.
21 * 9-Oct-98: dek@cgl.ucsf.edu Added stars.
23 * 8-Oct-98: jwz@jwz.org Made the 512x512x1 xearth image be built in.
24 * Made it possible to load XPM or XBM files.
25 * Made the planet bounce and roll around.
27 * 8-Oct-98: Released initial version of "glplanet"
28 * (David Konerding, dek@cgl.ucsf.edu)
33 * For even more spectacular results, grab the images from the "SSystem"
34 * package (http://www.msu.edu/user/kamelkev/) and use its JPEGs!
39 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
40 * otherwise caddr_t is not defined correctly
43 #include <X11/Intrinsic.h>
46 # define PROGCLASS "Planet"
47 # define HACK_INIT init_planet
48 # define HACK_DRAW draw_planet
49 # define HACK_RESHAPE reshape_planet
50 # define planet_opts xlockmore_opts
51 #define DEFAULTS "*delay: 15000 \n" \
52 "*showFPS: False \n" \
56 "*wireframe: False \n" \
60 "*image: BUILTIN \n" \
61 "*imageForeground: Green \n" \
62 "*imageBackground: Blue \n"
64 # include "xlockmore.h" /* from the xscreensaver distribution */
65 #else /* !STANDALONE */
66 # include "xlock.h" /* from the xlockmore distribution */
67 #endif /* !STANDALONE */
69 #ifdef USE_GL /* whole file */
75 # include <X11/Xmu/Drawing.h>
77 # include <Xmu/Drawing.h>
84 #define DEF_ROTATE "True"
85 #define DEF_ROLL "True"
86 #define DEF_BOUNCE "True"
87 #define DEF_TEXTURE "True"
88 #define DEF_STARS "True"
89 #define DEF_LIGHT "True"
90 #define DEF_IMAGE "BUILTIN"
93 #define countof(x) (sizeof((x))/sizeof((*x)))
98 static int do_texture;
101 static char *which_image;
102 static XrmOptionDescRec opts[] = {
103 {"-rotate", ".glplanet.rotate", XrmoptionNoArg, (caddr_t) "true" },
104 {"+rotate", ".glplanet.rotate", XrmoptionNoArg, (caddr_t) "false" },
105 {"-roll", ".glplanet.roll", XrmoptionNoArg, (caddr_t) "true" },
106 {"+roll", ".glplanet.roll", XrmoptionNoArg, (caddr_t) "false" },
107 {"-bounce", ".glplanet.bounce", XrmoptionNoArg, (caddr_t) "true" },
108 {"+bounce", ".glplanet.bounce", XrmoptionNoArg, (caddr_t) "false" },
109 {"-texture", ".glplanet.texture", XrmoptionNoArg, (caddr_t) "true" },
110 {"+texture", ".glplanet.texture", XrmoptionNoArg, (caddr_t) "false" },
111 {"-stars", ".glplanet.stars", XrmoptionNoArg, (caddr_t) "true" },
112 {"+stars", ".glplanet.stars", XrmoptionNoArg, (caddr_t) "false" },
113 {"-light", ".glplanet.light", XrmoptionNoArg, (caddr_t) "true" },
114 {"+light", ".glplanet.light", XrmoptionNoArg, (caddr_t) "false" },
115 {"-image", ".glplanet.image", XrmoptionSepArg, (caddr_t) 0 },
118 static argtype vars[] = {
119 {(caddr_t *) &do_rotate, "rotate", "Rotate", DEF_ROTATE, t_Bool},
120 {(caddr_t *) &do_roll, "roll", "Roll", DEF_ROLL, t_Bool},
121 {(caddr_t *) &do_bounce, "bounce", "Bounce", DEF_BOUNCE, t_Bool},
122 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
123 {(caddr_t *) &do_stars, "stars", "Stars", DEF_STARS, t_Bool},
124 {(caddr_t *) &do_light, "light", "Light", DEF_LIGHT, t_Bool},
125 {(caddr_t *) &which_image, "image", "Image", DEF_IMAGE, t_String},
128 ModeSpecOpt planet_opts = {countof(opts), opts, countof(vars), vars, NULL};
131 ModStruct planet_description =
132 {"planet", "init_planet", "draw_planet", "release_planet",
133 "draw_planet", "init_planet", NULL, &planet_opts,
134 1000, 1, 2, 1, 4, 1.0, "",
135 "Animates texture mapped sphere (planet)", 0, NULL};
138 #include "../images/earth.xbm"
139 #include "xpm-ximage.h"
143 * slices and stacks are used in the sphere parameterization routine.
144 * more slices and stacks will increase the quality of the sphere,
145 * at the expense of rendering speed
148 #define NUM_STARS 1000
152 /* radius of the sphere- fairly arbitrary */
155 /* distance away from the sphere model */
160 /* structure for holding the planet data */
164 int screen_width, screen_height;
165 GLXContext *glx_context;
171 GLfloat dtx, dty, dtz;
172 GLfloat xpos, ypos, zpos;
174 GLfloat box_width, box_height, box_depth;
179 static planetstruct *planets = NULL;
183 normalize(GLfloat v[3])
185 GLfloat d = (GLfloat) sqrt((double) (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]));
192 v[0] = v[1] = v[2] = 0;
197 /* Set up and enable texturing on our object */
199 setup_xbm_texture (char *bits, int width, int height,
200 XColor *fgc, XColor *bgc)
202 unsigned int fg = (((fgc->red >> 8) << 16) |
203 ((fgc->green >> 8) << 8) |
205 unsigned int bg = (((bgc->red >> 8) << 16) |
206 ((bgc->green >> 8) << 8) |
209 unsigned char *data = (unsigned char *)
210 malloc ((width * height * 24) / 8);
211 unsigned char *out = data;
214 for (y = 0; y < height; y++)
215 for (x = 0; x < width; x++)
217 unsigned char byte = bits [(y * (width / 8) + (x / 8))];
218 unsigned char bit = (byte & (1 << (x % 8))) >> (x % 8);
219 unsigned int word = (bit ? bg : fg);
220 *out++ = (word & 0xFF0000) >> 16;
221 *out++ = (word & 0x00FF00) >> 8;
222 *out++ = (word & 0x0000FF);
226 glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
227 GL_RGB, GL_UNSIGNED_BYTE, data);
228 check_gl_error("texture");
230 /* setup parameters for texturing */
231 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
232 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
233 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
234 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
235 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
236 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
241 setup_file_texture (ModeInfo *mi, char *filename)
243 Display *dpy = mi->dpy;
244 Visual *visual = mi->xgwa.visual;
246 Colormap cmap = mi->xgwa.colormap;
247 XImage *image = xpm_file_to_ximage (dpy, visual, cmap, filename);
250 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
251 image->width, image->height, 0,
252 GL_RGBA, GL_UNSIGNED_BYTE, image->data);
253 check_gl_error("texture");
255 /* setup parameters for texturing */
256 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
257 glPixelStorei(GL_UNPACK_ROW_LENGTH, image->width);
259 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
260 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
261 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
262 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
263 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
268 setup_texture(ModeInfo * mi)
270 planetstruct *gp = &planets[MI_SCREEN(mi)];
273 !strcmp(which_image, "BUILTIN"))
274 setup_xbm_texture (earth_bits, earth_width, earth_height,
277 setup_file_texture (mi, which_image);
281 /* Set up and enable lighting */
285 /* set a number of parameters which make the scene look much nicer */
287 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
288 glShadeModel(GL_SMOOTH);
292 /* Set up and enable face culling so we don't see the inside of the sphere */
296 glEnable(GL_CULL_FACE);
302 /* Function for determining points on the surface of the sphere */
303 static void inline ParametricSphere(float theta, float rho, GLfloat *vector)
305 vector[0] = -sin(theta) * sin(rho);
306 vector[1] = cos(theta) * sin(rho);
307 vector[2] = cos(rho);
310 vector[0] = -(1- cos(theta)) * cos(rho);
311 vector[1] = -(1- cos(theta)) * sin(rho);
312 vector[2] = -(sin(theta) + rho);
313 #endif /* DO_HELIX */
320 /* lame way to generate some random stars */
321 void generate_stars(int width, int height)
326 int steps = max_size / inc;
328 planetstruct *gp = &planets[MI_SCREEN(mi)];
330 gp->starlist = glGenLists(1);
331 glNewList(gp->starlist, GL_COMPILE);
333 /* this hackery makes the viewport map one-to-one with Vertex arguments */
334 glMatrixMode(GL_PROJECTION);
336 glMatrixMode(GL_PROJECTION);
338 gluOrtho2D(0, width, 0, height);
339 glMatrixMode(GL_MODELVIEW);
342 /* disable depth testing for the stars, so they don't obscure the planet */
343 glDisable(GL_DEPTH_TEST);
345 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
349 glEnable(GL_POINT_SMOOTH);
351 for (j = 1; j <= steps; j++)
353 glPointSize(inc * j);
355 for(i = 0 ; i < NUM_STARS / steps; i++)
357 glColor3f (0.6 + frand(0.3),
360 glVertex2f ((GLfloat) (random() % width),
361 (GLfloat) (random() % height));
366 /* return to original PROJECT and MODELVIEW */
367 glMatrixMode(GL_PROJECTION);
369 glMatrixMode(GL_MODELVIEW);
376 /* Set up lighting */
378 init_sun (ModeInfo * mi)
381 light[0] = frand(2.0) - 1.0;
382 light[1] = frand(2.0) - 1.0;
386 glLightfv(GL_LIGHT0, GL_POSITION, light);
390 /* Initialization function for screen saver */
394 Bool wire = MI_IS_WIREFRAME(mi);
395 planetstruct *gp = &planets[MI_SCREEN(mi)];
398 glEnable(GL_LINE_SMOOTH);
402 /* turn on various options we like */
411 glEnable(GL_POINT_SMOOTH);
412 generate_stars(MI_WIDTH(mi), MI_HEIGHT(mi));
415 gp->platelist=glGenLists(1);
416 glNewList(gp->platelist, GL_COMPILE);
418 glScalef (RADIUS, RADIUS, RADIUS);
419 unit_sphere (STACKS, SLICES, wire);
427 draw_sphere(ModeInfo * mi)
429 planetstruct *gp = &planets[MI_SCREEN(mi)];
431 glEnable(GL_DEPTH_TEST);
433 /* turn on the various attributes for making the sphere look nice */
435 glEnable(GL_TEXTURE_2D);
439 glEnable(GL_LIGHTING);
441 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
442 glEnable(GL_COLOR_MATERIAL);
445 glCallList(gp->platelist);
450 #define RANDSIGN() ((random() & 1) ? 1 : -1)
453 pick_velocity (ModeInfo * mi)
455 planetstruct *gp = &planets[MI_SCREEN(mi)];
457 gp->box_width = 15.0;
458 gp->box_height = 15.0;
465 gp->dtx = (frand(0.4) + frand(0.3)) * RANDSIGN();
466 gp->dty = (frand(0.4) + frand(0.3)) * RANDSIGN();
467 gp->dtz = (frand(5.0) + frand(5.0)); /* the sun sets in the west */
469 gp->dx = (frand(0.2) + frand(0.2)) * RANDSIGN();
470 gp->dy = (frand(0.2) + frand(0.2)) * RANDSIGN();
471 gp->dz = (frand(0.2) + frand(0.2)) * RANDSIGN();
476 rotate_and_move (ModeInfo * mi)
478 planetstruct *gp = &planets[MI_SCREEN(mi)];
483 while (gp->tx < 0) gp->tx += 360;
484 while (gp->tx > 360) gp->tx -= 360;
487 while (gp->ty < 0) gp->ty += 360;
488 while (gp->ty > 360) gp->ty -= 360;
494 while (gp->tz < 0) gp->tz += 360;
495 while (gp->tz > 360) gp->tz -= 360;
500 static int frame = 0;
501 # define SINOID(SCALE,SIZE) \
502 ((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
503 gp->xpos = SINOID(0.031, gp->box_width);
504 gp->ypos = SINOID(0.023, gp->box_height);
505 gp->zpos = SINOID(0.017, gp->box_depth);
511 /* Standard reshape function */
513 reshape_planet(ModeInfo *mi, int width, int height)
515 GLfloat h = (GLfloat) height / (GLfloat) width;
517 glViewport(0, 0, (GLint) width, (GLint) height);
518 glMatrixMode(GL_PROJECTION);
520 glFrustum(-1.0, 1.0, -h, h, 5.0, 100.0);
521 glMatrixMode(GL_MODELVIEW);
523 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
524 glTranslatef(0.0, 0.0, -DIST);
529 init_planet(ModeInfo * mi)
531 int screen = MI_SCREEN(mi);
535 if (planets == NULL) {
536 if ((planets = (planetstruct *) calloc(MI_NUM_SCREENS(mi),
537 sizeof (planetstruct))) == NULL)
540 gp = &planets[screen];
545 char *f = get_string_resource("imageForeground", "Foreground");
546 char *b = get_string_resource("imageBackground", "Background");
548 if (!f) f = strdup("white");
549 if (!b) b = strdup("black");
551 for (s = f + strlen(f)-1; s > f; s--)
552 if (*s == ' ' || *s == '\t')
554 for (s = b + strlen(b)-1; s > b; s--)
555 if (*s == ' ' || *s == '\t')
558 if (!XParseColor(mi->dpy, mi->xgwa.colormap, f, &gp->fg))
560 fprintf(stderr, "%s: unparsable color: \"%s\"\n", progname, f);
563 if (!XParseColor(mi->dpy, mi->xgwa.colormap, b, &gp->bg))
565 fprintf(stderr, "%s: unparsable color: \"%s\"\n", progname, f);
574 gp->window = MI_WINDOW(mi);
575 if ((gp->glx_context = init_GL(mi)) != NULL) {
576 reshape_planet(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
584 draw_planet(ModeInfo * mi)
586 planetstruct *gp = &planets[MI_SCREEN(mi)];
587 Display *display = MI_DISPLAY(mi);
588 Window window = MI_WINDOW(mi);
590 if (!gp->glx_context)
593 glDrawBuffer(GL_BACK);
594 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
596 glXMakeCurrent(display, window, *(gp->glx_context));
600 /* protect our modelview matrix and attributes */
602 glCallList(gp->starlist);
606 /* protect our modelview matrix and attributes */
608 glPushAttrib(GL_ALL_ATTRIB_BITS);
610 /* this pair of rotations seem to be necessary to orient the earth correctly */
614 glTranslatef(gp->xpos, gp->ypos, gp->zpos);
615 glRotatef(gp->tx, 1, 0, 0);
616 glRotatef(gp->ty, 0, 1, 0);
617 glRotatef(gp->tz, 0, 0, 1);
618 /* draw the sphere */
626 if (mi->fps_p) do_fps (mi);
628 glXSwapBuffers(display, window);
630 rotate_and_move (mi);
634 release_planet(ModeInfo * mi)
636 if (planets != NULL) {
639 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
640 planetstruct *gp = &planets[screen];
642 if (gp->glx_context) {
643 /* Display lists MUST be freed while their glXContext is current. */
644 glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
646 if (glIsList(gp->platelist))
647 glDeleteLists(gp->platelist, 1);
648 if (glIsList(gp->starlist))
649 glDeleteLists(gp->starlist, 1);
652 (void) free((void *) planets);