726177e6787ceeed530fdc0e634b64ab313900ad
[xscreensaver] / hacks / glx / extrusion.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* extrusion --- extrusion 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  * Tue Oct 19 22:24:47 PDT 1999    Initial creation by David Konerding
18  *                                 <dek@cgl.ucsf.edu>
19  *                                                                 
20  * Notes:
21  * This screensaver requires the GLE ("OpenGL Tubing and Extrusion Library")
22  * which can be obtained from http://www.linas.org/gle/index.html
23   */
24
25 /*-
26  * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
27  * otherwise caddr_t is not defined correctly
28  */
29
30 #include <X11/Intrinsic.h>
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef STANDALONE
37 # define PROGCLASS                                              "Screensaver"
38 # define HACK_INIT                                              init_screensaver
39 # define HACK_DRAW                                              draw_screensaver
40 # define screensaver_opts                               xlockmore_opts
41 #define DEFAULTS                        "*light:                        True    \n" \
42                                         "*wire:                         False   \n" \
43                                         "*texture:                      False   \n" \
44                                                                                 "*image:                        BUILTIN \n" \
45                                         "*name:             RANDOM  \n" \
46                                         "*example:          0       \n"
47
48 # include "xlockmore.h"                         /* from the xscreensaver distribution */
49 #else /* !STANDALONE */
50 # include "xlock.h"                                     /* from the xlockmore distribution */
51 #endif /* !STANDALONE */
52
53 #ifdef USE_GL /* whole file */
54
55 #ifdef HAVE_XPM
56 # include <X11/xpm.h>
57 # ifndef PIXEL_ALREADY_TYPEDEFED
58 # define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
59 # endif
60 #endif
61
62 #ifdef HAVE_XMU
63 # ifndef VMS
64 #  include <X11/Xmu/Drawing.h>
65 #else  /* VMS */
66 #  include <Xmu/Drawing.h>
67 # endif /* VMS */
68 #endif
69
70 #include <malloc.h>
71 #include <string.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <malloc.h>
75 #include <GL/gl.h>
76 #include <GL/glu.h>
77 #ifdef HAVE_GLE3
78 #include <GL/gle.h>
79 #else
80 #include <GL/tube.h>
81 #endif
82
83 #undef countof
84 #define countof(x) (sizeof((x))/sizeof((*x)))
85
86
87 #define checkImageWidth 64
88 #define checkImageHeight 64
89
90
91 extern void InitStuff_helix2(void);
92 extern void DrawStuff_helix2(void);
93 extern void InitStuff_helix3(void);
94 extern void DrawStuff_helix3(void);
95 extern void InitStuff_helix4(void);
96 extern void DrawStuff_helix4(void);
97 extern void InitStuff_joinoffset(void);
98 extern void DrawStuff_joinoffset(void);
99 extern void InitStuff_screw(void);
100 extern void DrawStuff_screw(void);
101 extern void InitStuff_taper(void);
102 extern void DrawStuff_taper(void);
103 extern void InitStuff_twistoid(void);
104 extern void DrawStuff_twistoid(void);
105
106
107
108 #define WIDTH 640
109 #define HEIGHT 480
110
111 #define DEF_LIGHT               "True"
112 #define DEF_WIRE                "False"
113 #define DEF_TEXTURE             "False"
114 #define DEF_TEXTURE_QUALITY   "False"
115 #define DEF_MIPMAP      "False"
116 #define DEF_NAME        "RANDOM"
117 #define DEF_IMAGE       "BUILTIN"
118
119 static int do_light;
120 static int do_wire;
121 static int do_texture;
122 static int do_texture_quality;
123 static int do_mipmap;
124 static char *which_name;
125 static char *which_image;
126
127 static XrmOptionDescRec opts[] = {
128   {"-light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "true" },
129   {"+light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "false" },
130   {"-wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "true" },
131   {"+wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "false" },
132   {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
133   {"+texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
134   {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
135   {"+texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
136   {"-texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
137   {"+mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "false" },
138   {"-mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "true" },
139   {"-name",   ".extrusion.name",  XrmoptionSepArg, (caddr_t) NULL },
140   {"-image",   ".extrusion.image",  XrmoptionSepArg, (caddr_t) NULL },
141 };
142
143
144 static argtype vars[] = {
145   {(caddr_t *) &do_light,    "light",   "Light",   DEF_LIGHT,   t_Bool},
146   {(caddr_t *) &do_wire,    "wire",   "Wire",   DEF_WIRE,   t_Bool},
147   {(caddr_t *) &do_texture,    "texture",   "Texture",   DEF_TEXTURE,   t_Bool},
148   {(caddr_t *) &do_texture_quality,    "texture_quality",   "Texture_Quality",   DEF_TEXTURE_QUALITY,   t_Bool},
149   {(caddr_t *) &do_mipmap,    "mipmap",   "Mipmap",   DEF_MIPMAP,   t_Bool},
150   {(caddr_t *) &which_name, "name",   "Name",   DEF_NAME,   t_String},
151   {(caddr_t *) &which_image, "image",   "Image",   DEF_IMAGE,   t_String},
152 };
153
154
155 static OptionStruct desc[] =
156 {
157   {"-name num", "example 'name' to draw (helix2, helix3, helix4, joinoffset, screw, taper, twistoid)"},
158   {"-/+ light", "whether to do enable lighting (slower)"},
159   {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
160   {"-/+ texture", "whether to apply a texture (slower)"},
161   {"-image <filename>", "texture image to load (PPM or PPM4)"},
162   {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
163   {"-/+ mipmap", "whether to use texture mipmap (slower)"},
164 };
165
166 ModeSpecOpt screensaver_opts = {countof(opts), opts, countof(vars), vars, desc};
167
168 #ifdef USE_MODULES
169 ModStruct   screensaver_description =
170 {"screensaver", "init_screensaver", "draw_screensaver", "release_screensaver",
171  "draw_screensaver", "init_screensaver", NULL, &screensaver_opts,
172  1000, 1, 2, 1, 4, 1.0, "",
173  "OpenGL screensaver", 0, NULL};
174 #endif
175
176
177 /* structure for holding the screensaver data */
178 typedef struct {
179   int screen_width, screen_height;
180   GLXContext *glx_context;
181   Window window;
182   XColor fg, bg;
183 } screensaverstruct;
184 static screensaverstruct *Screensaver = NULL;
185
186
187
188
189 /* convenient access to the screen width */
190 static int global_width=640, global_height=480;
191
192 /* set up a light */
193 static GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
194 static GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
195
196 static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
197 static GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
198
199 float rot_x=0, rot_y=0, rot_z=0;
200 static float dx=0, dy=0, dz=0;
201 static float ddx=0, ddy=0, ddz=0;
202 static float d_max = 0;
203 static int screensaver_number;
204
205 static float max_lastx=300, max_lasty=400;
206 static float min_lastx=-400, min_lasty=-400;
207 static float d_lastx=0, d_lasty=0;
208 static float dd_lastx=0, dd_lasty=0;
209 static float max_dlastx=0, max_dlasty=0;
210 float lastx=0, lasty=0;
211
212 static int errCode;
213 static GLubyte * errString;
214
215 struct functions {
216   void (*InitStuff)(void);
217   void (*DrawStuff)(void);
218   char *name;
219 };
220
221 /* currently joinoffset and twistoid look funny-
222    like we're looking at them from the back or something
223 */
224
225 struct functions funcs_ptr[] = {
226   {InitStuff_helix2, DrawStuff_helix2, "helix2"},
227   {InitStuff_helix3, DrawStuff_helix3, "helix3"},
228   {InitStuff_helix4, DrawStuff_helix4, "helix4"},
229   {InitStuff_joinoffset, DrawStuff_joinoffset, "joinoffset"},
230   {InitStuff_screw, DrawStuff_screw, "screw"},
231   {InitStuff_taper, DrawStuff_taper, "taper"},
232   {InitStuff_twistoid, DrawStuff_twistoid, "twistoid"},
233 };
234
235 static int num_screensavers = countof(funcs_ptr);
236
237
238 /* BEGINNING OF FUNCTIONS */
239
240
241 /* check for errors, bail if any.  useful for debugging */
242 int checkError(int line, char *file)
243 {
244   if((errCode = glGetError()) != GL_NO_ERROR) {
245     errString = (GLubyte *)gluErrorString(errCode);
246     fprintf(stderr, "%s: OpenGL error: %s detected at line %d in file %s\n",
247             progname, errString, line, file);
248     exit(1);
249   }
250   return 0;
251 }
252
253
254 /* generate a checkered image for texturing */
255 GLubyte *Generate_Image(int *width, int *height, int *format)
256 {
257   GLubyte *result;
258   int i, j, c;
259   int counter=0;
260
261   *width = checkImageWidth;
262   *height = checkImageHeight;
263   result = (GLubyte *)malloc(4 * *width * *height);
264
265   counter = 0;
266   for (i = 0; i < checkImageWidth; i++) {
267     for (j = 0; j < checkImageHeight; j++) {
268       c = (((((i&0x8)==0))^(((j&0x8))==0)))*255;
269       result[counter++] = (GLubyte) c;
270       result[counter++] = (GLubyte) c;
271       result[counter++] = (GLubyte) c;
272       result[counter++] = (GLubyte) 255;
273     }
274   }
275
276   *format = GL_RGBA;
277   return result;
278 }
279 /* Load a modified version of PPM format with an extra byte for alpha */
280 GLubyte *LoadPPM4(const char *filename, int *width, int *height, int *format)
281 {
282   char buff[1024];
283   GLubyte *data;
284   int sizeX, sizeY;
285   FILE *fp;
286   int maxval;
287
288   fp = fopen(filename, "rb");
289   if (!fp)
290     {
291       fprintf(stderr, "%s: unable to open file '%s'\n", progname, filename);
292       return  Generate_Image(width, height, format);
293     }
294
295   if (!fgets(buff, sizeof(buff), fp))
296     {
297       perror("Unable to read header filename\n");
298       return  Generate_Image(width, height, format);
299     }
300
301   if (buff[0] != '6' || buff[1] != 'P')
302     {
303       fprintf(stderr, "%s: Invalid image format (must be `6P')\n", progname);
304       return  Generate_Image(width, height, format);
305     }
306
307   do
308     {
309       fgets(buff, sizeof(buff), fp);
310     }
311   while (buff[0] == '#');
312     
313   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
314     {
315       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
316       return  Generate_Image(width, height, format);
317     }
318
319   if (fscanf(fp, "%d", &maxval) != 1)
320     {
321       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
322       return  Generate_Image(width, height, format);
323     }
324
325   while (fgetc(fp) != '\n')
326     ;
327
328   data = (GLubyte *)malloc(4 * sizeX * sizeY);
329   if (data == NULL)
330     {
331       fprintf(stderr, "%s: unable to allocate memory\n", progname);
332           exit(1);
333     }
334
335   if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
336     {
337       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
338       return  Generate_Image(width, height, format);
339     }
340
341   fclose(fp);
342
343   *width = sizeX;
344   *height = sizeY;
345   *format = GL_RGBA;
346   return data;
347 }
348
349 /* Load a plain PPM image */
350 GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
351 {
352   char buff[1024];
353   GLubyte *data;
354   GLint sizeX, sizeY;
355   FILE *fp;
356   int maxval;
357
358   fp = fopen(filename, "rb");
359   if (!fp)
360     {
361       fprintf(stderr, "%s: unable to open file '%s'\n", progname, filename);
362       return  Generate_Image(width, height, format);
363       exit(1);
364     }
365   if (!fgets(buff, sizeof(buff), fp))
366     {
367       perror(filename);
368       return  Generate_Image(width, height, format);
369     }
370
371   if (buff[0] != 'P' || buff[1] != '6')
372     {
373       fprintf(stderr, "%s: invalid image format (must be `P6')\n", progname);
374       return  Generate_Image(width, height, format);
375     }
376
377   do
378     {
379       fgets(buff, sizeof(buff), fp);
380     }
381   while (buff[0] == '#');
382     
383   if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
384     {
385       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
386       return  Generate_Image(width, height, format);
387     }
388
389   if (fscanf(fp, "%d", &maxval) != 1)
390     {
391       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
392       return  Generate_Image(width, height, format);
393     }
394
395   while (fgetc(fp) != '\n')
396     ;
397
398   data = (GLubyte *)malloc(3 * sizeX * sizeY);
399   if (data == NULL)
400     {
401       fprintf(stderr, "%s: unable to allocate memory\n", progname);
402           exit(1);
403     }
404
405   if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
406     {
407       fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
408       return  Generate_Image(width, height, format);
409     }
410
411   fclose(fp);
412
413   *width = sizeX;
414   *height = sizeY;
415   *format = GL_RGB;
416   return data;
417 }
418
419 /* create a texture to be applied to the surface
420    this function loads a file using a loader depending on
421    that extension of the file. there is very little error
422    checking.  
423 */
424
425 void Create_Texture(char *filename, int do_mipmap, int do_texture_quality)
426 {
427   int height, width;
428   GLubyte *image;
429   int format;
430
431   if ( !strncmp(filename, "BUILTIN", 7))
432     image = Generate_Image(&width, &height, &format);
433   else if ( !strncmp((filename+strlen(filename)-3), "ppm", 3))
434     image = LoadPPM(filename, &width, &height, &format);
435   else if ( !strncmp((filename+strlen(filename)-4), "ppm4", 4))
436     image = LoadPPM4(filename, &width, &height, &format);
437   else {
438     fprintf(stderr, "%s: unknown file format extension: '%s'\n",
439             progname, filename);
440         exit(1);
441   }
442
443   /* GL_MODULATE or GL_DECAL depending on what you want */
444   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
445   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
446   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
447   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
448
449   /* default is to do it quick and dirty */
450   /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
451   if (do_texture_quality) {
452     /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
453     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
454     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
455   } else {
456     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
457     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
458   }
459
460   if (do_mipmap) {
461     gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, 
462                       format, GL_UNSIGNED_BYTE, image);
463   }
464   else {
465     glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
466                  format, GL_UNSIGNED_BYTE, image);
467   }
468   free(image);
469 }
470
471
472 /* mostly lifted from lament.c */
473 static void
474 rotate (float *pos, float *v, float *dv, float max_v)
475 {
476   double ppos = *pos;
477
478   /* tick position */
479   if (ppos < 0)
480     ppos = -(ppos + *v);
481   else
482     ppos += *v;
483
484   if (ppos > 360)
485     ppos -= 360;
486   else if (ppos < 0)
487     ppos += 360;
488
489   if (ppos < 0) abort();
490   if (ppos > 360) abort();
491   *pos = (*pos > 0 ? ppos : -ppos);
492
493   /* accelerate */
494   *v += *dv;
495
496   /* clamp velocity */
497   if (*v > max_v || *v < -max_v)
498     {
499       *dv = -*dv;
500     }
501   /* If it stops, start it going in the other direction. */
502   else if (*v < 0)
503     {
504       if (random() % 4)
505         {
506           *v = 0;
507
508           /* keep going in the same direction */
509           if (random() % 2)
510             *dv = 0;
511           else if (*dv < 0)
512             *dv = -*dv;
513         }
514       else
515         {
516           /* reverse gears */
517           *v = -*v;
518           *dv = -*dv;
519           *pos = -*pos;
520         }
521     }
522
523   /* Alter direction of rotational acceleration randomly. */
524   if (! (random() % 120))
525     *dv = -*dv;
526
527   /* Change acceleration very occasionally. */
528   if (! (random() % 200))
529     {
530       if (*dv == 0)
531         *dv = 0.00001;
532       else if (random() & 1)
533         *dv *= 1.2;
534       else
535         *dv *= 0.8;
536     }
537 }
538
539
540 static void
541 bounce (float *pos, float *v, float *dv, float max_v)
542 {
543   *pos += *v;
544
545   if (*pos > 1.0)
546     *pos = 1.0, *v = -*v, *dv = -*dv;
547   else if (*pos < 0)
548     *pos = 0, *v = -*v, *dv = -*dv;
549
550   if (*pos < 0.0) abort();
551   if (*pos > 1.0) abort();
552
553   /* accelerate */
554   *v += *dv;
555
556   /* clamp velocity */
557   if (*v > max_v || *v < -max_v)
558     {
559       *dv = -*dv;
560     }
561
562   /* Alter direction of rotational acceleration randomly. */
563   if (! (random() % 120))
564     *dv = -*dv;
565
566   /* Change acceleration very occasionally. */
567   if (! (random() % 200))
568     {
569       if (*dv == 0)
570         *dv = 0.00001;
571       else if (random() & 1)
572         *dv *= 1.2;
573       else
574         *dv *= 0.8;
575     }
576 }
577
578
579 static void
580 init_rotation (void)
581 {
582   rot_x = (float) (random() % (360 * 2)) - 360;  /* -360 - 360 */
583   rot_y = (float) (random() % (360 * 2)) - 360;
584   rot_z = (float) (random() % (360 * 2)) - 360;
585
586   /* bell curve from 0-1.5 degrees, avg 0.75 */
587   dx = (frand(1) + frand(1) + frand(1)) / 2.0;
588   dy = (frand(1) + frand(1) + frand(1)) / 2.0;
589   dz = (frand(1) + frand(1) + frand(1)) / 2.0;
590
591   d_max = dx * 2;
592
593   ddx = 0.004;
594   ddy = 0.004;
595   ddz = 0.004;
596
597   lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
598   lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
599   d_lastx = (frand(1) + frand(1) + frand(1));
600   d_lasty = (frand(1) + frand(1) + frand(1));
601   max_dlastx = d_lastx * 2;
602   max_dlasty = d_lasty * 2;
603   dd_lastx = 0.004;
604   dd_lasty = 0.004;
605 }
606
607
608 /* draw the screensaver once */
609 void draw_screensaver(ModeInfo * mi)
610 {
611   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
612   Display    *display = MI_DISPLAY(mi);
613   Window      window = MI_WINDOW(mi);
614
615   Window root, child;
616   int rootx, rooty, winx, winy;
617   unsigned int mask;
618   XEvent event;
619
620   if (!gp->glx_context)
621         return;
622
623   glXMakeCurrent(display, window, *(gp->glx_context));
624
625   funcs_ptr[screensaver_number].DrawStuff();
626           
627   rotate(&rot_x, &dx, &ddx, d_max);
628   rotate(&rot_y, &dy, &ddy, d_max);
629   rotate(&rot_z, &dz, &ddz, d_max);
630
631   /* swallow any ButtonPress events */
632   while (XCheckMaskEvent (MI_DISPLAY(mi), ButtonPressMask, &event))
633     ;
634   /* check the pointer position and button state. */
635   XQueryPointer (MI_DISPLAY(mi), MI_WINDOW(mi),
636                  &root, &child, &rootx, &rooty, &winx, &winy, &mask);
637
638   /* track the mouse only if a button is down. */
639   if (mask & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask))
640     {
641       lastx = winx;
642       lasty = winy;
643     }
644   else
645     {
646       float scale = (max_lastx - min_lastx);
647       lastx -= min_lastx;
648       lasty -= min_lasty;
649       lastx /= scale;
650       lasty /= scale;
651       d_lastx /= scale;
652       d_lasty /= scale;
653       dd_lastx /= scale;
654       dd_lasty /= scale;
655       bounce(&lastx, &d_lastx, &dd_lastx, max_dlastx);
656       bounce(&lasty, &d_lasty, &dd_lasty, max_dlasty);
657       lastx *= scale;
658       lasty *= scale;
659       lastx += min_lastx;
660       lasty += min_lasty;
661       d_lastx *= scale;
662       d_lasty *= scale;
663       dd_lastx *= scale;
664       dd_lasty *= scale;
665     }
666
667   glXSwapBuffers(display, window);
668 }
669
670
671 /* set up lighting conditions */
672 void SetupLight(void)
673 {
674   glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
675   glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
676   glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
677   glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
678
679   glEnable (GL_LIGHT0);
680   glEnable (GL_LIGHT1);
681   glEnable (GL_LIGHTING);
682
683   glColorMaterial (GL_FRONT, GL_DIFFUSE);
684   glColorMaterial (GL_BACK, GL_DIFFUSE);
685   glEnable (GL_COLOR_MATERIAL);
686 }
687
688 /* reset the projection matrix */
689 void resetProjection(void) {
690   glMatrixMode(GL_PROJECTION);
691   glLoadIdentity();
692   glFrustum (-9, 9, -9, 9, 50, 150.0);
693   glMatrixMode(GL_MODELVIEW);
694   glLoadIdentity();
695 }
696
697 /* Standard reshape function */
698 static void
699 reshape(int width, int height)
700 {
701   global_width=width;
702   global_height=height;
703   glViewport( 0, 0, global_width, global_height );
704   resetProjection();
705 }
706
707
708 /* decide which screensaver example to run */
709 void chooseScreensaverExample(void) {
710   int i;
711   /* call the extrusion init routine */
712
713   if (!strncmp(which_name, "RANDOM", strlen(which_name))) {
714     screensaver_number = random() % num_screensavers;
715   }
716   else {
717         screensaver_number=-1;
718         for (i=0; i < num_screensavers; i++) {
719           if (!strncmp(which_name, funcs_ptr[i].name, strlen(which_name))) {
720                 screensaver_number = i;
721           }
722         }         
723   }
724         
725   if (screensaver_number < 0 || screensaver_number >= num_screensavers) {
726         fprintf(stderr, "%s: invalid screensaver example number!\n", progname);
727         fprintf(stderr, "%s: known screensavers:\n", progname);
728         for (i=0; i < num_screensavers; i++)
729           fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
730         exit(1);
731   }
732   init_rotation();
733   funcs_ptr[screensaver_number].InitStuff();
734 }
735
736 /* main OpenGL initialization routine */
737 void initializeGL(GLsizei width, GLsizei height) 
738 {
739   int style;
740   int mode;
741
742   reshape(width, height);
743   glViewport( 0, 0, width, height ); 
744
745   glEnable(GL_DEPTH_TEST);
746   glClearColor(0,0,0,0);
747 /*    glCullFace(GL_BACK); */
748 /*    glEnable(GL_CULL_FACE); */
749   glShadeModel(GL_SMOOTH);
750
751   if (do_light)
752         SetupLight();
753   if (do_wire) {
754         glPolygonMode(GL_FRONT,GL_LINE);
755         glPolygonMode(GL_BACK,GL_LINE);
756   }
757   if (do_texture) {
758         Create_Texture(which_image, do_mipmap, do_texture_quality);
759         glEnable(GL_TEXTURE_2D);
760
761         /* configure the pipeline */
762         style = TUBE_JN_CAP;
763         style |= TUBE_CONTOUR_CLOSED;
764         style |= TUBE_NORM_FACET;
765         style |= TUBE_JN_ANGLE;
766         gleSetJoinStyle (style);
767
768         if (do_texture) {
769           mode = GLE_TEXTURE_ENABLE | GLE_TEXTURE_VERTEX_MODEL_FLAT;
770           glMatrixMode (GL_TEXTURE); glLoadIdentity ();
771           glScalef (0.25, 0.1, 1); glMatrixMode (GL_MODELVIEW);
772           gleTextureMode (mode);
773         }
774   }
775
776 }
777
778 /* xscreensaver initialization routine */
779 void init_screensaver(ModeInfo * mi)
780 {
781   int screen = MI_SCREEN(mi);
782   screensaverstruct *gp;
783
784   if (Screensaver == NULL) {
785         if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
786           return;
787   }
788   gp = &Screensaver[screen];
789
790   gp->window = MI_WINDOW(mi);
791   if ((gp->glx_context = init_GL(mi)) != NULL) {
792         reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
793         initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
794         chooseScreensaverExample();
795   } else {
796         MI_CLEARWINDOW(mi);
797   }
798
799 }
800
801 /* all sorts of nice cleanup code should go here! */
802 void release_screensaver(ModeInfo * mi)
803 {
804   int screen;
805   if (Screensaver != NULL) {
806         for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
807           /*      screensaverstruct *gp = &Screensaver[screen];*/
808         }
809         (void) free((void *) Screensaver);
810         Screensaver = NULL;
811   }
812   FreeAllGL(mi);
813 }
814 #endif
815