From http://www.jwz.org/xscreensaver/xscreensaver-5.18.tar.gz
[xscreensaver] / hacks / glx / polyhedra-gl.c
index 5efec83a7569ba9f704fdc83321592da9ad1a64c..48e7ec53a58bf2a6d481469fb201524d4513c403 100644 (file)
@@ -1,4 +1,4 @@
-/* polyhedra, Copyright (c) 2004-2006 Jamie Zawinski <jwz@jwz.org>
+/* polyhedra, Copyright (c) 2004-2011 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -18,9 +18,9 @@
 #define DEFAULTS       "*delay:        30000         \n" \
                        "*showFPS:      False         \n" \
                        "*wireframe:    False         \n" \
-                       "*titleFont:  -*-times-bold-r-normal-*-180-*\n" \
-                       "*titleFont2: -*-times-bold-r-normal-*-120-*\n" \
-                       "*titleFont3: -*-times-bold-r-normal-*-80-*\n"  \
+                       "*titleFont:  -*-helvetica-medium-r-normal-*-140-*\n" \
+                       "*titleFont2: -*-helvetica-medium-r-normal-*-100-*\n" \
+                       "*titleFont3: -*-helvetica-medium-r-normal-*-80-*\n"  \
 
 
 # define refresh_polyhedra 0
 
 #include "xlockmore.h"
 
+#ifdef HAVE_COCOA
+# include "jwxyz.h"
+#else
+# include <X11/Xlib.h>
+# include <GL/gl.h>
+# include <GL/glu.h>
+#endif
+
+#ifdef HAVE_JWZGLES
+# include "jwzgles.h"
+#endif /* HAVE_JWZGLES */
 
 #define DEF_SPIN        "True"
 #define DEF_WANDER      "True"
 #include "colors.h"
 #include "rotator.h"
 #include "gltrackball.h"
+#include "teapot.h"
+
+#ifndef HAVE_COCOA
+# define XK_MISCELLANY
+# include <X11/keysymdef.h>
+#endif
+
+#ifndef HAVE_JWZGLES
+# define HAVE_TESS
+#endif
+
 
 #ifdef USE_GL /* whole file */
 
