]> git.sesse.net Git - movit/blob - test.cpp
Small Makefile cleanup to prepare for more files.
[movit] / test.cpp
1 #define GL_GLEXT_PROTOTYPES 1
2 #define NO_SDL_GLEXT 1
3
4 #define WIDTH 1280
5 #define HEIGHT 720
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 #define HSV_WHEEL_SIZE 128
9
10 #include <math.h>
11 #include <time.h>
12
13 #include <string>
14 #include <vector>
15
16 #include <SDL/SDL.h>
17 #include <SDL/SDL_opengl.h>
18 #include <SDL/SDL_image.h>
19
20 #include <GL/gl.h>
21 #include <GL/glext.h>
22
23 #include "util.h"
24
25 #ifdef NDEBUG
26 #define check_error()
27 #else
28 #define check_error() { int err = glGetError(); if (err != GL_NO_ERROR) { printf("GL error 0x%x at line %d\n", err, __LINE__); exit(1); } }
29 #endif
30
31 unsigned char result[WIDTH * HEIGHT * 4];
32
33 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
34 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
35 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
36 float saturation = 1.0f;
37
38 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
39 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
40 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
41
42 struct ImageFormat {
43         enum { FORMAT_RGB, FORMAT_RGBA } format;
44
45         // Note: sRGB and 709 use the same colorspace primaries.
46         enum { COLORSPACE_sRGB, COLORSPACE_REC_601_525, COLORSPACE_REC_601_625, COLORSPACE_REC_709 } color_space;
47
48         // Note: Rec. 601 and 709 use the same gamma curve.
49         enum { LINEAR_LIGHT, GAMMA_sRGB, GAMMA_REC_601, GAMMA_REC_709 } gamma_curve;
50 };
51
52 enum EffectId {
53         // Mostly for internal use.
54         GAMMA_CONVERSION = 0,
55         RGB_PRIMARIES_CONVERSION,
56
57         // Color.
58         LIFT_GAMMA_GAIN,
59 };
60
61 class Effect {
62 public: 
63         virtual bool needs_linear_light() { return true; }
64         virtual bool needs_srgb_primaries() { return true; }
65         virtual bool needs_many_samples() { return false; }
66         virtual bool needs_mipmaps() { return false; }
67         bool set_float(const std::string& key, float value);
68         bool set_float_array(const std::string&, const float *values, size_t num_values);
69
70 private:
71         bool register_float(const std::string& key, float value);
72         bool register_float_array(const std::string& key, float *values, size_t num_values);
73 };      
74
75 class EffectChain {
76 public:
77         void set_size(unsigned width, unsigned height);
78         void add_input(const ImageFormat &format);
79         Effect *add_effect(EffectId effect);
80         void add_output(const ImageFormat &format);
81
82         void render(unsigned char *src, unsigned char *dst);
83
84 private:
85         unsigned width, height;
86         ImageFormat input_format, output_format;
87         std::vector<Effect *> effects;
88 };
89
90 enum textures {
91         SOURCE_IMAGE = 1,
92         SRGB_LUT = 2,
93         SRGB_REVERSE_LUT = 3,
94         HSV_WHEEL = 4,
95 };
96
97 GLhandleARB read_shader(const char* filename, GLenum type)
98 {
99         static char buf[131072];
100         FILE *fp = fopen(filename, "r");
101         if (fp == NULL) {
102                 perror(filename);
103                 exit(1);
104         }
105
106         int len = fread(buf, 1, sizeof(buf), fp);
107         fclose(fp);
108
109         GLhandleARB obj = glCreateShaderObjectARB(type);
110         const GLchar* source[] = { buf };
111         const GLint length[] = { len };
112         glShaderSource(obj, 1, source, length);
113         glCompileShader(obj);
114
115         GLchar info_log[4096];
116         GLsizei log_length = sizeof(info_log) - 1;
117         glGetShaderInfoLog(obj, log_length, &log_length, info_log);
118         info_log[log_length] = 0; 
119         printf("shader compile log: %s\n", info_log);
120
121         GLint status;
122         glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
123         if (status == GL_FALSE) {
124                 exit(1);
125         }
126
127         return obj;
128 }
129
130 void draw_picture_quad(GLint prog, int frame)
131 {
132         glUseProgramObjectARB(prog);
133         check_error();
134
135         glActiveTexture(GL_TEXTURE0);
136         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
137         glUniform1i(glGetUniformLocation(prog, "tex"), 0);
138
139         glActiveTexture(GL_TEXTURE1);
140         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
141         glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
142
143         glActiveTexture(GL_TEXTURE2);
144         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
145         glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
146
147         glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
148         //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
149         glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
150                     2.2f / gamma_r,
151                     2.2f / gamma_g,
152                     2.2f / gamma_b);
153         glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
154                     pow(gain_r, 1.0f / gamma_r),
155                     pow(gain_g, 1.0f / gamma_g),
156                     pow(gain_b, 1.0f / gamma_b));
157         glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
158
159         glDisable(GL_BLEND);
160         check_error();
161         glDisable(GL_DEPTH_TEST);
162         check_error();
163         glDepthMask(GL_FALSE);
164         check_error();
165
166         glMatrixMode(GL_PROJECTION);
167         glLoadIdentity();
168         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
169
170         glMatrixMode(GL_MODELVIEW);
171         glLoadIdentity();
172
173         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
174 //      glClear(GL_COLOR_BUFFER_BIT);
175         check_error();
176
177         glBegin(GL_QUADS);
178
179         glTexCoord2f(0.0f, 1.0f);
180         glVertex2f(0.0f, 0.0f);
181
182         glTexCoord2f(1.0f, 1.0f);
183         glVertex2f(1.0f, 0.0f);
184
185         glTexCoord2f(1.0f, 0.0f);
186         glVertex2f(1.0f, 1.0f);
187
188         glTexCoord2f(0.0f, 0.0f);
189         glVertex2f(0.0f, 1.0f);
190
191         glEnd();
192         check_error();
193 }
194
195 void draw_hsv_wheel(float y, float rad, float theta, float value)
196 {
197         glUseProgramObjectARB(0);
198         check_error();
199         glActiveTexture(GL_TEXTURE0);
200         check_error();
201         glEnable(GL_TEXTURE_2D);
202         check_error();
203         glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
204         check_error();
205         glActiveTexture(GL_TEXTURE1);
206         check_error();
207         glBindTexture(GL_TEXTURE_2D, 0);
208         check_error();
209         glActiveTexture(GL_TEXTURE0);
210         glEnable(GL_BLEND);
211         check_error();
212         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
213         check_error();
214
215         // wheel
216         glBegin(GL_QUADS);
217
218         glTexCoord2f(0.0f, 1.0f);
219         glVertex2f(0.0f, y);
220
221         glTexCoord2f(1.0f, 1.0f);
222         glVertex2f(0.2f * 9.0f / 16.0f, y);
223
224         glTexCoord2f(1.0f, 0.0f);
225         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.2f);
226
227         glTexCoord2f(0.0f, 0.0f);
228         glVertex2f(0.0f, y + 0.2f);
229
230         glEnd();
231
232         // wheel selector
233         glDisable(GL_TEXTURE_2D);
234         glColor3f(0.0f, 0.0f, 0.0f);
235         glPointSize(5.0f);
236         glBegin(GL_POINTS);
237         glVertex2f((0.1f + rad * cos(theta) * 0.1f) * 9.0f / 16.0f, y + 0.1f - rad * sin(theta) * 0.1f);
238         glEnd();
239         
240         // value slider
241         glDisable(GL_TEXTURE_2D);
242         glBegin(GL_QUADS);
243
244         glColor3f(0.0f, 0.0f, 0.0f);
245         glVertex2f(0.22f * 9.0f / 16.0f, y);
246         glVertex2f(0.24f * 9.0f / 16.0f, y);
247
248         glColor3f(1.0f, 1.0f, 1.0f);
249         glVertex2f(0.24f * 9.0f / 16.0f, y + 0.2f);
250         glVertex2f(0.22f * 9.0f / 16.0f, y + 0.2f);
251
252         glEnd();
253
254         // value selector
255         glColor3f(0.0f, 0.0f, 0.0f);
256         glPointSize(5.0f);
257         glBegin(GL_POINTS);
258         glVertex2f(0.23f * 9.0f / 16.0f, y + value * 0.2f);
259         glEnd();
260
261         glColor3f(1.0f, 1.0f, 1.0f);
262 }
263
264 void draw_saturation_bar(float y)
265 {
266         glUseProgramObjectARB(0);
267         check_error();
268
269         // value slider
270         glDisable(GL_TEXTURE_2D);
271         glBegin(GL_QUADS);
272
273         glColor3f(0.0f, 0.0f, 0.0f);
274         glVertex2f(0.0f * 9.0f / 16.0f, y + 0.02f);
275         glVertex2f(0.0f * 9.0f / 16.0f, y);
276
277         glColor3f(1.0f, 1.0f, 1.0f);
278         glVertex2f(0.2f * 9.0f / 16.0f, y);
279         glVertex2f(0.2f * 9.0f / 16.0f, y + 0.02f);
280
281         glEnd();
282
283         // value selector
284         glColor3f(0.0f, 0.0f, 0.0f);
285         glPointSize(5.0f);
286         glBegin(GL_POINTS);
287         glVertex2f(0.2f * (saturation / 4.0f) * 9.0f / 16.0f, y + 0.01f);
288         glEnd();
289
290         glColor3f(1.0f, 1.0f, 1.0f);
291 }
292
293 void make_hsv_wheel_texture()
294 {
295         static unsigned char hsv_pix[HSV_WHEEL_SIZE * HSV_WHEEL_SIZE * 4];
296         for (int y = 0; y < HSV_WHEEL_SIZE; ++y) {
297                 for (int x = 0; x < HSV_WHEEL_SIZE; ++x) {
298                         float yf = 2.0f * y / (float)(HSV_WHEEL_SIZE) - 1.0f;
299                         float xf = 2.0f * x / (float)(HSV_WHEEL_SIZE) - 1.0f;
300                         float rad = hypot(xf, yf);
301                         float theta = atan2(yf, xf);
302
303                         float r, g, b;
304                         hsv2rgb(theta, rad, 1.0f, &r, &g, &b);
305                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 0] = lrintf(r * 255.0f);
306                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 1] = lrintf(g * 255.0f);
307                         hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 2] = lrintf(b * 255.0f);
308
309                         if (rad > 1.0f) {
310                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 0;
311                         } else {
312                                 hsv_pix[(y * HSV_WHEEL_SIZE + x) * 4 + 3] = 255;
313                         }
314                 }
315                 printf("\n");
316         }
317
318         glBindTexture(GL_TEXTURE_2D, HSV_WHEEL);
319         check_error();
320         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
321         check_error();
322         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, HSV_WHEEL_SIZE, HSV_WHEEL_SIZE, 0, GL_RGBA, GL_UNSIGNED_BYTE, hsv_pix);
323         check_error();
324 }
325
326 void update_hsv()
327 {
328         hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
329         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
330         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
331
332         if (saturation < 0.0) {
333                 saturation = 0.0;
334         }
335
336         printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
337         printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
338         printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
339         printf("saturation: %f\n", saturation);
340         printf("\n");
341 }
342
343 void read_colorwheel(float xf, float yf, float *rad, float *theta, float *value)
344 {
345         if (xf < 0.2f && yf < 0.2f) {
346                 float xp = 2.0f * xf / 0.2f - 1.0f;
347                 float yp = -(2.0f * yf / 0.2f - 1.0f);
348                 *rad = hypot(xp, yp);
349                 *theta = atan2(yp, xp);
350                 if (*rad > 1.0) {
351                         *rad = 1.0;
352                 }
353         } else if (xf >= 0.22f && xf <= 0.24f) {
354                 *value = yf / 0.2f;
355         }
356 }
357
358 void mouse(int x, int y)
359 {
360         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
361         float yf = (HEIGHT - y) / (float)HEIGHT;
362
363         if (yf < 0.2f) {
364                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
365         } else if (yf >= 0.2f && yf < 0.4f) {
366                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
367         } else if (yf >= 0.4f && yf < 0.6f) {
368                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
369         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
370                 saturation = (xf / 0.2f) * 4.0f;
371         }
372
373         update_hsv();
374 }
375
376 void load_texture(const char *filename)
377 {
378         SDL_Surface *img = IMG_Load(filename);
379         if (img == NULL) {
380                 fprintf(stderr, "Load of '%s' failed\n", filename);
381                 exit(1);
382         }
383
384         // Convert to RGB.
385         SDL_PixelFormat *fmt = img->format;
386         SDL_LockSurface(img);
387         unsigned char *src_pixels = (unsigned char *)img->pixels;
388         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
389         for (unsigned i = 0; i < img->w * img->h; ++i) {
390                 unsigned char r, g, b;
391                 unsigned int temp;
392                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
393
394                 temp = pixel & fmt->Rmask;
395                 temp = temp >> fmt->Rshift;
396                 temp = temp << fmt->Rloss;
397                 r = temp;
398
399                 temp = pixel & fmt->Gmask;
400                 temp = temp >> fmt->Gshift;
401                 temp = temp << fmt->Gloss;
402                 g = temp;
403
404                 temp = pixel & fmt->Bmask;
405                 temp = temp >> fmt->Bshift;
406                 temp = temp << fmt->Bloss;
407                 b = temp;
408
409                 dst_pixels[i * 3 + 0] = r;
410                 dst_pixels[i * 3 + 1] = g;
411                 dst_pixels[i * 3 + 2] = b;
412         }
413         SDL_UnlockSurface(img);
414
415 #if 1
416         // we will convert to sRGB in the shader
417         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
418         check_error();
419 #else
420         // implicit sRGB conversion in hardware
421         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, img->w, img->h, 0, GL_RGB, GL_UNSIGNED_BYTE, dst_pixels);
422         check_error();
423 #endif
424         free(dst_pixels);
425         SDL_FreeSurface(img);
426 }
427
428 void write_ppm(const char *filename, unsigned char *screenbuf)
429 {
430         FILE *fp = fopen(filename, "w");
431         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
432         for (unsigned y = 0; y < HEIGHT; ++y) {
433                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
434                 for (unsigned x = 0; x < WIDTH; ++x) {
435                         fputc(srcptr[x * 4 + 2], fp);
436                         fputc(srcptr[x * 4 + 1], fp);
437                         fputc(srcptr[x * 4 + 0], fp);
438                 }
439         }
440         fclose(fp);
441 }
442
443 int main(int argc, char **argv)
444 {
445         int quit = 0;
446
447         SDL_Init(SDL_INIT_EVERYTHING);
448         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
449         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
450         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
451         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
452         SDL_WM_SetCaption("OpenGL window", NULL);
453         
454         // geez 
455         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
456         glPixelStorei(GL_PACK_ALIGNMENT, 1);
457
458         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
459         check_error();
460         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
461         check_error();
462
463         load_texture("blg_wheels_woman_1.jpg");
464         //glGenerateMipmap(GL_TEXTURE_2D);
465         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
466         //check_error();
467
468         //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
469         //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
470         //load_texture("glacier-lake-1280-720-4087.jpg");
471
472 #if 0
473         // sRGB reverse LUT
474         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
475         check_error();
476         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
477         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
478         check_error();
479         float srgb_reverse_tex[4096];
480         for (unsigned i = 0; i < 4096; ++i) {
481                 float x = i / 4095.0;
482                 if (x < 0.0031308f) {
483                         srgb_reverse_tex[i] = 12.92f * x;
484                 } else {
485                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
486                 }
487         }
488         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
489         check_error();
490
491         // sRGB LUT
492         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
493         check_error();
494         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
495         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
496         check_error();
497         float srgb_tex[256];
498         for (unsigned i = 0; i < 256; ++i) {
499                 float x = i / 255.0;
500                 if (x < 0.04045f) {
501                         srgb_tex[i] = x * (1.0f / 12.92f);
502                 } else {
503                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
504                 }
505         }
506         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
507         check_error();
508 #endif
509
510         // generate a PDO to hold the data we read back with glReadPixels()
511         // (Intel/DRI goes into a slow path if we don't read to PDO)
512         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
513         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
514
515         make_hsv_wheel_texture();
516         update_hsv();
517
518         int prog = glCreateProgram();
519         GLhandleARB vs_obj = read_shader("vs.glsl", GL_VERTEX_SHADER);
520         GLhandleARB fs_obj = read_shader("fs.glsl", GL_FRAGMENT_SHADER);
521         glAttachObjectARB(prog, vs_obj);
522         check_error();
523         glAttachObjectARB(prog, fs_obj);
524         check_error();
525         glLinkProgram(prog);
526         check_error();
527
528         GLchar info_log[4096];
529         GLsizei log_length = sizeof(info_log) - 1;
530         log_length = sizeof(info_log) - 1;
531         glGetProgramInfoLog(prog, log_length, &log_length, info_log);
532         info_log[log_length] = 0; 
533         printf("link: %s\n", info_log);
534
535         struct timespec start, now;
536         int frame = 0, screenshot = 0;
537         clock_gettime(CLOCK_MONOTONIC, &start);
538
539         while (!quit) {
540                 SDL_Event event;
541                 while (SDL_PollEvent(&event)) {
542                         if (event.type == SDL_QUIT) {
543                                 quit = 1;
544                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
545                                 quit = 1;
546                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
547                                 screenshot = 1;
548                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
549                                 mouse(event.button.x, event.button.y);
550                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
551                                 mouse(event.motion.x, event.motion.y);
552                         }
553                 }
554
555                 ++frame;
556
557                 draw_picture_quad(prog, frame);
558                 
559                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
560                 check_error();
561
562                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
563                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
564                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
565                 draw_saturation_bar(0.6f);
566
567                 SDL_GL_SwapBuffers();
568                 check_error();
569
570                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
571                 check_error();
572                 if (screenshot) {
573                         char filename[256];
574                         sprintf(filename, "frame%05d.ppm", frame);
575                         write_ppm(filename, screenbuf);
576                         printf("Screenshot: %s\n", filename);
577                         screenshot = 0;
578                 }
579                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
580                 check_error();
581
582 #if 1
583                 clock_gettime(CLOCK_MONOTONIC, &now);
584                 double elapsed = now.tv_sec - start.tv_sec +
585                         1e-9 * (now.tv_nsec - start.tv_nsec);
586                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
587                         frame, elapsed, frame / elapsed,
588                         1e3 * elapsed / frame);
589 #endif
590         }
591         return 0; 
592 }