http://www.tienza.es/crux/src/www.jwz.org/xscreensaver/xscreensaver-5.05.tar.gz
[xscreensaver] / hacks / glx / sierpinski3d.c
index 3a753a998a45121bf9ed26bcf91d39e659bb4f82..41a8fd2ea92f02bbfe0a4cbad5de6dd27050f6be 100644 (file)
@@ -1,9 +1,8 @@
 /* -*- Mode: C; tab-width: 4 -*- */
 /* Sierpinski3D --- 3D sierpinski gasket */
 
-#if !defined( lint ) && !defined( SABER )
+#if 0
 static const char sccsid[] = "@(#)sierpinski3D.c       00.01 99/11/04 xlockmore";
-
 #endif
 
 /*-
@@ -27,24 +26,12 @@ static const char sccsid[] = "@(#)sierpinski3D.c    00.01 99/11/04 xlockmore";
  *                  random scattering of points.
  */
 
-/*-
- * due to a Bug/feature in VMS X11/Intrinsic.h has to be placed before xlock.
- * otherwise caddr_t is not defined correctly
- */
-
-#include <X11/Intrinsic.h>
-
 #ifdef STANDALONE
-# define PROGCLASS                                     "Sierpinski3D"
-# define HACK_INIT                                     init_gasket
-# define HACK_DRAW                                     draw_gasket
-# define gasket_opts                           xlockmore_opts
-# define DEFAULTS      "*count:                1       \n"                     \
-                       "*cycles:               9999    \n"                     \
-                       "*delay:                20000   \n"                     \
-                       "*maxDepth:             5       \n"                     \
-                       "*speed:                150     \n"                     \
-                       "*wireframe:    False   \n"
+# define DEFAULTS                                      "*delay:                20000   \n"                     \
+                                                                       "*showFPS:      False   \n"                     \
+                                                                       "*wireframe:    False   \n"                     \
+
+# define refresh_gasket 0
 # include "xlockmore.h"                /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"                    /* from the xlockmore distribution */
@@ -52,23 +39,40 @@ static const char sccsid[] = "@(#)sierpinski3D.c    00.01 99/11/04 xlockmore";
 
 #ifdef USE_GL
 
+#define DEF_SPIN                               "True"
+#define DEF_WANDER                             "True"
+#define DEF_SPEED                              "150"
+#define DEF_MAXDEPTH                   "5"
+
+#include "rotator.h"
+#include "gltrackball.h"
+
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
 static int max_depth;
 static int speed;
+static Bool do_spin;
+static Bool do_wander;
+
 static XrmOptionDescRec opts[] = {
-  {"-depth", ".sierpinski3d.maxDepth", XrmoptionSepArg, (caddr_t) 0 },
-  {"-speed", ".sierpinski3d.speed",    XrmoptionSepArg, (caddr_t) 0 }
+  {"-depth", ".sierpinski3d.maxDepth", XrmoptionSepArg, 0 },
+  {"-speed", ".sierpinski3d.speed",    XrmoptionSepArg, 0 },
+  { "-spin",   ".spin",   XrmoptionNoArg, "True" },
+  { "+spin",   ".spin",   XrmoptionNoArg, "False" },
+  { "-wander", ".wander", XrmoptionNoArg, "True" },
+  { "+wander", ".wander", XrmoptionNoArg, "False" },
 };
 
 static argtype vars[] = {
-  {(caddr_t *) &max_depth, "maxDepth", "MaxDepth", "5", t_Int},
-  {(caddr_t *) &speed,     "speed",    "Speed",   "150", t_Int},
+  {&do_spin,   "spin",     "Spin",     DEF_SPIN,     t_Bool},
+  {&do_wander, "wander",   "Wander",   DEF_WANDER,   t_Bool},
+  {&speed,     "speed",    "Speed",    DEF_SPEED,    t_Int},
+  {&max_depth, "maxDepth", "MaxDepth", DEF_MAXDEPTH, t_Int},
 };
 
 
-ModeSpecOpt gasket_opts = {countof(opts), opts, countof(vars), vars, NULL};
+ENTRYPOINT ModeSpecOpt gasket_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 #ifdef USE_MODULES
 ModStruct   gasket_description =
