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 # ifndef PIXEL_ALREADY_TYPEDEFED
80 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
86 # include <X11/Xmu/Drawing.h>
88 # include <Xmu/Drawing.h>
100 /* Functions for loading and storing textures */
102 #define checkImageWidth 64
103 #define checkImageHeight 64
105 /* Functions for handling the frames per second timer */
109 #define countof(x) (sizeof((x))/sizeof((*x)))
115 #define DEF_NUM_QUADS "5"
116 #define DEF_LIGHT "False"
117 #define DEF_WIRE "False"
118 #define DEF_BLEND "True"
119 #define DEF_FOG "False"
120 #define DEF_ANTIALIAS "False"
121 #define DEF_TEXTURE "False"
122 #define DEF_TEXTURE_QUALITY "False"
123 #define DEF_MIPMAP "False"
124 #define DEF_DO_DEPTH "False"
125 #define DEF_IMAGE "BUILTIN"
127 static int num_quads;
132 static int do_antialias;
133 static int do_texture;
134 static int do_texture_quality;
135 static int do_mipmap;
137 static char *which_image;
140 static XrmOptionDescRec opts[] = {
141 {"-quads", ".pulsar.quads", XrmoptionSepArg, (caddr_t) NULL },
142 {"-light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "true" },
143 {"+light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "false" },
144 {"-wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "true" },
145 {"+wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "false" },
146 {"-blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "true" },
147 {"+blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "false" },
148 {"-fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "true" },
149 {"+fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "false" },
150 {"-antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "true" },
151 {"+antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "false" },
152 {"-texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "true" },
153 {"+texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "false" },
154 {"-texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "true" },
155 {"+texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "false" },
156 {"-mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "true" },
157 {"+mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "false" },
158 {"-do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "true" },
159 {"+do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "false" },
160 {"-image", ".pulsar.image", XrmoptionSepArg, (caddr_t) NULL },
164 static argtype vars[] = {
165 {(caddr_t *) &num_quads, "quads", "Quads", DEF_NUM_QUADS, t_Int},
166 {(caddr_t *) &do_light, "light", "Light", DEF_LIGHT, t_Bool},
167 {(caddr_t *) &do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
168 {(caddr_t *) &do_blend, "blend", "Blend", DEF_BLEND, t_Bool},
169 {(caddr_t *) &do_fog, "fog", "Fog", DEF_FOG, t_Bool},
170 {(caddr_t *) &do_antialias, "antialias", "Antialias", DEF_ANTIALIAS, t_Bool},
171 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
172 {(caddr_t *) &do_texture_quality, "texture_quality", "Texture_quality", DEF_TEXTURE_QUALITY, t_Bool},
173 {(caddr_t *) &do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
174 {(caddr_t *) &do_depth, "doDepth", "DoDepth", DEF_DO_DEPTH, t_Bool},
175 {(caddr_t *) &which_image, "image", "Image", DEF_IMAGE, t_String},
179 static OptionStruct desc[] =
181 {"-quads num", "how many quads to draw"},
182 {"-/+ light", "whether to do enable lighting (slower)"},
183 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
184 {"-/+ blend", "whether to do enable blending (slower)"},
185 {"-/+ fog", "whether to do enable fog (?)"},
186 {"-/+ antialias", "whether to do enable antialiased lines (slower)"},
187 {"-/+ texture", "whether to do enable texturing (much slower)"},
188 {"-/+ texture_quality", "whether to do enable linear/mipmap filtering (much much slower)"},
189 {"-/+ mipmap", "whether to do enable mipmaps (much slower)"},
190 {"-/+ depth", "whether to do enable depth buffer checking (slower)"},
191 {"-image <filename>", "texture image to load (PPM, PPM4, TIFF(?), XPM(?))"},
194 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
197 ModStruct screensaver_description =
198 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
199 "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
200 1000, 1, 2, 1, 4, 1.0, "",
201 "OpenGL screensaver", 0, NULL};
205 /* structure for holding the screensaver data */
207 int screen_width, screen_height;
208 GLXContext *glx_context;
213 static screensaverstruct *Screensaver = NULL;
220 GLfloat dtx, dty, dtz;
221 GLfloat drx, dry, drz;
228 static float scale_x=1, scale_y=1, scale_z=1;
229 static int frame = 0;
233 GLubyte *Generate_Image(int *width, int *height, int *format)
239 *width = checkImageWidth;
240 *height = checkImageHeight;
241 result = (GLubyte *)malloc(4 * *width * *height);
244 for (i = 0; i < checkImageWidth; i++) {
245 for (j = 0; j < checkImageHeight; j++) {
246 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
247 result[counter++] = (GLubyte) c;
248 result[counter++] = (GLubyte) c;
249 result[counter++] = (GLubyte) c;
250 result[counter++] = (GLubyte) 255;
262 /* Load a TIFF texture: requires libtiff */
263 uint32 *LoadTIFF(char *filename, int *width, int *height, int *format)
271 tif = TIFFOpen(filename, "r");
273 fprintf(stderr, "Problem showing %s\n", filename);
274 return Generate_Image(&width, &height, &format);
276 if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
277 npixels = (tsize_t) (img.width * img.height);
278 raster = (uint32 *) _TIFFmalloc(npixels * (tsize_t) sizeof(uint32));
279 if (raster != NULL) {
280 if (TIFFRGBAImageGet(&img, raster, img.width, img.height) == 0) {
281 TIFFError(filename, emsg);
282 return Generate_Image(&width, &height, &format);
285 TIFFRGBAImageEnd(&img);
287 TIFFError(filename, emsg);
288 return Generate_Image(&width, &height, &format);
292 *height = img.height;
300 /* Load a modified version of PPM format with an extra byte for alpha */
301 GLubyte *LoadPPM4(const char *filename, int *width, int *height, int *format)
309 fp = fopen(filename, "rb");
312 fprintf(stderr, "Unable to open file '%s'\n", filename);
313 return Generate_Image(width, height, format);
316 if (!fgets(buff, sizeof(buff), fp))
318 perror("Unable to read header filename\n");
319 return Generate_Image(width, height, format);
322 if (buff[0] != '6' || buff[1] != 'P')
324 fprintf(stderr, "Invalid image format (must be `6P')\n");
325 return Generate_Image(width, height, format);
330 fgets(buff, sizeof(buff), fp);
332 while (buff[0] == '#');
334 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
336 fprintf(stderr, "Error loading image `%s'\n", filename);
337 return Generate_Image(width, height, format);
340 if (fscanf(fp, "%d", &maxval) != 1)
342 fprintf(stderr, "Error loading image `%s'\n", filename);
343 return Generate_Image(width, height, format);
346 while (fgetc(fp) != '\n')
349 data = (GLubyte *)malloc(4 * sizeX * sizeY);
352 fprintf(stderr, "Unable to allocate memory\n");
356 if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
358 fprintf(stderr, "Error loading image `%s'\n", filename);
359 return Generate_Image(width, height, format);
370 /* Load a plain PPM image */
371 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
379 fp = fopen(filename, "rb");
382 fprintf(stderr, "Unable to open file '%s'\n", filename);
383 return Generate_Image(width, height, format);
386 if (!fgets(buff, sizeof(buff), fp))
389 return Generate_Image(width, height, format);
392 if (buff[0] != 'P' || buff[1] != '6')
394 fprintf(stderr, "Invalid image format (must be `P6')\n");
395 return Generate_Image(width, height, format);
400 fgets(buff, sizeof(buff), fp);
402 while (buff[0] == '#');
404 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
406 fprintf(stderr, "Error loading image `%s'\n", filename);
407 return Generate_Image(width, height, format);
410 if (fscanf(fp, "%d", &maxval) != 1)
412 fprintf(stderr, "Error loading image `%s'\n", filename);
413 return Generate_Image(width, height, format);
416 while (fgetc(fp) != '\n')
419 data = (GLubyte *)malloc(3 * sizeX * sizeY);
422 fprintf(stderr, "Unable to allocate memory\n");
426 if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
428 fprintf(stderr, "Error loading image `%s'\n", filename);
429 return Generate_Image(width, height, format);
440 /* Create a texture in OpenGL. First an image is loaded
441 and stored in a raster buffer, then it's */
442 void Create_Texture(char *filename)
449 if ( !strncmp(filename, "BUILTIN", 7))
450 image = Generate_Image(&width, &height, &format);
451 else if ( !strncmp((filename+strlen(filename)-3), "ppm", 3))
452 image = LoadPPM(filename, &width, &height, &format);
453 else if ( !strncmp((filename+strlen(filename)-4), "ppm4", 4))
454 image = LoadPPM4(filename, &width, &height, &format);
456 else if ( !strncmp((filename+strlen(filename)-4), "tiff", 4))
457 image = (GLubyte *)LoadTIFF(filename, &width, &height, &format);
460 fprintf(stderr, "Unknown file format extension: '%s'\n", filename);
461 image = Generate_Image(&width, &height, &format);
464 /* GL_MODULATE or GL_DECAL depending on what you want */
465 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
466 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
467 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
468 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
469 /* perhaps we can edge a bit more speed at the expense of quality */
470 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
472 if (do_texture_quality) {
473 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
474 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
475 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
478 /* default is to do it quick and dirty */
479 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
480 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
481 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
484 /* mipmaps make the image look much nicer */
486 a=gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, image);
490 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
491 format, GL_UNSIGNED_BYTE, image);
492 check_gl_error("texture");
498 void resetProjection(void) {
499 glMatrixMode(GL_PROJECTION);
501 glFrustum(-1, 1, -1, 1, 1, 100);
502 glMatrixMode(GL_MODELVIEW);
507 void GenerateQuad(void)
511 quad_list = glGenLists(1);
512 glNewList(quad_list,GL_COMPILE);
515 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
516 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
517 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
518 glColor4f(1,1,1,1); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
520 glBegin(GL_TRIANGLE_STRIP);
521 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
522 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
523 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
524 glColor4f(1,1,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
529 quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
530 for (i=0; i < num_quads; i++)
539 quads[i].drx = frand(5.0);
540 quads[i].dry = frand(5.0);
545 void initializeGL(GLsizei width, GLsizei height)
547 GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
549 glViewport( 0, 0, width, height );
553 glEnable(GL_DEPTH_TEST);
557 glEnable(GL_LINE_SMOOTH);
562 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
567 glShadeModel(GL_SMOOTH);
568 glEnable(GL_COLOR_MATERIAL);
569 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
573 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
575 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
579 glFogi(GL_FOG_MODE, GL_LINEAR);
580 glFogfv(GL_FOG_COLOR, fogColor);
581 glFogf(GL_FOG_DENSITY, 0.35);
582 /* glHint(GL_FOG_HINT, GL_FASTEST); */
583 glFogf(GL_FOG_START, 50.0);
584 glFogf(GL_FOG_END, 100.0);
589 Create_Texture(which_image);
593 void drawQuads(void) {
595 for (i=0; i < num_quads; i++)
598 glTranslatef(quads[i].tx,0,0);
599 glTranslatef(0,quads[i].ty,0);
600 glTranslatef(0,0,quads[i].tz);
601 glRotatef(quads[i].rx, 1,0,0);
602 glRotatef(quads[i].ry, 0,1,0);
603 glRotatef(quads[i].rz, 0,0,1);
604 glCallList(quad_list);
607 quads[i].rx += quads[i].drx;
608 quads[i].ry += quads[i].dry;
609 quads[i].rz += quads[i].drz;
614 GLvoid drawScene(ModeInfo * mi)
616 check_gl_error ("drawScene");
617 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
619 /* we have to do this here because the FPS meter turns these 3 features off!! */
622 glEnable(GL_LIGHTING);
627 glEnable(GL_TEXTURE_2D);
632 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
638 /* use XYZ scaling factors to change the size of the pulsar */
639 glScalef(scale_x, scale_y, scale_z);
642 /* update the scaling factors- cyclic */
643 scale_x = cos(frame/360.)*10.;
644 scale_y = sin(frame/360.)*10.;
647 /* increment frame-counter */
650 check_gl_error ("drawScene");
654 void draw_screensaver(ModeInfo * mi)
656 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
657 Display *display = MI_DISPLAY(mi);
658 Window window = MI_WINDOW(mi);
660 if (!gp->glx_context)
663 glXMakeCurrent(display, window, *(gp->glx_context));
665 if (mi->fps_p) do_fps (mi);
666 glXSwapBuffers(display, window);
669 /* Standard reshape function */
671 reshape_screensaver(ModeInfo *mi, int width, int height)
673 glViewport( 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi) );
678 init_screensaver(ModeInfo * mi)
680 int screen = MI_SCREEN(mi);
682 screensaverstruct *gp;
684 if (Screensaver == NULL) {
685 if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
688 gp = &Screensaver[screen];
690 gp->window = MI_WINDOW(mi);
691 if ((gp->glx_context = init_GL(mi)) != NULL) {
692 reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
693 initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
700 /* all sorts of nice cleanup code should go here! */
701 void release_screensaver(ModeInfo * mi)
704 if (Screensaver != NULL) {
705 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
706 /* screensaverstruct *gp = &Screensaver[screen];*/
708 (void) free((void *) Screensaver);