http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[xscreensaver] / hacks / glx / spheremonics.c
index a4c06902f2b9286f95d8ca319d0d9bf0876cdcce..30157710c53bb69ed186116abf1eb6aaf71b28ea 100644 (file)
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 2002 Jamie Zawinski <jwz@jwz.org>
+/* xscreensaver, Copyright (c) 2002, 2004 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
@@ -97,6 +97,8 @@ extern XtAppContext app;
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
 #include "xlockmore.h"
+#include "glxfonts.h"
+#include "normals.h"
 #include "colors.h"
 #include "rotator.h"
 #include "gltrackball.h"
@@ -106,10 +108,6 @@ extern XtAppContext app;
 
 #include <GL/glu.h>
 
-typedef struct {
-   double x,y,z;
-} XYZ;
-
 typedef struct {
   GLXContext *glx_context;
   rotator *rot;
@@ -134,6 +132,7 @@ typedef struct {
 
   XFontStruct *font;
   GLuint font_list;
+  int change_tick;
 
 } spheremonics_configuration;
 
@@ -165,14 +164,14 @@ static XrmOptionDescRec opts[] = {
 };
 
 static argtype vars[] = {
-  {(caddr_t *) &do_spin,   "spin",   "Spin",   DEF_SPIN,   t_String},
-  {(caddr_t *) &do_wander, "wander", "Wander", DEF_WANDER, t_Bool},
-  {(caddr_t *) &res,       "resolution", "Resolution", DEF_RESOLUTION, t_Int},
-  {(caddr_t *) &duration,  "duration",   "Duration",   DEF_DURATION,   t_Int},
-  {(caddr_t *) &do_bbox,   "bbox",   "BBox",   DEF_BBOX,   t_Bool},
-  {(caddr_t *) &do_grid,   "grid",   "Grid",   DEF_GRID,   t_Bool},
-  {(caddr_t *) &smooth_p,  "smooth", "Smooth", DEF_SMOOTH, t_Bool},
-  {(caddr_t *) &static_parms, "parameters", "Parameters", DEF_PARMS, t_String},
+  {&do_spin,      "spin",       "Spin",       DEF_SPIN,       t_String},
+  {&do_wander,    "wander",     "Wander",     DEF_WANDER,     t_Bool},
+  {&res,          "resolution", "Resolution", DEF_RESOLUTION, t_Int},
+  {&duration,     "duration",   "Duration",   DEF_DURATION,   t_Int},
+  {&do_bbox,      "bbox",       "BBox",       DEF_BBOX,       t_Bool},
+  {&do_grid,      "grid",       "Grid",       DEF_GRID,       t_Bool},
+  {&smooth_p,     "smooth",     "Smooth",     DEF_SMOOTH,     t_Bool},
+  {&static_parms, "parameters", "Parameters", DEF_PARMS,      t_String},
 };
 
 ModeSpecOpt ccs_opts = {countof(opts), opts, countof(vars), vars, NULL};
@@ -265,46 +264,6 @@ sphere_eval (double theta, double phi, int *m)
 }
 
 
-/* Normalise a vector */
-static void
-normalize (XYZ *p)
-{
-  double length;
-  length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
-  if (length != 0) {
-    p->x /= length;
-    p->y /= length;
-    p->z /= length;
-  } else {
-    p->x = 0;
-    p->y = 0;
-    p->z = 0;
-  }       
-}
-
-/*-------------------------------------------------------------------------
-        Calculate the unit normal at p given two other points 
-        p1,p2 on the surface. The normal points in the direction 
-        of p1 crossproduct p2
- */
-static XYZ
-calc_normal (XYZ p, XYZ p1, XYZ p2)
-{
-  XYZ n, pa, pb;
-  pa.x = p1.x - p.x;
-  pa.y = p1.y - p.y;
-  pa.z = p1.z - p.z;
-  pb.x = p2.x - p.x;
-  pb.y = p2.y - p.y;
-  pb.z = p2.z - p.z;
-  n.x = pa.y * pb.z - pa.z * pb.y;
-  n.y = pa.z * pb.x - pa.x * pb.z;
-  n.z = pa.x * pb.y - pa.y * pb.x;
-  normalize (&n);
-  return (n);
-}
-
-
 static void
 do_color (int i, XColor *colors)
 {
@@ -736,66 +695,6 @@ generate_spheremonics (ModeInfo *mi)
 }
 
 