@@ -86,112 +90,30 @@ typedef struct{
 } GL_VECTOR;
 
 typedef struct {
-  GLfloat     view_rotx, view_roty, view_rotz;
-  GLfloat     light_colour[4];/* = {6.0, 6.0, 6.0, 1.0}; */
-  GLfloat     pos[3];/* = {0.0, 0.0, 0.0}; */
-  GLfloat     xinc,yinc,zinc;
-  GLfloat     angle;
-  GLuint      gasket1;
+  GLuint      gasket0, gasket1, gasket2, gasket3;
   GLXContext *glx_context;
   Window      window;
+  rotator    *rot;
+  trackball_state *trackball;
+  Bool           button_down_p;
 
   int current_depth;
 
-} gasketstruct;
-
-static gasketstruct *gasket = NULL;
-
-#include <GL/glu.h>
-
-/* static GLuint limit; */
-
-\f
-/* Computing normal vectors (thanks to Nat Friedman <ndf@mit.edu>)
- */
-
-typedef struct vector {
-  GLfloat x, y, z;
-} vector;
-
-typedef struct plane {
-  vector p1, p2, p3;
-} plane;
+  int ncolors;
+  XColor *colors;
+  int ccolor0;
+  int ccolor1;
+  int ccolor2;
+  int ccolor3;
 
-static void
-vector_set(vector *v, GLfloat x, GLfloat y, GLfloat z)
-{
-  v->x = x;
-  v->y = y;
-  v->z = z;
-}
-
-static void
-vector_cross(vector v1, vector v2, vector *v3)
-{
-  v3->x = (v1.y * v2.z) - (v1.z * v2.y);
-  v3->y = (v1.z * v2.x) - (v1.x * v2.z);
-  v3->z = (v1.x * v2.y) - (v1.y * v2.x);
-}
+  int tick;
 
-static void
-vector_subtract(vector v1, vector v2, vector *res)
-{
-  res->x = v1.x - v2.x;
-  res->y = v1.y - v2.y;
-  res->z = v1.z - v2.z;
-}
+  GLfloat normals[4][3];
 
-static void
-plane_normal(plane p, vector *n)
-{
-  vector v1, v2;
-  vector_subtract(p.p1, p.p2, &v1);
-  vector_subtract(p.p1, p.p3, &v2);
-  vector_cross(v2, v1, n);
-}
+} gasketstruct;
 
-static void
-do_normal(GLfloat x1, GLfloat y1, GLfloat z1,
-         GLfloat x2, GLfloat y2, GLfloat z2,
-         GLfloat x3, GLfloat y3, GLfloat z3)
-{
-  plane plane;
-  vector n;
-  vector_set(&plane.p1, x1, y1, z1);
-  vector_set(&plane.p2, x2, y2, z2);
-  vector_set(&plane.p3, x3, y3, z3);
-  plane_normal(plane, &n);
-  n.x = -n.x; n.y = -n.y; n.z = -n.z;
-
-  glNormal3f(n.x, n.y, n.z);
-
-#ifdef DEBUG
-  /* Draw a line in the direction of this face's normal. */
-  {
-    GLfloat ax = n.x > 0 ? n.x : -n.x;
-    GLfloat ay = n.y > 0 ? n.y : -n.y;
-    GLfloat az = n.z > 0 ? n.z : -n.z;
-    GLfloat mx = (x1 + x2 + x3) / 3;
-    GLfloat my = (y1 + y2 + y3) / 3;
-    GLfloat mz = (z1 + z2 + z3) / 3;
-    GLfloat xx, yy, zz;
-
-    GLfloat max = ax > ay ? ax : ay;
-    if (az > max) max = az;
-    max *= 2;
-    xx = n.x / max;
-    yy = n.y / max;
-    zz = n.z / max;
-
-    glBegin(GL_LINE_LOOP);
-    glVertex3f(mx, my, mz);
-    glVertex3f(mx+xx, my+yy, mz+zz);
-    glEnd();
-  }
-#endif /* DEBUG */
-}
+static gasketstruct *gasket = NULL;
 
