From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / polytopes.c
index f4043dcaa78cef7e110f8025cc092082bb790a99..0b4d68481d7c7718d2f496075540fa26a5cb3168 100644 (file)
@@ -1,10 +1,10 @@
 /* polytopes --- Shows one of the six regular polytopes rotating in 4d */
 
 #if 0
-static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
+static const char sccsid[] = "@(#)polytopes.c  1.2 05/09/28 xlockmore";
 #endif
 
-/* Copyright (c) 2003 Carsten Steger <carsten@mirsanmir.org>. */
+/* Copyright (c) 2003-2009 Carsten Steger <carsten@mirsanmir.org>. */
 
 /*
  * Permission to use, copy, modify, and distribute this software and its
@@ -21,6 +21,9 @@ 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
+ * C. Steger - 09/08/23: Removed check-config.pl warnings
  */
 
 /*
@@ -50,77 +53,48 @@ static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
 #define GOLDENINV22 0.27009075673772645360 /* (((sqrt(5)-1)/2)^2)/sqrt(2) */
 
 #define DISP_WIREFRAME            0
-#define DISP_WIREFRAME_STR       "0"
 #define DISP_SURFACE              1
-#define DISP_SURFACE_STR         "1"
 #define DISP_TRANSPARENT          2
-#define DISP_TRANSPARENT_STR     "2"
 
 #define POLYTOPE_RANDOM          -1
-#define POLYTOPE_RANDOM_STR      "-1"
 #define POLYTOPE_5_CELL           0
-#define POLYTOPE_5_CELL_STR      "0"
 #define POLYTOPE_8_CELL           1
-#define POLYTOPE_8_CELL_STR      "1"
 #define POLYTOPE_16_CELL          2
-#define POLYTOPE_16_CELL_STR     "2"
 #define POLYTOPE_24_CELL          3
-#define POLYTOPE_24_CELL_STR     "3"
 #define POLYTOPE_120_CELL         4
-#define POLYTOPE_120_CELL_STR    "4"
 #define POLYTOPE_600_CELL         5
-#define POLYTOPE_600_CELL_STR    "5"
 #define POLYTOPE_LAST_CELL        POLYTOPE_600_CELL
 
 #define COLORS_SINGLE             0
-#define COLORS_SINGLE_STR        "0"
 #define COLORS_DEPTH              1
-#define COLORS_DEPTH_STR         "1"
 
 #define DISP_3D_PERSPECTIVE       0
-#define DISP_3D_PERSPECTIVE_STR  "0"
 #define DISP_3D_ORTHOGRAPHIC      1
-#define DISP_3D_ORTHOGRAPHIC_STR "1"
 
 #define DISP_4D_PERSPECTIVE       0
 #define DISP_4D_PERSPECTIVE_STR  "0"
 #define DISP_4D_ORTHOGRAPHIC      1
 #define DISP_4D_ORTHOGRAPHIC_STR "1"
 
-#define DALPHA                    1.1
-#define DALPHA_STR               "1.1"
-#define DBETA                     1.3
-#define DBETA_STR                "1.3"
-#define DDELTA                    1.5
-#define DDELTA_STR               "1.5"
-#define DZETA                     1.7
-#define DZETA_STR                "1.7"
-#define DETA                      1.9
-#define DETA_STR                 "1.9"
-#define DTHETA                    2.1
-#define DTHETA_STR               "2.1"
-
-#define DEF_DISPLAY_MODE          DISP_TRANSPARENT_STR   
-#define DEF_POLYTOPE              POLYTOPE_RANDOM_STR
-#define DEF_COLORS                COLORS_DEPTH_STR
-#define DEF_3D_PROJECTION         DISP_3D_PERSPECTIVE_STR
-#define DEF_4D_PROJECTION         DISP_4D_PERSPECTIVE_STR
-#define DEF_DALPHA                DALPHA_STR
-#define DEF_DBETA                 DBETA_STR
-#define DEF_DDELTA                DDELTA_STR
-#define DEF_DZETA                 DZETA_STR
-#define DEF_DETA                  DETA_STR
-#define DEF_DTHETA                DTHETA_STR
+#define DEF_DISPLAY_MODE          "transparent"
+#define DEF_POLYTOPE              "random"
+#define DEF_COLORS                "depth"
+#define DEF_PROJECTION_3D         "perspective"
+#define DEF_PROJECTION_4D         "perspective"
+#define DEF_SPEEDWX               "1.1"
+#define DEF_SPEEDWY               "1.3"
+#define DEF_SPEEDWZ               "1.5"
+#define DEF_SPEEDXY               "1.7"
+#define DEF_SPEEDXZ               "1.9"
+#define DEF_SPEEDYZ               "2.1"
 
 #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"
