From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / morph3d.c
index ab3613c5e97c506c176c70ad87dfcef33b0d805c..4bea5766f90002f2545eb7a9dd62b7a5d1c87e07 100644 (file)
@@ -1,9 +1,8 @@
 /* -*- Mode: C; tab-width: 4 -*- */
 /* morph3d --- Shows 3D morphing objects */
 
-#if !defined( lint ) && !defined( SABER )
+#if 0
 static const char sccsid[] = "@(#)morph3d.c    5.01 2001/03/01 xlockmore";
-
 #endif
 
 #undef DEBUG_CULL_FACE
@@ -52,25 +51,16 @@ static const char sccsid[] = "@(#)morph3d.c 5.01 2001/03/01 xlockmore";
  *
  */
 
-#ifdef VMS
-/*-
- * 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>
-#endif
-
 #ifdef STANDALONE
 # define MODE_moebius
-# define PROGCLASS             "Morph3d"
-# define HACK_INIT             init_morph3d
-# define HACK_DRAW             draw_morph3d
-# define HACK_RESHAPE  reshape
-# define morph3d_opts  xlockmore_opts
 # define DEFAULTS              "*delay:                40000   \n"             \
                                                "*showFPS:      False   \n"             \
-                                               "*count:                0               \n"
+                                               "*count:                0               \n"             \
+                                               "*suppressRotationAnimation: True\n" \
+
+# define refresh_morph3d 0
+# define release_morph3d 0
+# define morph3d_handle_event 0
 # include "xlockmore.h"                /* from the xscreensaver distribution */
 #else /* !STANDALONE */
 # include "xlock.h"            /* from the xlockmore distribution */
@@ -78,12 +68,12 @@ static const char sccsid[] = "@(#)morph3d.c 5.01 2001/03/01 xlockmore";
 
 #ifdef MODE_moebius
 