-\f
-
-static void
-load_font (ModeInfo *mi, char *res, XFontStruct **fontP, GLuint *dlistP)
-{
-  const char *font = get_string_resource (res, "Font");
-  XFontStruct *f;
-  Font id;
-  int first, last;
-
-  if (!font) font = "-*-times-bold-r-normal-*-140-*";
-
-  f = XLoadQueryFont(mi->dpy, font);
-  if (!f) f = XLoadQueryFont(mi->dpy, "fixed");
-
-  id = f->fid;
-  first = f->min_char_or_byte2;
-  last = f->max_char_or_byte2;
-  
-  clear_gl_error ();
-  *dlistP = glGenLists ((GLuint) last+1);
-  check_gl_error ("glGenLists");
-  glXUseXFont(id, first, last-first+1, *dlistP + first);
-  check_gl_error ("glXUseXFont");
-
-  *fontP = f;
-}
-
-static void
-draw_label (ModeInfo *mi, const char *s)
-{
-  spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
-  int i;
-  
-  glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT);
-  glDisable(GL_LIGHTING);
-  glDisable(GL_DEPTH_TEST);
-  glMatrixMode(GL_PROJECTION);
-  glPushMatrix();
-  glLoadIdentity();
-  glMatrixMode(GL_MODELVIEW);
-  glPushMatrix();
-  glLoadIdentity();
-  gluOrtho2D(0, mi->xgwa.width, 0, mi->xgwa.height);
-  glColor3f(1.0, 1.0, 0.0);
-
-  glRasterPos2f (10,
-                 (mi->xgwa.height
-                  - 10
-                  - (cc->font->ascent + cc->font->descent)));
-  for (i = 0; i < strlen(s); i++)
-    glCallList (cc->font_list + (int)s[i]);
-
-  glPopMatrix();
-  glMatrixMode(GL_PROJECTION);
-  glPopMatrix();
-  glPopAttrib();
-}
-
-
 \f
 
 void 
@@ -864,14 +763,14 @@ init_spheremonics (ModeInfo *mi)
 
   cc->resolution = res;
 
-  load_font (mi, "labelfont", &cc->font, &cc->font_list);
+  load_font (mi->dpy, "labelfont", &cc->font, &cc->font_list);
 
   cc->dlist = glGenLists(1);
   cc->dlist2 = glGenLists(1);
 
   cc->m_max = 4; /* 9? */
   {
-    int i;
+    unsigned int i;
     for (i = 0; i < countof(cc->dm); i++)
       cc->dm[i] = 1;  /* going up! */
 
@@ -890,7 +789,7 @@ spheremonics_handle_event (ModeInfo *mi, XEvent *event)
   spheremonics_configuration *cc = &ccs[MI_SCREEN(mi)];
 
   if (event->xany.type == ButtonPress &&
-      event->xbutton.button & Button1)
+      event->xbutton.button == Button1)
     {
       cc->button_down_p = True;
       gltrackball_start (cc->trackball,
@@ -899,11 +798,31 @@ spheremonics_handle_event (ModeInfo *mi, XEvent *event)
       return True;
     }
   else if (event->xany.type == ButtonRelease &&
-           event->xbutton.button & Button1)
+           event->xbutton.button == Button1)
     {
       cc->button_down_p = False;
       return True;
     }
+  else if (event->xany.type == ButtonPress &&
+           (event->xbutton.button == Button4 ||
+            event->xbutton.button == Button5))
+    {
+      gltrackball_mousewheel (cc->trackball, event->xbutton.button, 10,
+                              !!event->xbutton.state);
+      return True;
+    }
+  else if (event->xany.type == KeyPress)
+    {
+      KeySym keysym;
+      char c = 0;
+      XLookupString (&event->xkey, &c, 1, &keysym, 0);
+
+      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
+        {
+          cc->change_tick = duration;
+          return True;
+        }
+    }
   else if (event->xany.type == MotionNotify &&
            cc->button_down_p)
     {
@@ -978,16 +897,20 @@ draw_spheremonics (ModeInfo *mi)
                 : "%d %d %d %d %d %d %d %d"),
                cc->m[0], cc->m[1], cc->m[2], cc->m[3],
                cc->m[4], cc->m[5], cc->m[6], cc->m[7]);
-      draw_label (mi, buf);
+
+      glColor3f(1.0, 1.0, 0.0);
+      print_gl_string (mi->dpy, cc->font, cc->font_list,
+                       mi->xgwa.width, mi->xgwa.height,
+                       10, mi->xgwa.height - 10,
+                       buf);
     }
 
   if (!static_parms)
     {
-      static int tick = 0;
-      if (tick++ >= duration && !cc->button_down_p)
+      if (cc->change_tick++ >= duration && !cc->button_down_p)
         {
           generate_spheremonics(mi);
-          tick = 0;
+          cc->change_tick = 0;
           cc->mesher = -1;  /* turn off the mesh when switching objects */
         }
     }