@@ -67,8 +89,12 @@ typedef struct {
   int ncolors;
   XColor *colors;
 
+# ifdef HAVE_GLBITMAP
   XFontStruct *xfont1, *xfont2, *xfont3;
   GLuint font1_dlist, font2_dlist, font3_dlist;
+# else
+  texture_font_data *font1_data, *font2_data, *font3_data;
+# endif
 
   time_t last_change_time;
   int change_tick;
@@ -150,9 +176,15 @@ static void
 load_fonts (ModeInfo *mi)
 {
   polyhedra_configuration *bp = &bps[MI_SCREEN(mi)];
+# ifdef HAVE_GLBITMAP
   load_font (mi->dpy, "titleFont",  &bp->xfont1, &bp->font1_dlist);
   load_font (mi->dpy, "titleFont2", &bp->xfont2, &bp->font2_dlist);
   load_font (mi->dpy, "titleFont3", &bp->xfont3, &bp->font3_dlist);
+# else /* !HAVE_GLBITMAP */
+  bp->font1_data = load_texture_font (mi->dpy, "titleFont");
+  bp->font2_data = load_texture_font (mi->dpy, "titleFont2");
+  bp->font3_data = load_texture_font (mi->dpy, "titleFont3");
+# endif /* !HAVE_GLBITMAP */
 }
 
 
@@ -162,12 +194,29 @@ startup_blurb (ModeInfo *mi)
 {
   polyhedra_configuration *bp = &bps[MI_SCREEN(mi)];
   const char *s = "Computing polyhedra...";
+# ifdef HAVE_GLBITMAP
+  XFontStruct *f = bp->xfont1;
+# else /* !HAVE_GLBITMAP */
+  texture_font_data *f = bp->font1_data;
+# endif /* !HAVE_GLBITMAP */
+
   glColor3f (0.8, 0.8, 0);
-  print_gl_string (mi->dpy, bp->xfont1, bp->font1_dlist,
+  print_gl_string (mi->dpy, 
+# ifdef HAVE_GLBITMAP
+                   bp->xfont1, bp->font1_dlist,
+# else /* !HAVE_GLBITMAP */
+                   bp->font1_data,
+# endif /* !HAVE_GLBITMAP */
                    mi->xgwa.width, mi->xgwa.height,
-                   mi->xgwa.width - (string_width (bp->xfont1, s) + 40),
+                   mi->xgwa.width - (
+# ifdef HAVE_GLBITMAP
+                                     string_width (f, s, 0)
+# else /* !HAVE_GLBITMAP */
+                                     texture_string_width (f, s, 0)
+# endif /* !HAVE_GLBITMAP */
+                                     + 40),
                    mi->xgwa.height - 10,
-                   s);
+                   s, False);
   glFinish();
   glXSwapBuffers(MI_DISPLAY(mi), MI_WINDOW(mi));
 }
@@ -224,7 +273,9 @@ polyhedra_handle_event (ModeInfo *mi, XEvent *event)
     }
   else if (event->xany.type == ButtonPress &&
            (event->xbutton.button == Button4 ||
-            event->xbutton.button == Button5))
+            event->xbutton.button == Button5 ||
+            event->xbutton.button == Button6 ||
+            event->xbutton.button == Button7))
     {
       gltrackball_mousewheel (bp->trackball, event->xbutton.button, 10,
                               !!event->xbutton.state);
@@ -239,10 +290,12 @@ polyhedra_handle_event (ModeInfo *mi, XEvent *event)
       bp->change_to = -1;
       if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
         bp->change_to = random() % bp->npolyhedra;
-      else if (c == '>' || c == '.' || c == '+' || c == '=')
+      else if (c == '>' || c == '.' || c == '+' || c == '=' ||
+               keysym == XK_Right || keysym == XK_Up)
         bp->change_to = (bp->which + 1) % bp->npolyhedra;
       else if (c == '<' || c == ',' || c == '-' || c == '_' ||
-               c == '\010' || c == '\177')
+               c == '\010' || c == '\177' ||
+               keysym == XK_Left || keysym == XK_Down)
         bp->change_to = (bp->which + bp->npolyhedra - 1) % bp->npolyhedra;
 
       if (bp->change_to != -1)
@@ -294,33 +347,55 @@ new_label (ModeInfo *mi)
                p->density, (p->chi < 0 ? "" : "  "), p->chi);
 
       {
+# ifdef HAVE_GLBITMAP
         XFontStruct *f;
         GLuint fl;
+# else /* !HAVE_GLBITMAP */
+        texture_font_data *f;
+# endif /* !HAVE_GLBITMAP */
         if (MI_WIDTH(mi) >= 500 && MI_HEIGHT(mi) >= 375)
+# ifdef HAVE_GLBITMAP
           f = bp->xfont1, fl = bp->font1_dlist;                       /* big font */
+# else /* !HAVE_GLBITMAP */
+          f = bp->font1_data;
+# endif /* !HAVE_GLBITMAP */
         else if (MI_WIDTH(mi) >= 350 && MI_HEIGHT(mi) >= 260)
+# ifdef HAVE_GLBITMAP
           f = bp->xfont2, fl = bp->font2_dlist;                       /* small font */
+# else /* !HAVE_GLBITMAP */
+          f = bp->font2_data;                                 /* small font */
+# endif /* !HAVE_GLBITMAP */
         else
+# ifdef HAVE_GLBITMAP
           f = bp->xfont3, fl = bp->font3_dlist;                       /* tiny font */
+# else /* !HAVE_GLBITMAP */
+          f = bp->font3_data;                                 /* tiny font */
+# endif /* !HAVE_GLBITMAP */
 
         glColor3f (0.8, 0.8, 0);
-        print_gl_string (mi->dpy, f, fl,
+        print_gl_string (mi->dpy, f,
+# ifdef HAVE_GLBITMAP
+                         fl,
+# endif /* HAVE_GLBITMAP */
                          mi->xgwa.width, mi->xgwa.height,
                          10, mi->xgwa.height - 10,
-                         label);
+                         label, False);
       }
     }
   glEndList ();
 }
 
 
