1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* pulsar --- pulsar module for xscreensaver */
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.
17 * 4-Apr-1999: dek@cgl.ucsf.edu Created module "pulsar"
18 * 27-Apr-1999: dek@cgl.ucsf.edu Submitted module "pulsar"
19 * 4-May-1999: jwz@jwz.org Added module "pulsar"
20 * 4-May-1999: dek@cgl.ucsf.edu Submitted module "pulsar" updates
23 * The pulsar screensaver draws a number of rotating, pulsing rectangles
24 * on your screen. Depending on the options you choose, you can set a number
25 * of interesting OpenGL parameters, including alpha blending, depth testing, fog,
26 * lighting, texturing, mipmapping, bilinear filtering, and line antialiasing.
27 * Additionally, there is a "frames per second" meter which gives an estimate of
28 * the speed of your graphics card.
30 * Example command-line switches:
31 * Only draw a single quad, and don't fill it (boring but fast)
32 * pulsar -wire -quads 1 -fps
34 * Only try this with hardware graphics acceleration (PPro 200 w/ a Voodoo 2 runs great)
35 * pulsar -quads 10 -texture -mipmap -texture_quality -light -fog -fps
44 * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
45 * otherwise caddr_t is not defined correctly
48 #include <X11/Intrinsic.h>
51 # define PROGCLASS "Screensaver"
52 # define HACK_INIT init_screensaver
53 # define HACK_DRAW draw_screensaver
54 # define HACK_RESHAPE reshape_screensaver
55 # define screensaver_opts xlockmore_opts
56 #define DEFAULTS "*delay: 10000 \n" \
57 "*showFPS: False \n" \
63 "*antialias: False \n" \
64 "*texture: False \n" \
65 "*texture_quality: False \n" \
67 "*doDepth: False \n" \
70 # include "xlockmore.h" /* from the xscreensaver distribution */
71 #else /* !STANDALONE */
72 # include "xlock.h" /* from the xlockmore distribution */
73 #endif /* !STANDALONE */
75 #ifdef USE_GL /* whole file */
79 # include <X11/Xmu/Drawing.h>
81 # include <Xmu/Drawing.h>
93 /* Functions for loading and storing textures */
95 #define checkImageWidth 64
96 #define checkImageHeight 64
98 /* Functions for handling the frames per second timer */
102 #define countof(x) (sizeof((x))/sizeof((*x)))
108 #define DEF_NUM_QUADS "5"
109 #define DEF_LIGHT "False"
110 #define DEF_WIRE "False"
111 #define DEF_BLEND "True"
112 #define DEF_FOG "False"
113 #define DEF_ANTIALIAS "False"
114 #define DEF_TEXTURE "False"
115 #define DEF_TEXTURE_QUALITY "False"
116 #define DEF_MIPMAP "False"
117 #define DEF_DO_DEPTH "False"
118 #define DEF_IMAGE "BUILTIN"
120 static int num_quads;
125 static int do_antialias;
126 static int do_texture;
127 static int do_texture_quality;
128 static int do_mipmap;
130 static char *which_image;
133 static XrmOptionDescRec opts[] = {
134 {"-quads", ".pulsar.quads", XrmoptionSepArg, (caddr_t) NULL },
135 {"-light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "true" },
136 {"+light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "false" },
137 {"-wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "true" },
138 {"+wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "false" },
139 {"-blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "true" },
140 {"+blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "false" },
141 {"-fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "true" },
142 {"+fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "false" },
143 {"-antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "true" },
144 {"+antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "false" },
145 {"-texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "true" },
146 {"+texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "false" },
147 {"-texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "true" },
148 {"+texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "false" },
149 {"-mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "true" },
150 {"+mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "false" },
151 {"-do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "true" },
152 {"+do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "false" },
153 {"-image", ".pulsar.image", XrmoptionSepArg, (caddr_t) NULL },
157 static argtype vars[] = {
158 {(caddr_t *) &num_quads, "quads", "Quads", DEF_NUM_QUADS, t_Int},
159 {(caddr_t *) &do_light, "light", "Light", DEF_LIGHT, t_Bool},
160 {(caddr_t *) &do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
161 {(caddr_t *) &do_blend, "blend", "Blend", DEF_BLEND, t_Bool},
162 {(caddr_t *) &do_fog, "fog", "Fog", DEF_FOG, t_Bool},
163 {(caddr_t *) &do_antialias, "antialias", "Antialias", DEF_ANTIALIAS, t_Bool},
164 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
165 {(caddr_t *) &do_texture_quality, "texture_quality", "Texture_quality", DEF_TEXTURE_QUALITY, t_Bool},
166 {(caddr_t *) &do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
167 {(caddr_t *) &do_depth, "doDepth", "DoDepth", DEF_DO_DEPTH, t_Bool},
168 {(caddr_t *) &which_image, "image", "Image", DEF_IMAGE, t_String},
172 static OptionStruct desc[] =
174 {"-quads num", "how many quads to draw"},
175 {"-/+ light", "whether to do enable lighting (slower)"},
176 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
177 {"-/+ blend", "whether to do enable blending (slower)"},
178 {"-/+ fog", "whether to do enable fog (?)"},
179 {"-/+ antialias", "whether to do enable antialiased lines (slower)"},
180 {"-/+ texture", "whether to do enable texturing (much slower)"},
181 {"-/+ texture_quality", "whether to do enable linear/mipmap filtering (much much slower)"},
182 {"-/+ mipmap", "whether to do enable mipmaps (much slower)"},
183 {"-/+ depth", "whether to do enable depth buffer checking (slower)"},
184 {"-image <filename>", "texture image to load (PPM, PPM4, TIFF(?), XPM(?))"},
187 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
190 ModStruct screensaver_description =
191 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
192 "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
193 1000, 1, 2, 1, 4, 1.0, "",
194 "OpenGL screensaver", 0, NULL};
198 /* structure for holding the screensaver data */
200 int screen_width, screen_height;
201 GLXContext *glx_context;
206 static screensaverstruct *Screensaver = NULL;
213 GLfloat dtx, dty, dtz;
214 GLfloat drx, dry, drz;
221 static float scale_x=1, scale_y=1, scale_z=1;
222 static int frame = 0;
226 GLubyte *Generate_Image(int *width, int *height, int *format)
232 *width = checkImageWidth;
233 *height = checkImageHeight;
234 result = (GLubyte *)malloc(4 * *width * *height);
237 for (i = 0; i < checkImageWidth; i++) {
238 for (j = 0; j < checkImageHeight; j++) {
239 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
240 result[counter++] = (GLubyte) c;
241 result[counter++] = (GLubyte) c;
242 result[counter++] = (GLubyte) c;
243 result[counter++] = (GLubyte) 255;
255 /* Load a TIFF texture: requires libtiff */
256 uint32 *LoadTIFF(char *filename, int *width, int *height, int *format)
264 tif = TIFFOpen(filename, "r");
266 fprintf(stderr, "Problem showing %s\n", filename);
267 return Generate_Image(&width, &height, &format);
269 if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
270 npixels = (tsize_t) (img.width * img.height);
271 raster = (uint32 *) _TIFFmalloc(npixels * (tsize_t) sizeof(uint32));
272 if (raster != NULL) {
273 if (TIFFRGBAImageGet(&img, raster, img.width, img.height) == 0) {
274 TIFFError(filename, emsg);
275 return Generate_Image(&width, &height, &format);
278 TIFFRGBAImageEnd(&img);
280 TIFFError(filename, emsg);
281 return Generate_Image(&width, &height, &format);
285 *height = img.height;
293 /* Load a modified version of PPM format with an extra byte for alpha */
294 GLubyte *LoadPPM4(const char *filename, int *width, int *height, int *format)
302 fp = fopen(filename, "rb");
305 fprintf(stderr, "Unable to open file '%s'\n", filename);
306 return Generate_Image(width, height, format);
309 if (!fgets(buff, sizeof(buff), fp))
311 perror("Unable to read header filename\n");
312 return Generate_Image(width, height, format);
315 if (buff[0] != '6' || buff[1] != 'P')
317 fprintf(stderr, "Invalid image format (must be `6P')\n");
318 return Generate_Image(width, height, format);
323 fgets(buff, sizeof(buff), fp);
325 while (buff[0] == '#');
327 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
329 fprintf(stderr, "Error loading image `%s'\n", filename);
330 return Generate_Image(width, height, format);
333 if (fscanf(fp, "%d", &maxval) != 1)
335 fprintf(stderr, "Error loading image `%s'\n", filename);
336 return Generate_Image(width, height, format);
339 while (fgetc(fp) != '\n')
342 data = (GLubyte *)malloc(4 * sizeX * sizeY);
345 fprintf(stderr, "Unable to allocate memory\n");
349 if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
351 fprintf(stderr, "Error loading image `%s'\n", filename);
352 return Generate_Image(width, height, format);
363 /* Load a plain PPM image */
364 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
372 fp = fopen(filename, "rb");
375 fprintf(stderr, "Unable to open file '%s'\n", filename);
376 return Generate_Image(width, height, format);
379 if (!fgets(buff, sizeof(buff), fp))
382 return Generate_Image(width, height, format);
385 if (buff[0] != 'P' || buff[1] != '6')
387 fprintf(stderr, "Invalid image format (must be `P6')\n");
388 return Generate_Image(width, height, format);
393 fgets(buff, sizeof(buff), fp);
395 while (buff[0] == '#');
397 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
399 fprintf(stderr, "Error loading image `%s'\n", filename);
400 return Generate_Image(width, height, format);
403 if (fscanf(fp, "%d", &maxval) != 1)
405 fprintf(stderr, "Error loading image `%s'\n", filename);
406 return Generate_Image(width, height, format);
409 while (fgetc(fp) != '\n')
412 data = (GLubyte *)malloc(3 * sizeX * sizeY);
415 fprintf(stderr, "Unable to allocate memory\n");
419 if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
421 fprintf(stderr, "Error loading image `%s'\n", filename);
422 return Generate_Image(width, height, format);
433 /* Create a texture in OpenGL. First an image is loaded
434 and stored in a raster buffer, then it's */
435 void Create_Texture(char *filename)
442 if ( !strncmp(filename, "BUILTIN", 7))
443 image = Generate_Image(&width, &height, &format);
444 else if ( !strncmp((filename+strlen(filename)-3), "ppm", 3))
445 image = LoadPPM(filename, &width, &height, &format);
446 else if ( !strncmp((filename+strlen(filename)-4), "ppm4", 4))
447 image = LoadPPM4(filename, &width, &height, &format);
449 else if ( !strncmp((filename+strlen(filename)-4), "tiff", 4))
450 image = (GLubyte *)LoadTIFF(filename, &width, &height, &format);
453 fprintf(stderr, "Unknown file format extension: '%s'\n", filename);
454 image = Generate_Image(&width, &height, &format);
457 /* GL_MODULATE or GL_DECAL depending on what you want */
458 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
459 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
460 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
461 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
462 /* perhaps we can edge a bit more speed at the expense of quality */
463 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
465 if (do_texture_quality) {
466 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
467 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
468 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
471 /* default is to do it quick and dirty */
472 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
474 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
477 /* mipmaps make the image look much nicer */
479 a=gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, image);
483 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
484 format, GL_UNSIGNED_BYTE, image);
485 check_gl_error("texture");
491 void resetProjection(void) {
492 glMatrixMode(GL_PROJECTION);
494 glFrustum(-1, 1, -1, 1, 1, 100);
495 glMatrixMode(GL_MODELVIEW);
500 void GenerateQuad(void)
504 quad_list = glGenLists(1);
505 glNewList(quad_list,GL_COMPILE);
508 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
509 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
510 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
511 glColor4f(1,1,1,1); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
513 glBegin(GL_TRIANGLE_STRIP);
514 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
515 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
516 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
517 glColor4f(1,1,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
522 quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
523 for (i=0; i < num_quads; i++)
532 quads[i].drx = frand(5.0);
533 quads[i].dry = frand(5.0);
538 void initializeGL(GLsizei width, GLsizei height)
540 GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
542 glViewport( 0, 0, width, height );
546 glEnable(GL_DEPTH_TEST);
550 glEnable(GL_LINE_SMOOTH);
555 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
560 glShadeModel(GL_SMOOTH);
561 glEnable(GL_COLOR_MATERIAL);
562 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
566 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
568 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
572 glFogi(GL_FOG_MODE, GL_LINEAR);
573 glFogfv(GL_FOG_COLOR, fogColor);
574 glFogf(GL_FOG_DENSITY, 0.35);
575 /* glHint(GL_FOG_HINT, GL_FASTEST); */
576 glFogf(GL_FOG_START, 50.0);
577 glFogf(GL_FOG_END, 100.0);
582 Create_Texture(which_image);
586 void drawQuads(void) {
588 for (i=0; i < num_quads; i++)
591 glTranslatef(quads[i].tx,0,0);
592 glTranslatef(0,quads[i].ty,0);
593 glTranslatef(0,0,quads[i].tz);
594 glRotatef(quads[i].rx, 1,0,0);
595 glRotatef(quads[i].ry, 0,1,0);
596 glRotatef(quads[i].rz, 0,0,1);
597 glCallList(quad_list);
600 quads[i].rx += quads[i].drx;
601 quads[i].ry += quads[i].dry;
602 quads[i].rz += quads[i].drz;
607 GLvoid drawScene(ModeInfo * mi)
609 /* check_gl_error ("drawScene"); */
610 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
612 /* we have to do this here because the FPS meter turns these 3 features off!! */
615 glEnable(GL_LIGHTING);
620 glEnable(GL_TEXTURE_2D);
625 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
631 /* use XYZ scaling factors to change the size of the pulsar */
632 glScalef(scale_x, scale_y, scale_z);
635 /* update the scaling factors- cyclic */
636 scale_x = cos(frame/360.)*10.;
637 scale_y = sin(frame/360.)*10.;
640 /* increment frame-counter */
643 /* check_gl_error ("drawScene"); */
647 void draw_screensaver(ModeInfo * mi)
649 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
650 Display *display = MI_DISPLAY(mi);
651 Window window = MI_WINDOW(mi);
653 if (!gp->glx_context)
656 glXMakeCurrent(display, window, *(gp->glx_context));
658 if (mi->fps_p) do_fps (mi);
659 glXSwapBuffers(display, window);
662 /* Standard reshape function */
664 reshape_screensaver(ModeInfo *mi, int width, int height)
666 glViewport( 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi) );
671 init_screensaver(ModeInfo * mi)
673 int screen = MI_SCREEN(mi);
675 screensaverstruct *gp;
677 if (Screensaver == NULL) {
678 if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
681 gp = &Screensaver[screen];
683 gp->window = MI_WINDOW(mi);
684 if ((gp->glx_context = init_GL(mi)) != NULL) {
685 reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
686 initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
693 /* all sorts of nice cleanup code should go here! */
694 void release_screensaver(ModeInfo * mi)
697 if (Screensaver != NULL) {
698 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
699 /* screensaverstruct *gp = &Screensaver[screen];*/
701 (void) free((void *) Screensaver);