--- /dev/null
+/*
+ * Use something like this:
+ *
+ * gcc -shared -fPIC -o glcapture.so glcapture.c -ldl
+ * LD_PRELOAD=`pwd`/glcapture.so ./demo
+ * for FILE in *.ppm; do convert -flip $FILE ${FILE/ppm/tga}; echo $FILE; done
+ * mencoder -noskip -endpos 76.920 -ovc x264 -x264encopts bitrate=3000:pass=1 -audiofile data/Juledemo.ogg -o tufs.avi mf://\*.tga -mf fps=50 -oac mp3lame -lameopts cbr:br=128
+ * mencoder -noskip -endpos 76.920 -ovc x264 -x264encopts bitrate=3000:pass=2 -audiofile data/Juledemo.ogg -o tufs.avi mf://\*.tga -mf fps=50 -oac mp3lame -lameopts cbr:br=128
+ */
+
+#include <stdio.h>
+#include <GL/glx.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <dlfcn.h>
+
+#ifndef RTLD_NEXT
+#define RTLD_NEXT ((void *) -1l)
+#endif
+
+static char buf[640 * 480 * 3];
+static int frameno = 0;
+
+void glXSwapBuffers( Display *dpy, GLXDrawable drawable )
+{
+ void (*real_swap)(Display*, GLXDrawable);
+ real_swap = (void(*)(Display*, GLXDrawable))dlsym(RTLD_NEXT, "glXSwapBuffers");
+
+ FILE *ppm;
+ char filename[256];
+ glReadPixels(0, 0, 640, 480, GL_RGB, GL_UNSIGNED_BYTE, buf);
+
+ sprintf(filename, "frame%05u.ppm", frameno++);
+ ppm = fopen(filename, "w");
+ fprintf(ppm, "P6\n640 480\n255\n");
+ fwrite(buf, 640*480*3, 1, ppm);
+ fclose(ppm);
+
+ real_swap(dpy, drawable);
+}
+
+int gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+ printf("gettimeofday frame %u\n", frameno);
+ tv->tv_sec = frameno / 50;
+ tv->tv_usec = ((double)(frameno % 50) / 50) * 1000000;
+ return 0;
+}