]> git.sesse.net Git - glcapture/blob - glcapture.c
c67c87d9e5dfb0a29b978b0b2d5d32c0e54f42b5
[glcapture] / glcapture.c
1 /*
2  * Use something like this:
3  *
4  * gcc -shared -fPIC -o glcapture.so glcapture.c -ldl
5  * LD_PRELOAD=`pwd`/glcapture.so ./demo
6  * for FILE in *.ppm; do convert -flip $FILE ${FILE/ppm/tga}; echo $FILE; done
7  * 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
8  * 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
9  */
10
11 #include <stdio.h>
12 #include <GL/glx.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15 #include <dlfcn.h>
16
17 #ifndef RTLD_NEXT
18 #define RTLD_NEXT ((void *) -1l)
19 #endif
20
21 #define WIDTH 640
22 #define HEIGHT 480
23
24 static char buf[WIDTH * HEIGHT * 3];
25 static int frameno = 0;
26
27 void glXSwapBuffers( Display *dpy, GLXDrawable drawable )
28 {
29         void (*real_swap)(Display*, GLXDrawable);
30         real_swap = (void(*)(Display*, GLXDrawable))dlsym(RTLD_NEXT, "glXSwapBuffers");
31
32         FILE *ppm;
33         char filename[256];
34         glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, buf);
35
36         sprintf(filename, "frame%05u.ppm", frameno++);
37         ppm = fopen(filename, "w");
38         fprintf(ppm, "P6\nWIDTH HEIGHT\n255\n");
39         fwrite(buf, WIDTH*HEIGHT*3, 1, ppm);
40         fclose(ppm);
41
42         real_swap(dpy, drawable);
43 }
44
45 int gettimeofday(struct timeval *tv, struct timezone *tz)
46 {
47         printf("gettimeofday frame %u\n", frameno);
48         tv->tv_sec = frameno / 50;
49         tv->tv_usec = ((double)(frameno % 50) / 50) * 1000000;
50         return 0;
51 }