+# define DEFAULTS           "*delay:      25000 \n" \
+                            "*showFPS:    False \n" \
+                           "*suppressRotationAnimation: True\n" \
 
+# define refresh_polytopes 0
+# define release_polytopes 0
 # include "xlockmore.h"         /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"             /* from the xlockmore distribution */
@@ -128,13 +102,15 @@ static const char sccsid[] = "@(#)polytopes.c  1.1 03/05/18 xlockmore";
 
 #ifdef USE_GL
 
-#include <GL/gl.h>
-#include <GL/glu.h>
+#ifndef HAVE_JWXYZ
+# include <X11/keysym.h>
+#endif
+#include "gltrackball.h"
 
 
 #ifdef USE_MODULES
 ModStruct   polytopes_description =
-{"polytopes", "init_polytopes", "draw_polytopes", "release_polytopes",
+{"polytopes", "init_polytopes", "draw_polytopes", NULL,
  "draw_polytopes", "change_polytopes", NULL, &polytopes_opts,
  25000, 1, 1, 1, 1.0, 4, "",
  "Shows one of the six regular 4d polytopes rotating in 4d", 0, NULL};
@@ -142,10 +118,15 @@ ModStruct   polytopes_description =
 #endif
 
 
+static char *mode;
 static int display_mode;
+static char *poly_str;
 static int polytope;
+static char *color_str;
 static int color_mode;
+static char *proj_3d;
 static int projection_3d;
+static char *proj_4d;
 static int projection_4d;
 static float speed_wx;
 static float speed_wy;
@@ -154,69 +135,51 @@ 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[] =
 {
-  {"-wireframe",       ".polytopes.displayMode",  XrmoptionNoArg,
-                       DISP_WIREFRAME_STR },
-  {"-surface",         ".polytopes.displayMode",  XrmoptionNoArg,
-                       DISP_SURFACE_STR },
-  {"-transparent",     ".polytopes.displayMode",  XrmoptionNoArg,
-                       DISP_TRANSPARENT_STR },
-  {"-random",          ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_RANDOM_STR },
-  {"-5-cell",          ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_5_CELL_STR },
-  {"-8-cell",          ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_8_CELL_STR },
-  {"-16-cell",         ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_16_CELL_STR },
-  {"-24-cell",         ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_24_CELL_STR },
-  {"-120-cell",        ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_120_CELL_STR },
-  {"-600-cell",        ".polytopes.polytope",     XrmoptionNoArg,
-                       POLYTOPE_600_CELL_STR },
-  {"-single-color",    ".polytopes.colors",       XrmoptionNoArg,
-                       COLORS_SINGLE_STR },
-  {"-depth-colors",    ".polytopes.colors",       XrmoptionNoArg,
-                       COLORS_DEPTH_STR },
-  {"-perspective-3d",  ".polytopes.projection3d", XrmoptionNoArg,
-                       DISP_3D_PERSPECTIVE_STR },
-  {"-orthographic-3d", ".polytopes.projection3d", XrmoptionNoArg,
-                       DISP_3D_ORTHOGRAPHIC_STR },
-  {"-perspective-4d",  ".polytopes.projection4d", XrmoptionNoArg,
-                       DISP_4D_PERSPECTIVE_STR },
-  {"-orthographic-4d", ".polytopes.projection4d", XrmoptionNoArg,
-                       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 }
+  {"-mode",            ".displayMode",  XrmoptionSepArg, 0 },
+  {"-wireframe",       ".displayMode",  XrmoptionNoArg,  "wireframe" },
+  {"-surface",         ".displayMode",  XrmoptionNoArg,  "surface" },
+  {"-transparent",     ".displayMode",  XrmoptionNoArg,  "transparent" },
+  {"-polytope",        ".polytope",     XrmoptionSepArg, 0 },
+  {"-random",          ".polytope",     XrmoptionNoArg,  "random" },
+  {"-5-cell",          ".polytope",     XrmoptionNoArg,  "5-cell" },
+  {"-8-cell",          ".polytope",     XrmoptionNoArg,  "8-cell" },
+  {"-16-cell",         ".polytope",     XrmoptionNoArg,  "16-cell" },
+  {"-24-cell",         ".polytope",     XrmoptionNoArg,  "24-cell" },
+  {"-120-cell",        ".polytope",     XrmoptionNoArg,  "120-cell" },
+  {"-600-cell",        ".polytope",     XrmoptionNoArg,  "600-cell" },
+  {"-single-color",    ".colors",       XrmoptionNoArg,  "single" },
+  {"-depth-colors",    ".colors",       XrmoptionNoArg,  "depth" },
+  {"-perspective-3d",  ".projection3d", XrmoptionNoArg,  "perspective" },
+  {"-orthographic-3d", ".projection3d", XrmoptionNoArg,  "orthographic" },
+  {"-perspective-4d",  ".projection4d", XrmoptionNoArg,  "perspective" },
+  {"-orthographic-4d", ".projection4d", XrmoptionNoArg,  "orthographic" },
+  {"-speed-wx",        ".speedwx",      XrmoptionSepArg, 0 },
+  {"-speed-wy",        ".speedwy",      XrmoptionSepArg, 0 },
+  {"-speed-wz",        ".speedwz",      XrmoptionSepArg, 0 },
+  {"-speed-xy",        ".speedxy",      XrmoptionSepArg, 0 },
+  {"-speed-xz",        ".speedxz",      XrmoptionSepArg, 0 },
+  {"-speed-yz",        ".speedyz",      XrmoptionSepArg, 0 }
 };
 
 static argtype vars[] =
 {
-  { &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}
+  { &mode,      "displayMode",  "DisplayMode", DEF_DISPLAY_MODE,  t_String },
+  { &poly_str,  "polytope",     "Polytope",    DEF_POLYTOPE,      t_String },
+  { &color_str, "colors",       "Colors",      DEF_COLORS,        t_String },
+  { &proj_3d,   "projection3d", "Projection3d",DEF_PROJECTION_3D, t_String },
+  { &proj_4d,   "projection4d", "Projection4d",DEF_PROJECTION_4D, t_String },
+  { &speed_wx,  "speedwx",      "Speedwx",     DEF_SPEEDWX,       t_Float},
+  { &speed_wy,  "speedwy",      "Speedwy",     DEF_SPEEDWY,       t_Float},
+  { &speed_wz,  "speedwz",      "Speedwz",     DEF_SPEEDWZ,       t_Float},
+  { &speed_xy,  "speedxy",      "Speedxy",     DEF_SPEEDXY,       t_Float},
+  { &speed_xz,  "speedxz",      "Speedxz",     DEF_SPEEDXZ,       t_Float},
+  { &speed_yz,  "speedyz",      "Speedyz",     DEF_SPEEDYZ,       t_Float}
 };
 
 static OptionStruct desc[] =
@@ -240,13 +203,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;
@@ -256,18 +327,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 },
@@ -275,33 +335,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 },
@@ -312,7 +357,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 },
@@ -321,7 +366,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 },
@@ -332,37 +377,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 },
@@ -371,23 +402,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 },
@@ -402,7 +419,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 },
@@ -421,7 +438,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 },
@@ -448,23 +465,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 },
@@ -1067,7 +1071,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 },
@@ -1310,7 +1314,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 },
@@ -1673,23 +1677,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 },
@@ -1812,7 +1802,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 },
@@ -1959,7 +1949,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 },
@@ -2262,9 +2252,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. */
@@ -2382,24 +2369,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;
 