+#ifdef HAVE_TESS
 static void
 tess_error (GLenum errorCode)
 {
   fprintf (stderr, "%s: tesselation error: %s\n",
            progname, gluErrorString(errorCode));
-  exit (0);
+  abort();
 }
+#endif /* HAVE_TESS */
+
 
 static void
 new_polyhedron (ModeInfo *mi)
@@ -333,11 +408,13 @@ new_polyhedron (ModeInfo *mi)
   /* Use the GLU polygon tesselator so that nonconvex faces are displayed
      correctly (e.g., for the "pentagrammic concave deltohedron").
    */
+# ifdef HAVE_TESS
   GLUtesselator *tobj = gluNewTess();
   gluTessCallback (tobj, GLU_TESS_BEGIN,  (void (*) (void)) &glBegin);
   gluTessCallback (tobj, GLU_TESS_END,    (void (*) (void)) &glEnd);
   gluTessCallback (tobj, GLU_TESS_VERTEX, (void (*) (void)) &glVertex3dv);
   gluTessCallback (tobj, GLU_TESS_ERROR,  (void (*) (void)) &tess_error);
+# endif /* HAVE_TESS */
 
   mi->polygon_count = 0;
 
@@ -362,40 +439,95 @@ new_polyhedron (ModeInfo *mi)
     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
   glNewList (bp->object_list, GL_COMPILE);
-  for (i = 0; i < p->nfaces; i++)
+  if (bp->which == bp->npolyhedra-1)
     {
-      int j;
-      face *f = &p->faces[i];
-
-      if (f->color > 64 || f->color < 0) abort();
+      GLfloat bcolor[4];
+      bcolor[0] = bp->colors[0].red   / 65536.0;
+      bcolor[1] = bp->colors[0].green / 65536.0;
+      bcolor[2] = bp->colors[0].blue  / 65536.0;
+      bcolor[3] = 1.0;
       if (wire)
         glColor3f (0, 1, 0);
       else
+        glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor);
+
+      glScalef (0.8, 0.8, 0.8);
+      p->nfaces = unit_teapot (6, wire);
+      p->nedges = p->nfaces * 3 / 2;
+      p->npoints = p->nfaces * 3;
+      p->logical_faces = p->nfaces;
+      p->logical_vertices = p->npoints;
+    }
+  else
+    {
+      glFrontFace (GL_CCW);
+      for (i = 0; i < p->nfaces; i++)
         {
-          GLfloat bcolor[4];
-          bcolor[0] = bp->colors[f->color].red   / 65536.0;
-          bcolor[1] = bp->colors[f->color].green / 65536.0;
-          bcolor[2] = bp->colors[f->color].blue  / 65536.0;
-          bcolor[2] = 1.0;
-          glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor);
-        }
+          int j;
+          face *f = &p->faces[i];
+
+          if (f->color > 64 || f->color < 0) abort();
+          if (wire)
+            glColor3f (0, 1, 0);
+          else
+            {
+              GLfloat bcolor[4];
+              bcolor[0] = bp->colors[f->color].red   / 65536.0;
+              bcolor[1] = bp->colors[f->color].green / 65536.0;
+              bcolor[2] = bp->colors[f->color].blue  / 65536.0;
+              bcolor[3] = 1.0;
+              glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, bcolor);
+            }
 
-      kludge_normal (f->npoints, f->points, p->points);
+          kludge_normal (f->npoints, f->points, p->points);
       
-      gluTessBeginPolygon (tobj, 0);
-      gluTessBeginContour (tobj);
-      for (j = 0; j < f->npoints; j++)
-        {
-          point *pp = &p->points[f->points[j]];
-          gluTessVertex (tobj, &pp->x, &pp->x);
+# ifdef HAVE_TESS
+          gluTessBeginPolygon (tobj, 0);
+          gluTessBeginContour (tobj);
+          for (j = 0; j < f->npoints; j++)
+            {
+              point *pp = &p->points[f->points[j]];
+              gluTessVertex (tobj, &pp->x, &pp->x);
+            }
+          gluTessEndContour (tobj);
+          gluTessEndPolygon (tobj);
+# else  /* !HAVE_TESS */
+          glBegin (wire ? GL_LINE_LOOP :
+                   f->npoints == 3 ? GL_TRIANGLES :
+                   f->npoints == 4 ? GL_QUADS :
+                   GL_POLYGON);
+          for (j = 0; j < f->npoints; j++)
+            {
+              point *pp = &p->points[f->points[j]];
+              glVertex3f (pp->x, pp->y, pp->z);
+            }
+          glEnd();
+# endif /* !HAVE_TESS */
         }