-ModeSpecOpt morph3d_opts =
+ENTRYPOINT ModeSpecOpt morph3d_opts =
 {0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
 
 #ifdef USE_MODULES
 ModStruct   morph3d_description =
-{"morph3d", "init_morph3d", "draw_morph3d", "release_morph3d",
+{"morph3d", "init_morph3d", "draw_morph3d", (char *) NULL,
  "draw_morph3d", "change_morph3d", (char *) NULL, &morph3d_opts,
  1000, 0, 1, 1, 4, 1.0, "",
  "Shows GL morphing polyhedra", 0, NULL};
@@ -134,43 +124,29 @@ typedef struct {
        int         VisibleSpikes;
        void        (*draw_object) (ModeInfo * mi);
        float       Magnitude;
-       float      *MaterialColor[20];
+       const float *MaterialColor[20];
        GLXContext *glx_context;
+    int         arrayninit;
+
 } morph3dstruct;
 
-static float front_shininess[] =
-{60.0};
-static float front_specular[] =
-{0.7, 0.7, 0.7, 1.0};
-static float ambient[] =
-{0.0, 0.0, 0.0, 1.0};
-static float diffuse[] =
-{1.0, 1.0, 1.0, 1.0};
-static float position0[] =
-{1.0, 1.0, 1.0, 0.0};
-static float position1[] =
-{-1.0, -1.0, 1.0, 0.0};
-static float lmodel_ambient[] =
-{0.5, 0.5, 0.5, 1.0};
-static float lmodel_twoside[] =
-{GL_TRUE};
-
-static float MaterialRed[] =
-{0.7, 0.0, 0.0, 1.0};
-static float MaterialGreen[] =
-{0.1, 0.5, 0.2, 1.0};
-static float MaterialBlue[] =
-{0.0, 0.0, 0.7, 1.0};
-static float MaterialCyan[] =
-{0.2, 0.5, 0.7, 1.0};
-static float MaterialYellow[] =
-{0.7, 0.7, 0.0, 1.0};
-static float MaterialMagenta[] =
-{0.6, 0.2, 0.5, 1.0};
-static float MaterialWhite[] =
-{0.7, 0.7, 0.7, 1.0};
-static float MaterialGray[] =
-{0.5, 0.5, 0.5, 1.0};
+static const GLfloat front_shininess[] = {60.0};
+static const GLfloat front_specular[]  = {0.7, 0.7, 0.7, 1.0};
+static const GLfloat ambient[]         = {0.0, 0.0, 0.0, 1.0};
+static const GLfloat diffuse[]         = {1.0, 1.0, 1.0, 1.0};
+static const GLfloat position0[]       = {1.0, 1.0, 1.0, 0.0};
+static const GLfloat position1[]       = {-1.0, -1.0, 1.0, 0.0};
+static const GLfloat lmodel_ambient[]  = {0.5, 0.5, 0.5, 1.0};
+static const GLfloat lmodel_twoside[]  = {GL_TRUE};
+
+static const GLfloat MaterialRed[]     = {0.7, 0.0, 0.0, 1.0};
+static const GLfloat MaterialGreen[]   = {0.1, 0.5, 0.2, 1.0};
+static const GLfloat MaterialBlue[]    = {0.0, 0.0, 0.7, 1.0};
+static const GLfloat MaterialCyan[]    = {0.2, 0.5, 0.7, 1.0};
+static const GLfloat MaterialYellow[]  = {0.7, 0.7, 0.0, 1.0};
+static const GLfloat MaterialMagenta[] = {0.6, 0.2, 0.5, 1.0};
+static const GLfloat MaterialWhite[]   = {0.7, 0.7, 0.7, 1.0};
+static const GLfloat MaterialGray[]    = {0.5, 0.5, 0.5, 1.0};
 
 static morph3dstruct *morph3d = (morph3dstruct *) NULL;
 
@@ -202,6 +178,7 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
       NeiBX=Factor2*Xf-VertX; NeiBY=Factor2*Yb-VertY; NeiBZ=Factor2*Zf-VertZ;                                    \
       glNormal3f(VectMul(NeiAX, NeiAY, NeiAZ, NeiBX, NeiBY, NeiBZ));                                             \
       glVertex3f(VertX, VertY, VertZ);                                                                           \
+      mi->polygon_count++;                                                                                       \
                                                                                                                  \
       Xf-=Ax; Yf-=Ay; Xa-=Ax; Yb-=Ay;                                                                            \
                                                                                                                  \
@@ -213,6 +190,7 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
       NeiBX=Factor2*Xf-VertX; NeiBY=Factor2*Yb-VertY; NeiBZ=Factor2*Zf-VertZ;                                    \
       glNormal3f(VectMul(NeiAX, NeiAY, NeiAZ, NeiBX, NeiBY, NeiBZ));                                             \
       glVertex3f(VertX, VertY, VertZ);                                                                           \
+      mi->polygon_count++;                                                                                       \
                                                                                                                  \
       Xf-=Ax; Yf+=Ay; Xa-=Ax; Yb+=Ay;                                                                            \
     }                                                                                                            \
@@ -268,6 +246,7 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
       NeiBX=Factor2*Xf-VertX; NeiBY=Factor2*Yb-VertY; NeiBZ=Factor2*Zf-VertZ;                                    \
       glNormal3f(VectMul(NeiAX, NeiAY, NeiAZ, NeiBX, NeiBY, NeiBZ));                                             \
       glVertex3f(VertX, VertY, VertZ);                                                                           \
+      mi->polygon_count++;                                                                                       \
     }                                                                                                            \
     glEnd();                                                                                                     \
   }                                                                                                              \
@@ -283,15 +262,11 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
   GLfloat   Zf=(Edge)*(Z);                                                                                       \
   GLfloat   AmpVr2=(Amp)/sqr((Edge)*cossec36_2);                                                                 \
                                                                                                                  \
-  static    GLfloat x[6],y[6];                                                                                   \
-  static    int arrayninit=1;                                                                                    \
+  GLfloat x[6],y[6];                                                                                             \
                                                                                                                  \
