http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / hacks / glx / spheremonics.c
index b894ae84c503a7bc994a386d63e95b1b71d29de7..29177c290a0f197f969d50a4ca5e15d2ebd3da81 100644 (file)
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 2002 Jamie Zawinski <jwz@jwz.org>
+/* xscreensaver, Copyright (c) 2002-2008 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
  * There tends to be a dark stripe in the colormaps.  I don't know why.
  * Perhaps utils/colors.c is at fault?
  *
- * With the hairier objects, some of the faces are inside out.  E.g.,
+ * Note that this equation sometimes generates faces that are inside out:
  *     -parameters 01210111
- * If we turn off GL_CULL_FACE, that object renders more solidly
- * (indicating wrong winding) and the altered surfaces are too dark
- * (indicating wrong normals.)
+ * To make this work, we need to render back-faces with two-sided lighting:
+ * figuring out how to correct the winding and normals on those inside out
+ * surfaces would be too hard.
  */
 
-#include <X11/Intrinsic.h>
+#define DEFAULTS "*delay:      30000       \n" \
+                "*showFPS:     False       \n" \
+                "*wireframe:   False       \n" \
+                "*labelfont:   -*-times-bold-r-normal-*-180-*\n"
 
-extern XtAppContext app;
+# define refresh_spheremonics 0
+# define release_spheremonics 0
+#undef countof
+#define countof(x) (sizeof((x))/sizeof((*x)))
+
+#include "xlockmore.h"
+#include "glxfonts.h"
+#include "normals.h"
+#include "colors.h"
+#include "rotator.h"
+#include "gltrackball.h"
+#include <ctype.h>
 
-#define PROGCLASS      "Spheremonics"
-#define HACK_INIT      init_spheremonics
-#define HACK_DRAW      draw_spheremonics
-#define HACK_RESHAPE   reshape_spheremonics
-#define ccs_opts       xlockmore_opts
+#ifdef USE_GL /* whole file */
 
 #define DEF_DURATION    "100"
 #define DEF_SPIN        "XYZ"
@@ -79,41 +89,11 @@ extern XtAppContext app;
 #define DEF_SMOOTH      "True"
 #define DEF_PARMS       "(default)"
 