-\f
 
 static void
 triangle (GLfloat x1, GLfloat y1, GLfloat z1,
@@ -202,10 +124,7 @@ triangle (GLfloat x1, GLfloat y1, GLfloat z1,
   if (wireframe_p)
     glBegin (GL_LINE_LOOP);
   else
-    {
-      do_normal (x1, y1, z1,  x2, y2, z2,  x3, y3, z3);
-      glBegin (GL_TRIANGLES);
-    }
+    glBegin (GL_TRIANGLES);
   glVertex3f (x1, y1, z1);
   glVertex3f (x2, y2, z2);
   glVertex3f (x3, y3, z3);
@@ -213,26 +132,45 @@ triangle (GLfloat x1, GLfloat y1, GLfloat z1,
 }
 
 static void
-four_tetras (GL_VECTOR *outer, Bool wireframe_p, int countdown)
+four_tetras (gasketstruct *gp, 
+             GL_VECTOR *outer, Bool wireframe_p, int countdown, int which,
+             int *countP)
 {
   if (countdown <= 0)
     {
-      triangle (outer[0].x, outer[0].y, outer[0].z,
-                outer[1].x, outer[1].y, outer[1].z,
-                outer[2].x, outer[2].y, outer[2].z,
-                wireframe_p);
-      triangle (outer[0].x, outer[0].y, outer[0].z,
-                outer[3].x, outer[3].y, outer[3].z,
-                outer[1].x, outer[1].y, outer[1].z,
-                wireframe_p);
-      triangle (outer[0].x, outer[0].y, outer[0].z,
-                outer[2].x, outer[2].y, outer[2].z,
-                outer[3].x, outer[3].y, outer[3].z,
-                wireframe_p);
-      triangle (outer[1].x, outer[1].y, outer[1].z,
-                outer[3].x, outer[3].y, outer[3].z,
-                outer[2].x, outer[2].y, outer[2].z,
-                wireframe_p);
+      (*countP)++;
+      if (which == 0)
+        {
+          glNormal3f (gp->normals[0][0], gp->normals[0][1], gp->normals[0][2]);
+          triangle (outer[0].x, outer[0].y, outer[0].z,
+                    outer[1].x, outer[1].y, outer[1].z,
+                    outer[2].x, outer[2].y, outer[2].z,
+                    wireframe_p);
+        }
+      else if (which == 1)
+        {
+          glNormal3f (gp->normals[1][0], gp->normals[1][1], gp->normals[1][2]);
+          triangle (outer[0].x, outer[0].y, outer[0].z,
+                    outer[3].x, outer[3].y, outer[3].z,
+                    outer[1].x, outer[1].y, outer[1].z,
+                    wireframe_p);
+        }
+      else if (which == 2)
+        {
+          glNormal3f (gp->normals[2][0], gp->normals[2][1], gp->normals[2][2]);
+          triangle (outer[0].x, outer[0].y, outer[0].z,
+                    outer[2].x, outer[2].y, outer[2].z,
+                    outer[3].x, outer[3].y, outer[3].z,
+                    wireframe_p);
+        }
+      else
+        {
+          glNormal3f (gp->normals[3][0], gp->normals[3][1], gp->normals[3][2]);
+          triangle (outer[1].x, outer[1].y, outer[1].z,
+                    outer[3].x, outer[3].y, outer[3].z,
+                    outer[2].x, outer[2].y, outer[2].z,
+                    wireframe_p);
+        }
     }
   else
     {
@@ -275,37 +213,55 @@ four_tetras (GL_VECTOR *outer, Bool wireframe_p, int countdown)
       corner[1] = inner[M01];
       corner[2] = inner[M02];
       corner[3] = inner[M03];
-      four_tetras (corner, wireframe_p, countdown);
+      four_tetras (gp, corner, wireframe_p, countdown, which, countP);
 
       corner[0] = inner[M01];
       corner[1] = outer[1];
       corner[2] = inner[M12];
       corner[3] = inner[M13];
-      four_tetras (corner, wireframe_p, countdown);
+      four_tetras (gp, corner, wireframe_p, countdown, which, countP);
 
       corner[0] = inner[M02];
       corner[1] = inner[M12];
       corner[2] = outer[2];
       corner[3] = inner[M23];
-      four_tetras (corner, wireframe_p, countdown);
+      four_tetras (gp, corner, wireframe_p, countdown, which, countP);
 
       corner[0] = inner[M03];
       corner[1] = inner[M13];
       corner[2] = inner[M23];
       corner[3] = outer[3];
-      four_tetras (corner, wireframe_p, countdown);
+      four_tetras (gp, corner, wireframe_p, countdown, which, countP);
     }
 }
 
 
 static void
-compile_gasket(ModeInfo *mi)
+compile_gasket(ModeInfo *mi, int which)
 {
   Bool wireframe_p = MI_IS_WIREFRAME(mi);
   gasketstruct *gp = &gasket[MI_SCREEN(mi)];
+  int count;
 
   GL_VECTOR   vertex[5];
 
+  gp->normals[0][0] =  0;
+  gp->normals[0][1] =  0;
+  gp->normals[0][2] = -sqrt(2.0 / 3.0);
+
+  gp->normals[1][0] =  0;
+  gp->normals[1][1] = -sqrt(0.75);
+  gp->normals[1][2] =  sqrt(2.0 / 3.0) / 3.0;
+
+  gp->normals[2][0] =  sqrt (0.5);
+  gp->normals[2][1] =  sqrt(0.75) / 2.0;
+  gp->normals[2][2] =  gp->normals[1][2];
+
+  gp->normals[3][0] = -gp->normals[2][0];
+  gp->normals[3][1] =  gp->normals[2][1];
+  gp->normals[3][2] =  gp->normals[1][2];
+
+
   /* define verticies */
   vertex[0].x =  0.5; 
   vertex[0].y = -(1.0/3.0)*sqrt((2.0/3.0));
@@ -327,93 +283,147 @@ compile_gasket(ModeInfo *mi)
   vertex[4].y = 0.0; 
   vertex[4].z = 0.0;
   
-  four_tetras (vertex, wireframe_p,
+  count = 0;
+  four_tetras (gp, vertex, wireframe_p,
                (gp->current_depth < 0
-                ? -gp->current_depth : gp->current_depth));
+                ? -gp->current_depth : gp->current_depth),
+               which,
+               &count);
+  mi->polygon_count = count;
 }
 
 static void
 draw(ModeInfo *mi)
 {
+  Bool wireframe_p = MI_IS_WIREFRAME(mi);
   gasketstruct *gp = &gasket[MI_SCREEN(mi)];
-  static int tick = 0;
   
-  static float position0[] = {-0.5,  1.2, 0.5, 0.0};
-  static float ambient0[]  = {0.4, 0.6, 0.4, 1.0};
-  static float spec[]      = {0.7, 0.7, 0.7, 1.0};
+  static const GLfloat pos[]    = {-4.0, 3.0, 10.0, 1.0};
+  static const GLfloat white[]  = {1.0, 1.0, 1.0, 1.0};
 
-  glLightfv(GL_LIGHT0, GL_POSITION,  position0);
-  glLightfv(GL_LIGHT0, GL_AMBIENT,   ambient0);
-  glLightfv(GL_LIGHT0, GL_SPECULAR,  spec);
-  glLightfv(GL_LIGHT0, GL_DIFFUSE,   gp->light_colour);
+  GLfloat color0[] = {0.0, 0.0, 0.0, 1.0};
+  GLfloat color1[] = {0.0, 0.0, 0.0, 1.0};
+  GLfloat color2[] = {0.0, 0.0, 0.0, 1.0};
+  GLfloat color3[] = {0.0, 0.0, 0.0, 1.0};
 
-  glShadeModel(GL_SMOOTH);
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-  glEnable(GL_LIGHTING);
-  glEnable(GL_LIGHT0);
+  if (!wireframe_p)
+    {
+      glColor4fv (white);
+
+      glLightfv(GL_LIGHT0, GL_POSITION,  pos);
+
+      color0[0] = gp->colors[gp->ccolor0].red   / 65536.0;
+      color0[1] = gp->colors[gp->ccolor0].green / 65536.0;
+      color0[2] = gp->colors[gp->ccolor0].blue  / 65536.0;
+
+      color1[0] = gp->colors[gp->ccolor1].red   / 65536.0;
+      color1[1] = gp->colors[gp->ccolor1].green / 65536.0;
+      color1[2] = gp->colors[gp->ccolor1].blue  / 65536.0;
+
+      color2[0] = gp->colors[gp->ccolor2].red   / 65536.0;
+      color2[1] = gp->colors[gp->ccolor2].green / 65536.0;
+      color2[2] = gp->colors[gp->ccolor2].blue  / 65536.0;
+
+      color3[0] = gp->colors[gp->ccolor3].red   / 65536.0;
+      color3[1] = gp->colors[gp->ccolor3].green / 65536.0;
+      color3[2] = gp->colors[gp->ccolor3].blue  / 65536.0;
+
+      gp->ccolor0++;
+      gp->ccolor1++;
+      gp->ccolor2++;
+      gp->ccolor3++;
+      if (gp->ccolor0 >= gp->ncolors) gp->ccolor0 = 0;
+      if (gp->ccolor1 >= gp->ncolors) gp->ccolor1 = 0;
+      if (gp->ccolor2 >= gp->ncolors) gp->ccolor2 = 0;
+      if (gp->ccolor3 >= gp->ncolors) gp->ccolor3 = 0;
+
+      glShadeModel(GL_SMOOTH);
+
+      glEnable(GL_LIGHTING);
+      glEnable(GL_LIGHT0);
+    }
 
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_NORMALIZE);
   glEnable(GL_CULL_FACE);
 
-  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
   glPushMatrix();
-  glTranslatef( gp->pos[0], gp->pos[1], gp->pos[2] );  
 
-  glPushMatrix();
-  glRotatef(2*gp->angle, 1.0, 0.0, 0.0);
-  glRotatef(3*gp->angle, 0.0, 1.0, 0.0);
-  glRotatef(  gp->angle, 0.0, 0.0, 1.0);
+  {
+    double x, y, z;
+    get_position (gp->rot, &x, &y, &z, !gp->button_down_p);
+    glTranslatef((x - 0.5) * 10,
+                 (y - 0.5) * 10,
+                 (z - 0.5) * 20);
+
+    gltrackball_rotate (gp->trackball);
+
+    get_rotation (gp->rot, &x, &y, &z, !gp->button_down_p);
+    glRotatef (x * 360, 1.0, 0.0, 0.0);
+    glRotatef (y * 360, 0.0, 1.0, 0.0);
+    glRotatef (z * 360, 0.0, 0.0, 1.0);
+  }
+
   glScalef( 8.0, 8.0, 8.0 );
+
+  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color0);
+  glCallList(gp->gasket0);
+  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color1);
   glCallList(gp->gasket1);
