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