a6b971b77e8d1b7f7ac889c557d79b4aa31e3f1e
[xscreensaver] / hacks / glx / pulsar.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* pulsar --- pulsar module for xscreensaver */
3 /*-
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.
9  *
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.
15
16  * Revision History:
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
21  *
22  * Notes:
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.  
29  *
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
33  *
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
36  *                                                                 
37  */
38
39 #include <math.h> 
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 /*-
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
46  */
47
48 #include <X11/Intrinsic.h>
49
50 #ifdef STANDALONE
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" \
56                                         "*wire:                         False   \n" \
57                                         "*quads:                        5       \n" \
58                                         "*blend:                        True    \n" \
59                                         "*fog:                          False   \n" \
60                                         "*antialias:            False   \n" \
61                                         "*texture:                      False   \n" \
62                                         "*texture_quality:      False   \n" \
63                                         "*mipmap:                       False   \n" \
64                                         "*fps:                          False   \n" \
65                                         "*doDepth:                      False   \n" \
66                                                                                 "*image:                        BUILTIN \n"
67
68 # include "xlockmore.h"                         /* from the xscreensaver distribution */
69 #else /* !STANDALONE */
70 # include "xlock.h"                                     /* from the xlockmore distribution */
71 #endif /* !STANDALONE */
72
73 #ifdef USE_GL /* whole file */
74
75 #ifdef HAVE_XPM
76 # include <X11/xpm.h>
77 # ifndef PIXEL_ALREADY_TYPEDEFED
78 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
79 # endif
80 #endif
81
82 #ifdef HAVE_XMU
83 # ifndef VMS
84 #  include <X11/Xmu/Drawing.h>
85 #else  /* VMS */
86 #  include <Xmu/Drawing.h>
87 # endif /* VMS */
88 #endif
89
90
91 #include <GL/gl.h>
92 #include <GL/glu.h>
93
94 #include <string.h>
95 #include <malloc.h>
96 #include <stdio.h>
97
98 /* Functions for loading and storing textures */
99
100 #define checkImageWidth 64
101 #define checkImageHeight 64
102
103 /* Functions for handling the frames per second timer */
104 #include "GL/glx.h"
105
106 #ifndef SAMPLE_FRAMES
107 #define SAMPLE_FRAMES 10
108 #endif
109
110 #undef countof
111 #define countof(x) (sizeof((x))/sizeof((*x)))
112
113 #define WIDTH 800
114 #define HEIGHT 600
115
116 #define NUM_QUADS 5
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"
129
130 static int num_quads;
131 static int do_light;
132 static int do_wire;
133 static int do_blend;
134 static int do_fog;
135 static int do_antialias;
136 static int do_texture;
137 static int do_texture_quality;
138 static int do_mipmap;
139 static int do_fps;
140 static int do_depth;
141 static char *which_image;
142
143
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 },
167 };
168
169
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},
183 };
184
185
186 static OptionStruct desc[] =
187 {
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(?))"},
200 };
201
202 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
203
204 #ifdef USE_MODULES
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};
210 #endif
211
212
213 /* structure for holding the screensaver data */
214 typedef struct {
215   int screen_width, screen_height;
216   GLXContext *glx_context;
217   Window window;
218   XColor fg, bg;
219 } screensaverstruct;
220
221 static screensaverstruct *Screensaver = NULL;
222
223 struct quad
224 {
225   GLfloat tx, ty, tz;
226   GLfloat rx, ry, rz;
227
228   GLfloat dtx, dty, dtz;
229   GLfloat drx, dry, drz;
230
231 };
232
233 int global_width=WIDTH, global_height=HEIGHT;
234
235
236 static GLint base;
237 static int FrameCounter = 0;
238 static double oldtime=-1., newtime=-1.;
239 static char FPSstring[1024]="FPS: NONE"; 
240
241 static float x_pos=0, y_pos=0;
242
243 #define FONT "-*-courier-medium-r-normal-*-240-*"
244 GLint quad_list;
245
246 static float scale_x=1, scale_y=1, scale_z=1;
247 static int frame = 0;
248
249 static GLenum errCode;
250 static const GLubyte *errString;
251
252 struct quad *quads;
253
254 int checkError(int line, char *file)
255 {
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);
259     exit(1);
260   }
261   return 0;
262 }
263
264
265 void FPS_Setup(void)
266 {
267   Display *Dpy;
268   XFontStruct *fontInfo;
269   Font id;
270   int first=0, last=255;
271
272   Dpy = XOpenDisplay(NULL);
273   fontInfo = XLoadQueryFont(Dpy, FONT);
274   if (fontInfo == NULL)
275     {
276       fprintf(stderr, "Failed to load font %s\n", FONT);
277       exit(1);
278     }
279
280   id = fontInfo->fid;
281   first = fontInfo->min_char_or_byte2;
282   last = fontInfo->max_char_or_byte2;
283   
284   base = glGenLists((GLuint) last+1);
285   if (base == 0) {
286     fprintf (stderr, "out of display lists\n");
287     exit(1);
288   }
289   glXUseXFont(id, first, last-first+1, base+first);
290
291 }
292
293 void PrintString(float x, float y, char *string)
294 {
295   int len, i;
296
297   /* save the current state */
298   /* note: could be expensive! */
299   glPushAttrib(GL_ALL_ATTRIB_BITS);
300   glMatrixMode(GL_PROJECTION);
301   glLoadIdentity();
302   gluOrtho2D(0, global_width, 0, global_height);
303   glMatrixMode(GL_MODELVIEW);
304   glLoadIdentity();
305
306   /* disable lighting and texturing when drawing bitmaps! */
307   glDisable(GL_TEXTURE_2D);
308   glDisable(GL_LIGHTING);
309   glDisable(GL_BLEND);
310
311   /* draw a black background */
312
313   /* draw the text */
314   glColor3f(1,1,1);
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]);
320         else
321           fprintf(stderr, "%d+string[%d] is not a display list!\n", base, i);
322   }
323
324   /* clean up after our state changes */
325   glPopAttrib();
326 }
327
328 double gettime(void)
329 {
330   struct timeval now;
331 #ifdef GETTIMEOFDAY_TWO_ARGS
332   struct timezone tzp;
333   gettimeofday(&now, &tzp);
334 #else
335   gettimeofday(&now);
336 #endif
337   return (double) (now.tv_sec + (((double) now.tv_usec) * 0.000001));
338 }
339
340 void DoFPS(void)
341 {
342   /* every SAMPLE_FRAMES frames, get the time and use it to get the 
343      frames per second */
344   if (!(FrameCounter % SAMPLE_FRAMES)) {
345     oldtime = newtime;
346     newtime = gettime();
347     sprintf(FPSstring, "FPS: %.02f", SAMPLE_FRAMES/(newtime-oldtime));
348   }
349
350   PrintString(x_pos,y_pos,FPSstring);
351
352   FrameCounter++;
353 }
354
355 GLubyte *Generate_Image(int *width, int *height, int *format)
356 {
357   GLubyte *result;
358   int i, j, c;
359   int counter=0;
360
361   *width = checkImageWidth;
362   *height = checkImageHeight;
363   result = (GLubyte *)malloc(4 * *width * *height);
364
365   counter = 0;
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;
373     }
374   }
375
376   *format = GL_RGBA;
377   return result;
378 }
379
380
381
382 #ifdef TIFF
383 #include <tiffio>
384 /* Load a TIFF texture: requires libtiff */
385 uint32 *LoadTIFF(char *filename, int *width, int *height, int *format)
386 {
387   TIFF *tif;
388   char emsg[1024];
389   uint32 *raster;
390   TIFFRGBAImage img;
391   tsize_t npixels;
392
393   tif = TIFFOpen(filename, "r");
394   if (tif == NULL) {
395     fprintf(stderr, "Problem showing %s\n", filename);
396     return Generate_Image(&width, &height, &format);
397   }
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);
405       }
406     }
407     TIFFRGBAImageEnd(&img);
408   } else {
409     TIFFError(filename, emsg);
410     return Generate_Image(&width, &height, &format);
411   }
412
413   *width = img.width;
414   *height = img.height;
415   *format = GL_RGBA;
416
417   TIFFClose(tif);
418   return raster;
419 }
420 #endif
421
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)
424 {
425   char buff[1024];
426   GLubyte *data;
427   int sizeX, sizeY;
428   FILE *fp;
429   int maxval;
430
431   fp = fopen(filename, "rb");
432   if (!fp)
433     {
434       fprintf(stderr, "Unable to open file '%s'\n", filename);
435       return  Generate_Image(width, height, format);
436     }
437
438   if (!fgets(buff, sizeof(buff), fp))
439     {
440       perror("Unable to read header filename\n");
441       return  Generate_Image(width, height, format);
442     }
443
444   if (buff[0] != '6' || buff[1] != 'P')
445     {
446       fprintf(stderr, "Invalid image format (must be `6P')\n");
447       return  Generate_Image(width, height, format);
448     }
449
450   do
451     {
452       fgets(buff, sizeof(buff), fp);
453     }
454   while (buff[0] == '#');
455     
456   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
457     {
458       fprintf(stderr, "Error loading image `%s'\n", filename);
459       return  Generate_Image(width, height, format);
460     }
461
462   if (fscanf(fp, "%d", &maxval) != 1)
463     {
464       fprintf(stderr, "Error loading image `%s'\n", filename);
465       return  Generate_Image(width, height, format);
466     }
467
468   while (fgetc(fp) != '\n')
469     ;
470
471   data = (GLubyte *)malloc(4 * sizeX * sizeY);
472   if (data == NULL)
473     {
474       fprintf(stderr, "Unable to allocate memory\n");
475           exit(1);
476     }
477
478   if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
479     {
480       fprintf(stderr, "Error loading image `%s'\n", filename);
481       return  Generate_Image(width, height, format);
482     }
483
484   fclose(fp);
485
486   *width = sizeX;
487   *height = sizeY;
488   *format = GL_RGBA;
489   return data;
490 }
491
492 /* Load a plain PPM image */
493 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
494 {
495   char buff[1024];
496   GLubyte *data;
497   GLint sizeX, sizeY;
498   FILE *fp;
499   int maxval;
500
501   fp = fopen(filename, "rb");
502   if (!fp)
503     {
504       fprintf(stderr, "Unable to open file '%s'\n", filename);
505       return  Generate_Image(width, height, format);
506       exit(1);
507     }
508   if (!fgets(buff, sizeof(buff), fp))
509     {
510       perror(filename);
511       return  Generate_Image(width, height, format);
512     }
513
514   if (buff[0] != 'P' || buff[1] != '6')
515     {
516       fprintf(stderr, "Invalid image format (must be `P6')\n");
517       return  Generate_Image(width, height, format);
518     }
519
520   do
521     {
522       fgets(buff, sizeof(buff), fp);
523     }
524   while (buff[0] == '#');
525     
526   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
527     {
528       fprintf(stderr, "Error loading image `%s'\n", filename);
529       return  Generate_Image(width, height, format);
530     }
531
532   if (fscanf(fp, "%d", &maxval) != 1)
533     {
534       fprintf(stderr, "Error loading image `%s'\n", filename);
535       return  Generate_Image(width, height, format);
536     }
537
538   while (fgetc(fp) != '\n')
539     ;
540
541   data = (GLubyte *)malloc(3 * sizeX * sizeY);
542   if (data == NULL)
543     {
544       fprintf(stderr, "Unable to allocate memory\n");
545           exit(1);
546     }
547
548   if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
549     {
550       fprintf(stderr, "Error loading image `%s'\n", filename);
551       return  Generate_Image(width, height, format);
552     }
553
554   fclose(fp);
555
556   *width = sizeX;
557   *height = sizeY;
558   *format = GL_RGB;
559   return data;
560 }
561
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)
565 {
566   int height, width;
567   GLubyte *image;
568   GLint a;
569   int format;
570
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);
577 #ifdef TIFF
578   else if ( !strncmp((filename+strlen(filename)-4), "tiff", 4))
579     image = (GLubyte *)LoadTIFF(filename, &width, &height, &format);
580 #endif
581   else {
582     fprintf(stderr, "Unknown file format extension: '%s'\n", filename);
583     image = Generate_Image(&width, &height, &format);
584   }
585
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);
593
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);
598   }
599   else {
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);
604   }
605
606   /* mipmaps make the image look much nicer */
607   if (do_mipmap)
608         a=gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, image);
609   else 
610         glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
611                                  format, GL_UNSIGNED_BYTE, image);
612
613   free(image);
614 }
615
616 void resetProjection(void) {
617   glMatrixMode(GL_PROJECTION);
618   glLoadIdentity();
619   glFrustum(-1, 1, -1, 1, 1, 100); 
620   glMatrixMode(GL_MODELVIEW);
621   glLoadIdentity();
622 }
623
624
625 void GenerateQuad(void)
626 {
627   int i;
628
629   quad_list = glGenLists(1);
630   glNewList(quad_list,GL_COMPILE);
631 #if 1
632   glBegin(GL_QUADS);
633   glColor4f(1,0,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,0); glVertex2f(-1, -1);
634   glColor4f(0,1,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,1); glVertex2f(-1,  1);
635   glColor4f(0,0,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,1); glVertex2f( 1,  1);
636   glColor4f(1,1,1,1); glNormal3f(0,0,1);  glTexCoord2f(1,0); glVertex2f( 1,  -1);
637 #else
638   glBegin(GL_TRIANGLE_STRIP);
639   glColor4f(0,1,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,1); glVertex2f(-1,  1);
640   glColor4f(1,0,0,.4); glNormal3f(0,0,1);  glTexCoord2f(0,0); glVertex2f(-1, -1);
641   glColor4f(0,0,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,1); glVertex2f( 1,  1);
642   glColor4f(1,1,1,.4); glNormal3f(0,0,1);  glTexCoord2f(1,0); glVertex2f( 1,  -1);
643 #endif
644   glEnd();
645   glEndList();
646
647   quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
648   for (i=0; i < num_quads; i++)
649     {
650       quads[i].rx = 0.;
651       quads[i].ry = 0.;
652       quads[i].rz = 0.;
653       quads[i].tx = 0.;
654       quads[i].ty = 0.;
655       quads[i].tz = -10;
656
657       quads[i].drx = frand(5.0);
658       quads[i].dry = frand(5.0);
659       quads[i].drz = 0;
660     }
661 }
662
663 void initializeGL(GLsizei width, GLsizei height) 
664 {
665   GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
666
667   global_width=width;
668   global_height=height;
669
670   glViewport( 0, 0, width, height ); 
671   resetProjection();
672
673   if (do_depth)
674         glEnable(GL_DEPTH_TEST);
675
676   if (do_fps)
677         FPS_Setup();
678
679   if (do_antialias) {
680         do_blend = 1;
681         glEnable(GL_LINE_SMOOTH);
682   }
683
684   if (do_blend) {
685         glEnable(GL_BLEND);
686         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
687   }
688
689
690   if (do_light) {
691         glShadeModel(GL_SMOOTH);
692         glEnable(GL_COLOR_MATERIAL);
693         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
694   }
695
696   if (do_wire)
697         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
698   else 
699         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
700
701   if (do_fog) {
702         glEnable(GL_FOG);
703         glFogi(GL_FOG_MODE, GL_LINEAR);
704         glFogfv(GL_FOG_COLOR, fogColor);
705         glFogf(GL_FOG_DENSITY, 0.35);
706 /*      glHint(GL_FOG_HINT, GL_FASTEST); */
707         glFogf(GL_FOG_START, 50.0);
708         glFogf(GL_FOG_END, 100.0);
709   }
710         
711
712   if (do_texture)
713           Create_Texture(which_image); 
714
715   GenerateQuad();
716 }
717 void drawQuads(void) {
718   int i;
719   for (i=0; i < num_quads; i++)
720     {
721       glPushMatrix();
722       glTranslatef(quads[i].tx,0,0);
723       glTranslatef(0,quads[i].ty,0);
724       glTranslatef(0,0,quads[i].tz);
725       glRotatef(quads[i].rx, 1,0,0);
726       glRotatef(quads[i].ry, 0,1,0);
727       glRotatef(quads[i].rz, 0,0,1);
728       glCallList(quad_list);
729       glPopMatrix();
730
731       quads[i].rx += quads[i].drx;
732       quads[i].ry += quads[i].dry;
733       quads[i].rz += quads[i].drz;
734
735     }
736 }
737
738 GLvoid drawScene(void) 
739 {
740
741   checkError(__LINE__, __FILE__);
742   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
743
744   /* we have to do this here because the FPS meter turns these 3 features off!! */
745   {
746         if (do_light) {
747           glEnable(GL_LIGHTING);
748           glEnable(GL_LIGHT0);
749         }
750         
751         if (do_texture) {
752           glEnable(GL_TEXTURE_2D);
753         }
754         
755         if (do_blend) {
756           glEnable(GL_BLEND);
757           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
758         }
759   }
760
761   resetProjection();
762
763   /* use XYZ scaling factors to change the size of the pulsar */
764   glScalef(scale_x, scale_y, scale_z);
765   drawQuads();
766
767   /* update the scaling factors- cyclic */
768   scale_x = cos(frame/360.)*10.;
769   scale_y = sin(frame/360.)*10.;
770   scale_z = 1;
771
772   /* increment frame-counter */
773   frame++;
774
775   if (do_fps)
776         DoFPS();
777   checkError(__LINE__, __FILE__);
778 }
779
780
781 void draw_screensaver(ModeInfo * mi)
782 {
783   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
784   Display    *display = MI_DISPLAY(mi);
785   Window      window = MI_WINDOW(mi);
786
787   if (!gp->glx_context)
788         return;
789
790   glXMakeCurrent(display, window, *(gp->glx_context));
791   drawScene();
792   glXSwapBuffers(display, window);
793 }
794
795 /* Standard reshape function */
796 static void
797 reshape(int width, int height)
798 {
799   glViewport( 0, 0, global_width, global_height );
800   resetProjection();
801 }
802
803 void
804 init_screensaver(ModeInfo * mi)
805 {
806   int screen = MI_SCREEN(mi);
807
808   screensaverstruct *gp;
809
810   if (Screensaver == NULL) {
811         if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
812           return;
813   }
814   gp = &Screensaver[screen];
815
816   gp->window = MI_WINDOW(mi);
817   if ((gp->glx_context = init_GL(mi)) != NULL) {
818         reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
819         initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
820   } else {
821         MI_CLEARWINDOW(mi);
822   }
823 }
824
825
826 /* all sorts of nice cleanup code should go here! */
827 void release_screensaver(ModeInfo * mi)
828 {
829   int screen;
830   if (Screensaver != NULL) {
831         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
832 /*        screensaverstruct *gp = &Screensaver[screen];*/
833         }
834         (void) free((void *) Screensaver);
835         Screensaver = NULL;
836   }
837   free(quads);
838   FreeAllGL(mi);
839 }
840 #endif
841