ftp://netsw.org/x11/tools/desktop/xscreensaver-4.07.tar.gz
[xscreensaver] / hacks / glx / gflux.c
index 2e9f067037b0564e01beff16234688a488073f46..f45e9ed7f4ab8b3b36a6e218b1ee9a7a720a0d3f 100644 (file)
@@ -35,6 +35,7 @@
  * 21 July 2000 : cleaned up code from bug hunts, manpage written
  * 24 November 2000 : fixed x co-ord calculation in solid - textured
  * 05 March 2001 : put back non pnmlib code with #ifdefs
+ * 11 May 2002 : fixed image problems with large images
  */
 
 
 
 #ifdef USE_GL /* whole file */
 
-#ifdef HAVE_XPM
-# include <X11/xpm.h>
-# ifndef PIXEL_ALREADY_TYPEDEFED
-# define PIXEL_ALREADY_TYPEDEFED /* Sigh, Xmu/Drawing.h needs this... */
-# endif
-#endif
-
 #ifdef HAVE_XMU
 # ifndef VMS
 #  include <X11/Xmu/Drawing.h>
@@ -428,9 +422,11 @@ void initTexture(void)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+
+    clear_gl_error();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, gflux->imageWidth,
                        gflux->imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, *(gflux->image));
-       
+    check_gl_error("texture");
 }
 
 #else /* HAVE_PPM FALSE */
@@ -581,18 +577,21 @@ grabTexture(void)
   int real_height = gflux->modeinfo->xgwa.height;
   XImage *ximage = screen_to_ximage (gflux->modeinfo->xgwa.screen,
                                      gflux->window);
+  Bool bigimage = False;
+  int size = 0;
 
   if (ximage->width > 1280 ||   /* that's too damned big... */
       ximage->height > 1280)
     {
       Display *dpy = gflux->modeinfo->dpy;
       Visual *v = gflux->modeinfo->xgwa.visual;
-      int size = (ximage->width < ximage->height ?
-                  ximage->width : ximage->height);
       int real_size = (ximage->width < ximage->height ?
                        real_width : real_height);
       XImage *x2;
       int x, y, xoff, yoff;
+      size = (ximage->width < ximage->height ?
+              ximage->width : ximage->height);
+      bigimage = True;
 
       if (size > 1024) size = 1024;
 
@@ -619,16 +618,18 @@ grabTexture(void)
   /* Add a border. */
   {
     unsigned long gray = 0xAAAAAAAAL;  /* so shoot me */
+    int width  = (bigimage ? size : real_width);
+    int height = (bigimage ? size : real_height);
     int i;
     for (i = 0; i < real_height; i++)
       {
         XPutPixel (ximage, 0, i, gray);
-        XPutPixel (ximage, real_width-1, i, gray);
+        XPutPixel (ximage, width-1, i, gray);
       }
     for (i = 0; i < real_width; i++)
       {
         XPutPixel (ximage, i, 0, gray);
-        XPutPixel (ximage, i, real_height-1, gray);
+        XPutPixel (ximage, i, height-1, gray);
       }
   }
 
@@ -636,8 +637,16 @@ grabTexture(void)
   gflux->imageHeight = ximage->height;
   gflux->image = ximage->data;
 
-  gflux->tex_xscale = ((GLfloat) real_width  / (GLfloat) ximage->width);
-  gflux->tex_yscale = ((GLfloat) real_height / (GLfloat) ximage->height);
+  if (bigimage)  /* don't scale really large images */
+    {
+      gflux->tex_xscale = 1;
+      gflux->tex_yscale = 1;
+    }
+  else
+    {
+      gflux->tex_xscale = ((GLfloat) real_width  / (GLfloat) ximage->width);
+      gflux->tex_yscale = ((GLfloat) real_height / (GLfloat) ximage->height);
+    }
 
   ximage->data = 0;
   XDestroyImage (ximage);
@@ -903,9 +912,9 @@ void calcGrid(void)
     tmp = 1.0/((double)_waveChange);
     if(!(counter%_waveChange)) {
         newWave = ((int)(counter*tmp))%_waves;
-        gflux->dispx[newWave] = 1.0 - ((double)random())/RAND_MAX;
-        gflux->dispy[newWave] = 1.0 - ((double)random())/RAND_MAX;
-        gflux->freq[newWave] = _waveFreq * ((float)random())/RAND_MAX;
+        gflux->dispx[newWave] = -frand(1.0);
+        gflux->dispy[newWave] = -frand(1.0);
+        gflux->freq[newWave] = _waveFreq * frand(1.0);
         gflux->wa[newWave] = 0.0;
     }
     counter++;