-  
-  glPopMatrix();
+  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color2);
+  glCallList(gp->gasket2);
+  glMaterialfv (GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color3);
+  glCallList(gp->gasket3);
 
   glPopMatrix();
 
 
-  if (tick++ >= speed)
+  if (gp->tick++ >= speed)
     {
-      tick = 0;
+      gp->tick = 0;
       if (gp->current_depth >= max_depth)
         gp->current_depth = -max_depth;
       gp->current_depth++;
 
+      /* We make four different lists so that each face of the tetrahedrons
+         can have a different color (all triangles facing in the same
+         direction have the same color, which is different from all
+         triangles facing in other directions.)
+       */
+      glDeleteLists (gp->gasket0, 1);
       glDeleteLists (gp->gasket1, 1);
-      glNewList (gp->gasket1, GL_COMPILE);
-      compile_gasket (mi);
-      glEndList();
-
-      /* do the colour change */
-      gp->light_colour[0] = 3.0*SINF(gp->angle/20.0) + 4.0;
-      gp->light_colour[1] = 3.0*SINF(gp->angle/30.0) + 4.0;
-      gp->light_colour[2] = 3.0*SINF(gp->angle/60.0) + 4.0;
+      glDeleteLists (gp->gasket2, 1);
+      glDeleteLists (gp->gasket3, 1);
+
+      mi->polygon_count = 0;
+      glNewList (gp->gasket0, GL_COMPILE); compile_gasket (mi, 0); glEndList();
+      glNewList (gp->gasket1, GL_COMPILE); compile_gasket (mi, 1); glEndList();
+      glNewList (gp->gasket2, GL_COMPILE); compile_gasket (mi, 2); glEndList();
+      glNewList (gp->gasket3, GL_COMPILE); compile_gasket (mi, 3); glEndList();
+
     }
 }
 
 
 /* new window size or exposure */