-#define DEFAULTS       "*delay:        30000       \n" \
-                       "*resolution: " DEF_RESOLUTION "\n" \
-                       "*showFPS:      False       \n" \
-                       "*wireframe:    False       \n" \
-                       "*duration:   " DEF_DURATION "\n" \
-                       "*spin:       " DEF_SPIN   "\n" \
-                       "*wander:     " DEF_WANDER "\n" \
-                       "*bbox:       " DEF_BBOX   "\n" \
-                       "*grid:       " DEF_GRID   "\n" \
-                        "*smooth:     " DEF_SMOOTH "\n" \
-                        "*parameters: " DEF_PARMS  "\n" \
-
-#undef countof
-#define countof(x) (sizeof((x))/sizeof((*x)))
-
-#include "xlockmore.h"
-#include "colors.h"
-#include <ctype.h>
-
-#ifdef USE_GL /* whole file */
-
-#include <GL/glu.h>
-
-typedef struct {
-   double x,y,z;
-} XYZ;
-
 typedef struct {
   GLXContext *glx_context;
-
-  GLfloat rotx, roty, rotz;       /* current object rotation */
-  GLfloat dx, dy, dz;             /* current rotational velocity */
-  GLfloat ddx, ddy, ddz;          /* current rotational acceleration */
-  GLfloat d_max;                  /* max velocity */
-  Bool spin_x, spin_y, spin_z;
+  rotator *rot;
+  trackball_state *trackball;
+  Bool button_down_p;
 
   GLuint dlist, dlist2;
   GLfloat scale;
@@ -129,9 +109,12 @@ typedef struct {
 
   int tracer;
   int mesher;
+  int polys1, polys2;  /* polygon counts */
 
   XFontStruct *font;
   GLuint font_list;
+  int change_tick;
+  int done_once;
 
 } spheremonics_configuration;
 
@@ -163,22 +146,22 @@ static XrmOptionDescRec opts[] = {
 };
 
 static argtype vars[] = {
-  {(caddr_t *) &do_spin,   "spin",   "Spin",   DEF_SPIN,   t_String},
-  {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
-  {(caddr_t *) &res,       "resolution", "Resolution", DEF_RESOLUTION, t_Int},
-  {(caddr_t *) &duration,  "duration",   "Duration",   DEF_DURATION,   t_Int},
-  {(caddr_t *) &do_bbox,   "bbox",   "BBox",   DEF_BBOX,   t_Bool},
-  {(caddr_t *) &do_grid,   "grid",   "Grid",   DEF_GRID,   t_Bool},
-  {(caddr_t *) &smooth_p,  "smooth", "Smooth", DEF_SMOOTH, t_Bool},
-  {(caddr_t *) &static_parms, "parameters", "Parameters", DEF_PARMS, t_String},
+  {&do_spin,      "spin",       "Spin",       DEF_SPIN,       t_String},
+  {&do_wander,    "wander",     "Wander",     DEF_WANDER,     t_Bool},
+  {&res,          "resolution", "Resolution", DEF_RESOLUTION, t_Int},
+  {&duration,     "duration",   "Duration",   DEF_DURATION,   t_Int},
+  {&do_bbox,      "bbox",       "BBox",       DEF_BBOX,       t_Bool},
+  {&do_grid,      "grid",       "Grid",       DEF_GRID,       t_Bool},
+  {&smooth_p,     "smooth",     "Smooth",     DEF_SMOOTH,     t_Bool},
+  {&static_parms, "parameters", "Parameters", DEF_PARMS,      t_String},
 };
 
-ModeSpecOpt ccs_opts = {countof(opts), opts, countof(vars), vars, NULL};
+ENTRYPOINT ModeSpecOpt spheremonics_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 
 /* Window management, etc
  */
-void
+ENTRYPOINT void
 reshape_spheremonics (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
@@ -187,14 +170,13 @@ reshape_spheremonics (ModeInfo *mi, int width, int height)
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
+  gluPerspective (30.0, 1/h, 1.0, 100.0);
 
-  gluPerspective( 30.0, 1/h, 1.0, 100.0 );
-  gluLookAt( 0.0, 0.0, 15.0,
-             0.0, 0.0, 0.0,
-             0.0, 1.0, 0.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
-  glTranslatef(0.0, 0.0, -15.0);
+  gluLookAt( 0.0, 0.0, 30.0,
+             0.0, 0.0, 0.0,
+             0.0, 1.0, 0.0);
 
   glClear(GL_COLOR_BUFFER_BIT);
 }
@@ -206,91 +188,42 @@ gl_init (ModeInfo *mi)
 /*  spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)]; */
   int wire = MI_IS_WIREFRAME(mi);
 
-  static GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
+  static const GLfloat pos[4] = {5.0, 5.0, 10.0, 1.0};
 
   glEnable(GL_NORMALIZE);
 
   if (!wire)
     {
       glLightfv(GL_LIGHT0, GL_POSITION, pos);
-      glEnable(GL_CULL_FACE);
       glEnable(GL_LIGHTING);
       glEnable(GL_LIGHT0);
       glEnable(GL_DEPTH_TEST);
-    }
-}
-
-
-/* lifted from lament.c */
-#define RAND(n) ((long) ((random() & 0x7fffffff) % ((long) (n))))
-#define RANDSIGN() ((random() & 1) ? 1 : -1)
-
-static void
-rotate(GLfloat *pos, GLfloat *v, GLfloat *dv, GLfloat max_v)
-{
-  double ppos = *pos;
-
-  /* tick position */
-  if (ppos < 0)
-    ppos = -(ppos + *v);
-  else
-    ppos += *v;
-
-  if (ppos > 1.0)
-    ppos -= 1.0;
-  else if (ppos < 0)
-    ppos += 1.0;
-
-  if (ppos < 0) abort();
-  if (ppos > 1.0) abort();
-  *pos = (*pos > 0 ? ppos : -ppos);
-
-  /* accelerate */
-  *v += *dv;
-
-  /* clamp velocity */
-  if (*v > max_v || *v < -max_v)
-    {
-      *dv = -*dv;
-    }
-  /* If it stops, start it going in the other direction. */
-  else if (*v < 0)
-    {
-      if (random() % 4)
-       {
-         *v = 0;
-
-         /* keep going in the same direction */
-         if (random() % 2)
-           *dv = 0;
-         else if (*dv < 0)
-           *dv = -*dv;
-       }
-      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;
+      /* With objects that have proper winding and normals set up on all
+         their faces, one can cull back-faces; however, these equations
+         generate objects that are sometimes "inside out", and determining
+         whether a facet has been inverted like that is really hard.
+         So we render both front and back faces, at a probable performance
+         penalty on non-accelerated systems.
+
+         When rendering back faces, we also need to do two-sided lighting,
+         or the fact that the normals are flipped gives us too-dark surfaces
+         on the inside-out surfaces.
+
+         This isn't generally something you'd want, because you end up
+         with half the lighting dynamic range (kind of.)  So if you had
+         a sphere with correctly pointing normals, and a single light
+         source, it would be illuminated from two sides.  In this case,
+         though, it saves us from a difficult and time consuming
+         inside/outside test.  And we don't really care about a precise
+         lighting effect.
+       */
+      glDisable(GL_CULL_FACE);
+      glLightModeli (GL_LIGHT_MODEL_TWO_SIDE, True);
     }
 }
 
+
 \f
 /* generate the object */
 
@@ -313,46 +246,6 @@ sphere_eval (double theta, double phi, int *m)
 }
 
 
-/* Normalise a vector */
-static void
-normalize (XYZ *p)
-{
-  double length;
-  length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
-  if (length != 0) {
-    p->x /= length;
-    p->y /= length;
-    p->z /= length;
-  } else {
-    p->x = 0;
-    p->y = 0;
-    p->z = 0;
-  }       
-}
-
-/*-------------------------------------------------------------------------
-        Calculate the unit normal at p given two other points 
-        p1,p2 on the surface. The normal points in the direction 
-        of p1 crossproduct p2
- */
-static XYZ
-calc_normal (XYZ p, XYZ p1, XYZ p2)
-{
-  XYZ n, pa, pb;
-  pa.x = p1.x - p.x;
-  pa.y = p1.y - p.y;
-  pa.z = p1.z - p.z;
-  pb.x = p2.x - p.x;
-  pb.y = p2.y - p.y;
-  pb.z = p2.z - p.z;
-  n.x = pa.y * pb.z - pa.z * pb.y;
-  n.y = pa.z * pb.x - pa.x * pb.z;
-  n.z = pa.x * pb.y - pa.y * pb.x;
-  normalize (&n);
-  return (n);
-}
-
-
 static void
 do_color (int i, XColor *colors)
 {
@@ -411,8 +304,8 @@ draw_bounding_box (ModeInfo *mi)
 {
   spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
 
-  static GLfloat c1[4] = { 0.2, 0.2, 0.6, 1.0 };
-  static GLfloat c2[4] = { 1.0, 0.0, 0.0, 1.0 };
+  static const GLfloat c1[4] = { 0.2, 0.2, 0.6, 1.0 };
+  static const GLfloat c2[4] = { 1.0, 0.0, 0.0, 1.0 };
   int wire = MI_IS_WIREFRAME(mi);
 
   GLfloat x1 = cc->bbox[0].x;
@@ -431,6 +324,7 @@ draw_bounding_box (ModeInfo *mi)
     {
       glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, c1);
       glFrontFace(GL_CCW);
+      glEnable(GL_CULL_FACE);
 
       glBegin(wire ? GL_LINE_LOOP : GL_QUADS);
       glNormal3f(0, 1, 0);
@@ -462,6 +356,7 @@ draw_bounding_box (ModeInfo *mi)
       glVertex3f(x2, y1, z1); glVertex3f(x2, y1, z2);
       glVertex3f(x2, y2, z2); glVertex3f(x2, y2, z1);
       glEnd();
+      glDisable(GL_CULL_FACE);
     }
 
   glPushAttrib (GL_LIGHTING);
@@ -525,7 +420,7 @@ do_tracer (ModeInfo *mi)
 
       if (s > 0.001)
         {
-          static GLfloat c[4] = { 0.6, 0.5, 1.0, 1.0 };
+          static const GLfloat c[4] = { 0.6, 0.5, 1.0, 1.0 };
 
           glPushAttrib (GL_LIGHTING);
           glDisable (GL_LIGHTING);
@@ -549,12 +444,12 @@ do_tracer (ModeInfo *mi)
 }
 
 
-static void
+static int
 unit_spheremonics (ModeInfo *mi,
                    int resolution, Bool wire, int *m, XColor *colors)
 {
   spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
-
+  int polys = 0;
   int i, j;
   double du, dv;
   XYZ q[4];
@@ -572,8 +467,6 @@ unit_spheremonics (ModeInfo *mi,
   if (wire)
     glColor3f (1, 1, 1);
 
-/*  mi->polygon_count = 0; */
-
   glBegin (wire ? GL_LINE_LOOP : GL_QUADS);
 
   for (i = 0; i < res; i++) {
@@ -612,7 +505,7 @@ unit_spheremonics (ModeInfo *mi,
       if (!wire) do_color (i, colors);
       glVertex3f(q[3].x,q[3].y,q[3].z);
 
-/*      mi->polygon_count++; */
+      polys++;
 
 # define CHECK_BBOX(N) \
          if (q[(N)].x < cc->bbox[0].x) cc->bbox[0].x = q[(N)].x; \
@@ -650,6 +543,7 @@ unit_spheremonics (ModeInfo *mi,
         glPopMatrix();
       }
   }
+  return polys;
 }
 
 
@@ -758,18 +652,15 @@ generate_spheremonics (ModeInfo *mi)
 
   tweak_parameters (mi);
 
-  {
-    static Bool done = False;
-    if (!done || (0 == (random() % 20)))
-      {
-        init_colors (mi);
-        done = True;
-      }
-  }
+  if (!cc->done_once || (0 == (random() % 20)))
+    {
+      init_colors (mi);
+      cc->done_once = True;
+    }
 
   {
     glNewList(cc->dlist, GL_COMPILE);
-    unit_spheremonics (mi, cc->resolution, wire, cc->m, cc->colors);
+    cc->polys1 = unit_spheremonics (mi, cc->resolution, wire,cc->m,cc->colors);
     glEndList();
 
     glNewList(cc->dlist2, GL_COMPILE);
@@ -777,7 +668,7 @@ generate_spheremonics (ModeInfo *mi)
     glDisable (GL_LIGHTING);
     glPushMatrix();
     glScalef (1.05, 1.05, 1.05);
-    unit_spheremonics (mi, cc->resolution, 2, cc->m, cc->colors);
+    cc->polys2 = unit_spheremonics (mi, cc->resolution, 2, cc->m, cc->colors);
     glPopMatrix();
     glPopAttrib();
     glEndList();
@@ -787,67 +678,7 @@ generate_spheremonics (ModeInfo *mi)
 
 \f
 
-static void
-load_font (ModeInfo *mi, char *res, XFontStruct **fontP, GLuint *dlistP)
-{
-  const char *font = get_string_resource (res, "Font");
-  XFontStruct *f;
-  Font id;
-  int first, last;
-
-  if (!font) font = "-*-times-bold-r-normal-*-140-*";
-
-  f = XLoadQueryFont(mi->dpy, font);
-  if (!f) f = XLoadQueryFont(mi->dpy, "fixed");
-
-  id = f->fid;
-  first = f->min_char_or_byte2;
-  last = f->max_char_or_byte2;
-  
-  clear_gl_error ();
-  *dlistP = glGenLists ((GLuint) last+1);
-  check_gl_error ("glGenLists");
-  glXUseXFont(id, first, last-first+1, *dlistP + first);
-  check_gl_error ("glXUseXFont");
-
-  *fontP = f;
-}
-
-static void
-draw_label (ModeInfo *mi, const char *s)
-{
-  spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
-  int i;
-  
-  glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT);
-  glDisable(GL_LIGHTING);
-  glDisable(GL_DEPTH_TEST);
-  glMatrixMode(GL_PROJECTION);
-  glPushMatrix();
-  glLoadIdentity();
-  glMatrixMode(GL_MODELVIEW);
-  glPushMatrix();
-  glLoadIdentity();
-  gluOrtho2D(0, mi->xgwa.width, 0, mi->xgwa.height);
-  glColor3f(1.0, 1.0, 0.0);
-
-  glRasterPos2f (10,
-                 (mi->xgwa.height
-                  - 10
-                  - (cc->font->ascent + cc->font->descent)));
-  for (i = 0; i < strlen(s); i++)
-    glCallList (cc->font_list + (int)s[i]);
-
-  glPopMatrix();
-  glMatrixMode(GL_PROJECTION);
-  glPopMatrix();
-  glPopAttrib();
-}
-
-
-\f
-
-void 
+ENTRYPOINT void 
 init_spheremonics (ModeInfo *mi)
 {
   spheremonics_configuration *cc;
@@ -879,12 +710,17 @@ init_spheremonics (ModeInfo *mi)
     }
 
   {
+    Bool spinx=False, spiny=False, spinz=False;
+    double spin_speed   = 1.0;
+    double wander_speed = 0.03;
+
     char *s = do_spin;
     while (*s)
       {
-        if      (*s == 'x' || *s == 'X') cc->spin_x = 1;
-        else if (*s == 'y' || *s == 'Y') cc->spin_y = 1;
-        else if (*s == 'z' || *s == 'Z') cc->spin_z = 1;
+        if      (*s == 'x' || *s == 'X') spinx = True;
+        else if (*s == 'y' || *s == 'Y') spiny = True;
+        else if (*s == 'z' || *s == 'Z') spinz = True;
+        else if (*s == '0') ;
         else
           {
             fprintf (stderr,
@@ -894,35 +730,29 @@ init_spheremonics (ModeInfo *mi)
           }
         s++;
       }
-  }
 
-  cc->rotx = frand(1.0) * RANDSIGN();
-  cc->roty = frand(1.0) * RANDSIGN();
-  cc->rotz = frand(1.0) * RANDSIGN();
-
-  /* bell curve from 0-6 degrees, avg 3 */
-  cc->dx = (frand(0.4) + frand(0.4) + frand(0.4)) / (360/2);
-  cc->dy = (frand(0.4) + frand(0.4) + frand(0.4)) / (360/2);
-  cc->dz = (frand(0.4) + frand(0.4) + frand(0.4)) / (360/2);
-
-  cc->d_max = cc->dx * 2;
+    cc->rot = make_rotator (spinx ? spin_speed : 0,
+                            spinz ? spin_speed : 0,
+                            spiny ? spin_speed : 0,
+                            1.0,
+                            do_wander ? wander_speed : 0,
+                            (spinx && spiny && spinz));
+    cc->trackball = gltrackball_init ();
+  }
 
-  cc->ddx = 0.00006 + frand(0.00003);
-  cc->ddy = 0.00006 + frand(0.00003);
-  cc->ddz = 0.00006 + frand(0.00003);
   cc->tracer = -1;
   cc->mesher = -1;
 
   cc->resolution = res;
 
-  load_font (mi, "labelfont", &cc->font, &cc->font_list);
+  load_font (mi->dpy, "labelfont", &cc->font, &cc->font_list);
 
   cc->dlist = glGenLists(1);
   cc->dlist2 = glGenLists(1);
 
   cc->m_max = 4; /* 9? */
   {
-    int i;
+    unsigned int i;
     for (i = 0; i < countof(cc->dm); i++)
       cc->dm[i] = 1;  /* going up! */
 
@@ -935,33 +765,72 @@ init_spheremonics (ModeInfo *mi)
 }
 
 
-static Bool
-mouse_down_p (ModeInfo *mi)
+ENTRYPOINT Bool
+spheremonics_handle_event (ModeInfo *mi, XEvent *event)
 {
-  Window root, child;
-  int rx, ry, wx, wy;
-  unsigned int mask;
-  if (!XQueryPointer (MI_DISPLAY(mi), MI_WINDOW(mi),
-                      &root, &child, &rx, &ry, &wx, &wy, &mask))
-    return False;
-  if (! (mask & Button1Mask))
-    return False;
-  return True;
+  spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
+
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button == Button1)
+    {
+      cc->button_down_p = True;
+      gltrackball_start (cc->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)
+    {
+      cc->button_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == ButtonPress &&
+           (event->xbutton.button == Button4 ||
+            event->xbutton.button == Button5 ||
+            event->xbutton.button == Button6 ||
+            event->xbutton.button == Button7))
+    {
+      gltrackball_mousewheel (cc->trackball, event->xbutton.button, 10,
+                              !!event->xbutton.state);
+      return True;
+    }
+  else if (event->xany.type == KeyPress)
+    {
+      KeySym keysym;
+      char c = 0;
+      XLookupString (&event->xkey, &c, 1, &keysym, 0);
+
+      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
+        {
+          cc->change_tick = duration;
+          return True;
+        }
+    }
+  else if (event->xany.type == MotionNotify &&
+           cc->button_down_p)
+    {
+      gltrackball_track (cc->trackball,
+                         event->xmotion.x, event->xmotion.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+
+  return False;
 }
 
 
-void
+ENTRYPOINT void
 draw_spheremonics (ModeInfo *mi)
 {
   spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
   Display *dpy = MI_DISPLAY(mi);
   Window window = MI_WINDOW(mi);
-  Bool mouse_p;
 
   if (!cc->glx_context)
     return;
 
-  mouse_p = mouse_down_p (mi);
+  glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(cc->glx_context));
 
   glShadeModel(GL_SMOOTH);
 
@@ -972,70 +841,62 @@ draw_spheremonics (ModeInfo *mi)
   glScalef(1.1, 1.1, 1.1);
 
   {
-    GLfloat x, y, z;
-
-    if (do_wander)
-      {
-        static int frame = 0;
-
-#       define SINOID(SCALE,SIZE) \
-        ((((1 + sin((frame * (SCALE)) / 2 * M_PI)) / 2.0) * (SIZE)) - (SIZE)/2)
-
-        x = SINOID(0.0071, 8.0);
-        y = SINOID(0.0053, 6.0);
-        z = SINOID(0.0037, 15.0);
-        frame++;
-        glTranslatef(x, y, z);
-      }
-
-    if (cc->spin_x || cc->spin_y || cc->spin_z)
-      {
-        x = cc->rotx;
-        y = cc->roty;
-        z = cc->rotz;
-        if (x < 0) x = 1 - (x + 1);
-        if (y < 0) y = 1 - (y + 1);
-        if (z < 0) z = 1 - (z + 1);
-
-        if (cc->spin_x) glRotatef(x * 360, 1.0, 0.0, 0.0);
-        if (cc->spin_y) glRotatef(y * 360, 0.0, 1.0, 0.0);
-        if (cc->spin_z) glRotatef(z * 360, 0.0, 0.0, 1.0);
-
-        rotate(&cc->rotx, &cc->dx, &cc->ddx, cc->d_max);
-        rotate(&cc->roty, &cc->dy, &cc->ddy, cc->d_max);
-        rotate(&cc->rotz, &cc->dz, &cc->ddz, cc->d_max);
-      }
+    double x, y, z;
+    get_position (cc->rot, &x, &y, &z, !cc->button_down_p);
+    glTranslatef((x - 0.5) * 8,
+                 (y - 0.5) * 6,
+                 (z - 0.5) * 8);
+
+    gltrackball_rotate (cc->trackball);
+
+    get_rotation (cc->rot, &x, &y, &z, !cc->button_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);
   }
 
   glScalef(7,7,7);
 
+  mi->polygon_count = 0;
+
   glScalef (cc->scale, cc->scale, cc->scale);
   glCallList (cc->dlist);
+  mi->polygon_count += cc->polys1;
+
   if (cc->mesher >= 0 /* || mouse_p */)
     {
       glCallList (cc->dlist2);
+      mi->polygon_count += cc->polys2;
       if (cc->mesher >= 0)
         cc->mesher--;
     }
   do_tracer(mi);
 
 
-  if (mouse_p)
+  if (cc->button_down_p)
     {
       char buf[200];
-      sprintf (buf, "%d %d %d %d %d %d %d %d",
+      sprintf (buf,
+               ((cc->m[0]<10 && cc->m[1]<10 && cc->m[2]<10 && cc->m[3]<10 &&
+                 cc->m[4]<10 && cc->m[5]<10 && cc->m[6]<10 && cc->m[7]<10)
+                ? "%d%d%d%d%d%d%d%d"
+                : "%d %d %d %d %d %d %d %d"),
                cc->m[0], cc->m[1], cc->m[2], cc->m[3],
                cc->m[4], cc->m[5], cc->m[6], cc->m[7]);
-      draw_label (mi, buf);
+
+      glColor3f(1.0, 1.0, 0.0);
+      print_gl_string (mi->dpy, cc->font, cc->font_list,
+                       mi->xgwa.width, mi->xgwa.height,
+                       10, mi->xgwa.height - 10,
+                       buf);
     }
 
   if (!static_parms)
     {
-      static int tick = 0;
-      if (tick++ == duration)
+      if (cc->change_tick++ >= duration && !cc->button_down_p)
         {
           generate_spheremonics(mi);
-          tick = 0;
+          cc->change_tick = 0;
           cc->mesher = -1;  /* turn off the mesh when switching objects */
         }
     }
@@ -1048,4 +909,6 @@ draw_spheremonics (ModeInfo *mi)
   glXSwapBuffers(dpy, window);
 }
 
+XSCREENSAVER_MODULE ("Spheremonics", spheremonics)
+
 #endif /* USE_GL */