-  if (arrayninit) {                                                                                              \
-    for(Fi=0;Fi<6;Fi++) {                                                                                        \
-      x[Fi]=-cos( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);                                             \
-      y[Fi]=sin( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);                                              \
-    }                                                                                                            \
-    arrayninit=0;                                                                                                \
+  for(Fi=0;Fi<6;Fi++) {                                                                                          \
+    x[Fi]=-cos( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);                                               \
+    y[Fi]=sin( Fi*2*Pi/5 + Pi/10 )/(Divisions)*cossec36_2*(Edge);                                                \
   }                                                                                                              \
                                                                                                                  \
   for (Ri=1; Ri<=(Divisions); Ri++) {                                                                            \
@@ -301,25 +276,27 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
         Xf=(float)(Ri-Ti)*x[Fi] + (float)Ti*x[Fi+1];                                                             \
         Yf=(float)(Ri-Ti)*y[Fi] + (float)Ti*y[Fi+1];                                                             \
         Xa=Xf+0.001; Yb=Yf+0.001;                                                                                \
-       Factor=1-(((Xf2=sqr(Xf))+(Yf2=sqr(Yf)))*AmpVr2);                                                         \
-       Factor1=1-((sqr(Xa)+Yf2)*AmpVr2);                                                                        \
-       Factor2=1-((Xf2+sqr(Yb))*AmpVr2);                                                                        \
+       Factor=1-(((Xf2=sqr(Xf))+(Yf2=sqr(Yf)))*AmpVr2);                                                             \
+       Factor1=1-((sqr(Xa)+Yf2)*AmpVr2);                                                                            \
+       Factor2=1-((Xf2+sqr(Yb))*AmpVr2);                                                                            \
         VertX=Factor*Xf;        VertY=Factor*Yf;        VertZ=Factor*Zf;                                         \
         NeiAX=Factor1*Xa-VertX; NeiAY=Factor1*Yf-VertY; NeiAZ=Factor1*Zf-VertZ;                                  \
         NeiBX=Factor2*Xf-VertX; NeiBY=Factor2*Yb-VertY; NeiBZ=Factor2*Zf-VertZ;                                  \
         glNormal3f(VectMul(NeiAX, NeiAY, NeiAZ, NeiBX, NeiBY, NeiBZ));                                           \
-       glVertex3f(VertX, VertY, VertZ);                                                                         \
+               glVertex3f(VertX, VertY, VertZ);                                                                         \
+           mi->polygon_count++;                                                                                     \
                                                                                                                  \
         Xf-=x[Fi]; Yf-=y[Fi]; Xa-=x[Fi]; Yb-=y[Fi];                                                              \
                                                                                                                  \
-       Factor=1-(((Xf2=sqr(Xf))+(Yf2=sqr(Yf)))*AmpVr2);                                                         \
-       Factor1=1-((sqr(Xa)+Yf2)*AmpVr2);                                                                        \
-       Factor2=1-((Xf2+sqr(Yb))*AmpVr2);                                                                        \
+               Factor=1-(((Xf2=sqr(Xf))+(Yf2=sqr(Yf)))*AmpVr2);                                                         \
+               Factor1=1-((sqr(Xa)+Yf2)*AmpVr2);                                                                        \
+               Factor2=1-((Xf2+sqr(Yb))*AmpVr2);                                                                        \
         VertX=Factor*Xf;        VertY=Factor*Yf;        VertZ=Factor*Zf;                                         \
         NeiAX=Factor1*Xa-VertX; NeiAY=Factor1*Yf-VertY; NeiAZ=Factor1*Zf-VertZ;                                  \
         NeiBX=Factor2*Xf-VertX; NeiBY=Factor2*Yb-VertY; NeiBZ=Factor2*Zf-VertZ;                                  \
         glNormal3f(VectMul(NeiAX, NeiAY, NeiAZ, NeiBX, NeiBY, NeiBZ));                                           \
-       glVertex3f(VertX, VertY, VertZ);                                                                         \
+               glVertex3f(VertX, VertY, VertZ);                                                                         \
+           mi->polygon_count++;                                                                                     \
                                                                                                                  \
       }                                                                                                          \
       Xf=(float)Ri*x[Fi+1];                                                                                      \
@@ -336,7 +313,7 @@ static morph3dstruct *morph3d = (morph3dstruct *) NULL;
       glEnd();                                                                                                   \
     }                                                                                                            \
   }                                                                                                              \
