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