Make check_error() output the file as well as the line, now that we have several.
[movit] / main.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 #include <string.h>
9 #include <math.h>
10 #include <time.h>
11 #include <assert.h>
12
13 #include <string>
14 #include <vector>
15 #include <map>
16
17 #include <SDL/SDL.h>
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
20
21 #include <GL/gl.h>
22 #include <GL/glext.h>
23
24 #include "effect.h"
25 #include "effect_chain.h"
26 #include "util.h"
27 #include "widgets.h"
28 #include "texture_enum.h"
29
30 unsigned char result[WIDTH * HEIGHT * 4];
31
32 float lift_theta = 0.0f, lift_rad = 0.0f, lift_v = 0.0f;
33 float gamma_theta = 0.0f, gamma_rad = 0.0f, gamma_v = 0.5f;
34 float gain_theta = 0.0f, gain_rad = 0.0f, gain_v = 0.25f;
35 float saturation = 1.0f;
36
37 float lift_r = 0.0f, lift_g = 0.0f, lift_b = 0.0f;
38 float gamma_r = 1.0f, gamma_g = 1.0f, gamma_b = 1.0f;
39 float gain_r = 1.0f, gain_g = 1.0f, gain_b = 1.0f;
40
41 void draw_picture_quad(GLint prog, int frame)
42 {
43         glUseProgramObjectARB(prog);
44         check_error();
45
46         glActiveTexture(GL_TEXTURE0);
47         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
48         glUniform1i(glGetUniformLocation(prog, "tex"), 0);
49
50         glActiveTexture(GL_TEXTURE1);
51         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
52         glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
53
54         glActiveTexture(GL_TEXTURE2);
55         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
56         glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
57
58         glUniform3f(glGetUniformLocation(prog, "lift"), lift_r, lift_g, lift_b);
59         //glUniform3f(glGetUniformLocation(prog, "gamma"), gamma_r, gamma_g, gamma_b);
60         glUniform3f(glGetUniformLocation(prog, "inv_gamma_22"),
61                     2.2f / gamma_r,
62                     2.2f / gamma_g,
63                     2.2f / gamma_b);
64         glUniform3f(glGetUniformLocation(prog, "gain_pow_inv_gamma"),
65                     pow(gain_r, 1.0f / gamma_r),
66                     pow(gain_g, 1.0f / gamma_g),
67                     pow(gain_b, 1.0f / gamma_b));
68         glUniform1f(glGetUniformLocation(prog, "saturation"), saturation);
69
70         glDisable(GL_BLEND);
71         check_error();
72         glDisable(GL_DEPTH_TEST);
73         check_error();
74         glDepthMask(GL_FALSE);
75         check_error();
76
77         glMatrixMode(GL_PROJECTION);
78         glLoadIdentity();
79         glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
80
81         glMatrixMode(GL_MODELVIEW);
82         glLoadIdentity();
83
84         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
85 //      glClear(GL_COLOR_BUFFER_BIT);
86         check_error();
87
88         glBegin(GL_QUADS);
89
90         glTexCoord2f(0.0f, 1.0f);
91         glVertex2f(0.0f, 0.0f);
92
93         glTexCoord2f(1.0f, 1.0f);
94         glVertex2f(1.0f, 0.0f);
95
96         glTexCoord2f(1.0f, 0.0f);
97         glVertex2f(1.0f, 1.0f);
98
99         glTexCoord2f(0.0f, 0.0f);
100         glVertex2f(0.0f, 1.0f);
101
102         glEnd();
103         check_error();
104 }
105
106 void update_hsv()
107 {
108         hsv2rgb(lift_theta, lift_rad, lift_v, &lift_r, &lift_g, &lift_b);
109         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma_r, &gamma_g, &gamma_b);
110         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain_r, &gain_g, &gain_b);
111
112         if (saturation < 0.0) {
113                 saturation = 0.0;
114         }
115
116         printf("lift: %f %f %f\n", lift_r, lift_g, lift_b);
117         printf("gamma: %f %f %f\n", gamma_r, gamma_g, gamma_b);
118         printf("gain: %f %f %f\n", gain_r, gain_g, gain_b);
119         printf("saturation: %f\n", saturation);
120         printf("\n");
121 }
122
123 void mouse(int x, int y)
124 {
125         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
126         float yf = (HEIGHT - y) / (float)HEIGHT;
127
128         if (yf < 0.2f) {
129                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
130         } else if (yf >= 0.2f && yf < 0.4f) {
131                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
132         } else if (yf >= 0.4f && yf < 0.6f) {
133                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
134         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
135                 saturation = (xf / 0.2f) * 4.0f;
136         }
137
138         update_hsv();
139 }
140
141 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
142 {
143         SDL_Surface *img = IMG_Load(filename);
144         if (img == NULL) {
145                 fprintf(stderr, "Load of '%s' failed\n", filename);
146                 exit(1);
147         }
148
149         // Convert to RGB.
150         SDL_PixelFormat *fmt = img->format;
151         SDL_LockSurface(img);
152         unsigned char *src_pixels = (unsigned char *)img->pixels;
153         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 3);
154         for (unsigned i = 0; i < img->w * img->h; ++i) {
155                 unsigned char r, g, b;
156                 unsigned int temp;
157                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
158
159                 temp = pixel & fmt->Rmask;
160                 temp = temp >> fmt->Rshift;
161                 temp = temp << fmt->Rloss;
162                 r = temp;
163
164                 temp = pixel & fmt->Gmask;
165                 temp = temp >> fmt->Gshift;
166                 temp = temp << fmt->Gloss;
167                 g = temp;
168
169                 temp = pixel & fmt->Bmask;
170                 temp = temp >> fmt->Bshift;
171                 temp = temp << fmt->Bloss;
172                 b = temp;
173
174                 dst_pixels[i * 3 + 0] = r;
175                 dst_pixels[i * 3 + 1] = g;
176                 dst_pixels[i * 3 + 2] = b;
177         }
178         SDL_UnlockSurface(img);
179
180         *w = img->w;
181         *h = img->h;
182
183         SDL_FreeSurface(img);
184
185         return dst_pixels;
186 }
187
188 void load_texture(const char *filename)
189 {
190         unsigned w, h;
191         unsigned char *pixels = load_image(filename, &w, &h);
192
193 #if 1
194         // we will convert to sRGB in the shader
195         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
196         check_error();
197 #else
198         // implicit sRGB conversion in hardware
199         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
200         check_error();
201 #endif
202
203         free(pixels);
204 }
205
206 void write_ppm(const char *filename, unsigned char *screenbuf)
207 {
208         FILE *fp = fopen(filename, "w");
209         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
210         for (unsigned y = 0; y < HEIGHT; ++y) {
211                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
212                 for (unsigned x = 0; x < WIDTH; ++x) {
213                         fputc(srcptr[x * 4 + 2], fp);
214                         fputc(srcptr[x * 4 + 1], fp);
215                         fputc(srcptr[x * 4 + 0], fp);
216                 }
217         }
218         fclose(fp);
219 }
220
221 int main(int argc, char **argv)
222 {
223         int quit = 0;
224
225         SDL_Init(SDL_INIT_EVERYTHING);
226         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
227         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
228         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
229         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
230         SDL_WM_SetCaption("OpenGL window", NULL);
231         
232         // geez 
233         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
234         glPixelStorei(GL_PACK_ALIGNMENT, 1);
235
236         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
237         check_error();
238         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
239         check_error();
240
241         load_texture("blg_wheels_woman_1.jpg");
242
243         EffectChain chain(WIDTH, HEIGHT);
244
245         ImageFormat inout_format;
246         inout_format.pixel_format = FORMAT_RGB;
247         inout_format.color_space = COLORSPACE_sRGB;
248         inout_format.gamma_curve = GAMMA_sRGB;
249
250         chain.add_input(inout_format);
251         Effect *lift_gamma_gain_effect = chain.add_effect(LIFT_GAMMA_GAIN);
252         chain.add_output(inout_format);
253         chain.finalize();
254
255         //glGenerateMipmap(GL_TEXTURE_2D);
256         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
257         //check_error();
258
259         //load_texture("maserati_gts_wallpaper_1280x720_01.jpg");
260         //load_texture("90630d1295075297-console-games-wallpapers-wallpaper_need_for_speed_prostreet_09_1920x1080.jpg");
261         //load_texture("glacier-lake-1280-720-4087.jpg");
262
263 #if 0
264         // sRGB reverse LUT
265         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
266         check_error();
267         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
268         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
269         check_error();
270         float srgb_reverse_tex[4096];
271         for (unsigned i = 0; i < 4096; ++i) {
272                 float x = i / 4095.0;
273                 if (x < 0.0031308f) {
274                         srgb_reverse_tex[i] = 12.92f * x;
275                 } else {
276                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
277                 }
278         }
279         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
280         check_error();
281
282         // sRGB LUT
283         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
284         check_error();
285         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
286         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
287         check_error();
288         float srgb_tex[256];
289         for (unsigned i = 0; i < 256; ++i) {
290                 float x = i / 255.0;
291                 if (x < 0.04045f) {
292                         srgb_tex[i] = x * (1.0f / 12.92f);
293                 } else {
294                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
295                 }
296         }
297         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
298         check_error();
299 #endif
300
301         // generate a PDO to hold the data we read back with glReadPixels()
302         // (Intel/DRI goes into a slow path if we don't read to PDO)
303         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
304         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
305
306         make_hsv_wheel_texture();
307         update_hsv();
308
309         int prog = glCreateProgram();
310         GLhandleARB vs_obj = compile_shader(read_file("vs.glsl"), GL_VERTEX_SHADER);
311         GLhandleARB fs_obj = compile_shader(read_file("fs.glsl"), GL_FRAGMENT_SHADER);
312         glAttachObjectARB(prog, vs_obj);
313         check_error();
314         glAttachObjectARB(prog, fs_obj);
315         check_error();
316         glLinkProgram(prog);
317         check_error();
318
319         GLchar info_log[4096];
320         GLsizei log_length = sizeof(info_log) - 1;
321         log_length = sizeof(info_log) - 1;
322         glGetProgramInfoLog(prog, log_length, &log_length, info_log);
323         info_log[log_length] = 0; 
324         printf("link: %s\n", info_log);
325
326         struct timespec start, now;
327         int frame = 0, screenshot = 0;
328         clock_gettime(CLOCK_MONOTONIC, &start);
329
330         while (!quit) {
331                 SDL_Event event;
332                 while (SDL_PollEvent(&event)) {
333                         if (event.type == SDL_QUIT) {
334                                 quit = 1;
335                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
336                                 quit = 1;
337                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
338                                 screenshot = 1;
339                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
340                                 mouse(event.button.x, event.button.y);
341                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
342                                 mouse(event.motion.x, event.motion.y);
343                         }
344                 }
345
346                 ++frame;
347
348                 
349                 draw_picture_quad(prog, frame);
350                 
351                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
352                 check_error();
353
354                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
355                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
356                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
357                 draw_saturation_bar(0.6f, saturation);
358
359                 SDL_GL_SwapBuffers();
360                 check_error();
361
362                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
363                 check_error();
364                 if (screenshot) {
365                         char filename[256];
366                         sprintf(filename, "frame%05d.ppm", frame);
367                         write_ppm(filename, screenbuf);
368                         printf("Screenshot: %s\n", filename);
369                         screenshot = 0;
370                 }
371                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
372                 check_error();
373
374 #if 1
375                 clock_gettime(CLOCK_MONOTONIC, &now);
376                 double elapsed = now.tv_sec - start.tv_sec +
377                         1e-9 * (now.tv_nsec - start.tv_nsec);
378                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
379                         frame, elapsed, frame / elapsed,
380                         1e3 * elapsed / frame);
381 #endif
382         }
383         return 0; 
384 }