a7695027e983fc84de1c834fcc352122f1bb26a0
[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 HACK_RESHAPE                                   reshape_screensaver
55 # define screensaver_opts                               xlockmore_opts
56 #define DEFAULTS                        "*delay:                        10000   \n" \
57                                                                                 "*showFPS:          False   \n" \
58                                                                                 "*light:                        False   \n" \
59                                         "*wire:                         False   \n" \
60                                         "*quads:                        5       \n" \
61                                         "*blend:                        True    \n" \
62                                         "*fog:                          False   \n" \
63                                         "*antialias:            False   \n" \
64                                         "*texture:                      False   \n" \
65                                         "*texture_quality:      False   \n" \
66                                         "*mipmap:                       False   \n" \
67                                         "*doDepth:                      False   \n" \
68                                                                                 "*image:                        BUILTIN \n"
69
70 # include "xlockmore.h"                         /* from the xscreensaver distribution */
71 #else /* !STANDALONE */
72 # include "xlock.h"                                     /* from the xlockmore distribution */
73 #endif /* !STANDALONE */
74
75 #ifdef USE_GL /* whole file */
76
77 #ifdef HAVE_XPM
78 # include <X11/xpm.h>
79 # ifndef PIXEL_ALREADY_TYPEDEFED
80 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
81 # endif
82 #endif
83
84 #ifdef HAVE_XMU
85 # ifndef VMS
86 #  include <X11/Xmu/Drawing.h>
87 #else  /* VMS */
88 #  include <Xmu/Drawing.h>
89 # endif /* VMS */
90 #endif
91
92
93 #include <GL/gl.h>
94 #include <GL/glu.h>
95
96 #include <string.h>
97 #include <malloc.h>
98 #include <stdio.h>
99
100 /* Functions for loading and storing textures */
101
102 #define checkImageWidth 64
103 #define checkImageHeight 64
104
105 /* Functions for handling the frames per second timer */
106 #include "GL/glx.h"
107
108 #undef countof
109 #define countof(x) (sizeof((x))/sizeof((*x)))
110
111 #define WIDTH 800
112 #define HEIGHT 600
113
114 #define NUM_QUADS 5
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"
126
127 static int num_quads;
128 static int do_light;
129 static int do_wire;
130 static int do_blend;
131 static int do_fog;
132 static int do_antialias;
133 static int do_texture;
134 static int do_texture_quality;
135 static int do_mipmap;
136 static int do_depth;
137 static char *which_image;
138
139
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 },
161 };
162
163
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},
176 };
177
178
179 static OptionStruct desc[] =
180 {
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(?))"},
192 };
193
194 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
195
196 #ifdef USE_MODULES
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};
202 #endif
203
204
205 /* structure for holding the screensaver data */
206 typedef struct {
207   int screen_width, screen_height;
208   GLXContext *glx_context;
209   Window window;
210   XColor fg, bg;
211 } screensaverstruct;
212
213 static screensaverstruct *Screensaver = NULL;
214
215 struct quad
216 {
217   GLfloat tx, ty, tz;
218   GLfloat rx, ry, rz;
219
220   GLfloat dtx, dty, dtz;
221   GLfloat drx, dry, drz;
222
223 };
224
225
226 GLint quad_list;
227
228 static float scale_x=1, scale_y=1, scale_z=1;
229 static int frame = 0;
230
231 struct quad *quads;
232
233 GLubyte *Generate_Image(int *width, int *height, int *format)
234 {
235   GLubyte *result;
236   int i, j, c;
237   int counter=0;
238
239   *width = checkImageWidth;
240   *height = checkImageHeight;
241   result = (GLubyte *)malloc(4 * *width * *height);
242
243   counter = 0;
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;
251     }
252   }
253
254   *format = GL_RGBA;
255   return result;
256 }
257
258
259
260 #ifdef TIFF
261 #include <tiffio>
262 /* Load a TIFF texture: requires libtiff */
263 uint32 *LoadTIFF(char *filename, int *width, int *height, int *format)
264 {
265   TIFF *tif;
266   char emsg[1024];
267   uint32 *raster;
268   TIFFRGBAImage img;
269   tsize_t npixels;
270
271   tif = TIFFOpen(filename, "r");
272   if (tif == NULL) {
273     fprintf(stderr, "Problem showing %s\n", filename);
274     return Generate_Image(&width, &height, &format);
275   }
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);
283       }
284     }
285     TIFFRGBAImageEnd(&img);
286   } else {
287     TIFFError(filename, emsg);
288     return Generate_Image(&width, &height, &format);
289   }
290
291   *width = img.width;
292   *height = img.height;
293   *format = GL_RGBA;
294
295   TIFFClose(tif);
296   return raster;
297 }
298 #endif
299
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)
302 {
303   char buff[1024];
304   GLubyte *data;
305   int sizeX, sizeY;
306   FILE *fp;
307   int maxval;
308
309   fp = fopen(filename, "rb");
310   if (!fp)
311     {
312       fprintf(stderr, "Unable to open file '%s'\n", filename);
313       return  Generate_Image(width, height, format);
314     }
315
316   if (!fgets(buff, sizeof(buff), fp))
317     {
318       perror("Unable to read header filename\n");
319       return  Generate_Image(width, height, format);
320     }
321
322   if (buff[0] != '6' || buff[1] != 'P')
323     {
324       fprintf(stderr, "Invalid image format (must be `6P')\n");
325       return  Generate_Image(width, height, format);
326     }
327
328   do
329     {
330       fgets(buff, sizeof(buff), fp);
331     }
332   while (buff[0] == '#');
333     
334   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
335     {
336       fprintf(stderr, "Error loading image `%s'\n", filename);
337       return  Generate_Image(width, height, format);
338     }
339
340   if (fscanf(fp, "%d", &maxval) != 1)
341     {
342       fprintf(stderr, "Error loading image `%s'\n", filename);
343       return  Generate_Image(width, height, format);
344     }
345
346   while (fgetc(fp) != '\n')
347     ;
348
349   data = (GLubyte *)malloc(4 * sizeX * sizeY);
350   if (data == NULL)
351     {
352       fprintf(stderr, "Unable to allocate memory\n");
353           exit(1);
354     }
355
356   if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
357     {
358       fprintf(stderr, "Error loading image `%s'\n", filename);
359       return  Generate_Image(width, height, format);
360     }
361
362   fclose(fp);
363
364   *width = sizeX;
365   *height = sizeY;
366   *format = GL_RGBA;
367   return data;
368 }
369
370 /* Load a plain PPM image */
371 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
372 {
373   char buff[1024];
374   GLubyte *data;
375   GLint sizeX, sizeY;
376   FILE *fp;
377   int maxval;
378
379   fp = fopen(filename, "rb");
380   if (!fp)
381     {
382       fprintf(stderr, "Unable to open file '%s'\n", filename);
383       return  Generate_Image(width, height, format);
384       exit(1);
385     }
386   if (!fgets(buff, sizeof(buff), fp))
387     {
388       perror(filename);
389       return  Generate_Image(width, height, format);
390     }
391
392   if (buff[0] != 'P' || buff[1] != '6')
393     {
394       fprintf(stderr, "Invalid image format (must be `P6')\n");
395       return  Generate_Image(width, height, format);
396     }
397
398   do
399     {
400       fgets(buff, sizeof(buff), fp);
401     }
402   while (buff[0] == '#');
403     
404   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
405     {
406       fprintf(stderr, "Error loading image `%s'\n", filename);
407       return  Generate_Image(width, height, format);
408     }
409
410   if (fscanf(fp, "%d", &maxval) != 1)
411     {
412       fprintf(stderr, "Error loading image `%s'\n", filename);
413       return  Generate_Image(width, height, format);
414     }
415
416   while (fgetc(fp) != '\n')
417     ;
418
419   data = (GLubyte *)malloc(3 * sizeX * sizeY);
420   if (data == NULL)
421     {
422       fprintf(stderr, "Unable to allocate memory\n");
423           exit(1);
424     }
425
426   if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
427     {
428       fprintf(stderr, "Error loading image `%s'\n", filename);
429       return  Generate_Image(width, height, format);
430     }
431
432   fclose(fp);
433
434   *width = sizeX;
435   *height = sizeY;
436   *format = GL_RGB;
437   return data;
438 }
439
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)
443 {
444   int height, width;
445   GLubyte *image;
446   GLint a;
447   int format;
448
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);
455 #ifdef TIFF
456   else if ( !strncmp((filename+strlen(filename)-4), "tiff", 4))
457     image = (GLubyte *)LoadTIFF(filename, &width, &height, &format);
458 #endif
459   else {
460     fprintf(stderr, "Unknown file format extension: '%s'\n", filename);
461     image = Generate_Image(&width, &height, &format);
462   }
463
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);
471
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);
476   }
477   else {
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);
482   }
483
484   /* mipmaps make the image look much nicer */
485   if (do_mipmap)
486         a=gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, format, GL_UNSIGNED_BYTE, image);
487   else
488     {
489       clear_gl_error();
490       glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
491                    format, GL_UNSIGNED_BYTE, image);
492       check_gl_error("texture");
493     }
494
495   free(image);
496 }
497
498 void resetProjection(void) {
499   glMatrixMode(GL_PROJECTION);
500   glLoadIdentity();
501   glFrustum(-1, 1, -1, 1, 1, 100); 
502   glMatrixMode(GL_MODELVIEW);
503   glLoadIdentity();
504 }
505
506
507 void GenerateQuad(void)
508 {
509   int i;
510
511   quad_list = glGenLists(1);
512   glNewList(quad_list,GL_COMPILE);
513 #if 1
514   glBegin(GL_QUADS);
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);
519 #else
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);
525 #endif
526   glEnd();
527   glEndList();
528
529   quads = (struct quad *) malloc(sizeof(struct quad) * num_quads);
530   for (i=0; i < num_quads; i++)
531     {
532       quads[i].rx = 0.;
533       quads[i].ry = 0.;
534       quads[i].rz = 0.;
535       quads[i].tx = 0.;
536       quads[i].ty = 0.;
537       quads[i].tz = -10;
538
539       quads[i].drx = frand(5.0);
540       quads[i].dry = frand(5.0);
541       quads[i].drz = 0;
542     }
543 }
544
545 void initializeGL(GLsizei width, GLsizei height) 
546 {
547   GLfloat fogColor[4] = { 0.1, 0.1, 0.1, 0.1 };
548
549   glViewport( 0, 0, width, height ); 
550   resetProjection();
551
552   if (do_depth)
553         glEnable(GL_DEPTH_TEST);
554
555   if (do_antialias) {
556         do_blend = 1;
557         glEnable(GL_LINE_SMOOTH);
558   }
559
560   if (do_blend) {
561         glEnable(GL_BLEND);
562         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
563   }
564
565
566   if (do_light) {
567         glShadeModel(GL_SMOOTH);
568         glEnable(GL_COLOR_MATERIAL);
569         glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
570   }
571
572   if (do_wire)
573         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
574   else 
575         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
576
577   if (do_fog) {
578         glEnable(GL_FOG);
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);
585   }
586         
587
588   if (do_texture)
589           Create_Texture(which_image); 
590
591   GenerateQuad();
592 }
593 void drawQuads(void) {
594   int i;
595   for (i=0; i < num_quads; i++)
596     {
597       glPushMatrix();
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);
605       glPopMatrix();
606
607       quads[i].rx += quads[i].drx;
608       quads[i].ry += quads[i].dry;
609       quads[i].rz += quads[i].drz;
610
611     }
612 }
613
614 GLvoid drawScene(ModeInfo * mi) 
615 {
616 /*  check_gl_error ("drawScene"); */
617   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
618
619   /* we have to do this here because the FPS meter turns these 3 features off!! */
620   {
621         if (do_light) {
622           glEnable(GL_LIGHTING);
623           glEnable(GL_LIGHT0);
624         }
625         
626         if (do_texture) {
627           glEnable(GL_TEXTURE_2D);
628         }
629         
630         if (do_blend) {
631           glEnable(GL_BLEND);
632           glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
633         }
634   }
635
636   resetProjection();
637
638   /* use XYZ scaling factors to change the size of the pulsar */
639   glScalef(scale_x, scale_y, scale_z);
640   drawQuads();
641
642   /* update the scaling factors- cyclic */
643   scale_x = cos(frame/360.)*10.;
644   scale_y = sin(frame/360.)*10.;
645   scale_z = 1;
646
647   /* increment frame-counter */
648   frame++;
649
650 /*  check_gl_error ("drawScene"); */
651 }
652
653
654 void draw_screensaver(ModeInfo * mi)
655 {
656   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
657   Display    *display = MI_DISPLAY(mi);
658   Window      window = MI_WINDOW(mi);
659
660   if (!gp->glx_context)
661         return;
662
663   glXMakeCurrent(display, window, *(gp->glx_context));
664   drawScene(mi);
665   if (mi->fps_p) do_fps (mi);
666   glXSwapBuffers(display, window);
667 }
668
669 /* Standard reshape function */
670 void
671 reshape_screensaver(ModeInfo *mi, int width, int height)
672 {
673   glViewport( 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi) );
674   resetProjection();
675 }
676
677 void
678 init_screensaver(ModeInfo * mi)
679 {
680   int screen = MI_SCREEN(mi);
681
682   screensaverstruct *gp;
683
684   if (Screensaver == NULL) {
685         if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
686           return;
687   }
688   gp = &Screensaver[screen];
689
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));
694   } else {
695         MI_CLEARWINDOW(mi);
696   }
697 }
698
699
700 /* all sorts of nice cleanup code should go here! */
701 void release_screensaver(ModeInfo * mi)
702 {
703   int screen;
704   if (Screensaver != NULL) {
705         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
706 /*        screensaverstruct *gp = &Screensaver[screen];*/
707         }
708         (void) free((void *) Screensaver);
709         Screensaver = NULL;
710   }
711   free(quads);
712   FreeAllGL(mi);
713 }
714 #endif