From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / mirrorblob.c
index a2f1ed6ea48d03d7a4fec6b8ec39786e21012f9f..7595e70d243ea6170c6ac1c798e232e14f6bc748 100644 (file)
                  "*useSHM:              True       \n"                      \
                  "*desktopGrabber:  xscreensaver-getimage -no-desktop %s\n" \
                  "*grabDesktopImages:   True  \n"                           \
-                 "*chooseRandomImages:  True  \n"
+                 "*chooseRandomImages:  True  \n"                           \
+                "*suppressRotationAnimation: True\n"                       \
 
 # define refresh_mirrorblob 0
-/*
-# define mirrorblob_handle_event 0
-*/
+# define release_mirrorblob 0
 # include "xlockmore.h"
 #else /* !STANDALONE */
 # include "xlock.h"        /* from the xlockmore distribution */
@@ -179,7 +178,7 @@ ENTRYPOINT ModeSpecOpt mirrorblob_opts = {countof(opts), opts, countof(vars), va
 
 #ifdef USE_MODULES
 ModStruct   mirrorblob_description =
-{"mirrorblob", "init_mirrorblob", "draw_mirrorblob", "release_mirrorblob",
+{"mirrorblob", "init_mirrorblob", "draw_mirrorblob", NULL,
  "draw_mirrorblob", "init_mirrorblob", "handle_event", &mirrorblob_opts,
  1000, 1, 2, 1, 4, 1.0, "",
  "OpenGL mirrorblob", 0, NULL};
@@ -191,20 +190,20 @@ ModStruct   mirrorblob_description =
 
 typedef struct
 {
-  GLdouble x, y;
+  GLfloat x, y;
 } Vector2D;
 
 typedef struct
 {
-  GLdouble x, y, z;
+  GLfloat x, y, z;
 } Vector3D;
 
 typedef struct
 {
-  GLdouble w;
-  GLdouble x;
-  GLdouble y;
-  GLdouble z;
+  GLfloat w;
+  GLfloat x;
+  GLfloat y;
+  GLfloat z;
 } Quaternion;
 
 typedef struct
@@ -246,7 +245,7 @@ typedef struct
 #define PMM {  sqrt_3, -sqrt_3, -sqrt_3 }       /* +X, -Y, -Z */
 
 /* Structure describing a tetrahedron */
-Vector3D tetrahedron[4][3] = {
+static Vector3D tetrahedron[4][3] = {
     {PPP, MMP, MPM},
     {PMM, MPM, MMP},
     {PPP, MPM, PMM},
@@ -257,7 +256,7 @@ Vector3D tetrahedron[4][3] = {
  * Static blob data
  *****************************************************************************/
 
-const Vector3D zero_vector = { 0.0, 0.0, 0.0 };
+static const Vector3D zero_vector = { 0.0, 0.0, 0.0 };
 
 /* Use 2 textures to allow a gradual fade between images */
 #define NUM_TEXTURES 2
@@ -461,9 +460,9 @@ normalise (const Vector3D v)
  * Calculate the transform matrix for the given quaternion
  */
 static void
-quaternion_transform (Quaternion q, GLdouble * transform)
+quaternion_transform (Quaternion q, GLfloat * transform)
 {
-  GLdouble x, y, z, w;
+  GLfloat x, y, z, w;
   x = q.x;
   y = q.y;
   z = q.z;
@@ -495,7 +494,7 @@ quaternion_transform (Quaternion q, GLdouble * transform)
  * Apply a matrix transform to the given vector
  */
 static inline Vector3D
-vector_transform (Vector3D u, GLdouble * t)
+vector_transform (Vector3D u, GLfloat * t)
 {
   Vector3D result;
 
@@ -516,7 +515,7 @@ partial (Vector3D node1, Vector3D node2, double distance)
 {
   Vector3D result;
   Vector3D rotation_axis;
-  GLdouble transformation[16];
+  GLfloat transformation[16];
   double angle;
   Quaternion rotation;
 
@@ -605,6 +604,10 @@ grab_texture(ModeInfo *mi, int texture_index)
 static void
 set_parameters(void)
 {
+# ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
+  wireframe = 0;
+# endif
+
   /* In wire frame mode do not draw a texture */
   if (wireframe)
     {
@@ -737,7 +740,7 @@ initialize_gl(ModeInfo *mi, GLsizei width, GLsizei height)
  * Initialise the openGL state data.
  */
 static void
-set_blob_gl_state(GLdouble alpha)
+set_blob_gl_state(GLfloat alpha)
 {
   if (do_antialias)
     {
@@ -776,12 +779,12 @@ set_blob_gl_state(GLdouble alpha)
       glEnable (GL_BLEND);
       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       /* Set the default blob colour to off-white. */
-      glColor4d (0.9, 0.9, 1.0, alpha);
+      glColor4f (0.9, 0.9, 1.0, alpha);
     }
   else
     {
       glDisable(GL_BLEND);
-      glColor4d (0.9, 0.9, 1.0, 1.0);
+      glColor4f (0.9, 0.9, 1.0, 1.0);
     }
     
   glEnable(GL_DEPTH_TEST);
@@ -1119,11 +1122,13 @@ initialise_blob(mirrorblobstruct *gp,
  *
  * Return the magnitude of the given vector
  */
+#if 0
 static inline double
 length (Vector3D u)
 {
   return sqrt (u.x * u.x + u.y * u.y + u.z * u.z);
 }
+#endif
 
 /******************************************************************************
  *
@@ -1394,10 +1399,10 @@ draw_vertex(mirrorblobstruct *gp, int index)
     }
   if (load_textures)
     {
-      glTexCoord3dv((GLdouble *) &gp->tex_coords[index]);
+      glTexCoord2fv(&gp->tex_coords[index].x);
     }
-  glNormal3dv((GLdouble *) &gp->normals[index]);
-  glVertex3dv((GLdouble *) &gp->dots[index]);
+  glNormal3fv(&gp->normals[index].x);
+  glVertex3fv(&gp->dots[index].x);
 }
 
 /******************************************************************************
@@ -1412,6 +1417,7 @@ draw_blob (mirrorblobstruct *gp)
 
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
+/*  glRotatef(current_device_rotation(), 0, 0, 1); */
 
   /* Move down the z-axis. */
   glTranslatef (0.0, 0.0, -4.0);
@@ -1435,9 +1441,9 @@ draw_blob (mirrorblobstruct *gp)
       if (gp->normals[gp->faces[face].node1].z > 0.0)
         {
           Vector3D end = gp->dots[gp->faces[face].node1];
-          glVertex3dv((GLdouble *) &end);
+          glVertex3dv(&end);
           add(&end, scale(gp->normals[gp->faces[face].node1], 0.25));
-          glVertex3dv((GLdouble *) &end);
+          glVertex3dv(&end);
         }
     }
   glEnd();
@@ -1454,6 +1460,7 @@ static void
 draw_background (ModeInfo *mi)
 {
   mirrorblobstruct *gp = &Mirrorblob[MI_SCREEN(mi)];
+  GLfloat rot = current_device_rotation();
     
   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   glEnable (GL_TEXTURE_2D);
@@ -1467,6 +1474,16 @@ draw_background (ModeInfo *mi)
   glPushMatrix();
   glLoadIdentity();
 
+  glRotatef (-rot, 0, 0, 1);
+/*
+  if ((rot >  45 && rot <  135) ||
+      (rot < -45 && rot > -135))
+    {
+      GLfloat s = MI_WIDTH(mi) / (GLfloat) MI_HEIGHT(mi);
+      glScalef (s, 1/s, 1);
+    }
+*/
+
   glOrtho(0.0, MI_WIDTH(mi), MI_HEIGHT(mi), 0.0, -1000.0, 1000.0);
 
   glBegin (GL_QUADS);
@@ -1503,13 +1520,13 @@ draw_scene(ModeInfo * mi)
   check_gl_error ("draw_scene");
 
   mi->polygon_count = 0;
-  glColor4d(1.0, 1.0, 1.0, 1.0);
+  glColor4(1.0, 1.0, 1.0, 1.0);
 
   current_time = double_time();
   switch (gp->state)
     {
     case INITIALISING:
-      glColor4d(0.0, 0.0, 0.0, 1.0);
+      glColor4(0.0, 0.0, 0.0, 1.0);
       fade = 1.0;
       break;
 
@@ -1540,7 +1557,7 @@ draw_scene(ModeInfo * mi)
         {
           glClear(GL_DEPTH_BUFFER_BIT);
           glEnable (GL_BLEND);
-          glColor4d (1.0, 1.0, 1.0, motion_blur);
+          glColor4f (1.0, 1.0, 1.0, motion_blur);
         }
       else
         {
@@ -1557,7 +1574,7 @@ draw_scene(ModeInfo * mi)
           glEnable (GL_BLEND);
           /* Select the texture to transition to */
           glBindTexture (GL_TEXTURE_2D, gp->textures[1 - gp->current_texture]);
-          glColor4d (1.0, 1.0, 1.0, 1.0 - fade);
+          glColor4f (1.0, 1.0, 1.0, 1.0 - fade);
 
           draw_background (mi);
           mi->polygon_count++;
@@ -1571,7 +1588,7 @@ draw_scene(ModeInfo * mi)
   else if (motion_blur > 0.0)
     {
       glEnable (GL_BLEND);
-      glColor4d (0.0, 0.0, 0.0, motion_blur);
+      glColor4f (0.0, 0.0, 0.0, motion_blur);
       glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
       glTranslatef (0.0, 0.0, -4.0);
       glRectd (-10.0, -10.0, 10.0, 10.0);
@@ -1669,7 +1686,7 @@ draw_scene(ModeInfo * mi)
                 }
               else
                 {
-                  glColor4d(0.9, 0.9, 1.0, (1.0 - fade) * blend);
+                  glColor4(0.9, 0.9, 1.0, (1.0 - fade) * blend);
                 }
 
               draw_blob (gp);
@@ -1741,21 +1758,7 @@ mirrorblob_handle_event (ModeInfo * mi, XEvent * event)
   mirrorblobstruct *gp = &Mirrorblob[MI_SCREEN (mi)];
 
   if (event->xany.type == ButtonPress &&
-      event->xbutton.button == Button1)
-    {
-      gp->button_down = 1;
-      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 = 0;
-      return True;
-    }
-  else if (event->xany.type == ButtonPress &&
-           event->xbutton.button == Button4)
+      event->xbutton.button == Button4)
     {
       zoom *= 1.1;
       return True;
@@ -1767,15 +1770,24 @@ mirrorblob_handle_event (ModeInfo * mi, XEvent * event)
       zoom *= 0.9;
       return True;
     }
-  else if (event->xany.type == MotionNotify && gp->button_down)
+  else if (gltrackball_event_handler (event, gp->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &gp->button_down))
+    {
+      return True;
+    }
+  else if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
     {
-      gltrackball_track (gp->trackball, event->xmotion.x,
-                         event->xmotion.y, MI_WIDTH (mi), MI_HEIGHT (mi));
+      gp->state_start_time = 0;
+      gp->state = HOLDING;
       return True;
     }
+
   return False;
 }
 
+static void free_mirrorblob(ModeInfo * mi);
+
 /******************************************************************************
  *
  * XMirrorblob initialise entry
@@ -1787,14 +1799,7 @@ init_mirrorblob(ModeInfo * mi)
 
   mirrorblobstruct *gp;
 
-  if (Mirrorblob == NULL)
-    {
-      if ((Mirrorblob = (mirrorblobstruct *)
-           calloc(MI_NUM_SCREENS(mi), sizeof (mirrorblobstruct))) == NULL)
-        {
-          return;
-        }
-    }
+  MI_INIT(mi, Mirrorblob, free_mirrorblob);
   gp = &Mirrorblob[screen];
 
   gp->window = MI_WINDOW(mi);
@@ -1807,7 +1812,7 @@ init_mirrorblob(ModeInfo * mi)
     {
       MI_CLEARWINDOW(mi);
     }
-  gp->trackball = gltrackball_init();
+  gp->trackball = gltrackball_init(False);
     
   initialise_blob(gp, MI_WIDTH(mi), MI_HEIGHT(mi), BUMP_ARRAY_SIZE);
   gp->state = INITIALISING;
@@ -1820,27 +1825,18 @@ init_mirrorblob(ModeInfo * mi)
  *
  * XMirrorblob cleanup entry
  */
-ENTRYPOINT void
-release_mirrorblob(ModeInfo * mi)
+static void
+free_mirrorblob(ModeInfo * mi)
 {
-  if (Mirrorblob != NULL) {
-    int i;
-    for (i = 0; i < MI_NUM_SCREENS(mi); i++) {
-      mirrorblobstruct *gp = &Mirrorblob[i];
-      if (gp->nodes) free(gp->nodes);
-      if (gp->faces) free(gp->faces);
-      if (gp->bump_data) free(gp->bump_data);
-      if (gp->colours) free(gp->colours);
-      if (gp->tex_coords) free(gp->tex_coords);
-      if (gp->dots) free(gp->dots);
-      if (gp->wall_shape) free(gp->wall_shape);
-      if (gp->bump_shape) free(gp->bump_shape);
-    }
-
-    free(Mirrorblob);
-    Mirrorblob = NULL;
-  }
-  FreeAllGL(mi);
+  mirrorblobstruct *gp = &Mirrorblob[MI_SCREEN(mi)];
+  if (gp->nodes) free(gp->nodes);
+  if (gp->faces) free(gp->faces);
+  if (gp->bump_data) free(gp->bump_data);
+  if (gp->colours) free(gp->colours);
+  if (gp->tex_coords) free(gp->tex_coords);
+  if (gp->dots) free(gp->dots);
+  if (gp->wall_shape) free(gp->wall_shape);
+  if (gp->bump_shape) free(gp->bump_shape);
 }
 
 XSCREENSAVER_MODULE ("MirrorBlob", mirrorblob)