http://ftp.nluug.nl/pub/os/Linux/distr/pardusrepo/sources/xscreensaver-5.02.tar.gz
[xscreensaver] / hacks / glx / polytopes.c
index d4a9eaa53669d14ced50e3e78ebd8b397ad529cf..257481554614f7588c5ae8687e3655f397d9d2dc 100644 (file)
@@ -1,11 +1,10 @@
 /* polytopes --- Shows one of the six regular polytopes rotating in 4d */
 
-#if !defined( lint ) && !defined( SABER )
-static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
-
+#if 0
+static const char sccsid[] = "@(#)polytopes.c  1.2 05/09/28 xlockmore";
 #endif
 
-/* Copyright (c) 2003 Carsten Steger <carsten@mirsanmir.org>. */
+/* Copyright (c) 2003-2007 Carsten Steger <carsten@mirsanmir.org>. */
 
 /*
  * Permission to use, copy, modify, and distribute this software and its
@@ -22,6 +21,8 @@ static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
  *
  * REVISION HISTORY:
  * C. Steger - 03/08/10: Initial version
+ * C. Steger - 05/09/28: Added trackball support
+ * C. Steger - 07/01/23: Improved 4d trackball support
  */
 
 /*
@@ -114,25 +115,10 @@ static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
 #define DEF_DTHETA                DTHETA_STR
 
 #ifdef STANDALONE
-# define PROGCLASS       "Polytopes"
-# define HACK_INIT       init_polytopes
-# define HACK_DRAW       draw_polytopes
-# define HACK_RESHAPE    reshape_polytopes
-# define polytopes_opts xlockmore_opts
-# define DEFAULTS        "*delay:      25000 \n" \
-                         "*showFPS:    False \n" \
-                         "*wireframe:  False \n" \
-                         "*displayMode: "  DEF_DISPLAY_MODE  " \n" \
-                         "*polytope: "     DEF_POLYTOPE      " \n" \
-                         "*colors: "       DEF_COLORS        " \n" \
-                         "*projection3d: " DEF_3D_PROJECTION " \n" \
-                         "*projection4d: " DEF_4D_PROJECTION " \n" \
-                         "speedwx: "       DEF_DALPHA        " \n" \
-                         "speedwy: "       DEF_DBETA         " \n" \
-                         "speedwz: "       DEF_DDELTA        " \n" \
-                         "speedxy: "       DEF_DZETA         " \n" \
-                         "speedxz: "       DEF_DETA          " \n" \
-                         "speedyz: "       DEF_DTHETA        " \n"
+# define DEFAULTS           "*delay:      25000 \n" \
+                            "*showFPS:    False \n"
+
+# define refresh_polytopes 0
 # include "xlockmore.h"         /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"             /* from the xlockmore distribution */
@@ -140,8 +126,8 @@ static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
 
 #ifdef USE_GL
 
-#include <GL/gl.h>
-#include <GL/glu.h>
+#include <X11/keysym.h>
+#include "gltrackball.h"
 
 
 #ifdef USE_MODULES
@@ -166,91 +152,73 @@ static float speed_xy;
 static float speed_xz;
 static float speed_yz;
 
-/* 4D rotation angles */
-static float alpha, beta, delta, zeta, eta, theta;
-static float aspect;
-
 static const float offset4d[4] = {  0.0,  0.0,  0.0,  3.0 };
 static const float offset3d[4] = {  0.0,  0.0, -2.0,  0.0 };
 
 
 static XrmOptionDescRec opts[] =
 {
-  {"-mesh",            ".polytopes.displayMode",  XrmoptionNoArg,
-                       (caddr_t)DISP_WIREFRAME_STR },
+  {"-mode",            ".polytopes.displayMode",  XrmoptionSepArg, 0 },
+  {"-wireframe",       ".polytopes.displayMode",  XrmoptionNoArg,
+                       DISP_WIREFRAME_STR },
   {"-surface",         ".polytopes.displayMode",  XrmoptionNoArg,
-                       (caddr_t)DISP_SURFACE_STR },
+                       DISP_SURFACE_STR },
   {"-transparent",     ".polytopes.displayMode",  XrmoptionNoArg,
-                       (caddr_t)DISP_TRANSPARENT_STR },
+                       DISP_TRANSPARENT_STR },
   {"-random",          ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_RANDOM_STR },
+                       POLYTOPE_RANDOM_STR },
+
+  {"-polytope",        ".polytopes.polytope",     XrmoptionSepArg, 0 },
   {"-5-cell",          ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_5_CELL_STR },
+                       POLYTOPE_5_CELL_STR },
   {"-8-cell",          ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_8_CELL_STR },
+                       POLYTOPE_8_CELL_STR },
   {"-16-cell",         ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_16_CELL_STR },
+                       POLYTOPE_16_CELL_STR },
   {"-24-cell",         ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_24_CELL_STR },
+                       POLYTOPE_24_CELL_STR },
   {"-120-cell",        ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_120_CELL_STR },
