http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[xscreensaver] / hacks / glx / extrusion.c
index 726177e6787ceeed530fdc0e634b64ab313900ad..1930f4e49d78dc68c1da5aa051cc17b1e5d61f01 100644 (file)
  * which can be obtained from http://www.linas.org/gle/index.html
   */
 
-/*-
- * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
- * otherwise caddr_t is not defined correctly
- */
-
-#include <X11/Intrinsic.h>
-
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 # define PROGCLASS                                             "Screensaver"
 # define HACK_INIT                                             init_screensaver
 # define HACK_DRAW                                             draw_screensaver
+# define HACK_RESHAPE                                  reshape_screensaver
+# define HACK_HANDLE_EVENT                             screensaver_handle_event
+# define EVENT_MASK                                            PointerMotionMask
 # define screensaver_opts                              xlockmore_opts
-#define        DEFAULTS                        "*light:                        True    \n" \
+#define        DEFAULTS                        "*delay:                        20000   \n" \
+                                                                               "*showFPS:              False   \n" \
+                                                                               "*light:                        True    \n" \
                                         "*wire:                                False   \n" \
                                         "*texture:                     False   \n" \
                                                                                "*image:                        BUILTIN \n" \
 
 #ifdef USE_GL /* whole file */
 
-#ifdef HAVE_XPM
-# include <X11/xpm.h>
-# ifndef PIXEL_ALREADY_TYPEDEFED
-# define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
-# endif
-#endif
-
 #ifdef HAVE_XMU
 # ifndef VMS
 #  include <X11/Xmu/Drawing.h>
 # endif /* VMS */
 #endif
 
-#include <malloc.h>
+#include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <malloc.h>
 #include <GL/gl.h>
 #include <GL/glu.h>
 #ifdef HAVE_GLE3
@@ -83,6 +73,9 @@
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
+#include "xpm-ximage.h"
+#include "rotator.h"
+#include "gltrackball.h"
 
 #define checkImageWidth 64
 #define checkImageHeight 64
@@ -111,7 +104,7 @@ extern void DrawStuff_twistoid(void);
 #define DEF_LIGHT              "True"
 #define DEF_WIRE               "False"
 #define DEF_TEXTURE            "False"
-#define DEF_TEXTURE_QUALITY   "False"
+#define DEF_TEX_QUAL   "False"
 #define DEF_MIPMAP     "False"
 #define DEF_NAME        "RANDOM"
 #define DEF_IMAGE      "BUILTIN"
@@ -119,36 +112,36 @@ extern void DrawStuff_twistoid(void);
 static int do_light;
 static int do_wire;
 static int do_texture;