-  VS=(Factor<0);                                                                                             \
+  VS=(Factor<0);                                                                                                    \
 }
 
 static void
@@ -614,8 +591,8 @@ draw_icosa(ModeInfo * mi)
        TRIANGLE(1.5, mp->seno, mp->edgedivisions, (3 * SQRT3 + SQRT15) / 12, mp->VisibleSpikes);
 }
 
-void
-reshape(ModeInfo * mi, int width, int height)
+ENTRYPOINT void
+reshape_morph3d(ModeInfo * mi, int width, int height)
 {
        morph3dstruct *mp = &morph3d[MI_SCREEN(mi)];
 
@@ -632,7 +609,6 @@ pinit(ModeInfo * mi)
        morph3dstruct *mp = &morph3d[MI_SCREEN(mi)];
 
        glClearDepth(1.0);
-       glClearColor(0.0, 0.0, 0.0, 1.0);
        glColor3f(1.0, 1.0, 1.0);
 
        glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
@@ -738,23 +714,19 @@ pinit(ModeInfo * mi)
        }
 }
 
-void
+ENTRYPOINT void
 init_morph3d(ModeInfo * mi)
 {
        morph3dstruct *mp;
 
-       if (morph3d == NULL) {
-               if ((morph3d = (morph3dstruct *) calloc(MI_NUM_SCREENS(mi),
-                                           sizeof (morph3dstruct))) == NULL)
-                       return;
-       }
+       MI_INIT (mi, morph3d, NULL);
        mp = &morph3d[MI_SCREEN(mi)];
        mp->step = NRAND(90);
        mp->VisibleSpikes = 1;
 
        if ((mp->glx_context = init_GL(mi)) != NULL) {
 
-               reshape(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+               reshape_morph3d(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
                glDrawBuffer(GL_BACK);
                mp->object = MI_COUNT(mi);
                if (mp->object <= 0 || mp->object > 5)
@@ -765,7 +737,7 @@ init_morph3d(ModeInfo * mi)
        }
 }
 
-void
+ENTRYPOINT void
 draw_morph3d(ModeInfo * mi)
 {
        Display    *display = MI_DISPLAY(mi);
@@ -781,6 +753,7 @@ draw_morph3d(ModeInfo * mi)
        if (!mp->glx_context)
                return;
 
+    mi->polygon_count = 0;
        glXMakeCurrent(display, window, *(mp->glx_context));
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -796,6 +769,15 @@ draw_morph3d(ModeInfo * mi)
                glScalef(Scale4Iconic * mp->WindH / mp->WindW, Scale4Iconic, Scale4Iconic);
        }
 
+# ifdef HAVE_MOBILE    /* Keep it the same relative size when rotated. */
+    {
+      GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
+      int o = (int) current_device_rotation();
+      if (o != 0 && o != 180 && o != -180)
+        glScalef (1/h, h, 1);
+    }
+# endif
+
        glRotatef(mp->step * 100, 1, 0, 0);
        glRotatef(mp->step * 95, 0, 1, 0);
        glRotatef(mp->step * 90, 0, 0, 1);
@@ -830,7 +812,8 @@ draw_morph3d(ModeInfo * mi)
        mp->step += 0.05;
 }
 
-void
+#ifndef STANDALONE
+ENTRYPOINT void
 change_morph3d(ModeInfo * mi)
 {
        morph3dstruct *mp = &morph3d[MI_SCREEN(mi)];
@@ -841,15 +824,8 @@ change_morph3d(ModeInfo * mi)
        mp->object = (mp->object) % 5 + 1;
        pinit(mi);
 }
-
-void
-release_morph3d(ModeInfo * mi)
-{
-       if (morph3d != NULL) {
-               (void) free((void *) morph3d);
-               morph3d = (morph3dstruct *) NULL;
-       }
-       FreeAllGL(mi);
-}
+#endif /* !STANDALONE */
 
 #endif
+
+XSCREENSAVER_MODULE ("Morph3D", morph3d)