From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / cube21.c
index 5e09af2805b937dfbfaa2cca0890e26cb1ce2543..0cfd2123f62d80091a42030feaa9293f40b676b2 100644 (file)
@@ -26,7 +26,7 @@
 
 /* TODO:
  *  some simple "solve mode"
- *  use rotator and trackball
+ *  use rotator
  */
 
 /*-
                    "*wireframe:     False         \n"
 
 # define refresh_cube21 0
-# define cube21_handle_event 0
+# define release_cube21 0
 #include "xlockmore.h"
 
+#include "gltrackball.h"
+
 #ifdef USE_GL
 
-#define DEF_XYSPEED   "1.0"
-#define DEF_TSPEED    "3.0"
-#define DEF_WSPEED    "1.0"
-#define DEF_TWAIT     "40.0"
-#define DEF_SIZE      "0.7"
-#define DEF_COLMODE   "six"
+#define DEF_SPIN        "True"
+#define DEF_WANDER      "True"
+#define DEF_TEXTURE     "True"
+#define DEF_RANDOMIZE   "True"
+#define DEF_SPINSPEED   "1.0"
+#define DEF_ROTSPEED    "3.0"
+#define DEF_WANDERSPEED "0.02"
+#define DEF_WAIT        "40.0"
+#define DEF_CUBESIZE    "0.7"
+#define DEF_COLORMODE   "six"
 
 #ifdef Pi
 #undef Pi
@@ -90,16 +96,16 @@ static char *colmode_s;
 static int colmode;
 
 static argtype vars[] = {
-  { &spin,      "spin",        "Spin",        "True",      t_Bool},
-  { &wander,    "wander",      "Wander",      "True",      t_Bool},
-  { &rndstart,  "randomize",   "Randomize",   "True",      t_Bool},
-  { &tex,       "texture",     "Texture",     "True",      t_Bool},
-  { &spinspeed, "spinspeed",   "SpinSpeed",   DEF_XYSPEED, t_Float},
-  { &tspeed,    "rotspeed",    "RotSpeed",    DEF_TSPEED,  t_Float},
-  { &wspeed,    "wanderspeed", "WanderSpeed", DEF_WSPEED,  t_Float},
-  { &twait,     "wait",        "Wait",        DEF_TWAIT,   t_Float},
-  { &size,      "cubesize",    "CubeSize",    DEF_SIZE,    t_Float},
-  { &colmode_s, "colormode",   "ColorMode",   DEF_COLMODE, t_String}
+  { &spin,      "spin",        "Spin",        DEF_SPIN,        t_Bool},
+  { &wander,    "wander",      "Wander",      DEF_WANDER,      t_Bool},
+  { &rndstart,  "randomize",   "Randomize",   DEF_RANDOMIZE,   t_Bool},
+  { &tex,       "texture",     "Texture",     DEF_TEXTURE,     t_Bool},
+  { &spinspeed, "spinspeed",   "SpinSpeed",   DEF_SPINSPEED,   t_Float},
+  { &tspeed,    "rotspeed",    "RotSpeed",    DEF_ROTSPEED,    t_Float},
+  { &wspeed,    "wanderspeed", "WanderSpeed", DEF_WANDERSPEED, t_Float},
+  { &twait,     "wait",        "Wait",        DEF_WAIT,        t_Float},
+  { &size,      "cubesize",    "CubeSize",    DEF_CUBESIZE,    t_Float},
+  { &colmode_s, "colormode",   "ColorMode",   DEF_COLORMODE,   t_String}
 };
 
 static XrmOptionDescRec opts[] = {
@@ -116,14 +122,14 @@ static XrmOptionDescRec opts[] = {
   { "-rotspeed",    ".rotspeed",    XrmoptionSepArg, 0 },
   { "-wait",        ".wait",        XrmoptionSepArg, 0 },
   { "-cubesize",    ".cubesize",    XrmoptionSepArg, 0 },
-  { "-colormode",   ".colormode",   XrmoptionSepArg, DEF_COLMODE }
+  { "-colormode",   ".colormode",   XrmoptionSepArg, 0 }
 };
 
 ENTRYPOINT ModeSpecOpt cube21_opts = {countof(opts), opts, countof(vars), vars, NULL};
 
 #ifdef USE_MODULES
 ModStruct   cube21_description =
-{ "cube21", "init_cube21", "draw_cube21", "release_cube21",
+{ "cube21", "init_cube21", "draw_cube21", NULL,
   "draw_cube21", "change_cube21", NULL, &cube21_opts,
   25000, 1, 1, 1, 1.0, 4, "",
   "Shows randomly shuffling Cube 21", 0, NULL
@@ -174,6 +180,9 @@ typedef struct {
   GLfloat texp, texq, posc[6];
   GLfloat color_inner[4];
 
+  Bool button_down_p;
+  trackball_state *trackball;
+
 } cube21_conf;
 
 static cube21_conf *cube21 = NULL;
@@ -317,7 +326,7 @@ static void finish(cube21_conf *cp)
   cp->t = 0;
 }
 
-static void draw_narrow_piece(cube21_conf *cp, GLfloat s, int c1, int c2, col_t colors) 
+static void draw_narrow_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, col_t colors) 
 {
   GLfloat s1 = cp->posc[0]*s;
   glBegin(GL_TRIANGLES);
@@ -326,12 +335,14 @@ static void draw_narrow_piece(cube21_conf *cp, GLfloat s, int c1, int c2, col_t
   glTexCoord2f(0.5, 0.5);  glVertex3f(0.0, 0.0, s);
   glTexCoord2f(cp->texq, 0.0); glVertex3f(cp->posc[1], 0.0, s);
   glTexCoord2f(cp->texp, 0.0); glVertex3f(cp->posc[2], cp->posc[3], s);
+  mi->polygon_count++;
   glNormal3f(0.0, 0.0, -s);
   if(cp->cmat) glColor3fv(cp->color_inner);
   glTexCoord2f(TEX_GRAY);
   glVertex3f(0.0, 0.0, s1);
   glVertex3f(cp->posc[1], 0.0, s1);
   glVertex3f(cp->posc[2], cp->posc[3], s1);
+  mi->polygon_count++;
   glEnd();
   glBegin(GL_QUADS);
   glNormal3f(0.0, -1.0, 0.0);
@@ -341,12 +352,14 @@ static void draw_narrow_piece(cube21_conf *cp, GLfloat s, int c1, int c2, col_t
   glVertex3f(cp->posc[1], 0.0, s);
   glVertex3f(cp->posc[1], 0.0, s1);
   glVertex3f(0.0, 0.0, s1);
+  mi->polygon_count++;
   glNormal3f(COS15, SIN15, 0.0);
   if(cp->cmat) glColor3fv(colors[c2]);
   glTexCoord2f(cp->texq, cp->texq); glVertex3f(cp->posc[1], 0.0, s);
   glTexCoord2f(cp->texq, cp->texp); glVertex3f(cp->posc[2], cp->posc[3], s);
   glTexCoord2f(1.0, cp->texp);  glVertex3f(cp->posc[2], cp->posc[3], s1);
   glTexCoord2f(1.0, cp->texq);  glVertex3f(cp->posc[1], 0.0, s1);
+  mi->polygon_count++;
   glNormal3f(-SIN30, COS30, 0.0);
   if(cp->cmat) glColor3fv(cp->color_inner);
   glTexCoord2f(TEX_GRAY);
@@ -354,11 +367,12 @@ static void draw_narrow_piece(cube21_conf *cp, GLfloat s, int c1, int c2, col_t
   glVertex3f(cp->posc[2], cp->posc[3], s);
   glVertex3f(cp->posc[2], cp->posc[3], s1);
   glVertex3f(0.0, 0.0, s1);
+  mi->polygon_count++;
   glEnd();
   glRotatef(30.0, 0.0, 0.0, 1.0);
 }
 
-static void draw_wide_piece(cube21_conf *cp, GLfloat s, int c1, int c2, int c3, col_t colors) 
+static void draw_wide_piece(ModeInfo *mi, cube21_conf *cp, GLfloat s, int c1, int c2, int c3, col_t colors) 
 {
   GLfloat s1 = cp->posc[0]*s;
   glBegin(GL_TRIANGLES);
@@ -466,32 +480,32 @@ static void draw_middle(cube21_conf *cp)
   if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
 }
 
-static void draw_half_face(cube21_conf *cp, int s, int o) 
+static void draw_half_face(ModeInfo *mi, cube21_conf *cp, int s, int o) 
 {
   int i, s1 = 1-s*2, s2 = s*2;
   for(i = o; i<o+6; i++) {
     if(cp->pieces[s][i+1])
-      draw_narrow_piece(cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
+      draw_narrow_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->colors);
     else {
-      draw_wide_piece(cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
+      draw_wide_piece(mi, cp, s1, cp->cind[s2][i], cp->cind[s2+1][i], cp->cind[s2+1][i+1], cp->colors);
       i++;
     }
   }
 }
 
-static void draw_top_face(cube21_conf *cp) 
+static void draw_top_face(ModeInfo *mi, cube21_conf *cp) 
 {
-  draw_half_face(cp, 0, 0);
-  draw_half_face(cp, 0, 6);
+  draw_half_face(mi, cp, 0, 0);
+  draw_half_face(mi, cp, 0, 6);
 }
 
-static void draw_bottom_face(cube21_conf *cp) 
+static void draw_bottom_face(ModeInfo *mi, cube21_conf *cp) 
 {
-  draw_half_face(cp, 1, 0);
-  draw_half_face(cp, 1, 6);
+  draw_half_face(mi, cp, 1, 0);
+  draw_half_face(mi, cp, 1, 6);
 }
 
-static Bool draw_main(cube21_conf *cp) 
+static Bool draw_main(ModeInfo *mi, cube21_conf *cp) 
 {
   GLfloat theta = cp->ramount<0?cp->t:-cp->t;
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -501,36 +515,49 @@ static Bool draw_main(cube21_conf *cp)
   else
     glTranslatef(0, 0, zpos);
   glScalef(size, size, size);
+
+# 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, 1/h, 1/h);
+  }
+# endif
+
   glRotatef(cp->xrot, 1.0, 0.0, 0.0);
   glRotatef(cp->yrot, 0.0, 1.0, 0.0);
+
+  gltrackball_rotate (cp->trackball);
+
   if(cp->wire) glColor3f(0.7, 0.7, 0.7);
   switch(cp->state) {
     case CUBE21_PAUSE1:
     case CUBE21_PAUSE2:
-      draw_top_face(cp);
-      draw_bottom_face(cp);
+      draw_top_face(mi, cp);
+      draw_bottom_face(mi, cp);
       draw_middle(cp);
       break;
     case CUBE21_ROT_TOP:
       glRotatef(theta, 0.0, 0.0, 1.0);
-      draw_top_face(cp);
+      draw_top_face(mi, cp);
       glRotatef(-theta, 0.0, 0.0, 1.0);
-      draw_bottom_face(cp);
+      draw_bottom_face(mi, cp);
       draw_middle(cp);
       break;
     case CUBE21_ROT_BOTTOM:
-      draw_top_face(cp);
+      draw_top_face(mi, cp);
       glRotatef(theta, 0.0, 0.0, 1.0);
-      draw_bottom_face(cp);
+      draw_bottom_face(mi, cp);
       glRotatef(-theta, 0.0, 0.0, 1.0);
       draw_middle(cp);
       break;
     case CUBE21_HALF1:
       glRotatef(theta, 0.0, 1.0, 0.0);
     case CUBE21_HALF2:
-      draw_half_face(cp, 0, 0);
+      draw_half_face(mi, cp, 0, 0);
       glRotatef(-180.0, 0.0, 0.0, 1.0);
-      draw_half_face(cp, 1, 0);
+      draw_half_face(mi, cp, 1, 0);
       glRotatef(-180.0, 0.0, 0.0, 1.0);
       if(cp->hf[0]) glRotatef(180.0, 0.0, 1.0, 0.0);
       draw_middle_piece(cp, 0, cp->cind, cp->colors);
@@ -540,9 +567,9 @@ static Bool draw_main(cube21_conf *cp)
       else
         glRotatef(theta, 0.0, 1.0, 0.0);
       glRotatef(180.0, 0.0, 0.0, 1.0);
-      draw_half_face(cp, 0, 6);
+      draw_half_face(mi, cp, 0, 6);
       glRotatef(-180.0, 0.0, 0.0, 1.0);
-      draw_half_face(cp, 1, 6);
+      draw_half_face(mi, cp, 1, 6);
       glRotatef(-180.0, 0.0, 0.0, 1.0);
       if(cp->hf[1]) glRotatef(180.0, 0.0, 1.0, 0.0);
       draw_middle_piece(cp, 1, cp->cind, cp->colors);
@@ -683,6 +710,11 @@ static void init_gl(ModeInfo *mi)
     tex = False;
     cp->cmat = False;
   }
+
+# ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
+  cp->wire = 0;
+# endif
+
   if(cp->wire) {
     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
     return;
@@ -693,7 +725,6 @@ static void init_gl(ModeInfo *mi)
     cp->color_inner[0] = cp->color_inner[1] = cp->color_inner[2] = 1.0;
 
   glClearDepth(1.0);
-  glClearColor(0.0, 0.0, 0.0, 1.0);
   glDrawBuffer(GL_BACK);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glShadeModel(GL_FLAT);
@@ -825,36 +856,37 @@ ENTRYPOINT void reshape_cube21(ModeInfo *mi, int width, int height)
   glClear(GL_COLOR_BUFFER_BIT);
 }
 
-ENTRYPOINT void release_cube21(ModeInfo *mi) 
+ENTRYPOINT Bool
+cube21_handle_event (ModeInfo *mi, XEvent *event)
 {
-  if (cube21 != NULL) {
-    int screen;
-    for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-      cube21_conf *cp = &cube21[screen];
-      if (cp->glx_context) {
-        cp->glx_context = NULL;
-      }
-    }
-    free((void *)cube21);
-    cube21 = NULL;
-  }
-  FreeAllGL(mi);
+  cube21_conf *cp = &cube21[MI_SCREEN(mi)];
+
+  if (gltrackball_event_handler (event, cp->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &cp->button_down_p))
+    return True;
+
+  return False;
 }
 
+
 ENTRYPOINT void init_cube21(ModeInfo *mi) 
 {
   cube21_conf *cp;
-  if(!cube21) {
-    cube21 = (cube21_conf *)calloc(MI_NUM_SCREENS(mi), sizeof(cube21_conf));
-    if(!cube21) return;
-  }
+  MI_INIT(mi, cube21, NULL);
   cp = &cube21[MI_SCREEN(mi)];
 
+  cp->trackball = gltrackball_init (False);
+
   if(!cp->texp) {
     init_posc(cp);
     make_texture(cp);
   }
 
+#ifdef HAVE_MOBILE
+  size *= 2;
+#endif
+
   if ((cp->glx_context = init_GL(mi)) != NULL) {
     init_gl(mi);
     init_cp(cp);
@@ -873,9 +905,10 @@ ENTRYPOINT void draw_cube21(ModeInfo * mi)
   cp = &cube21[MI_SCREEN(mi)];
   MI_IS_DRAWN(mi) = True;
   if (!cp->glx_context) return;
+  mi->polygon_count = 0;
   glXMakeCurrent(display, window, *(cp->glx_context));
-  if (!draw_main(cp)) {
-    release_cube21(mi);
+  if (!draw_main(mi, cp)) {
+    MI_ABORT(mi);
     return;
   }
   if (MI_IS_FPS(mi)) do_fps (mi);