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 screensaver_opts xlockmore_opts
55 #define DEFAULTS "*light: False \n" \
60 "*antialias: False \n" \
61 "*texture: False \n" \
62 "*texture_quality: False \n" \
65 "*doDepth: False \n" \
68 # include "xlockmore.h" /* from the xscreensaver distribution */
69 #else /* !STANDALONE */
70 # include "xlock.h" /* from the xlockmore distribution */
71 #endif /* !STANDALONE */
73 #ifdef USE_GL /* whole file */
77 # ifndef PIXEL_ALREADY_TYPEDEFED
78 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
84 # include <X11/Xmu/Drawing.h>
86 # include <Xmu/Drawing.h>
98 /* Functions for loading and storing textures */
100 #define checkImageWidth 64
101 #define checkImageHeight 64
103 /* Functions for handling the frames per second timer */
106 #ifndef SAMPLE_FRAMES
107 #define SAMPLE_FRAMES 10
111 #define countof(x) (sizeof((x))/sizeof((*x)))
117 #define DEF_NUM_QUADS "5"
118 #define DEF_LIGHT "False"
119 #define DEF_WIRE "False"
120 #define DEF_BLEND "True"
121 #define DEF_FOG "False"
122 #define DEF_ANTIALIAS "False"
123 #define DEF_TEXTURE "False"
124 #define DEF_TEXTURE_QUALITY "False"
125 #define DEF_MIPMAP "False"
126 #define DEF_FPS "False"
127 #define DEF_DO_DEPTH "False"
128 #define DEF_IMAGE "BUILTIN"
130 static int num_quads;
135 static int do_antialias;
136 static int do_texture;
137 static int do_texture_quality;
138 static int do_mipmap;
141 static char *which_image;
144 static XrmOptionDescRec opts[] = {
145 {"-quads", ".pulsar.quads", XrmoptionSepArg, (caddr_t) NULL },
146 {"-light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "true" },
147 {"+light", ".pulsar.light", XrmoptionNoArg, (caddr_t) "false" },
148 {"-wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "true" },
149 {"+wire", ".pulsar.wire", XrmoptionNoArg, (caddr_t) "false" },
150 {"-blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "true" },
151 {"+blend", ".pulsar.blend", XrmoptionNoArg, (caddr_t) "false" },
152 {"-fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "true" },
153 {"+fog", ".pulsar.fog", XrmoptionNoArg, (caddr_t) "false" },
154 {"-antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "true" },
155 {"+antialias", ".pulsar.antialias", XrmoptionNoArg, (caddr_t) "false" },
156 {"-texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "true" },
157 {"+texture", ".pulsar.texture", XrmoptionNoArg, (caddr_t) "false" },
158 {"-texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "true" },
159 {"+texture_quality", ".pulsar.texture_quality", XrmoptionNoArg, (caddr_t) "false" },
160 {"-mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "true" },
161 {"+mipmap", ".pulsar.mipmap", XrmoptionNoArg, (caddr_t) "false" },
162 {"-fps", ".pulsar.fps", XrmoptionNoArg, (caddr_t) "true" },
163 {"+fps", ".pulsar.fps", XrmoptionNoArg, (caddr_t) "false" },
164 {"-do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "true" },
165 {"+do_depth", ".pulsar.doDepth", XrmoptionNoArg, (caddr_t) "false" },
166 {"-image", ".pulsar.image", XrmoptionSepArg, (caddr_t) NULL },
170 static argtype vars[] = {
171 {(caddr_t *) &num_quads, "quads", "Quads", DEF_NUM_QUADS, t_Int},
172 {(caddr_t *) &do_light, "light", "Light", DEF_LIGHT, t_Bool},
173 {(caddr_t *) &do_wire, "wire", "Wire", DEF_WIRE, t_Bool},
174 {(caddr_t *) &do_blend, "blend", "Blend", DEF_BLEND, t_Bool},
175 {(caddr_t *) &do_fog, "fog", "Fog", DEF_FOG, t_Bool},
176 {(caddr_t *) &do_antialias, "antialias", "Antialias", DEF_ANTIALIAS, t_Bool},
177 {(caddr_t *) &do_texture, "texture", "Texture", DEF_TEXTURE, t_Bool},
178 {(caddr_t *) &do_texture_quality, "texture_quality", "Texture_quality", DEF_TEXTURE_QUALITY, t_Bool},
179 {(caddr_t *) &do_mipmap, "mipmap", "Mipmap", DEF_MIPMAP, t_Bool},
180 {(caddr_t *) &do_fps, "fps", "fps", DEF_FPS, t_Bool},
181 {(caddr_t *) &do_depth, "doDepth", "DoDepth", DEF_DO_DEPTH, t_Bool},
182 {(caddr_t *) &which_image, "image", "Image", DEF_IMAGE, t_String},
186 static OptionStruct desc[] =
188 {"-quads num", "how many quads to draw"},
189 {"-/+ light", "whether to do enable lighting (slower)"},
190 {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
191 {"-/+ blend", "whether to do enable blending (slower)"},
192 {"-/+ fog", "whether to do enable fog (?)"},
193 {"-/+ antialias", "whether to do enable antialiased lines (slower)"},
194 {"-/+ texture", "whether to do enable texturing (much slower)"},
195 {"-/+ texture_quality", "whether to do enable linear/mipmap filtering (much much slower)"},
196 {"-/+ mipmap", "whether to do enable mipmaps (much slower)"},
197 {"-/+ fps", "whether to do enable frames per second meter (?)"},
198 {"-/+ depth", "whether to do enable depth buffer checking (slower)"},
199 {"-image <filename>", "texture image to load (PPM, PPM4, TIFF(?), XPM(?))"},
202 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
205 ModStruct screensaver_description =
206 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
207 "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
208 1000, 1, 2, 1, 4, 1.0, "",
209 "OpenGL screensaver", 0, NULL};
213 /* structure for holding the screensaver data */
215 int screen_width, screen_height;
216 GLXContext *glx_context;
221 static screensaverstruct *Screensaver = NULL;
228 GLfloat dtx, dty, dtz;
229 GLfloat drx, dry, drz;
233 int global_width=WIDTH, global_height=HEIGHT;
237 static int FrameCounter = 0;
238 static double oldtime=-1., newtime=-1.;
239 static char FPSstring[1024]="FPS: NONE";
241 static float x_pos=0, y_pos=0;
243 #define FONT "-*-courier-medium-r-normal-*-240-*"
246 static float scale_x=1, scale_y=1, scale_z=1;
247 static int frame = 0;
249 static GLenum errCode;
250 static const GLubyte *errString;
254 int checkError(int line, char *file)
256 if((errCode = glGetError()) != GL_NO_ERROR) {
257 errString = (GLubyte *)gluErrorString(errCode);
258 fprintf(stderr, "OpenGL error: %s detected at line %d in file %s\n", errString, line, file);
268 XFontStruct *fontInfo;
270 int first=0, last=255;
272 Dpy = XOpenDisplay(NULL);
273 fontInfo = XLoadQueryFont(Dpy, FONT);
274 if (fontInfo == NULL)
276 fprintf(stderr, "Failed to load font %s\n", FONT);
281 first = fontInfo->min_char_or_byte2;
282 last = fontInfo->max_char_or_byte2;
284 base = glGenLists((GLuint) last+1);
286 fprintf (stderr, "out of display lists\n");
289 glXUseXFont(id, first, last-first+1, base+first);
293 void PrintString(float x, float y, char *string)
297 /* save the current state */
298 /* note: could be expensive! */
299 glPushAttrib(GL_ALL_ATTRIB_BITS);
300 glMatrixMode(GL_PROJECTION);
302 gluOrtho2D(0, global_width, 0, global_height);
303 glMatrixMode(GL_MODELVIEW);
306 /* disable lighting and texturing when drawing bitmaps! */
307 glDisable(GL_TEXTURE_2D);
308 glDisable(GL_LIGHTING);
311 /* draw a black background */
315 glRasterPos2f( x, y);
316 len = (int) strlen(string);
317 for (i = 0; i < len; i++) {
318 if (glIsList(base+string[i]))
319 glCallList(base+string[i]);
321 fprintf(stderr, "%d+string[%d] is not a display list!\n", base, i);
324 /* clean up after our state changes */
331 #ifdef GETTIMEOFDAY_TWO_ARGS
333 gettimeofday(&now, &tzp);
337 return (double) (now.tv_sec + (((double) now.tv_usec) * 0.000001));
342 /* every SAMPLE_FRAMES frames, get the time and use it to get the
344 if (!(FrameCounter % SAMPLE_FRAMES)) {
347 sprintf(FPSstring, "FPS: %.02f", SAMPLE_FRAMES/(newtime-oldtime));
350 PrintString(x_pos,y_pos,FPSstring);
355 GLubyte *Generate_Image(int *width, int *height, int *format)
361 *width = checkImageWidth;
362 *height = checkImageHeight;
363 result = (GLubyte *)malloc(4 * *width * *height);
366 for (i = 0; i < checkImageWidth; i++) {
367 for (j = 0; j < checkImageHeight; j++) {
368 c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
369 result[counter++] = (GLubyte) c;
370 result[counter++] = (GLubyte) c;
371 result[counter++] = (GLubyte) c;
372 result[counter++] = (GLubyte) 255;
384 /* Load a TIFF texture: requires libtiff */
385 uint32 *LoadTIFF(char *filename, int *width, int *height, int *format)
393 tif = TIFFOpen(filename, "r");
395 fprintf(stderr, "Problem showing %s\n", filename);
396 return Generate_Image(&width, &height, &format);
398 if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
399 npixels = (tsize_t) (img.width * img.height);
400 raster = (uint32 *) _TIFFmalloc(npixels * (tsize_t) sizeof(uint32));
401 if (raster != NULL) {
402 if (TIFFRGBAImageGet(&img, raster, img.width, img.height) == 0) {
403 TIFFError(filename, emsg);
404 return Generate_Image(&width, &height, &format);
407 TIFFRGBAImageEnd(&img);
409 TIFFError(filename, emsg);
410 return Generate_Image(&width, &height, &format);
414 *height = img.height;
422 /* Load a modified version of PPM format with an extra byte for alpha */
423 GLubyte *LoadPPM4(const char *filename, int *width, int *height, int *format)
431 fp = fopen(filename, "rb");
434 fprintf(stderr, "Unable to open file '%s'\n", filename);
435 return Generate_Image(width, height, format);
438 if (!fgets(buff, sizeof(buff), fp))
440 perror("Unable to read header filename\n");
441 return Generate_Image(width, height, format);
444 if (buff[0] != '6' || buff[1] != 'P')
446 fprintf(stderr, "Invalid image format (must be `6P')\n");
447 return Generate_Image(width, height, format);
452 fgets(buff, sizeof(buff), fp);
454 while (buff[0] == '#');
456 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
458 fprintf(stderr, "Error loading image `%s'\n", filename);
459 return Generate_Image(width, height, format);
462 if (fscanf(fp, "%d", &maxval) != 1)
464 fprintf(stderr, "Error loading image `%s'\n", filename);
465 return Generate_Image(width, height, format);
468 while (fgetc(fp) != '\n')
471 data = (GLubyte *)malloc(4 * sizeX * sizeY);
474 fprintf(stderr, "Unable to allocate memory\n");
478 if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
480 fprintf(stderr, "Error loading image `%s'\n", filename);
481 return Generate_Image(width, height, format);
492 /* Load a plain PPM image */
493 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
501 fp = fopen(filename, "rb");
504 fprintf(stderr, "Unable to open file '%s'\n", filename);
505 return Generate_Image(width, height, format);
508 if (!fgets(buff, sizeof(buff), fp))
511 return Generate_Image(width, height, format);
514 if (buff[0] != 'P' || buff[1] != '6')
516 fprintf(stderr, "Invalid image format (must be `P6')\n");
517 return Generate_Image(width, height, format);
522 fgets(buff, sizeof(buff), fp);
524 while (buff[0] == '#');
526 if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
528 fprintf(stderr, "Error loading image `%s'\n", filename);
529 return Generate_Image(width, height, format);
532 if (fscanf(fp, "%d", &maxval) != 1)
534 fprintf(stderr, "Error loading image `%s'\n", filename);
535 return Generate_Image(width, height, format);
538 while (fgetc(fp) != '\n')
541 data = (GLubyte *)malloc(3 * sizeX * sizeY);
544 fprintf(stderr, "Unable to allocate memory\n");
548 if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
550 fprintf(stderr, "Error loading image `%s'\n", filename);
551 return Generate_Image(width, height, format);
562 /* Create a texture in OpenGL. First an image is loaded
563 and stored in a raster buffer, then it's */
564 void Create_Texture(char *filename)
571 if ( !strncmp(filename, "BUILTIN", 7))
572 image = Generate_Image(&width, &height, &format);
573 else if ( !strncmp((filename+strlen(filename)-3), "ppm", 3))
574 image = LoadPPM(filename, &width, &height, &format);
575 else if ( !strncmp((filename+strlen(filename)-4), "ppm4", 4))
576 image = LoadPPM4(filename, &width, &height, &format);
578 else if ( !strncmp((filename+strlen(filename)-4), "tiff", 4))
579 image = (GLubyte *)LoadTIFF(filename, &width, &height, &format);
582 fprintf(stderr, "Unknown file format extension: '%s'\n", filename);
583 image = Generate_Image(&width, &height, &format);
586 /* GL_MODULATE or GL_DECAL depending on what you want */
587 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
588 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
589 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
590 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
591 /* perhaps we can edge a bit more speed at the expense of quality */
592 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
594 if (do_texture_quality) {
595 /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
596 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
597 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
600 /* default is to do it quick and dirty */
601 /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
602 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
603 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
606 /* mipmaps make the image look much nicer */
608 a=gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, image);
612 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
613 format, GL_UNSIGNED_BYTE, image);
614 check_gl_error("texture");
620 void resetProjection(void) {
621 glMatrixMode(GL_PROJECTION);
623 glFrustum(-1, 1, -1, 1, 1, 100);
624 glMatrixMode(GL_MODELVIEW);
629 void GenerateQuad(void)
633 quad_list = glGenLists(1);
634 glNewList(quad_list,GL_COMPILE);
637 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
638 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
639 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
640 glColor4f(1,1,1,1); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
642 glBegin(GL_TRIANGLE_STRIP);
643 glColor4f(0,1,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,1); glVertex2f(-1, 1);
644 glColor4f(1,0,0,.4); glNormal3f(0,0,1); glTexCoord2f(0,0); glVertex2f(-1, -1);
645 glColor4f(0,0,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,1); glVertex2f( 1, 1);
646 glColor4f(1,1,1,.4); glNormal3f(0,0,1); glTexCoord2f(1,0); glVertex2f( 1, -1);
651 quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
652 for (i=0; i < num_quads; i++)
661 quads[i].drx = frand(5.0);
662 quads[i].dry = frand(5.0);
667 void initializeGL(GLsizei width, GLsizei height)
669 GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
672 global_height=height;
674 glViewport( 0, 0, width, height );
678 glEnable(GL_DEPTH_TEST);
685 glEnable(GL_LINE_SMOOTH);
690 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
695 glShadeModel(GL_SMOOTH);
696 glEnable(GL_COLOR_MATERIAL);
697 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
701 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
703 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
707 glFogi(GL_FOG_MODE, GL_LINEAR);
708 glFogfv(GL_FOG_COLOR, fogColor);
709 glFogf(GL_FOG_DENSITY, 0.35);
710 /* glHint(GL_FOG_HINT, GL_FASTEST); */
711 glFogf(GL_FOG_START, 50.0);
712 glFogf(GL_FOG_END, 100.0);
717 Create_Texture(which_image);
721 void drawQuads(void) {
723 for (i=0; i < num_quads; i++)
726 glTranslatef(quads[i].tx,0,0);
727 glTranslatef(0,quads[i].ty,0);
728 glTranslatef(0,0,quads[i].tz);
729 glRotatef(quads[i].rx, 1,0,0);
730 glRotatef(quads[i].ry, 0,1,0);
731 glRotatef(quads[i].rz, 0,0,1);
732 glCallList(quad_list);
735 quads[i].rx += quads[i].drx;
736 quads[i].ry += quads[i].dry;
737 quads[i].rz += quads[i].drz;
742 GLvoid drawScene(void)
745 checkError(__LINE__, __FILE__);
746 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
748 /* we have to do this here because the FPS meter turns these 3 features off!! */
751 glEnable(GL_LIGHTING);
756 glEnable(GL_TEXTURE_2D);
761 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
767 /* use XYZ scaling factors to change the size of the pulsar */
768 glScalef(scale_x, scale_y, scale_z);
771 /* update the scaling factors- cyclic */
772 scale_x = cos(frame/360.)*10.;
773 scale_y = sin(frame/360.)*10.;
776 /* increment frame-counter */
781 checkError(__LINE__, __FILE__);
785 void draw_screensaver(ModeInfo * mi)
787 screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
788 Display *display = MI_DISPLAY(mi);
789 Window window = MI_WINDOW(mi);
791 if (!gp->glx_context)
794 glXMakeCurrent(display, window, *(gp->glx_context));
796 glXSwapBuffers(display, window);
799 /* Standard reshape function */
801 reshape(int width, int height)
803 glViewport( 0, 0, global_width, global_height );
808 init_screensaver(ModeInfo * mi)
810 int screen = MI_SCREEN(mi);
812 screensaverstruct *gp;
814 if (Screensaver == NULL) {
815 if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
818 gp = &Screensaver[screen];
820 gp->window = MI_WINDOW(mi);
821 if ((gp->glx_context = init_GL(mi)) != NULL) {
822 reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
823 initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
830 /* all sorts of nice cleanup code should go here! */
831 void release_screensaver(ModeInfo * mi)
834 if (Screensaver != NULL) {
835 for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
836 /* screensaverstruct *gp = &Screensaver[screen];*/
838 (void) free((void *) Screensaver);