From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / glx / glmatrix.c
index 55eda8ebc5fe472103b3616915259e8b6ad69b7b..6a9b1fe18529c1c1c42bd23571a6e5774198faad 100644 (file)
@@ -1,4 +1,4 @@
-/* glmatrix, Copyright (c) 2003, 2004 Jamie Zawinski <jwz@jwz.org>
+/* glmatrix, Copyright (c) 2003-2018 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
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -20,7 +20,7 @@
                        "*showFPS:      False         \n" \
                        "*wireframe:    False         \n" \
 
                        "*showFPS:      False         \n" \
                        "*wireframe:    False         \n" \
 
-# define refresh_matrix 0
+# define free_matrix 0
 # define release_matrix 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 # define release_matrix 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
 
 #include "xlockmore.h"
 #define BELLRAND(n) ((frand((n)) + frand((n)) + frand((n))) / 3)
 
 #include "xlockmore.h"
-#include "xpm-ximage.h"
+#include "ximage-loader.h"
 
 
-#ifdef __GNUC__
-  __extension__  /* don't warn about "string length is greater than the length
-                    ISO C89 compilers are required to support" when including
-                    the following XPM file... */
-#endif
-#include "../images/matrix3.xpm"
+#include "images/gen/matrix3_png.h"
 
 #ifdef USE_GL /* whole file */
 
 
 #ifdef USE_GL /* whole file */
 
@@ -51,8 +46,6 @@
 #define DEF_MODE        "Matrix"
 #define DEF_TIMEFMT     " %l%M%p "
 
 #define DEF_MODE        "Matrix"
 #define DEF_TIMEFMT     " %l%M%p "
 
-#include "gllist.h"
-
 
 #define CHAR_COLS 16
 #define CHAR_ROWS 13
 
 #define CHAR_COLS 16
 #define CHAR_ROWS 13
@@ -401,15 +394,17 @@ draw_glyph (ModeInfo *mi, int glyph, Bool highlight,
     }
 
   {
     }
 
   {
-    GLfloat r, g, b, a = 1;
+    GLfloat r, g, b, a;
 
     if (highlight)
       brightness *= 2;
 
     if (!do_texture && !spinner_p)
 
     if (highlight)
       brightness *= 2;
 
     if (!do_texture && !spinner_p)
-      r = b = 0, g = brightness;
+      r = b = 0, g = 1;
     else
     else
-      r = g = b = brightness;
+      r = g = b = 1;
+
+    a = brightness;
 
     /* If the glyph is very close to the screen (meaning it is very large,
        and is about to splash into the screen and vanish) then start fading
 
     /* If the glyph is very close to the screen (meaning it is very large,
        and is about to splash into the screen and vanish) then start fading
@@ -424,23 +419,7 @@ draw_glyph (ModeInfo *mi, int glyph, Bool highlight,
         if (i < 0) i = 0;
         else if (i >= WAVE_SIZE) i = WAVE_SIZE-1; 
 
         if (i < 0) i = 0;
         else if (i >= WAVE_SIZE) i = WAVE_SIZE-1; 
 
-        a = mp->brightness_ramp[i];
-#if 1
-        /* I don't understand this -- if I change the alpha on the color of
-           the quad, I'd expect that to make the quad more transparent.
-           But instead, it seems to be making the transparent parts of the
-           texture on the quad be *less* transparent!  So as we fade out,
-           we fade towards a completely solid rectangle.  WTF?
-
-           So, for now, instead of changing the alpha, just make the colors
-           be darker.  This works out ok so long as we use GL_ONE in
-           glBlendFunc, so that stacked glyph colors are added together.
-         */
-        r *= a;
-        g *= a;
-        b *= a;
-        a = 1;
-#endif
+        a *= mp->brightness_ramp[i];
       }
 
     glColor4f (r,g,b,a);
       }
 
     glColor4f (r,g,b,a);