+                       POLYTOPE_120_CELL_STR },
   {"-600-cell",        ".polytopes.polytope",     XrmoptionNoArg,
-                       (caddr_t)POLYTOPE_600_CELL_STR },
+                       POLYTOPE_600_CELL_STR },
   {"-single-color",    ".polytopes.colors",       XrmoptionNoArg,
-                       (caddr_t)COLORS_SINGLE_STR },
+                       COLORS_SINGLE_STR },
   {"-depth-colors",    ".polytopes.colors",       XrmoptionNoArg,
-                       (caddr_t)COLORS_DEPTH_STR },
+                       COLORS_DEPTH_STR },
   {"-perspective-3d",  ".polytopes.projection3d", XrmoptionNoArg,
-                       (caddr_t)DISP_3D_PERSPECTIVE_STR },
+                       DISP_3D_PERSPECTIVE_STR },
   {"-orthographic-3d", ".polytopes.projection3d", XrmoptionNoArg,
-                       (caddr_t)DISP_3D_ORTHOGRAPHIC_STR },
+                       DISP_3D_ORTHOGRAPHIC_STR },
   {"-perspective-4d",  ".polytopes.projection4d", XrmoptionNoArg,
-                       (caddr_t)DISP_4D_PERSPECTIVE_STR },
+                       DISP_4D_PERSPECTIVE_STR },
   {"-orthographic-4d", ".polytopes.projection4d", XrmoptionNoArg,
-                       (caddr_t)DISP_4D_ORTHOGRAPHIC_STR },
-  {"-speed-wx",        ".polytopes.speedwx",      XrmoptionSepArg,
-                       (caddr_t)NULL },
-  {"-speed-wy",        ".polytopes.speedwy",      XrmoptionSepArg,
-                       (caddr_t)NULL },
-  {"-speed-wz",        ".polytopes.speedwz",      XrmoptionSepArg,
-                       (caddr_t)NULL },
-  {"-speed-xy",        ".polytopes.speedxy",      XrmoptionSepArg,
-                       (caddr_t)NULL },
-  {"-speed-xz",        ".polytopes.speedxz",      XrmoptionSepArg,
-                       (caddr_t)NULL },
-  {"-speed-yz",        ".polytopes.speedyz",      XrmoptionSepArg,
-                       (caddr_t)NULL }
+                       DISP_4D_ORTHOGRAPHIC_STR },
+  {"-speed-wx",        ".polytopes.speedwx",      XrmoptionSepArg, 0 },
+  {"-speed-wy",        ".polytopes.speedwy",      XrmoptionSepArg, 0 },
+  {"-speed-wz",        ".polytopes.speedwz",      XrmoptionSepArg, 0 },
+  {"-speed-xy",        ".polytopes.speedxy",      XrmoptionSepArg, 0 },
+  {"-speed-xz",        ".polytopes.speedxz",      XrmoptionSepArg, 0 },
+  {"-speed-yz",        ".polytopes.speedyz",      XrmoptionSepArg, 0 }
 };
 
 static argtype vars[] =
 {
-  { (caddr_t *) &display_mode,  "displayMode",  "DisplayMode",
-    DEF_DISPLAY_MODE,  t_Int },
-  { (caddr_t *) &polytope,      "polytope",     "Polytope",
-    DEF_POLYTOPE,      t_Int },
-  { (caddr_t *) &color_mode,    "colors",       "Colors",
-    DEF_COLORS,        t_Int },
-  { (caddr_t *) &projection_3d, "projection3d", "Projection3d",
-    DEF_3D_PROJECTION, t_Int },
-  { (caddr_t *) &projection_4d, "projection4d", "Projection4d",
-    DEF_4D_PROJECTION, t_Int },
-  { (caddr_t *) &speed_wx,      "speedwx",      "Speedwx",
-    DEF_DALPHA,        t_Float},
-  { (caddr_t *) &speed_wy,      "speedwy",      "Speedwy",
-    DEF_DBETA,         t_Float},
-  { (caddr_t *) &speed_wz,      "speedwz",      "Speedwz",
-    DEF_DDELTA,        t_Float},
-  { (caddr_t *) &speed_xy,      "speedxy",      "Speedxy",
-    DEF_DZETA,         t_Float},
-  { (caddr_t *) &speed_xz,      "speedxz",      "Speedxz",
-    DEF_DETA,          t_Float},
-  { (caddr_t *) &speed_yz,      "speedyz",      "Speedyz",
-    DEF_DTHETA,        t_Float}
+  { &display_mode,  "displayMode",  "DisplayMode", DEF_DISPLAY_MODE,  t_Int },
+  { &polytope,      "polytope",     "Polytope",    DEF_POLYTOPE,      t_Int },
+  { &color_mode,    "colors",       "Colors",      DEF_COLORS,        t_Int },
+  { &projection_3d, "projection3d", "Projection3d",DEF_3D_PROJECTION, t_Int },
+  { &projection_4d, "projection4d", "Projection4d",DEF_4D_PROJECTION, t_Int },
+  { &speed_wx,      "speedwx",      "Speedwx",     DEF_DALPHA,        t_Float},
+  { &speed_wy,      "speedwy",      "Speedwy",     DEF_DBETA,         t_Float},
+  { &speed_wz,      "speedwz",      "Speedwz",     DEF_DDELTA,        t_Float},
+  { &speed_xy,      "speedxy",      "Speedxy",     DEF_DZETA,         t_Float},
+  { &speed_xz,      "speedxz",      "Speedxz",     DEF_DETA,          t_Float},
+  { &speed_yz,      "speedyz",      "Speedyz",     DEF_DTHETA,        t_Float}
 };
 
 static OptionStruct desc[] =
 {
-  { "-mesh",            "display the polytope as a wireframe mesh" },
+  { "-wireframe",       "display the polytope as a wireframe mesh" },
   { "-surface",         "display the polytope as a solid surface" },
   { "-transparent",     "display the polytope as a transparent surface" },
   { "-solid",           "display the polytope as a solid object" },
@@ -269,13 +237,121 @@ static OptionStruct desc[] =
   { "-speed-yz <arg>",  "rotation speed around the yz plane" }
 };
 
-ModeSpecOpt polytopes_opts =
+ENTRYPOINT ModeSpecOpt polytopes_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 
+/* 5-cell {3,3,3} */
+#define NUM_VERT_5 5
+#define NUM_EDGE_5 10
+#define NUM_FACE_5 10
+#define VERT_PER_FACE_5 3
+
+#define MIN_EDGE_DEPTH_5 (-0.5)
+#define MAX_EDGE_DEPTH_5 0.75
+#define MIN_FACE_DEPTH_5 (-0.5)
+#define MAX_FACE_DEPTH_5 (1.0/3.0)
+
+
+/* 8-cell {4,3,3} */
+#define NUM_VERT_8 16
+#define NUM_EDGE_8 32
+#define NUM_FACE_8 24
+#define VERT_PER_FACE_8 4
+
+#define MIN_EDGE_DEPTH_8 (-1.0)
+#define MAX_EDGE_DEPTH_8 1.0
+#define MIN_FACE_DEPTH_8 (-1.0)
+#define MAX_FACE_DEPTH_8 1.0
+
+/* 16-cell {3,3,4} */
+#define NUM_VERT_16 8
+#define NUM_EDGE_16 24
+#define NUM_FACE_16 32
+#define VERT_PER_FACE_16 3
+
+#define MIN_EDGE_DEPTH_16 (-1.0)
+#define MAX_EDGE_DEPTH_16 1.0
+#define MIN_FACE_DEPTH_16 (-2.0/3.0)
+#define MAX_FACE_DEPTH_16 (2.0/3.0)
+
+
+/* 24-cell {3,4,3} */
+#define NUM_VERT_24 24
+#define NUM_EDGE_24 96
+#define NUM_FACE_24 96
+#define VERT_PER_FACE_24 3
+
+#define MIN_EDGE_DEPTH_24 (-SQRT2)
+#define MAX_EDGE_DEPTH_24 SQRT2
+#define MIN_FACE_DEPTH_24 (-SQRT2)
+#define MAX_FACE_DEPTH_24 SQRT2
+
+
+/* 120-cell {5,3,3} */
+#define NUM_VERT_120 600
+#define NUM_EDGE_120 1200
+#define NUM_FACE_120 720
+#define VERT_PER_FACE_120 5
+
+#define MIN_EDGE_DEPTH_120 (-GOLDEN22)
+#define MAX_EDGE_DEPTH_120 GOLDEN22
+#define MIN_FACE_DEPTH_120 (-GOLDEN22)
+#define MAX_FACE_DEPTH_120 GOLDEN22
+
+
+/* 600-cell {3,3,5} */
+#define NUM_VERT_600 120
+#define NUM_EDGE_600 720
+#define NUM_FACE_600 1200
+#define VERT_PER_FACE_600 3
+
+#define MIN_EDGE_DEPTH_600 (-GOLDEN/2.0-1)
+#define MAX_EDGE_DEPTH_600 (GOLDEN/2.0+1)
+#define MIN_FACE_DEPTH_600 ((-2*GOLDEN-2)/3.0)
+#define MAX_FACE_DEPTH_600 ((2*GOLDEN+2)/3.0)
+
+
 typedef struct {
   GLint       WindH, WindW;
   GLXContext *glx_context;
+  /* 4D rotation angles */
+  float alpha, beta, delta, zeta, eta, theta;
+  /* Aspect ratio of the current window */
+  float aspect;
+  /* Counter */
+  int tick, poly;
+  /* Trackball states */
+  trackball_state *trackballs[2];
+  int current_trackball;
+  Bool button_pressed;
+
+  float edge_color_5[NUM_EDGE_5][4];
+  float face_color_5[NUM_FACE_5][4];
+  float face_color_trans_5[NUM_FACE_5][4];
+
+  float edge_color_8[NUM_EDGE_8][4];
+  float face_color_8[NUM_FACE_8][4];
+  float face_color_trans_8[NUM_FACE_8][4];
+
+  float edge_color_16[NUM_EDGE_16][4];
+  float face_color_16[NUM_FACE_16][4];
+  float face_color_trans_16[NUM_FACE_16][4];
+
+  float edge_color_24[NUM_EDGE_24][4];
+  float face_color_24[NUM_FACE_24][4];
+  float face_color_trans_24[NUM_FACE_24][4];
+
+  float edge_color_120[NUM_EDGE_120][4];
+  float face_color_120[NUM_FACE_120][4];
+  float face_color_trans_120[NUM_FACE_120][4];
+
+  float edge_color_600[NUM_EDGE_600][4];
+  float face_color_600[NUM_FACE_600][4];
+  float face_color_trans_600[NUM_FACE_600][4];
+
+  float speed_scale;
+
 } polytopesstruct;
 
 static polytopesstruct *poly = (polytopesstruct *) NULL;