-      gluTessEndContour (tobj);
-      gluTessEndPolygon (tobj);
     }
   glEndList ();
 
   mi->polygon_count += p->nfaces;
+# ifdef HAVE_TESS
   gluDeleteTess (tobj);
+# endif
+}
+
+
+static void
+construct_teapot (ModeInfo *mi)
+{
+  polyhedra_configuration *bp = &bps[MI_SCREEN(mi)];
+  int n = bp->npolyhedra-1;
+  polyhedron *p = (polyhedron *) calloc (1, sizeof(*p));
+  p->number = n;
+  p->wythoff = strdup("X00398|1984");
+  p->name = strdup("Teapot");
+  p->dual = strdup("");
+  p->config = strdup("Melitta");
+  p->group = strdup("Teapotahedral (Newell[1975])");
+  p->class = strdup("Utah Teapotahedron");
+  bp->polyhedra[n] = p;
 }
 
 
@@ -412,8 +544,6 @@ init_polyhedra (ModeInfo *mi)
       fprintf(stderr, "%s: out of memory\n", progname);
       exit(1);
     }
-
-    bp = &bps[MI_SCREEN(mi)];
   }
 
   bp = &bps[MI_SCREEN(mi)];
@@ -424,8 +554,6 @@ init_polyhedra (ModeInfo *mi)
   load_fonts (mi);
   startup_blurb (mi);
 
-  reshape_polyhedra (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
-
   if (!wire)
     {
       GLfloat pos[4] = {1.0, 1.0, 1.0, 0.0};
@@ -464,6 +592,7 @@ init_polyhedra (ModeInfo *mi)
   }
 
   bp->npolyhedra = construct_polyhedra (&bp->polyhedra);
+  construct_teapot (mi);
 
   bp->object_list = glGenLists (1);
   bp->title_list  = glGenLists (1);
@@ -476,7 +605,14 @@ init_polyhedra (ModeInfo *mi)
     if (!strcasecmp (do_which_str, "random"))
       ;
     else if (1 == sscanf (do_which_str, " %d %c", &x, &c))
-      do_which = x;
+      {
+        if (x >= 0 && x < bp->npolyhedra) 
+          do_which = x;
+        else
+          fprintf (stderr, 
+                   "%s: polyhedron %d does not exist: there are only %d.\n",
+                   progname, x, bp->npolyhedra-1);
+      }
     else if (*do_which_str)
       {
         char *s;
@@ -485,6 +621,7 @@ init_polyhedra (ModeInfo *mi)
 
         for (x = 0; x < bp->npolyhedra; x++)
           if (!strcasecmp (do_which_str, bp->polyhedra[x]->name) ||
+              !strcasecmp (do_which_str, bp->polyhedra[x]->class) ||
               !strcasecmp (do_which_str, bp->polyhedra[x]->wythoff) ||
               !strcasecmp (do_which_str, bp->polyhedra[x]->config))
             {
@@ -501,6 +638,9 @@ init_polyhedra (ModeInfo *mi)
   }
 
   new_polyhedron (mi);
+  reshape_polyhedra (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+  clear_gl_error(); /* WTF? sometimes "invalid op" from glViewport! */
+
 }
 
 
@@ -571,7 +711,10 @@ draw_polyhedra (ModeInfo *mi)
                  (y - 0.5) * 8,
                  (z - 0.5) * 15);
 
+    /* Do it twice because we don't track the device's orientation. */
+    glRotatef( current_device_rotation(), 0, 0, 1);
     gltrackball_rotate (bp->trackball);
+    glRotatef(-current_device_rotation(), 0, 0, 1);
 
     get_rotation (bp->rot, &x, &y, &z, !bp->button_down_p);
     glRotatef (x * 360, 1.0, 0.0, 0.0);