@@ -588,8 +567,15 @@ ENTRYPOINT void
 reshape_matrix (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
 reshape_matrix (ModeInfo *mi, int width, int height)
 {
   GLfloat h = (GLfloat) height / (GLfloat) width;
+  int y = 0;
 
 
-  glViewport (0, 0, (GLint) width, (GLint) height);
+  if (width > height * 5) {   /* tiny window: show middle */
+    height = width * 9/16;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
+
+  glViewport (0, y, (GLint) width, (GLint) height);
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
 
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
@@ -648,6 +634,10 @@ bigendian (void)
 
    If this hack ever grows into something that displays full Latin1 text,
    well then, Something Else Will Need To Be Done.
 
    If this hack ever grows into something that displays full Latin1 text,
    well then, Something Else Will Need To Be Done.
+
+   Since currently GLMatrix does not run textclient / xscreensaver-text,
+   it's not an issue.  (XMatrix does that.)
+
  */
 static void
 spank_image (matrix_configuration *mp, XImage *xi)
  */
 static void
 spank_image (matrix_configuration *mp, XImage *xi)
@@ -708,11 +698,11 @@ load_textures (ModeInfo *mi, Bool flip_p)
   int cw, ch;
   int orig_w, orig_h;
 
   int cw, ch;
   int orig_w, orig_h;
 
-  /* The Matrix XPM is 512x598 -- but GL texture sizes must be powers of 2.
+  /* The Matrix image is 512x598 -- but GL texture sizes must be powers of 2.
      So we waste some padding rows to round up.
    */
      So we waste some padding rows to round up.
    */
-  xi = xpm_to_ximage (mi->dpy, mi->xgwa.visual, mi->xgwa.colormap,
-                      matrix3_xpm);
+  xi = image_data_to_ximage (mi->dpy, mi->xgwa.visual, 
+                             matrix3_png, sizeof(matrix3_png));
   orig_w = xi->width;
   orig_h = xi->height;
   mp->real_char_rows = CHAR_ROWS;
   orig_w = xi->width;
   orig_h = xi->height;
   mp->real_char_rows = CHAR_ROWS;
@@ -781,7 +771,7 @@ load_textures (ModeInfo *mi, Bool flip_p)
           unsigned char r = (p >> rpos) & 0xFF;
           unsigned char g = (p >> gpos) & 0xFF;
           unsigned char b = (p >> bpos) & 0xFF;
           unsigned char r = (p >> rpos) & 0xFF;
           unsigned char g = (p >> gpos) & 0xFF;
           unsigned char b = (p >> bpos) & 0xFF;
-          unsigned char a = ~g;
+          unsigned char a = g;
           g = 0xFF;
           p = (r << rpos) | (g << gpos) | (b << bpos) | (a << apos);
           XPutPixel (xi, x, y, p);
           g = 0xFF;
           p = (r << rpos) | (g << gpos) | (b << bpos) | (a << apos);
           XPutPixel (xi, x, y, p);
@@ -794,11 +784,12 @@ load_textures (ModeInfo *mi, Bool flip_p)
   glGenTextures (1, &mp->texture);
 
   glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
   glGenTextures (1, &mp->texture);
 
   glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
-  glPixelStorei (GL_UNPACK_ROW_LENGTH, xi->width);
+  /* messes up -fps */
+  /* glPixelStorei (GL_UNPACK_ROW_LENGTH, xi->width);*/
   glBindTexture (GL_TEXTURE_2D, mp->texture);
   check_gl_error ("texture init");
   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, xi->width, xi->height, 0, GL_RGBA,
   glBindTexture (GL_TEXTURE_2D, mp->texture);
   check_gl_error ("texture init");
   glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, xi->width, xi->height, 0, GL_RGBA,
-                GL_UNSIGNED_INT_8_8_8_8_REV, xi->data);
+                GL_UNSIGNED_BYTE, xi->data);
   {
     char buf[255];
     sprintf (buf, "creating %dx%d texture:", xi->width, xi->height);
   {
     char buf[255];
     sprintf (buf, "creating %dx%d texture:", xi->width, xi->height);
@@ -833,14 +824,7 @@ init_matrix (ModeInfo *mi)
   if (wire)
     do_texture = False;
 
   if (wire)
     do_texture = False;
 
-  if (!mps) {
-    mps = (matrix_configuration *)
-      calloc (MI_NUM_SCREENS(mi), sizeof (matrix_configuration));
-    if (!mps) {
-      fprintf(stderr, "%s: out of memory\n", progname);
-      exit(1);
-    }
-  }
+  MI_INIT (mi, mps);
 
   mp = &mps[MI_SCREEN(mi)];
   mp->glx_context = init_GL(mi);
 
   mp = &mps[MI_SCREEN(mi)];
   mp->glx_context = init_GL(mi);
@@ -895,18 +879,7 @@ init_matrix (ModeInfo *mi)
   glEnable(GL_NORMALIZE);
 
   if (do_texture)
   glEnable(GL_NORMALIZE);
 
   if (do_texture)
-    {
-      load_textures (mi, flip_p);
-      glEnable(GL_TEXTURE_2D);
-      glEnable(GL_BLEND);
-
-      /* Jeff Epler points out:
-         By using GL_ONE instead of GL_SRC_ALPHA, glyphs are added to
-         each other, so that a bright glyph with a darker one in front
-         is a little brighter than the bright glyph alone.
-       */
-      glBlendFunc (GL_ONE_MINUS_SRC_ALPHA, /* GL_SRC_ALPHA */ GL_ONE);
-    }
+    load_textures (mi, flip_p);
 
   /* to scale coverage-percent to strips, this number looks about right... */
   mp->nstrips = (int) (density * 2.2);
 
   /* to scale coverage-percent to strips, this number looks about right... */
   mp->nstrips = (int) (density * 2.2);
@@ -1018,6 +991,20 @@ draw_matrix (ModeInfo *mi)
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
   glPushMatrix ();
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
   glPushMatrix ();
+  glRotatef(current_device_rotation(), 0, 0, 1);
+
+  if (do_texture)
+    {
+      glEnable(GL_TEXTURE_2D);
+      glEnable(GL_BLEND);
+
+      /* Jeff Epler points out:
+         By using GL_ONE instead of GL_SRC_ONE_MINUS_ALPHA, glyphs are
+         added to each other, so that a bright glyph with a darker one
+         in front is a little brighter than the bright glyph alone.
+       */
+      glBlendFunc (GL_SRC_ALPHA, GL_ONE);
+    }
 
   if (do_rotate)
     {
 
   if (do_rotate)
     {