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