+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ glGenTextures(1, &gflux->texName);
+ glBindTexture(GL_TEXTURE_2D, gflux->texName);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ 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);
+ check_gl_error("texture parameter");
+
+ /* Bail out if the texture is too large. */
+ {
+ GLint width;
+ glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, gflux->imageWidth,
+ gflux->imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
+ glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0,
+ GL_TEXTURE_WIDTH, &width);
+ if (width <= 0)
+ {
+ glGetIntegerv (GL_MAX_TEXTURE_SIZE, &width);
+ fprintf (stderr,
+ "%s: texture too large (%dx%d -- probable max %dx%d)\n",
+ progname, gflux->imageWidth, gflux->imageHeight,
+ width, width);
+ return;
+ }
+ }
+
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gflux->imageWidth,
+ gflux->imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, gflux->image);
+ check_gl_error("texture creation");
+}
+
+#undef presult
+#endif
+
+
+void
+grabTexture(void)
+{
+ int real_width = gflux->modeinfo->xgwa.width;
+ int real_height = gflux->modeinfo->xgwa.height;
+ XImage *ximage = screen_to_ximage (gflux->modeinfo->xgwa.screen,
+ gflux->window,
+ NULL);
+ 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 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;
+
+ x2 = XCreateImage (dpy, v, 32, ZPixmap, 0, 0, size, size, 32, 0);
+ xoff = (real_width > size ? (random() % (real_width - size)) : 0);
+ yoff = (real_height > size ? (random() % (real_height - size)) : 0);
+
+# if 0
+ fprintf(stderr, "%s: cropping texture from %dx%d to %dx%d @ %d,%d\n",
+ progname, ximage->width, ximage->height, x2->width, x2->height,
+ xoff, yoff);
+# endif
+ x2->data = ximage->data; /* we can reuse the same array */
+ for (y = 0; y < x2->height; y++)
+ for (x = 0; x < x2->width; x++)
+ XPutPixel (x2, x, y, XGetPixel (ximage, x+xoff, y+yoff));
+
+ real_width = real_height = real_size;
+ ximage->data = 0;
+ XDestroyImage (ximage);
+ ximage = x2;
+ }
+
+ /* 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, width-1, i, gray);
+ }
+ for (i = 0; i < real_width; i++)
+ {
+ XPutPixel (ximage, i, 0, gray);
+ XPutPixel (ximage, i, height-1, gray);
+ }
+ }
+
+ gflux->imageWidth = ximage->width;
+ gflux->imageHeight = ximage->height;
+ gflux->image = (GLubyte *) ximage->data;
+
+ 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);