@@ -285,18 +361,7 @@ static polytopesstruct *poly = (polytopesstruct *) NULL;
    polytopes are constructed with coordinates chosen such that their 4d
    circumsphere has a radius of 2. */
 
-/* 5-cell {3,3,3} */
-#define NUM_VERT_5 5
-#define NUM_EDGE_5 10
-#define NUM_FACE_5 10
-#define VERT_PER_FACE_5 3
-
-#define MIN_EDGE_DEPTH_5 (-0.5)
-#define MAX_EDGE_DEPTH_5 0.75
-#define MIN_FACE_DEPTH_5 (-0.5)
-#define MAX_FACE_DEPTH_5 (1.0/3.0)
-
-static float vert_5[NUM_VERT_5][4] = {
+static const float vert_5[NUM_VERT_5][4] = {
   { -SQRT5OVER2, -SQRT5OVER6, -SQRT5OVER12, -0.5 },
   {  SQRT5OVER2, -SQRT5OVER6, -SQRT5OVER12, -0.5 },
   {         0.0, SQRT10OVER3, -SQRT5OVER12, -0.5 },
@@ -304,33 +369,18 @@ static float vert_5[NUM_VERT_5][4] = {
   {         0.0,         0.0,          0.0,  2.0 }
 };
 
-static int edge_5[NUM_EDGE_5][2] = {
+static const int edge_5[NUM_EDGE_5][2] = {
   { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 1, 2 }, { 1, 3 }, { 1, 4 },
   { 2, 3 }, { 2, 4 }, { 3, 4 }
 };
 
-static int face_5[NUM_FACE_5][VERT_PER_FACE_5] = {
+static const int face_5[NUM_FACE_5][VERT_PER_FACE_5] = {
   { 0, 1, 2 }, { 0, 1, 3 }, { 0, 1, 4 }, { 0, 2, 3 }, { 0, 2, 4 }, { 0, 3, 4 },
   { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 }, { 2, 3, 4 }
 };
 
-static float edge_color_5[NUM_EDGE_5][4];
-static float face_color_5[NUM_FACE_5][4];
-static float face_color_trans_5[NUM_FACE_5][4];
 
-
-/* 8-cell {4,3,3} */
-#define NUM_VERT_8 16
-#define NUM_EDGE_8 32
-#define NUM_FACE_8 24
-#define VERT_PER_FACE_8 4
-
-#define MIN_EDGE_DEPTH_8 (-1.0)
-#define MAX_EDGE_DEPTH_8 1.0
-#define MIN_FACE_DEPTH_8 (-1.0)
-#define MAX_FACE_DEPTH_8 1.0
-
-static float vert_8[NUM_VERT_8][4] = {
+static const float vert_8[NUM_VERT_8][4] = {
   { -1.0, -1.0, -1.0, -1.0 }, {  1.0, -1.0, -1.0, -1.0 },
   { -1.0,  1.0, -1.0, -1.0 }, {  1.0,  1.0, -1.0, -1.0 },
   { -1.0, -1.0,  1.0, -1.0 }, {  1.0, -1.0,  1.0, -1.0 },
@@ -341,7 +391,7 @@ static float vert_8[NUM_VERT_8][4] = {
   { -1.0,  1.0,  1.0,  1.0 }, {  1.0,  1.0,  1.0,  1.0 }
 };
 
-static int edge_8[NUM_EDGE_8][2] = {
+static const int edge_8[NUM_EDGE_8][2] = {
   {  0,  1 }, {  0,  2 }, {  0,  4 }, {  0,  8 }, {  1,  3 }, {  1,  5 },
   {  1,  9 }, {  2,  3 }, {  2,  6 }, {  2, 10 }, {  3,  7 }, {  3, 11 },
   {  4,  5 }, {  4,  6 }, {  4, 12 }, {  5,  7 }, {  5, 13 }, {  6,  7 },
@@ -350,7 +400,7 @@ static int edge_8[NUM_EDGE_8][2] = {
   { 13, 15 }, { 14, 15 }
 };
 
-static int face_8[NUM_FACE_8][VERT_PER_FACE_8] = {
+static const int face_8[NUM_FACE_8][VERT_PER_FACE_8] = {
   {  0,  1,  3,  2 }, {  0,  1,  5,  4 }, {  0,  1,  9,  8 },
   {  0,  2,  6,  4 }, {  0,  2, 10,  8 }, {  0,  4, 12,  8 },
   {  1,  3,  7,  5 }, {  1,  3, 11,  9 }, {  1,  5, 13,  9 },
@@ -361,37 +411,23 @@ static int face_8[NUM_FACE_8][VERT_PER_FACE_8] = {
   {  9, 11, 15, 13 }, { 10, 11, 15, 14 }, { 12, 13, 15, 14 }
 };
 
-static float edge_color_8[NUM_EDGE_8][4];
-static float face_color_8[NUM_FACE_8][4];
-static float face_color_trans_8[NUM_FACE_8][4];
-
-
-/* 16-cell {3,3,4} */
-#define NUM_VERT_16 8
-#define NUM_EDGE_16 24
-#define NUM_FACE_16 32
-#define VERT_PER_FACE_16 3
 
-#define MIN_EDGE_DEPTH_16 (-1.0)
-#define MAX_EDGE_DEPTH_16 1.0
-#define MIN_FACE_DEPTH_16 (-2.0/3.0)
-#define MAX_FACE_DEPTH_16 (2.0/3.0)
 
-static float vert_16[NUM_VERT_16][4] = {
+static const float vert_16[NUM_VERT_16][4] = {
   {  0.0,  0.0,  0.0, -2.0 }, {  0.0,  0.0, -2.0,  0.0 },
   {  0.0, -2.0,  0.0,  0.0 }, { -2.0,  0.0,  0.0,  0.0 },
   {  2.0,  0.0,  0.0,  0.0 }, {  0.0,  2.0,  0.0,  0.0 },
   {  0.0,  0.0,  2.0,  0.0 }, {  0.0,  0.0,  0.0,  2.0 }
 };
 
-static int edge_16[NUM_EDGE_16][2] = {
+static const int edge_16[NUM_EDGE_16][2] = {
   { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 1, 2 },
   { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 7 }, { 2, 3 }, { 2, 4 }, { 2, 6 },
   { 2, 7 }, { 3, 5 }, { 3, 6 }, { 3, 7 }, { 4, 5 }, { 4, 6 }, { 4, 7 },
   { 5, 6 }, { 5, 7 }, { 6, 7 }
 };
 
-static int face_16[NUM_FACE_16][VERT_PER_FACE_16] = {
+static const int face_16[NUM_FACE_16][VERT_PER_FACE_16] = {
   { 0, 1, 2 }, { 0, 1, 3 }, { 0, 1, 4 }, { 0, 1, 5 }, { 0, 2, 3 }, { 0, 2, 4 },
   { 0, 2, 6 }, { 0, 3, 5 }, { 0, 3, 6 }, { 0, 4, 5 }, { 0, 4, 6 }, { 0, 5, 6 },
   { 1, 2, 3 }, { 1, 2, 4 }, { 1, 2, 7 }, { 1, 3, 5 }, { 1, 3, 7 }, { 1, 4, 5 },
@@ -400,23 +436,9 @@ static int face_16[NUM_FACE_16][VERT_PER_FACE_16] = {
   { 4, 6, 7 }, { 5, 6, 7 }
 };
 
-static float edge_color_16[NUM_EDGE_16][4];
-static float face_color_16[NUM_FACE_16][4];
-static float face_color_trans_16[NUM_FACE_16][4];
-
-
-/* 24-cell {3,4,3} */
-#define NUM_VERT_24 24
-#define NUM_EDGE_24 96
-#define NUM_FACE_24 96
-#define VERT_PER_FACE_24 3
 
-#define MIN_EDGE_DEPTH_24 (-SQRT2)
-#define MAX_EDGE_DEPTH_24 SQRT2
-#define MIN_FACE_DEPTH_24 (-SQRT2)
-#define MAX_FACE_DEPTH_24 SQRT2
 
-static float vert_24[NUM_VERT_24][4] = {
+static const float vert_24[NUM_VERT_24][4] = {
   {    0.0,    0.0, -SQRT2, -SQRT2 }, {    0.0, -SQRT2,    0.0, -SQRT2 },
   { -SQRT2,    0.0,    0.0, -SQRT2 }, {  SQRT2,    0.0,    0.0, -SQRT2 },
   {    0.0,  SQRT2,    0.0, -SQRT2 }, {    0.0,    0.0,  SQRT2, -SQRT2 },
@@ -431,7 +453,7 @@ static float vert_24[NUM_VERT_24][4] = {
   {    0.0,  SQRT2,    0.0,  SQRT2 }, {    0.0,    0.0,  SQRT2,  SQRT2 }
 };
 
-static int edge_24[NUM_EDGE_24][2] = {
+static const int edge_24[NUM_EDGE_24][2] = {
   {  0,  1 }, {  0,  2 }, {  0,  3 }, {  0,  4 }, {  0,  6 }, {  0,  7 },
   {  0,  8 }, {  0,  9 }, {  1,  2 }, {  1,  3 }, {  1,  5 }, {  1,  6 },
   {  1, 10 }, {  1, 11 }, {  1, 14 }, {  2,  4 }, {  2,  5 }, {  2,  7 },
@@ -450,7 +472,7 @@ static int edge_24[NUM_EDGE_24][2] = {
   { 19, 23 }, { 20, 22 }, { 20, 23 }, { 21, 22 }, { 21, 23 }, { 22, 23 }
 };
 
-static int face_24[NUM_FACE_24][VERT_PER_FACE_24] = {
+static const int face_24[NUM_FACE_24][VERT_PER_FACE_24] = {
   {  0,  1,  2 }, {  0,  1,  3 }, {  0,  1,  6 }, {  0,  2,  4 }, 
   {  0,  2,  7 }, {  0,  3,  4 }, {  0,  3,  8 }, {  0,  4,  9 },
   {  0,  6,  7 }, {  0,  6,  8 }, {  0,  7,  9 }, {  0,  8,  9 },
@@ -477,23 +499,10 @@ static int face_24[NUM_FACE_24][VERT_PER_FACE_24] = {
   { 19, 20, 23 }, { 19, 21, 23 }, { 20, 22, 23 }, { 21, 22, 23 }
 };
 
-static float edge_color_24[NUM_EDGE_24][4];
-static float face_color_24[NUM_FACE_24][4];
-static float face_color_trans_24[NUM_FACE_24][4];
 
 
-/* 120-cell {5,3,3} */
-#define NUM_VERT_120 600
-#define NUM_EDGE_120 1200
-#define NUM_FACE_120 720
-#define VERT_PER_FACE_120 5
 
-#define MIN_EDGE_DEPTH_120 (-GOLDEN22)
-#define MAX_EDGE_DEPTH_120 GOLDEN22
-#define MIN_FACE_DEPTH_120 (-GOLDEN22)
-#define MAX_FACE_DEPTH_120 GOLDEN22
-
-static float vert_120[NUM_VERT_120][4] = {
+static const float vert_120[NUM_VERT_120][4] = {
   { -GOLDENINV22,          0.0,    -SQRT2INV,    -GOLDEN22 },
   {  GOLDENINV22,          0.0,    -SQRT2INV,    -GOLDEN22 },
   {  -GOLDENINV2,  -GOLDENINV2,  -GOLDENINV2,    -GOLDEN22 },
@@ -1096,7 +1105,7 @@ static float vert_120[NUM_VERT_120][4] = {
   {  GOLDENINV22,          0.0,     SQRT2INV,     GOLDEN22 }
 };
 
-static int edge_120[NUM_EDGE_120][2] = {
+static const int edge_120[NUM_EDGE_120][2] = {
   {   0,   1 }, {   0,   2 }, {   0,   4 }, {   0,  20 }, {   1,   3 },
   {   1,   5 }, {   1,  21 }, {   2,   6 }, {   2,   8 }, {   2,  22 },
   {   3,   6 }, {   3,   9 }, {   3,  23 }, {   4,   7 }, {   4,  10 },
@@ -1339,7 +1348,7 @@ static int edge_120[NUM_EDGE_120][2] = {
   { 594, 598 }, { 595, 599 }, { 596, 598 }, { 597, 599 }, { 598, 599 }
 };
 
-static int face_120[NUM_FACE_120][VERT_PER_FACE_120] = {
+static const int face_120[NUM_FACE_120][VERT_PER_FACE_120] = {
   {   0,   1,   3,   6,   2 }, {   0,   1,   5,   7,   4 },
   {   0,   1,  21,  40,  20 }, {   0,   2,   8,  10,   4 },
   {   0,   2,  22,  41,  20 }, {   0,   4,  24,  43,  20 },
@@ -1702,23 +1711,9 @@ static int face_120[NUM_FACE_120][VERT_PER_FACE_120] = {
   { 592, 594, 598, 599, 595 }, { 593, 596, 598, 599, 597 }
 };
 
-static float edge_color_120[NUM_EDGE_120][4];
-static float face_color_120[NUM_FACE_120][4];
-static float face_color_trans_120[NUM_FACE_120][4];
 
 
-/* 600-cell {3,3,5} */
-#define NUM_VERT_600 120
-#define NUM_EDGE_600 720
-#define NUM_FACE_600 1200
-#define VERT_PER_FACE_600 3
-
-#define MIN_EDGE_DEPTH_600 (-GOLDEN/2.0-1)
-#define MAX_EDGE_DEPTH_600 (GOLDEN/2.0+1)
-#define MIN_FACE_DEPTH_600 ((-2*GOLDEN-2)/3.0)
-#define MAX_FACE_DEPTH_600 ((2*GOLDEN+2)/3.0)
-
-static float vert_600[NUM_VERT_600][4] = {
+static const float vert_600[NUM_VERT_600][4] = {
   {        0.0,        0.0,        0.0,       -2.0 },
   {        0.0, -GOLDENINV,       -1.0,    -GOLDEN },
   {        0.0,  GOLDENINV,       -1.0,    -GOLDEN },
@@ -1841,7 +1836,7 @@ static float vert_600[NUM_VERT_600][4] = {
   {        0.0,        0.0,        0.0,        2.0 }
 };
 
-static int edge_600[NUM_EDGE_600][2] = {
+static const int edge_600[NUM_EDGE_600][2] = {
   {   0,   1 }, {   0,   2 }, {   0,   3 }, {   0,   4 }, {   0,   5 },
   {   0,   6 }, {   0,   7 }, {   0,   8 }, {   0,   9 }, {   0,  10 },
   {   0,  11 }, {   0,  12 }, {   1,   2 }, {   1,   3 }, {   1,   4 },
@@ -1988,7 +1983,7 @@ static int edge_600[NUM_EDGE_600][2] = {
   { 116, 118 }, { 116, 119 }, { 117, 118 }, { 117, 119 }, { 118, 119 }
 };
 
-static int face_600[NUM_FACE_600][VERT_PER_FACE_600] = {
+static const int face_600[NUM_FACE_600][VERT_PER_FACE_600] = {
   {   0,   1,   2 }, {   0,   1,   3 }, {   0,   1,   4 }, {   0,   1,   5 },
   {   0,   1,   6 }, {   0,   2,   3 }, {   0,   2,   4 }, {   0,   2,   7 },
   {   0,   2,   8 }, {   0,   3,   5 }, {   0,   3,   7 }, {   0,   3,   9 },
@@ -2291,9 +2286,6 @@ static int face_600[NUM_FACE_600][VERT_PER_FACE_600] = {
   { 116, 117, 118 }, { 116, 117, 119 }, { 116, 118, 119 }, { 117, 118, 119 }
 };
 
-static float edge_color_600[NUM_EDGE_600][4];
-static float face_color_600[NUM_FACE_600][4];
-static float face_color_trans_600[NUM_FACE_600][4];
 
 
 /* Add a rotation around the wx-plane to the matrix m. */
@@ -2411,24 +2403,73 @@ static void rotateyz(float m[4][4], float phi)
 
 
 /* Compute the rotation matrix m from the rotation angles. */
-static void rotateall(float m[4][4])
+static void rotateall(float al, float be, float de, float ze, float et,
+                      float th, float m[4][4])
 {
   int i, j;
 
   for (i=0; i<4; i++)
     for (j=0; j<4; j++)
       m[i][j] = (i==j);
-  rotatewx(m,alpha);
-  rotatewy(m,beta);
-  rotatewz(m,delta);
-  rotatexy(m,zeta);
-  rotatexz(m,eta);
-  rotateyz(m,theta);
+  rotatewx(m,al);
+  rotatewy(m,be);
+  rotatewz(m,de);
+  rotatexz(m,et);
+  rotatexy(m,ze);
+  rotateyz(m,th);
+}
+
+
+/* Multiply two rotation matrices: o=m*n. */
+static void mult_rotmat(float m[4][4], float n[4][4], float o[4][4])
+{
+  int i, j, k;
+
+  for (i=0; i<4; i++)
+  {
+    for (j=0; j<4; j++)
+    {
+      o[i][j] = 0.0;
+      for (k=0; k<4; k++)
+        o[i][j] += m[i][k]*n[k][j];
+    }
+  }
+}
+
+
+/* Compute a 4D rotation matrix from two unit quaternions. */
+static void quats_to_rotmat(float p[4], float q[4], float m[4][4])
+{
+  double al, be, de, ze, et, th;
+  double r00, r01, r02, r12, r22;
+
+  r00 = 1.0-2.0*(p[1]*p[1]+p[2]*p[2]);
+  r01 = 2.0*(p[0]*p[1]+p[2]*p[3]);
+  r02 = 2.0*(p[2]*p[0]-p[1]*p[3]);
+  r12 = 2.0*(p[1]*p[2]+p[0]*p[3]);
+  r22 = 1.0-2.0*(p[1]*p[1]+p[0]*p[0]);
+
+  al = atan2(-r12,r22)*180.0/M_PI;
+  be = atan2(r02,sqrt(r00*r00+r01*r01))*180.0/M_PI;
+  de = atan2(-r01,r00)*180.0/M_PI;
+
+  r00 = 1.0-2.0*(q[1]*q[1]+q[2]*q[2]);
+  r01 = 2.0*(q[0]*q[1]+q[2]*q[3]);
+  r02 = 2.0*(q[2]*q[0]-q[1]*q[3]);
+  r12 = 2.0*(q[1]*q[2]+q[0]*q[3]);
+  r22 = 1.0-2.0*(q[1]*q[1]+q[0]*q[0]);
+
+  et = atan2(-r12,r22)*180.0/M_PI;
+  th = atan2(r02,sqrt(r00*r00+r01*r01))*180.0/M_PI;
+  ze = atan2(-r01,r00)*180.0/M_PI;
+
+  rotateall(al,be,de,ze,et,-th,m);
 }
 
 
 /* Compute the normal vector of a plane based on three points in the plane. */
-static void normal(float *p, float *q, float *r, float *n)
+static void normal(const float *p, const float *q, const float *r, 
+                   float *n)
 {
   float u[3], v[3], t;
 
@@ -2449,12 +2490,19 @@ static void normal(float *p, float *q, float *r, float *n)
 
 
 /* Project an array of vertices from 4d to 3d. */
-static void project(float vert[][4], float v[][4], int num)
+static void project(ModeInfo *mi, const float vert[][4], float v[][4], int num)
 {
-  float m[4][4], s;
+  float s, q1[4], q2[4], r1[4][4], r2[4][4], m[4][4];
   int i, j, k;
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
+
+  rotateall(pp->alpha,pp->beta,pp->delta,pp->zeta,pp->eta,pp->theta,r1);
 
-  rotateall(m);
+  gltrackball_get_quaternion(pp->trackballs[0],q1);
+  gltrackball_get_quaternion(pp->trackballs[1],q2);
+  quats_to_rotmat(q1,q2,r2);
+
+  mult_rotmat(r2,r1,m);
 
   /* Project the vertices from 4d to 3d. */
   for (i=0; i<num; i++)
@@ -2491,14 +2539,18 @@ static void project(float vert[][4], float v[][4], int num)
 
 /* Draw a single polytope. */
 static void draw(ModeInfo *mi,
-                 float v[][4], int edge[][2], int num_edge, int face[],
-                 int num_face, int vert_per_face, float edge_color[][4],
-                 float face_color[][4], float face_color_trans[][4])
+                 float v[][4], 
+                 const int edge[][2], int num_edge, 
+                 const int face[], int num_face, 
+                 int vert_per_face, 
+                 float edge_color[][4], 
+                 float face_color[][4], 
+                 float face_color_trans[][4])
 {
   int i, j;
   float n[3];
-  static float red[4] = { 1.0, 0.0, 0.0, 1.0 };
-  static float red_trans[4] = { 1.0, 0.0, 0.0, 1.0 };
+  GLfloat red[4] = { 1.0, 0.0, 0.0, 1.0 };
+  GLfloat red_trans[4] = { 1.0, 0.0, 0.0, 1.0 };
 
   mi->polygon_count = 0;
   if (display_mode == DISP_WIREFRAME)
@@ -2553,68 +2605,78 @@ static void draw(ModeInfo *mi,
 /* Draw a 5-cell {3,3,3} projected into 3d. */
 static void cell_5(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_5][4];
 
-  project(vert_5,v,NUM_VERT_5);
-  draw(mi, v,edge_5,NUM_EDGE_5,(int *)face_5,NUM_FACE_5,VERT_PER_FACE_5,
-       edge_color_5,face_color_5,face_color_trans_5);
+  project(mi,vert_5,v,NUM_VERT_5);
+  draw(mi,v,edge_5,NUM_EDGE_5,(int *)face_5,NUM_FACE_5,
+       VERT_PER_FACE_5,hp->edge_color_5,hp->face_color_5,
+       hp->face_color_trans_5);
 }
 
 
 /* Draw a 8-cell {4,3,3} projected into 3d. */
 static void cell_8(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_8][4];
 
-  project(vert_8,v,NUM_VERT_8);
-  draw(mi,v,edge_8,NUM_EDGE_8,(int *)face_8,NUM_FACE_8,VERT_PER_FACE_8,
-       edge_color_8,face_color_8,face_color_trans_8);
+  project(mi,vert_8,v,NUM_VERT_8);
+  draw(mi,v,edge_8,NUM_EDGE_8,(int *)face_8,NUM_FACE_8,
+       VERT_PER_FACE_8,hp->edge_color_8,hp->face_color_8,
+       hp->face_color_trans_8);
 }
 
 
 /* Draw a 16-cell {3,3,4} projected into 3d. */
 static void cell_16(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_16][4];
 
-  project(vert_16,v,NUM_VERT_16);
-  draw(mi,v,edge_16,NUM_EDGE_16,(int *)face_16,NUM_FACE_16,VERT_PER_FACE_16,
-       edge_color_16,face_color_16,face_color_trans_16);
+  project(mi,vert_16,v,NUM_VERT_16);
+  draw(mi,v,edge_16,NUM_EDGE_16,(int *)face_16,NUM_FACE_16,
+       VERT_PER_FACE_16,hp->edge_color_16,hp->face_color_16,
+       hp->face_color_trans_16);
 }
 
 
 /* Draw a 24-cell {3,4,3} projected into 3d. */
 static void cell_24(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_24][4];
 
-  project(vert_24,v,NUM_VERT_24);
-  draw(mi,v,edge_24,NUM_EDGE_24,(int *)face_24,NUM_FACE_24,VERT_PER_FACE_24,
-       edge_color_24,face_color_24,face_color_trans_24);
+  project(mi,vert_24,v,NUM_VERT_24);
+  draw(mi,v,edge_24,NUM_EDGE_24,(int *)face_24,NUM_FACE_24,
+       VERT_PER_FACE_24,hp->edge_color_24,hp->face_color_24,
+       hp->face_color_trans_24);
 }
 
 
 /* Draw a 120-cell {5,3,3} projected into 3d. */
 static void cell_120(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_120][4];
 
-  project(vert_120,v,NUM_VERT_120);
-  draw(mi,
-       v,edge_120,NUM_EDGE_120,(int *)face_120,NUM_FACE_120,VERT_PER_FACE_120,
-       edge_color_120,face_color_120,face_color_trans_120);
+  project(mi,vert_120,v,NUM_VERT_120);
+  draw(mi,v,edge_120,NUM_EDGE_120,(int *)face_120,NUM_FACE_120,
+       VERT_PER_FACE_120,hp->edge_color_120,hp->face_color_120,
+       hp->face_color_trans_120);
 }
 
 
 /* Draw a 600-cell {3,3,5} projected into 3d. */
 static void cell_600(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   float v[NUM_VERT_600][4];
 
-  project(vert_600,v,NUM_VERT_600);
-  draw(mi,
-       v,edge_600,NUM_EDGE_600,(int *)face_600,NUM_FACE_600,VERT_PER_FACE_600,
-       edge_color_600,face_color_600,face_color_trans_600);
+  project(mi,vert_600,v,NUM_VERT_600);
+  draw(mi,v,edge_600,NUM_EDGE_600,(int *)face_600,NUM_FACE_600,
+       VERT_PER_FACE_600,hp->edge_color_600,hp->face_color_600,
+       hp->face_color_trans_600);
 }
 
 
@@ -2670,10 +2732,15 @@ static void color(float depth, float alpha, float min, float max,
 
 
 /* Set the colors of a single polytope's edges and faces. */
-static void colors(float vert[][4], int edge[][2], int num_edge, int face[],
-                   int num_face, int vert_per_face, float edge_color[][4],
-                   float face_color[][4], float face_color_trans[][4],
-                   float alpha, float min_edge_depth, float max_edge_depth,
+static void colors(const float vert[][4], 
+                   const int edge[][2], int num_edge, 
+                   const int face[], int num_face, 
+                   int vert_per_face, 
+                   float edge_color[][4], 
+                   float face_color[][4],
+                   float face_color_trans[][4],
+                   float alpha, 
+                   float min_edge_depth, float max_edge_depth,
                    float min_face_depth, float max_face_depth)
 {
   int i, j;
@@ -2697,62 +2764,67 @@ static void colors(float vert[][4], int edge[][2], int num_edge, int face[],
 
 
 /* Set the colors of the polytopes' edges and faces. */
-static void set_colors(void)
+static void set_colors(ModeInfo *mi)
 {
+  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
   /* 5-cell. */
   colors(vert_5,edge_5,NUM_EDGE_5,(int *)face_5,NUM_FACE_5,
-         VERT_PER_FACE_5,edge_color_5,face_color_5,face_color_trans_5,
-         0.5,MIN_EDGE_DEPTH_5,MAX_EDGE_DEPTH_5,MIN_FACE_DEPTH_5,
-         MAX_FACE_DEPTH_5);
+         VERT_PER_FACE_5,hp->edge_color_5,hp->face_color_5,
+         hp->face_color_trans_5,0.5,MIN_EDGE_DEPTH_5,
+         MAX_EDGE_DEPTH_5,MIN_FACE_DEPTH_5,MAX_FACE_DEPTH_5);
 
   /* 8-cell. */
   colors(vert_8,edge_8,NUM_EDGE_8,(int *)face_8,NUM_FACE_8,
-         VERT_PER_FACE_8,edge_color_8,face_color_8,face_color_trans_8,
-         0.4,MIN_EDGE_DEPTH_8,MAX_EDGE_DEPTH_8,MIN_FACE_DEPTH_8,
-         MAX_FACE_DEPTH_8);
+         VERT_PER_FACE_8,hp->edge_color_8,hp->face_color_8,
+         hp->face_color_trans_8,0.4,MIN_EDGE_DEPTH_8,
+         MAX_EDGE_DEPTH_8,MIN_FACE_DEPTH_8,MAX_FACE_DEPTH_8);
 
   /* 16-cell. */
   colors(vert_16,edge_16,NUM_EDGE_16,(int *)face_16,NUM_FACE_16,
-         VERT_PER_FACE_16,edge_color_16,face_color_16,face_color_trans_16,
-         0.25,MIN_EDGE_DEPTH_16,MAX_EDGE_DEPTH_16,MIN_FACE_DEPTH_16,
-         MAX_FACE_DEPTH_16);
+         VERT_PER_FACE_16,hp->edge_color_16,hp->face_color_16,
+         hp->face_color_trans_16,0.25,MIN_EDGE_DEPTH_16,
+         MAX_EDGE_DEPTH_16,MIN_FACE_DEPTH_16,MAX_FACE_DEPTH_16);
 
   /* 24-cell. */
   colors(vert_24,edge_24,NUM_EDGE_24,(int *)face_24,NUM_FACE_24,
-         VERT_PER_FACE_24,edge_color_24,face_color_24,face_color_trans_24,
-         0.25,MIN_EDGE_DEPTH_24,MAX_EDGE_DEPTH_24,MIN_FACE_DEPTH_24,
-         MAX_FACE_DEPTH_24);
+         VERT_PER_FACE_24,hp->edge_color_24,hp->face_color_24,
+         hp->face_color_trans_24,0.25,MIN_EDGE_DEPTH_24,
+         MAX_EDGE_DEPTH_24,MIN_FACE_DEPTH_24,MAX_FACE_DEPTH_24);
 
   /* 120-cell. */
   colors(vert_120,edge_120,NUM_EDGE_120,(int *)face_120,NUM_FACE_120,
-         VERT_PER_FACE_120,edge_color_120,face_color_120,face_color_trans_120,
-         0.15,MIN_EDGE_DEPTH_120,MAX_EDGE_DEPTH_120,MIN_FACE_DEPTH_120,
-         MAX_FACE_DEPTH_120);
+         VERT_PER_FACE_120,hp->edge_color_120,hp->face_color_120,
+         hp->face_color_trans_120,0.15,MIN_EDGE_DEPTH_120,
+         MAX_EDGE_DEPTH_120,MIN_FACE_DEPTH_120,MAX_FACE_DEPTH_120);
 
   /* 600-cell. */
   colors(vert_600,edge_600,NUM_EDGE_600,(int *)face_600,NUM_FACE_600,
-         VERT_PER_FACE_600,edge_color_600,face_color_600,face_color_trans_600,
-         0.06,MIN_EDGE_DEPTH_600,MAX_EDGE_DEPTH_600,MIN_FACE_DEPTH_600,
-         MAX_FACE_DEPTH_600);
+         VERT_PER_FACE_600,hp->edge_color_600,hp->face_color_600,
+         hp->face_color_trans_600,0.06,MIN_EDGE_DEPTH_600,
+         MAX_EDGE_DEPTH_600,MIN_FACE_DEPTH_600,MAX_FACE_DEPTH_600);
 }
 
 
 static void init(ModeInfo *mi)
 {
-  static float light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
-  static float light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
-  static float light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
-  static float light_position[] = { 0.0, 0.0, 1.0, 0.0 };
-  static float mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
+  static const GLfloat light_ambient[]  = { 0.0, 0.0, 0.0, 1.0 };
+  static const GLfloat light_diffuse[]  = { 1.0, 1.0, 1.0, 1.0 };
+  static const GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
+  static const GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
+  static const GLfloat mat_specular[]   = { 1.0, 1.0, 1.0, 1.0 };
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
 
-  set_colors();
+  set_colors(mi);
 
-  alpha = 0.0;
-  beta = 0.0;
-  delta = 0.0;
-  zeta = 0.0;
-  eta = 0.0;
-  theta = 0.0;
+  pp->alpha = 0.0;
+  pp->beta = 0.0;
+  pp->delta = 0.0;
+  pp->zeta = 0.0;
+  pp->eta = 0.0;
+  pp->theta = 0.0;
+
+  pp->tick = 0;
+  pp->poly = 0;
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
@@ -2815,80 +2887,140 @@ static void init(ModeInfo *mi)
 /* Redisplay the polytopes. */
 static void display_polytopes(ModeInfo *mi)
 {
-  alpha += speed_wx;
-  if (alpha >= 360.0)
-    alpha -= 360.0;
-  beta += speed_wy;
-  if (beta >= 360.0)
-    beta -= 360.0;
-  delta += speed_wz;
-  if (delta >= 360.0)
-    delta -= 360.0;
-  zeta += speed_xy;
-  if (zeta >= 360.0)
-    zeta -= 360.0;
-  eta += speed_xz;
-  if (eta >= 360.0)
-    eta -= 360.0;
-  theta += speed_yz;
-  if (theta >= 360.0)
-    theta -= 360.0;
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
+
+  if (!pp->button_pressed)
+  {
+    pp->alpha += speed_wx * pp->speed_scale;
+    if (pp->alpha >= 360.0)
+      pp->alpha -= 360.0;
+    pp->beta += speed_wy * pp->speed_scale;
+    if (pp->beta >= 360.0)
+      pp->beta -= 360.0;
+    pp->delta += speed_wz * pp->speed_scale;
+    if (pp->delta >= 360.0)
+      pp->delta -= 360.0;
+    pp->zeta += speed_xy * pp->speed_scale;
+    if (pp->zeta >= 360.0)
+      pp->zeta -= 360.0;
+    pp->eta += speed_xz * pp->speed_scale;
+    if (pp->eta >= 360.0)
+      pp->eta -= 360.0;
+    pp->theta += speed_yz * pp->speed_scale;
+    if (pp->theta >= 360.0)
+      pp->theta -= 360.0;
+  }
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (projection_3d == DISP_3D_ORTHOGRAPHIC)
   {
-    if (aspect >= 1.0)
-      glOrtho(-aspect,aspect,-1.0,1.0,0.1,100.0);
+    if (pp->aspect >= 1.0)
+      glOrtho(-pp->aspect,pp->aspect,-1.0,1.0,0.1,100.0);
     else
-      glOrtho(-1.0,1.0,-1.0/aspect,1.0/aspect,0.1,100.0);
+      glOrtho(-1.0,1.0,-1.0/pp->aspect,1.0/pp->aspect,0.1,100.0);
   }
   else
   {
-    gluPerspective(60.0,aspect,0.1,100.0);
+    gluPerspective(60.0,pp->aspect,0.1,100.0);
   }
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
 
+  if (pp->tick == 0)
   {
-    static int tick = 0;
-    static int p;
-
-    if (tick == 0)
-      {
-        if (polytope == POLYTOPE_RANDOM)
-          p = random() % (POLYTOPE_LAST_CELL+1);
-        else
-          p = polytope;
-      }
-    if (++tick > 1000) tick = 0;
-
-    if (p == POLYTOPE_5_CELL)
-      cell_5(mi);
-    else if (p == POLYTOPE_8_CELL)
-      cell_8(mi);
-    else if (p == POLYTOPE_16_CELL)
-      cell_16(mi);
-    else if (p == POLYTOPE_24_CELL)
-      cell_24(mi);
-    else if (p == POLYTOPE_120_CELL)
-      cell_120(mi);
-    else if (p == POLYTOPE_600_CELL)
-      cell_600(mi);
+    if (polytope == POLYTOPE_RANDOM)
+      pp->poly = random() % (POLYTOPE_LAST_CELL+1);
     else
-      abort();
+      pp->poly = polytope;
   }
+  if (++pp->tick > 1000)
+    pp->tick = 0;
+
+  if (pp->poly == POLYTOPE_5_CELL)
+    cell_5(mi);
+  else if (pp->poly == POLYTOPE_8_CELL)
+    cell_8(mi);
+  else if (pp->poly == POLYTOPE_16_CELL)
+    cell_16(mi);
+  else if (pp->poly == POLYTOPE_24_CELL)
+    cell_24(mi);
+  else if (pp->poly == POLYTOPE_120_CELL)
+    cell_120(mi);
+  else if (pp->poly == POLYTOPE_600_CELL)
+    cell_600(mi);
+  else
+    abort();
 }
 
 
-void reshape_polytopes(ModeInfo * mi, int width, int height)
+ENTRYPOINT void reshape_polytopes(ModeInfo *mi, int width, int height)
 {
-  polytopesstruct *hp = &poly[MI_SCREEN(mi)];
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
 
-  hp->WindW = (GLint)width;
-  hp->WindH = (GLint)height;
+  pp->WindW = (GLint)width;
+  pp->WindH = (GLint)height;
   glViewport(0,0,width,height);
-  aspect = (GLfloat)width/(GLfloat)height;
+  pp->aspect = (GLfloat)width/(GLfloat)height;
+}
+
+
+ENTRYPOINT Bool polytopes_handle_event(ModeInfo *mi, XEvent *event)
+{
+  Display *display = MI_DISPLAY(mi);
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
+  KeySym  sym;
+
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button == Button1)
+  {
+    pp->button_pressed = True;
+    gltrackball_start(pp->trackballs[pp->current_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)
+  {
+    pp->button_pressed = False;
+    return True;
+  }
+  else if (event->xany.type == KeyPress)
+  {
+    sym = XKeycodeToKeysym(display,event->xkey.keycode,0);
+    if (sym == XK_Shift_L || sym == XK_Shift_R)
+    {
+      pp->current_trackball = 1;
+      if (pp->button_pressed)
+        gltrackball_start(pp->trackballs[pp->current_trackball],
+                          event->xbutton.x, event->xbutton.y,
+                          MI_WIDTH(mi), MI_HEIGHT(mi));
+      return True;
+    }
+  }
+  else if (event->xany.type == KeyRelease)
+  {
+    sym = XKeycodeToKeysym(display,event->xkey.keycode,0);
+    if (sym == XK_Shift_L || sym == XK_Shift_R)
+    {
+      pp->current_trackball = 0;
+      if (pp->button_pressed)
+        gltrackball_start(pp->trackballs[pp->current_trackball],
+                          event->xbutton.x, event->xbutton.y,
+                          MI_WIDTH(mi), MI_HEIGHT(mi));
+      return True;
+    }
+  }
+  else if (event->xany.type == MotionNotify && pp->button_pressed)
+  {
+    gltrackball_track(pp->trackballs[pp->current_trackball],
+                      event->xmotion.x, event->xmotion.y,
+                      MI_WIDTH(mi), MI_HEIGHT(mi));
+    return True;
+  }
+
+  return False;
 }
 
 
@@ -2906,9 +3038,9 @@ void reshape_polytopes(ModeInfo * mi, int width, int height)
  *-----------------------------------------------------------------------------
  */
 
-void init_polytopes(ModeInfo * mi)
+ENTRYPOINT void init_polytopes(ModeInfo *mi)
 {
-  polytopesstruct *hp;
+  polytopesstruct *pp;
 
   if (poly == NULL)
   {
@@ -2917,9 +3049,17 @@ void init_polytopes(ModeInfo * mi)
     if (poly == NULL)
       return;
   }
-  hp = &poly[MI_SCREEN(mi)];
+  pp = &poly[MI_SCREEN(mi)];
+
+  pp->trackballs[0] = gltrackball_init();
+  pp->trackballs[1] = gltrackball_init();
+  pp->current_trackball = 0;
+  pp->button_pressed = False;
 
-  if ((hp->glx_context = init_GL(mi)) != NULL)
+  /* make multiple screens rotate at slightly different rates. */
+  pp->speed_scale = 0.9 + frand(0.3);
+
+  if ((pp->glx_context = init_GL(mi)) != NULL)
   {
     reshape_polytopes(mi,MI_WIDTH(mi),MI_HEIGHT(mi));
     glDrawBuffer(GL_BACK);
@@ -2936,7 +3076,7 @@ void init_polytopes(ModeInfo * mi)
  *    Called by the mainline code periodically to update the display.
  *-----------------------------------------------------------------------------
  */
-void draw_polytopes(ModeInfo * mi)
+ENTRYPOINT void draw_polytopes(ModeInfo *mi)
 {
   Display         *display = MI_DISPLAY(mi);
   Window          window = MI_WINDOW(mi);
@@ -2974,7 +3114,7 @@ void draw_polytopes(ModeInfo * mi)
  *-----------------------------------------------------------------------------
  */
 
-void release_polytopes(ModeInfo * mi)
+ENTRYPOINT void release_polytopes(ModeInfo *mi)
 {
   if (poly != NULL)
   {
@@ -2993,7 +3133,8 @@ void release_polytopes(ModeInfo * mi)
   FreeAllGL(mi);
 }
 
-void change_polytopes(ModeInfo * mi)
+#ifndef STANDALONE
+ENTRYPOINT void change_polytopes(ModeInfo *mi)
 {
   polytopesstruct *hp = &poly[MI_SCREEN(mi)];
 
@@ -3003,5 +3144,8 @@ void change_polytopes(ModeInfo * mi)
   glXMakeCurrent(MI_DISPLAY(mi),MI_WINDOW(mi),*(hp->glx_context));
   init(mi);
 }
+#endif /* !STANDALONE */
+
+XSCREENSAVER_MODULE ("Polytopes", polytopes)
 
 #endif /* USE_GL */