-static void
-reshape(int width, int height)
+ENTRYPOINT void
+reshape_gasket(ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
 
   glViewport(0, 0, (GLint) width, (GLint) height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
+  gluPerspective (30.0, 1/h, 1.0, 100.0);
 
-  gluPerspective( 30.0, 1/h, 1.0, 100.0 );
-  gluLookAt( 0.0, 0.0, 15.0,
-             0.0, 0.0, 0.0,
-             0.0, 1.0, 0.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
-  glTranslatef(0.0, 0.0, -15.0);
+  gluLookAt( 0.0, 0.0, 30.0,
+             0.0, 0.0, 0.0,
+             0.0, 1.0, 0.0);
   
-  /* The depth buffer will be cleared, if needed, before the
-  * next frame.  Right now we just want to black the screen.
-  */
   glClear(GL_COLOR_BUFFER_BIT);
 }
 
@@ -422,25 +432,59 @@ pinit(ModeInfo *mi)
 {
   gasketstruct *gp = &gasket[MI_SCREEN(mi)];
 
-  gp->xinc = 0.1*(1.0*random()/RAND_MAX);
-  gp->yinc = 0.1*(1.0*random()/RAND_MAX);
-  gp->zinc = 0.1*(1.0*random()/RAND_MAX);
-  gp->light_colour[0] = 6.0;
-  gp->light_colour[1] = 6.0;
-  gp->light_colour[2] = 6.0;
-  gp->light_colour[3] = 1.0;
-  gp->pos[0] = 0.0;     
-  gp->pos[1] = 0.0;
-  gp->pos[2] = 0.0;    
   /* draw the gasket */
+  gp->gasket0 = glGenLists(1);
   gp->gasket1 = glGenLists(1);
+  gp->gasket2 = glGenLists(1);
+  gp->gasket3 = glGenLists(1);
   gp->current_depth = 1;       /* start out at level 1, not 0 */
-  glNewList(gp->gasket1, GL_COMPILE);
-    compile_gasket(mi);
-  glEndList();
 }
 
-void
+
+ENTRYPOINT Bool
+gasket_handle_event (ModeInfo *mi, XEvent *event)
+{
+  gasketstruct *gp = &gasket[MI_SCREEN(mi)];
+
+  if (event->xany.type == ButtonPress &&
+      event->xbutton.button == Button1)
+    {
+      gp->button_down_p = True;
+      gltrackball_start (gp->trackball,
+                         event->xbutton.x, event->xbutton.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+  else if (event->xany.type == ButtonRelease &&
+           event->xbutton.button == Button1)
+    {
+      gp->button_down_p = False;
+      return True;
+    }
+  else if (event->xany.type == ButtonPress &&
+           (event->xbutton.button == Button4 ||
+            event->xbutton.button == Button5 ||
+            event->xbutton.button == Button6 ||
+            event->xbutton.button == Button7))
+    {
+      gltrackball_mousewheel (gp->trackball, event->xbutton.button, 10,
+                              !!event->xbutton.state);
+      return True;
+    }
+  else if (event->xany.type == MotionNotify &&
+           gp->button_down_p)
+    {
+      gltrackball_track (gp->trackball,
+                         event->xmotion.x, event->xmotion.y,
+                         MI_WIDTH (mi), MI_HEIGHT (mi));
+      return True;
+    }
+
+  return False;
+}
+
+
+ENTRYPOINT void
 init_gasket(ModeInfo *mi)
 {
   int           screen = MI_SCREEN(mi);
@@ -455,14 +499,33 @@ init_gasket(ModeInfo *mi)
   gp = &gasket[screen];
 
   gp->window = MI_WINDOW(mi);
-  gp->view_rotx = NRAND(360);
-  gp->view_roty = NRAND(360);
-  gp->view_rotz = NRAND(360);
-  gp->angle = NRAND(360)/90.0;
+
+  {
+    double spin_speed   = 1.0;
+    double wander_speed = 0.03;
+    gp->rot = make_rotator (do_spin ? spin_speed : 0,
+                            do_spin ? spin_speed : 0,
+                            do_spin ? spin_speed : 0,
+                            1.0,
+                            do_wander ? wander_speed : 0,
+                            True);
+    gp->trackball = gltrackball_init ();
+  }
+
+  gp->ncolors = 255;
+  gp->colors = (XColor *) calloc(gp->ncolors, sizeof(XColor));
+  make_smooth_colormap (0, 0, 0,
+                        gp->colors, &gp->ncolors,
+                        False, 0, False);
+  gp->ccolor0 = 0;
+  gp->ccolor1 = gp->ncolors * 0.25;
+  gp->ccolor2 = gp->ncolors * 0.5;
+  gp->ccolor3 = gp->ncolors * 0.75;
+  gp->tick = 999999;
 
   if ((gp->glx_context = init_GL(mi)) != NULL)
   {
-    reshape(MI_WIDTH(mi), MI_HEIGHT(mi));
+    reshape_gasket(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
     pinit(mi);
   }
   else
@@ -471,14 +534,12 @@ init_gasket(ModeInfo *mi)
   }
 }
 
-void
+ENTRYPOINT void
 draw_gasket(ModeInfo * mi)
 {
   gasketstruct *gp = &gasket[MI_SCREEN(mi)];
   Display      *display = MI_DISPLAY(mi);
   Window        window = MI_WINDOW(mi);
-  int           angle_incr = 1;
-  int           rot_incr = 1;/*MI_COUNT(mi) ? MI_COUNT(mi) : 1;*/
 
   if (!gp->glx_context) return;
 
@@ -489,24 +550,12 @@ draw_gasket(ModeInfo * mi)
 
   glXMakeCurrent(display, window, *(gp->glx_context));
   draw(mi);
-
-  /* rotate */
-  gp->angle = (int) (gp->angle + angle_incr) % 360;
-  if ( FABSF( gp->pos[0] ) > 8.0 ) gp->xinc = -1.0 * gp->xinc;
-  if ( FABSF( gp->pos[1] ) > 6.0 ) gp->yinc = -1.0 * gp->yinc;
-  if ( FABSF( gp->pos[2] ) >15.0 ) gp->zinc = -1.0 * gp->zinc;
-  gp->pos[0] += gp->xinc;
-  gp->pos[1] += gp->yinc;
-  gp->pos[2] += gp->zinc;    
-  gp->view_rotx = (int) (gp->view_rotx + rot_incr) % 360;
-  gp->view_roty = (int) (gp->view_roty +(rot_incr/2.0)) % 360;
-  gp->view_rotz = (int) (gp->view_rotz +(rot_incr/3.0)) % 360;
-
+  if (mi->fps_p) do_fps (mi);
   glFinish();
   glXSwapBuffers(display, window);
 }
 
-void
+ENTRYPOINT void
 release_gasket(ModeInfo * mi)
 {
   if (gasket != NULL)
@@ -522,7 +571,10 @@ release_gasket(ModeInfo * mi)
        /* Display lists MUST be freed while their glXContext is current. */
         glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
 
+        if (glIsList(gp->gasket0)) glDeleteLists(gp->gasket0, 1);
         if (glIsList(gp->gasket1)) glDeleteLists(gp->gasket1, 1);
+        if (glIsList(gp->gasket2)) glDeleteLists(gp->gasket2, 1);
+        if (glIsList(gp->gasket3)) glDeleteLists(gp->gasket3, 1);
       }
     }
     (void) free((void *) gasket);
@@ -531,6 +583,7 @@ release_gasket(ModeInfo * mi)
   FreeAllGL(mi);
 }
 
+XSCREENSAVER_MODULE_2 ("Sierpinski3D", sierpinski3d, gasket)
 
 /*********************************************************/