-static int do_texture_quality;
+static int do_tex_qual;
 static int do_mipmap;
 static char *which_name;
 static char *which_image;
 
 static XrmOptionDescRec opts[] = {
-  {"-light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "true" },
-  {"+light",   ".extrusion.light",     XrmoptionNoArg, (caddr_t) "false" },
-  {"-wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "true" },
-  {"+wire",    ".extrusion.wire",      XrmoptionNoArg, (caddr_t) "false" },
-  {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
-  {"+texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
-  {"-texture", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
-  {"+texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "false" },
-  {"-texture_quality", ".extrusion.texture",   XrmoptionNoArg, (caddr_t) "true" },
-  {"+mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "false" },
-  {"-mipmap", ".extrusion.mipmap",   XrmoptionNoArg, (caddr_t) "true" },
-  {"-name",   ".extrusion.name",  XrmoptionSepArg, (caddr_t) NULL },
-  {"-image",   ".extrusion.image",  XrmoptionSepArg, (caddr_t) NULL },
+  {"-light",           ".extrusion.light",   XrmoptionNoArg, "true" },
+  {"+light",           ".extrusion.light",   XrmoptionNoArg, "false" },
+  {"-wire",            ".extrusion.wire",    XrmoptionNoArg, "true" },
+  {"+wire",            ".extrusion.wire",    XrmoptionNoArg, "false" },
+  {"-texture",         ".extrusion.texture", XrmoptionNoArg, "true" },
+  {"+texture",         ".extrusion.texture", XrmoptionNoArg, "false" },
+  {"-texture",         ".extrusion.texture", XrmoptionNoArg, "true" },
+  {"+texture_quality", ".extrusion.texture", XrmoptionNoArg, "false" },
+  {"-texture_quality", ".extrusion.texture", XrmoptionNoArg, "true" },
+  {"+mipmap",          ".extrusion.mipmap",  XrmoptionNoArg, "false" },
+  {"-mipmap",          ".extrusion.mipmap",  XrmoptionNoArg, "true" },
+  {"-name",            ".extrusion.name",    XrmoptionSepArg, 0 },
+  {"-image",           ".extrusion.image",   XrmoptionSepArg, 0 },
 };
 
 
 static argtype vars[] = {
-  {(caddr_t *) &do_light,    "light",   "Light",   DEF_LIGHT,   t_Bool},
-  {(caddr_t *) &do_wire,    "wire",   "Wire",   DEF_WIRE,   t_Bool},
-  {(caddr_t *) &do_texture,    "texture",   "Texture",   DEF_TEXTURE,   t_Bool},
-  {(caddr_t *) &do_texture_quality,    "texture_quality",   "Texture_Quality",   DEF_TEXTURE_QUALITY,   t_Bool},
-  {(caddr_t *) &do_mipmap,    "mipmap",   "Mipmap",   DEF_MIPMAP,   t_Bool},
-  {(caddr_t *) &which_name, "name",   "Name",   DEF_NAME,   t_String},
-  {(caddr_t *) &which_image, "image",   "Image",   DEF_IMAGE,   t_String},
+  {&do_light,   "light",           "Light",           DEF_LIGHT,    t_Bool},
+  {&do_wire,    "wire",            "Wire",            DEF_WIRE,     t_Bool},
+  {&do_texture,         "texture",         "Texture",         DEF_TEXTURE,  t_Bool},
+  {&do_tex_qual, "texture_quality", "Texture_Quality", DEF_TEX_QUAL, t_Bool},
+  {&do_mipmap,   "mipmap",          "Mipmap",          DEF_MIPMAP,   t_Bool},
+  {&which_name,  "name",            "Name",            DEF_NAME,     t_String},
+  {&which_image, "image",           "Image",           DEF_IMAGE,    t_String},
 };
 
 
@@ -158,7 +151,7 @@ static OptionStruct desc[] =
   {"-/+ light", "whether to do enable lighting (slower)"},
   {"-/+ wire", "whether to do use wireframe instead of filled (faster)"},
   {"-/+ texture", "whether to apply a texture (slower)"},
-  {"-image <filename>", "texture image to load (PPM or PPM4)"},
+  {"-image <filename>", "texture image to load"},
   {"-/+ texture_quality", "whether to use texture smoothing (slower)"},
   {"-/+ mipmap", "whether to use texture mipmap (slower)"},
 };
@@ -178,39 +171,35 @@ ModStruct   screensaver_description =
 typedef struct {
   int screen_width, screen_height;
   GLXContext *glx_context;
+  rotator *rot;
+  trackball_state *trackball;
+  Bool button_down_p;
+  Bool button2_down_p;
+  int mouse_start_x, mouse_start_y;
+  int mouse_x, mouse_y;
+  int mouse_dx, mouse_dy;
   Window window;
   XColor fg, bg;
 } screensaverstruct;
-static screensaverstruct *Screensaver = NULL;
-
 
+static screensaverstruct *Screensaver = NULL;
 
 
-/* convenient access to the screen width */
-static int global_width=640, global_height=480;
 
 /* set up a light */
 static GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
-static GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0}; 
+static GLfloat lightOneColor[] = {0.99, 0.99, 0.00, 1.0}; 
 
 static GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
-static GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0}; 
+static GLfloat lightTwoColor[] = {0.00, 0.99, 0.99, 1.0}; 
 
 float rot_x=0, rot_y=0, rot_z=0;
-static float dx=0, dy=0, dz=0;
-static float ddx=0, ddy=0, ddz=0;
-static float d_max = 0;
-static int screensaver_number;
+float lastx=0, lasty=0;
 
-static float max_lastx=300, max_lasty=400;
+static float max_lastx=400,  max_lasty=400;
 static float min_lastx=-400, min_lasty=-400;
-static float d_lastx=0, d_lasty=0;
-static float dd_lastx=0, dd_lasty=0;
-static float max_dlastx=0, max_dlasty=0;
-float lastx=0, lasty=0;
 
-static int errCode;
-static GLubyte * errString;
+static int screensaver_number;
 
 struct functions {
   void (*InitStuff)(void);
@@ -222,7 +211,7 @@ struct functions {
    like we're looking at them from the back or something
 */
 
-struct functions funcs_ptr[] = {
+static struct functions funcs_ptr[] = {
   {InitStuff_helix2, DrawStuff_helix2, "helix2"},
   {InitStuff_helix3, DrawStuff_helix3, "helix3"},
   {InitStuff_helix4, DrawStuff_helix4, "helix4"},
@@ -238,21 +227,8 @@ static int num_screensavers = countof(funcs_ptr);
 /* BEGINNING OF FUNCTIONS */
 
 
-/* check for errors, bail if any.  useful for debugging */
-int checkError(int line, char *file)
-{
-  if((errCode = glGetError()) != GL_NO_ERROR) {
-    errString = (GLubyte *)gluErrorString(errCode);
-    fprintf(stderr, "%s: OpenGL error: %s detected at line %d in file %s\n",
-            progname, errString, line, file);
-    exit(1);
-  }
-  return 0;
-}
-
-
-/* generate a checkered image for texturing */
-GLubyte *Generate_Image(int *width, int *height, int *format)
+GLubyte *
+Generate_Image(int *width, int *height, int *format)
 {
   GLubyte *result;
   int i, j, c;
@@ -260,7 +236,7 @@ GLubyte *Generate_Image(int *width, int *height, int *format)
 
   *width = checkImageWidth;
   *height = checkImageHeight;
-  result = (GLubyte *)malloc(4 * *width * *height);
+  result = (GLubyte *)malloc(4 * (*width) * (*height));
 
   counter = 0;
   for (i = 0; i < checkImageWidth; i++) {
@@ -276,153 +252,11 @@ GLubyte *Generate_Image(int *width, int *height, int *format)
   *format = GL_RGBA;
   return result;
 }
-/* Load a modified version of PPM format with an extra byte for alpha */
-GLubyte *LoadPPM4(const char *filename, int *width, int *height, int *format)
-{
-  char buff[1024];
-  GLubyte *data;
-  int sizeX, sizeY;
-  FILE *fp;
-  int maxval;
-
-  fp = fopen(filename, "rb");
-  if (!fp)
-    {
-      fprintf(stderr, "%s: unable to open file '%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  if (!fgets(buff, sizeof(buff), fp))
-    {
-      perror("Unable to read header filename\n");
-      return  Generate_Image(width, height, format);
-    }
-
-  if (buff[0] != '6' || buff[1] != 'P')
-    {
-      fprintf(stderr, "%s: Invalid image format (must be `6P')\n", progname);
-      return  Generate_Image(width, height, format);
-    }
-
-  do
-    {
-      fgets(buff, sizeof(buff), fp);
-    }
-  while (buff[0] == '#');
-    
-  if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  if (fscanf(fp, "%d", &maxval) != 1)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  while (fgetc(fp) != '\n')
-    ;
-
-  data = (GLubyte *)malloc(4 * sizeX * sizeY);
-  if (data == NULL)
-    {
-      fprintf(stderr, "%s: unable to allocate memory\n", progname);
-         exit(1);
-    }
-
-  if (fread(data, 4 * sizeX, sizeY, fp) != sizeY)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  fclose(fp);
-
-  *width = sizeX;
-  *height = sizeY;
-  *format = GL_RGBA;
-  return data;
-}
-
-/* Load a plain PPM image */
-GLubyte *LoadPPM(const char *filename, int *width, int *height, int *format)
-{
-  char buff[1024];
-  GLubyte *data;
-  GLint sizeX, sizeY;
-  FILE *fp;
-  int maxval;
-
-  fp = fopen(filename, "rb");
-  if (!fp)
-    {
-      fprintf(stderr, "%s: unable to open file '%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-      exit(1);
-    }
-  if (!fgets(buff, sizeof(buff), fp))
-    {
-      perror(filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  if (buff[0] != 'P' || buff[1] != '6')
-    {
-      fprintf(stderr, "%s: invalid image format (must be `P6')\n", progname);
-      return  Generate_Image(width, height, format);
-    }
-
-  do
-    {
-      fgets(buff, sizeof(buff), fp);
-    }
-  while (buff[0] == '#');
-    
-  if (sscanf(buff, "%d %d", &sizeX, &sizeY) != 2)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  if (fscanf(fp, "%d", &maxval) != 1)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  while (fgetc(fp) != '\n')
-    ;
-
-  data = (GLubyte *)malloc(3 * sizeX * sizeY);
-  if (data == NULL)
-    {
-      fprintf(stderr, "%s: unable to allocate memory\n", progname);
-         exit(1);
-    }
-
-  if (fread(data, 3 * sizeX, sizeY, fp) != sizeY)
-    {
-      fprintf(stderr, "%s: error loading image `%s'\n", progname, filename);
-      return  Generate_Image(width, height, format);
-    }
-
-  fclose(fp);
-
-  *width = sizeX;
-  *height = sizeY;
-  *format = GL_RGB;
-  return data;
-}
 
-/* create a texture to be applied to the surface
-   this function loads a file using a loader depending on
-   that extension of the file. there is very little error
-   checking.  
-*/
 
-void Create_Texture(char *filename, int do_mipmap, int do_texture_quality)
+/* Create a texture in OpenGL.  First an image is loaded 
+   and stored in a raster buffer, then it's  */
+void Create_Texture(ModeInfo *mi, const char *filename)
 {
   int height, width;
   GLubyte *image;
@@ -430,246 +264,143 @@ void Create_Texture(char *filename, int do_mipmap, int do_texture_quality)
 
   if ( !strncmp(filename, "BUILTIN", 7))
     image = Generate_Image(&width, &height, &format);
-  else if ( !strncmp((filename+strlen(filename)-3), "ppm", 3))
-    image = LoadPPM(filename, &width, &height, &format);
-  else if ( !strncmp((filename+strlen(filename)-4), "ppm4", 4))
-    image = LoadPPM4(filename, &width, &height, &format);
-  else {
-    fprintf(stderr, "%s: unknown file format extension: '%s'\n",
-            progname, filename);
-       exit(1);
-  }
+  else
+    {
+      XImage *ximage = xpm_file_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
+                                           MI_COLORMAP (mi), filename);
+      image  = (GLubyte *) ximage->data;
+      width  = ximage->width;
+      height = ximage->height;
+      format = GL_RGBA;
+    }
 
   /* GL_MODULATE or GL_DECAL depending on what you want */
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+  /* perhaps we can edge a bit more speed at the expense of quality */
+  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
 
-  /* default is to do it quick and dirty */
-  /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
-  if (do_texture_quality) {
-    /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-  } else {
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-  }
-
-  if (do_mipmap) {
-    gluBuild2DMipmaps(GL_TEXTURE_2D, format, width, height, 
-                     format, GL_UNSIGNED_BYTE, image);
+  if (do_tex_qual) {
+       /* with texture_quality, the min and mag filters look *much* nice but are *much* slower */
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   }
   else {
-    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
-                format, GL_UNSIGNED_BYTE, image);
+       /* default is to do it quick and dirty */
+       /* if you have mipmaps turned on, but not texture quality, nothing will happen! */
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
   }
-  free(image);
-}
-
-
-/* mostly lifted from lament.c */
-static void
-rotate (float *pos, float *v, float *dv, float max_v)
-{
-  double ppos = *pos;
 
-  /* tick position */
-  if (ppos < 0)
-    ppos = -(ppos + *v);
-  else
-    ppos += *v;
-
-  if (ppos > 360)
-    ppos -= 360;
-  else if (ppos < 0)
-    ppos += 360;
-
-  if (ppos < 0) abort();
-  if (ppos > 360) abort();
-  *pos = (*pos > 0 ? ppos : -ppos);
-
-  /* accelerate */
-  *v += *dv;
-
-  /* clamp velocity */
-  if (*v > max_v || *v < -max_v)
+  /* mipmaps make the image look much nicer */
+  if (do_mipmap)
     {
-      *dv = -*dv;
-    }
-  /* If it stops, start it going in the other direction. */
-  else if (*v < 0)
-    {
-      if (random() % 4)
+      int status;
+      clear_gl_error();
+      status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, format,
+                                 GL_UNSIGNED_BYTE, image);
+      if (status)
         {
-          *v = 0;
-
-          /* keep going in the same direction */
-          if (random() % 2)
-            *dv = 0;
-          else if (*dv < 0)
-            *dv = -*dv;
+          const char *s = (char *) gluErrorString (status);
+          fprintf (stderr, "%s: error mipmapping %dx%d texture: %s\n",
+                   progname, width, height,
+                   (s ? s : "(unknown)"));
+          exit (1);
         }
-      else
-        {
-          /* reverse gears */
-          *v = -*v;
-          *dv = -*dv;
-          *pos = -*pos;
-        }
-    }
-
-  /* Alter direction of rotational acceleration randomly. */
-  if (! (random() % 120))
-    *dv = -*dv;
-
-  /* Change acceleration very occasionally. */
-  if (! (random() % 200))
-    {
-      if (*dv == 0)
-        *dv = 0.00001;
-      else if (random() & 1)
-        *dv *= 1.2;
-      else
-        *dv *= 0.8;
-    }
-}
-
-
-static void
-bounce (float *pos, float *v, float *dv, float max_v)
-{
-  *pos += *v;
-
-  if (*pos > 1.0)
-    *pos = 1.0, *v = -*v, *dv = -*dv;
-  else if (*pos < 0)
-    *pos = 0, *v = -*v, *dv = -*dv;
-
-  if (*pos < 0.0) abort();
-  if (*pos > 1.0) abort();
-
-  /* accelerate */
-  *v += *dv;
-
-  /* clamp velocity */
-  if (*v > max_v || *v < -max_v)
-    {
-      *dv = -*dv;
+      check_gl_error("mipmapping");
     }
-
-  /* Alter direction of rotational acceleration randomly. */
-  if (! (random() % 120))
-    *dv = -*dv;
-
-  /* Change acceleration very occasionally. */
-  if (! (random() % 200))
+  else
     {
-      if (*dv == 0)
-        *dv = 0.00001;
-      else if (random() & 1)
-        *dv *= 1.2;
-      else
-        *dv *= 0.8;
+      clear_gl_error();
+      glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0,
+                   format, GL_UNSIGNED_BYTE, image);
+      check_gl_error("texture");
     }
 }
 
 
 static void
-init_rotation (void)
+init_rotation (ModeInfo *mi)
 {
-  rot_x = (float) (random() % (360 * 2)) - 360;  /* -360 - 360 */
-  rot_y = (float) (random() % (360 * 2)) - 360;
-  rot_z = (float) (random() % (360 * 2)) - 360;
-
-  /* bell curve from 0-1.5 degrees, avg 0.75 */
-  dx = (frand(1) + frand(1) + frand(1)) / 2.0;
-  dy = (frand(1) + frand(1) + frand(1)) / 2.0;
-  dz = (frand(1) + frand(1) + frand(1)) / 2.0;
-
-  d_max = dx * 2;
-
-  ddx = 0.004;
-  ddy = 0.004;
-  ddz = 0.004;
+  screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
+  double spin_speed = 0.5;
+  gp->rot = make_rotator (spin_speed, spin_speed, spin_speed,
+                          0.2,
+                          0.005,
+                          True);
+  gp->trackball = gltrackball_init ();
 
   lastx = (random() % (int) (max_lastx - min_lastx)) + min_lastx;
   lasty = (random() % (int) (max_lasty - min_lasty)) + min_lasty;
-  d_lastx = (frand(1) + frand(1) + frand(1));
-  d_lasty = (frand(1) + frand(1) + frand(1));
-  max_dlastx = d_lastx * 2;
-  max_dlasty = d_lasty * 2;
-  dd_lastx = 0.004;
-  dd_lasty = 0.004;
 }
 
 
 /* draw the screensaver once */
-void draw_screensaver(ModeInfo * mi)
+void
+draw_screensaver(ModeInfo * mi)
 {
   screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
   Display    *display = MI_DISPLAY(mi);
   Window      window = MI_WINDOW(mi);
 
-  Window root, child;
-  int rootx, rooty, winx, winy;
-  unsigned int mask;
-  XEvent event;
+  static GLfloat color[4] = {0.6, 0.6, 0.4, 1.0};
+  /* static GLfloat spec[4]  = {0.6, 0.6, 0.6, 1.0}; */
+  /* static GLfloat shiny    = 40.0; */
+
+  double x, y, z;
 
   if (!gp->glx_context)
        return;
 
-  glXMakeCurrent(display, window, *(gp->glx_context));
+  glPushMatrix();
 
-  funcs_ptr[screensaver_number].DrawStuff();
-         
-  rotate(&rot_x, &dx, &ddx, d_max);
-  rotate(&rot_y, &dy, &ddy, d_max);
-  rotate(&rot_z, &dz, &ddz, d_max);
+  gltrackball_rotate (gp->trackball);
 
-  /* swallow any ButtonPress events */
-  while (XCheckMaskEvent (MI_DISPLAY(mi), ButtonPressMask, &event))
-    ;
-  /* check the pointer position and button state. */
-  XQueryPointer (MI_DISPLAY(mi), MI_WINDOW(mi),
-                 &root, &child, &rootx, &rooty, &winx, &winy, &mask);
+  get_rotation (gp->rot, &x, &y, &z,
+                !(gp->button_down_p || gp->button2_down_p));
+  glRotatef (x * 360, 1.0, 0.0, 0.0);
+  glRotatef (y * 360, 0.0, 1.0, 0.0);
+  glRotatef (z * 360, 0.0, 0.0, 1.0);
 
   /* track the mouse only if a button is down. */
-  if (mask & (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask))
-    {
-      lastx = winx;
-      lasty = winy;
-    }
-  else
+  if (gp->button2_down_p)
     {
-      float scale = (max_lastx - min_lastx);
-      lastx -= min_lastx;
-      lasty -= min_lasty;
-      lastx /= scale;
-      lasty /= scale;
-      d_lastx /= scale;
-      d_lasty /= scale;
-      dd_lastx /= scale;
-      dd_lasty /= scale;
-      bounce(&lastx, &d_lastx, &dd_lastx, max_dlastx);
-      bounce(&lasty, &d_lasty, &dd_lasty, max_dlasty);
-      lastx *= scale;
-      lasty *= scale;
-      lastx += min_lastx;
-      lasty += min_lasty;
-      d_lastx *= scale;
-      d_lasty *= scale;
-      dd_lastx *= scale;
-      dd_lasty *= scale;
+      gp->mouse_dx += gp->mouse_x - gp->mouse_start_x;
+      gp->mouse_dy += gp->mouse_y - gp->mouse_start_y;
+      gp->mouse_start_x = gp->mouse_x;
+      gp->mouse_start_y = gp->mouse_y;
     }
 
+  {
+    float scale = (max_lastx - min_lastx);
+    get_position (gp->rot, &x, &y, &z,
+                  !(gp->button_down_p || gp->button2_down_p));
+    lastx = x * scale + min_lastx + gp->mouse_dx;
+    lasty = y * scale + min_lasty + gp->mouse_dy;
+  }
+
+  glScalef(0.5, 0.5, 0.5);
+
+  /* glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR,            spec); */
+  /* glMateriali  (GL_FRONT_AND_BACK, GL_SHININESS,           shiny); */
+
+  glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color);
+  glFrontFace(GL_CCW);
+
+  funcs_ptr[screensaver_number].DrawStuff();
+         
+  glPopMatrix();
+
+  if (mi->fps_p) do_fps (mi);
   glXSwapBuffers(display, window);
 }
 
 
 /* set up lighting conditions */
-void SetupLight(void)
+static void
+SetupLight(void)
 {
   glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
   glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
@@ -685,28 +416,32 @@ void SetupLight(void)
   glEnable (GL_COLOR_MATERIAL);
 }
 
-/* reset the projection matrix */
-void resetProjection(void) {
+/* Standard reshape function */
+void
+reshape_screensaver (ModeInfo *mi, int width, int height)
+{
+  GLfloat h = (GLfloat) height / (GLfloat) width;
+
+  glViewport (0, 0, (GLint) width, (GLint) height);
+
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  glFrustum (-9, 9, -9, 9, 50, 150.0);
+  gluPerspective (30.0, 1/h, 1.0, 100.0);
+
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
-}
+  gluLookAt( 0.0, 0.0, 30.0,
+             0.0, 0.0, 0.0,
+             0.0, 1.0, 0.0);
 
-/* Standard reshape function */
-static void
-reshape(int width, int height)
-{
-  global_width=width;
-  global_height=height;
-  glViewport( 0, 0, global_width, global_height );
-  resetProjection();
+  glClear(GL_COLOR_BUFFER_BIT);
 }
 
 
 /* decide which screensaver example to run */
-void chooseScreensaverExample(void) {
+static void
+chooseScreensaverExample (ModeInfo *mi)
+{
   int i;
   /* call the extrusion init routine */
 
@@ -729,23 +464,25 @@ void chooseScreensaverExample(void) {
          fprintf(stderr,"\t%s\n", funcs_ptr[i].name);
        exit(1);
   }
-  init_rotation();
+  init_rotation(mi);
   funcs_ptr[screensaver_number].InitStuff();
 }
 
+
 /* main OpenGL initialization routine */
-void initializeGL(GLsizei width, GLsizei height) 
+static void
+initializeGL(ModeInfo *mi, GLsizei width, GLsizei height) 
 {
   int style;
   int mode;
 
-  reshape(width, height);
+  reshape_screensaver(mi, width, height);
   glViewport( 0, 0, width, height ); 
 
   glEnable(GL_DEPTH_TEST);
   glClearColor(0,0,0,0);
-/*    glCullFace(GL_BACK); */
-/*    glEnable(GL_CULL_FACE); */
+  glDisable (GL_CULL_FACE);
+  glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
   glShadeModel(GL_SMOOTH);
 
   if (do_light)
@@ -755,7 +492,7 @@ void initializeGL(GLsizei width, GLsizei height)
        glPolygonMode(GL_BACK,GL_LINE);
   }
   if (do_texture) {
-       Create_Texture(which_image, do_mipmap, do_texture_quality);
+       Create_Texture(mi, which_image);
        glEnable(GL_TEXTURE_2D);
 
        /* configure the pipeline */
@@ -775,41 +512,91 @@ void initializeGL(GLsizei width, GLsizei height)
 
 }
 
+Bool
+screensaver_handle_event (ModeInfo *mi, XEvent *event)
+{
+  screensaverstruct *gp = &Screensaver[MI_SCREEN(mi)];
+
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button == Button1)
+    {
+      gp->button_down_p = True;
+      gltrackball_start (gp->trackball,
+                         event->xbutton.x, event->xbutton.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+  else if (event->xany.type == ButtonRelease &&
+           event->xbutton.button == Button1)
+    {
+      gp->button_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == ButtonPress &&
+           (event->xbutton.button == Button4 ||
+            event->xbutton.button == Button5))
+    {
+      gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
+                              !!event->xbutton.state);
+      return True;
+    }
+  else if (event->xany.type == ButtonPress &&
+           event->xbutton.button != Button1)
+    {
+      gp->button2_down_p = True;
+      gp->mouse_start_x = gp->mouse_x = event->xbutton.x;
+      gp->mouse_start_y = gp->mouse_y = event->xbutton.y;
+      return True;
+    }
+  else if (event->xany.type == ButtonRelease &&
+           event->xbutton.button != Button1)
+    {
+      gp->button2_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == MotionNotify)
+    {
+      if (gp->button_down_p)
+        gltrackball_track (gp->trackball,
+                           event->xmotion.x, event->xmotion.y,
+                           MI_WIDTH (mi), MI_HEIGHT (mi));
+      if (gp->button2_down_p)
+        {
+          gp->mouse_x = event->xmotion.x;
+          gp->mouse_y = event->xmotion.y;
+        }
+      return True;
+    }
+
+  return False;
+}
+
+
 /* xscreensaver initialization routine */
-void init_screensaver(ModeInfo * mi)
+void
+init_screensaver (ModeInfo * mi)
 {
   int screen = MI_SCREEN(mi);
   screensaverstruct *gp;
 
+  if (do_wire) do_light = 0;
+
   if (Screensaver == NULL) {
-       if ((Screensaver = (screensaverstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
+       if ((Screensaver = (screensaverstruct *)
+         calloc(MI_NUM_SCREENS(mi), sizeof (screensaverstruct))) == NULL)
          return;
   }
   gp = &Screensaver[screen];
 
   gp->window = MI_WINDOW(mi);
   if ((gp->glx_context = init_GL(mi)) != NULL) {
-       reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
-       initializeGL(MI_WIDTH(mi), MI_HEIGHT(mi));
-       chooseScreensaverExample();
+       reshape_screensaver(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+       initializeGL(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+       chooseScreensaverExample(mi);
   } else {
        MI_CLEARWINDOW(mi);
   }
 
 }
 
-/* all sorts of nice cleanup code should go here! */
-void release_screensaver(ModeInfo * mi)
-{
-  int screen;
-  if (Screensaver != NULL) {
-       for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-         /*      screensaverstruct *gp = &Screensaver[screen];*/
-       }
-       (void) free((void *) Screensaver);
-       Screensaver = NULL;
-  }
-  FreeAllGL(mi);
-}
-#endif
-
+#endif  /* USE_GL */