@@ -2420,12 +2456,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);
+
+  gltrackball_get_quaternion(pp->trackballs[0],q1);
+  gltrackball_get_quaternion(pp->trackballs[1],q2);
+  quats_to_rotmat(q1,q2,r2);
 
-  rotateall(m);
+  mult_rotmat(r2,r1,m);
 
   /* Project the vertices from 4d to 3d. */
   for (i=0; i<num; i++)
@@ -2462,14 +2505,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)
@@ -2524,68 +2571,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);
 }
 
 
@@ -2641,10 +2698,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;
@@ -2668,198 +2730,204 @@ 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 };
+  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;
 
-  glMatrixMode(GL_PROJECTION);
-  glLoadIdentity();
-  if (projection_3d == DISP_3D_PERSPECTIVE)
-    gluPerspective(60.0,1.0,0.1,100.0);
-  else
-    glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
-  glMatrixMode(GL_MODELVIEW);
-  glLoadIdentity();
-
-  if (display_mode == DISP_WIREFRAME)
-  {
-    glDisable(GL_DEPTH_TEST);
-    glDisable(GL_LIGHTING);
-    glDisable(GL_LIGHT0);
-    glDisable(GL_BLEND);
-  }
-  else if (display_mode == DISP_SURFACE)
-  {
-    glEnable(GL_DEPTH_TEST);
-    glDepthFunc(GL_LESS);
-    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
-    glEnable(GL_LIGHTING);
-    glEnable(GL_LIGHT0);
-    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
-    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
-    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
-    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
-    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
-    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
-    glDepthMask(GL_TRUE);
-    glDisable(GL_BLEND);
-  }
-  else if (display_mode == DISP_TRANSPARENT)
-  {
-    glDisable(GL_DEPTH_TEST);
-    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
-    glEnable(GL_LIGHTING);
-    glEnable(GL_LIGHT0);
-    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
-    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
-    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
-    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
-    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
-    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
-    glDepthMask(GL_FALSE);
-    glEnable(GL_BLEND);
-    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
-  }
-  else
-  {
-    glDisable(GL_DEPTH_TEST);
-    glDisable(GL_LIGHTING);
-    glDisable(GL_LIGHT0);
-    glDisable(GL_BLEND);
-  }
+  pp->tick = 0;
+  pp->poly = 0;
 }
 
 
 /* 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)
+{
+  polytopesstruct *pp = &poly[MI_SCREEN(mi)];
+  KeySym  sym = 0;
+  char c = 0;
+
+  if (event->xany.type == KeyPress || event->xany.type == KeyRelease)
+    XLookupString (&event->xkey, &c, 1, &sym, 0);
+
+  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)
+  {
+    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)
+  {
+    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;
 }
 
 
@@ -2877,20 +2945,116 @@ 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)
+  MI_INIT(mi, poly, NULL);
+  pp = &poly[MI_SCREEN(mi)];
+
+  pp->trackballs[0] = gltrackball_init(True);
+  pp->trackballs[1] = gltrackball_init(True);
+  pp->current_trackball = 0;
+  pp->button_pressed = False;
+
+  /* Set the display mode. */
+  if (!strcasecmp(mode,"wireframe") || !strcasecmp(mode,"0"))
   {
-    poly = (polytopesstruct *)calloc(MI_NUM_SCREENS(mi),
-                                     sizeof(polytopesstruct));
-    if (poly == NULL)
-      return;
+    display_mode = DISP_WIREFRAME;
+  }
+  else if (!strcasecmp(mode,"surface") || !strcasecmp(mode,"1"))
+  {
+    display_mode = DISP_SURFACE;
+  }
+  else if (!strcasecmp(mode,"transparent") || !strcasecmp(mode,"2"))
+  {
+    display_mode = DISP_TRANSPARENT;
+  }
+  else
+  {
+    display_mode = DISP_TRANSPARENT;
+  }
+
+  /* Set the Klein bottle. */
+  if (!strcasecmp(poly_str,"random") || !strcasecmp(poly_str,"-1"))
+  {
+    polytope = POLYTOPE_RANDOM;
+  }
+  else if (!strcasecmp(poly_str,"5-cell") || !strcasecmp(poly_str,"0"))
+  {
+    polytope = POLYTOPE_5_CELL;
+  }
+  else if (!strcasecmp(poly_str,"8-cell") || !strcasecmp(poly_str,"1"))
+  {
+    polytope = POLYTOPE_8_CELL;
+  }
+  else if (!strcasecmp(poly_str,"16-cell") || !strcasecmp(poly_str,"2"))
+  {
+    polytope = POLYTOPE_16_CELL;
+  }
+  else if (!strcasecmp(poly_str,"24-cell") || !strcasecmp(poly_str,"3"))
+  {
+    polytope = POLYTOPE_24_CELL;
+  }
+  else if (!strcasecmp(poly_str,"120-cell") || !strcasecmp(poly_str,"4"))
+  {
+    polytope = POLYTOPE_120_CELL;
+  }
+  else if (!strcasecmp(poly_str,"600-cell") || !strcasecmp(poly_str,"5"))
+  {
+    polytope = POLYTOPE_600_CELL;
+  }
+  else
+  {
+    polytope = POLYTOPE_RANDOM;
   }
