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