-  hp = &poly[MI_SCREEN(mi)];
 
-  if ((hp->glx_context = init_GL(mi)) != NULL)
+  /* Set the color mode. */
+  if (!strcasecmp(color_str,"single") || !strcasecmp(color_str,"0"))
+  {
+    color_mode = COLORS_SINGLE;
+  }
+  else if (!strcasecmp(color_str,"depth") || !strcasecmp(color_str,"1"))
+  {
+    color_mode = COLORS_DEPTH;
+  }
+  else
+  {
+    color_mode = COLORS_DEPTH;
+  }
+
+  /* Set the 3d projection mode. */
+  if (!strcasecmp(proj_3d,"perspective") || !strcasecmp(proj_3d,"0"))
+  {
+    projection_3d = DISP_3D_PERSPECTIVE;
+  }
+  else if (!strcasecmp(proj_3d,"orthographic") || !strcasecmp(proj_3d,"1"))
+  {
+    projection_3d = DISP_3D_ORTHOGRAPHIC;
+  }
+  else
+  {
+    projection_3d = DISP_3D_PERSPECTIVE;
+  }
+
+  /* Set the 4d projection mode. */
+  if (!strcasecmp(proj_4d,"perspective") || !strcasecmp(proj_4d,"0"))
+  {
+    projection_4d = DISP_4D_PERSPECTIVE;
+  }
+  else if (!strcasecmp(proj_4d,"orthographic") || !strcasecmp(proj_4d,"1"))
+  {
+    projection_4d = DISP_4D_ORTHOGRAPHIC;
+  }
+  else
+  {
+    projection_4d = DISP_4D_PERSPECTIVE;
+  }
+
+  /* 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);
@@ -2907,8 +3071,14 @@ 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)
 {
+  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 };
+
   Display         *display = MI_DISPLAY(mi);
   Window          window = MI_WINDOW(mi);
   polytopesstruct *hp;
@@ -2923,6 +3093,64 @@ void draw_polytopes(ModeInfo * mi)
 
   glXMakeCurrent(display,window,*(hp->glx_context));
 
+
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  if (projection_3d == DISP_3D_PERSPECTIVE)
+    gluPerspective(60.0,1.0,0.1,100.0);
+  else
+    glOrtho(-1.0,1.0,-1.0,1.0,0.1,100.0);;
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
+
+  if (display_mode == DISP_WIREFRAME)
+  {
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_LIGHTING);
+    glDisable(GL_LIGHT0);
+    glDisable(GL_BLEND);
+  }
+  else if (display_mode == DISP_SURFACE)
+  {
+    glEnable(GL_DEPTH_TEST);
+    glDepthFunc(GL_LESS);
+    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
+    glEnable(GL_LIGHTING);
+    glEnable(GL_LIGHT0);
+    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
+    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
+    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
+    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
+    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
+    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
+    glDepthMask(GL_TRUE);
+    glDisable(GL_BLEND);
+  }
+  else if (display_mode == DISP_TRANSPARENT)
+  {
+    glDisable(GL_DEPTH_TEST);
+    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
+    glEnable(GL_LIGHTING);
+    glEnable(GL_LIGHT0);
+    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
+    glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
+    glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
+    glLightfv(GL_LIGHT0,GL_POSITION,light_position);
+    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
+    glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,0.0);
+    glDepthMask(GL_FALSE);
+    glEnable(GL_BLEND);
+    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
+  }
+  else
+  {
+    glDisable(GL_DEPTH_TEST);
+    glDisable(GL_LIGHTING);
+    glDisable(GL_LIGHT0);
+    glDisable(GL_BLEND);
+  }
+
+
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
   glLoadIdentity();
 
@@ -2937,34 +3165,8 @@ void draw_polytopes(ModeInfo * mi)
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *    The display is being taken away from us.  Free up malloc'ed 
- *      memory and X resources that we've alloc'ed.  Only called
- *      once, we must zap everything for every screen.
- *-----------------------------------------------------------------------------
- */
-
-void release_polytopes(ModeInfo * mi)
-{
-  if (poly != NULL)
-  {
-    int screen;
-
-    for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++)
-    {
-      polytopesstruct *hp = &poly[screen];
-
-      if (hp->glx_context)
-        hp->glx_context = (GLXContext *)NULL;
-    }
-    (void) free((void *)poly);
-    poly = (polytopesstruct *)NULL;
-  }
-  FreeAllGL(mi);
-}
-
-void change_polytopes(ModeInfo * mi)
+#ifndef STANDALONE
+ENTRYPOINT void change_polytopes(ModeInfo *mi)
 {
   polytopesstruct *hp = &poly[MI_SCREEN(mi)];
 
@@ -2974,5 +3176,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 */