]> git.sesse.net Git - movit/blob - main.cpp
Kill the vertex shader system; it is too complicated to get it right until we have...
[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
7 #include <string.h>
8 #include <math.h>
9 #include <time.h>
10 #include <sys/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 radius = 0.3f;
38 float inner_radius = 0.3f;
39
40 void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect)
41 {
42         RGBTriplet lift(0.0f, 0.0f, 0.0f);
43         RGBTriplet gamma(1.0f, 1.0f, 1.0f);
44         RGBTriplet gain(1.0f, 1.0f, 1.0f);
45
46         hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b);
47         hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b);
48         hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b);
49
50         bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift);
51         ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma);
52         ok = ok && lift_gamma_gain_effect->set_vec3("gain", (float *)&gain);
53         assert(ok);
54
55         if (saturation < 0.0) {
56                 saturation = 0.0;
57         }
58         ok = saturation_effect->set_float("saturation", saturation);
59         assert(ok);
60 }
61
62 void mouse(int x, int y)
63 {
64         float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
65         float yf = (HEIGHT - y) / (float)HEIGHT;
66
67         if (yf < 0.2f) {
68                 read_colorwheel(xf, yf, &lift_rad, &lift_theta, &lift_v);
69         } else if (yf >= 0.2f && yf < 0.4f) {
70                 read_colorwheel(xf, yf - 0.2f, &gamma_rad, &gamma_theta, &gamma_v);
71         } else if (yf >= 0.4f && yf < 0.6f) {
72                 read_colorwheel(xf, yf - 0.4f, &gain_rad, &gain_theta, &gain_v);
73         } else if (yf >= 0.6f && yf < 0.62f && xf < 0.2f) {
74                 saturation = (xf / 0.2f) * 4.0f;
75         } else if (yf >= 0.65f && yf < 0.67f && xf < 0.2f) {
76                 radius = (xf / 0.2f);
77         } else if (yf >= 0.70f && yf < 0.72f && xf < 0.2f) {
78                 inner_radius = (xf / 0.2f);
79         }
80 }
81
82 unsigned char *load_image(const char *filename, unsigned *w, unsigned *h)
83 {
84         SDL_Surface *img = IMG_Load(filename);
85         if (img == NULL) {
86                 fprintf(stderr, "Load of '%s' failed\n", filename);
87                 exit(1);
88         }
89
90         // Convert to RGB.
91         SDL_PixelFormat *fmt = img->format;
92         SDL_LockSurface(img);
93         unsigned char *src_pixels = (unsigned char *)img->pixels;
94         unsigned char *dst_pixels = (unsigned char *)malloc(img->w * img->h * 4);
95         for (int i = 0; i < img->w * img->h; ++i) {
96                 unsigned char r, g, b;
97                 unsigned int temp;
98                 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
99
100                 temp = pixel & fmt->Rmask;
101                 temp = temp >> fmt->Rshift;
102                 temp = temp << fmt->Rloss;
103                 r = temp;
104
105                 temp = pixel & fmt->Gmask;
106                 temp = temp >> fmt->Gshift;
107                 temp = temp << fmt->Gloss;
108                 g = temp;
109
110                 temp = pixel & fmt->Bmask;
111                 temp = temp >> fmt->Bshift;
112                 temp = temp << fmt->Bloss;
113                 b = temp;
114
115                 dst_pixels[i * 4 + 0] = b;
116                 dst_pixels[i * 4 + 1] = g;
117                 dst_pixels[i * 4 + 2] = r;
118                 dst_pixels[i * 4 + 3] = 255;
119         }
120         SDL_UnlockSurface(img);
121
122         *w = img->w;
123         *h = img->h;
124
125         SDL_FreeSurface(img);
126
127         return dst_pixels;
128 }
129
130 void write_ppm(const char *filename, unsigned char *screenbuf)
131 {
132         FILE *fp = fopen(filename, "w");
133         fprintf(fp, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
134         for (unsigned y = 0; y < HEIGHT; ++y) {
135                 unsigned char *srcptr = screenbuf + ((HEIGHT - y - 1) * WIDTH) * 4;
136                 for (unsigned x = 0; x < WIDTH; ++x) {
137                         fputc(srcptr[x * 4 + 2], fp);
138                         fputc(srcptr[x * 4 + 1], fp);
139                         fputc(srcptr[x * 4 + 0], fp);
140                 }
141         }
142         fclose(fp);
143 }
144
145 int main(int argc, char **argv)
146 {
147         int quit = 0;
148
149         SDL_Init(SDL_INIT_EVERYTHING);
150         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
151         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
152         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
153         SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_OPENGL);
154         SDL_WM_SetCaption("OpenGL window", NULL);
155         
156         // geez 
157         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
158         glPixelStorei(GL_PACK_ALIGNMENT, 1);
159
160         glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
161         check_error();
162         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163         check_error();
164
165         unsigned img_w, img_h;
166         unsigned char *src_img = load_image("blg_wheels_woman_1.jpg", &img_w, &img_h);
167
168         EffectChain chain(WIDTH, HEIGHT);
169
170         ImageFormat inout_format;
171         inout_format.pixel_format = FORMAT_BGRA;
172         inout_format.color_space = COLORSPACE_sRGB;
173         inout_format.gamma_curve = GAMMA_sRGB;
174
175         chain.add_input(inout_format);
176         Effect *lift_gamma_gain_effect = chain.add_effect(EFFECT_LIFT_GAMMA_GAIN);
177         Effect *saturation_effect = chain.add_effect(EFFECT_SATURATION);
178         Effect *vignette_effect = chain.add_effect(EFFECT_VIGNETTE);
179         //chain.add_effect(EFFECT_MIRROR);
180         chain.add_output(inout_format);
181         chain.finalize();
182
183         //glGenerateMipmap(GL_TEXTURE_2D);
184         //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
185         //check_error();
186
187 #if 0
188         // sRGB reverse LUT
189         glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
190         check_error();
191         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
192         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
193         check_error();
194         float srgb_reverse_tex[4096];
195         for (unsigned i = 0; i < 4096; ++i) {
196                 float x = i / 4095.0;
197                 if (x < 0.0031308f) {
198                         srgb_reverse_tex[i] = 12.92f * x;
199                 } else {
200                         srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
201                 }
202         }
203         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
204         check_error();
205
206         // sRGB LUT
207         glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
208         check_error();
209         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
210         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
211         check_error();
212         float srgb_tex[256];
213         for (unsigned i = 0; i < 256; ++i) {
214                 float x = i / 255.0;
215                 if (x < 0.04045f) {
216                         srgb_tex[i] = x * (1.0f / 12.92f);
217                 } else {
218                         srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
219                 }
220         }
221         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
222         check_error();
223 #endif
224
225         // generate a PDO to hold the data we read back with glReadPixels()
226         // (Intel/DRI goes into a slow path if we don't read to PDO)
227         glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
228         glBufferData(GL_PIXEL_PACK_BUFFER_ARB, WIDTH * HEIGHT * 4, NULL, GL_STREAM_READ);
229
230         make_hsv_wheel_texture();
231
232         int frame = 0, screenshot = 0;
233 #if _POSIX_C_SOURCE >= 199309L
234         struct timespec start, now;
235         clock_gettime(CLOCK_MONOTONIC, &start);
236 #else
237         struct timeval start, now;
238         gettimeofday(&start, NULL);
239 #endif
240
241         while (!quit) {
242                 SDL_Event event;
243                 while (SDL_PollEvent(&event)) {
244                         if (event.type == SDL_QUIT) {
245                                 quit = 1;
246                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
247                                 quit = 1;
248                         } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
249                                 screenshot = 1;
250                         } else if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
251                                 mouse(event.button.x, event.button.y);
252                         } else if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(1))) {
253                                 mouse(event.motion.x, event.motion.y);
254                         }
255                 }
256
257                 ++frame;
258
259                 update_hsv(lift_gamma_gain_effect, saturation_effect);
260                 vignette_effect->set_float("radius", radius);
261                 vignette_effect->set_float("inner_radius", inner_radius);
262                 //vignette_effect->set_vec2("center", (float[]){ 0.7f, 0.5f });
263                 chain.render_to_screen(src_img);
264                 
265                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
266                 check_error();
267                 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
268                 check_error();
269                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
270                 check_error();
271
272                 draw_hsv_wheel(0.0f, lift_rad, lift_theta, lift_v);
273                 draw_hsv_wheel(0.2f, gamma_rad, gamma_theta, gamma_v);
274                 draw_hsv_wheel(0.4f, gain_rad, gain_theta, gain_v);
275                 draw_saturation_bar(0.6f, saturation / 4.0f);
276                 draw_saturation_bar(0.65f, radius);
277                 draw_saturation_bar(0.70f, inner_radius);
278
279                 SDL_GL_SwapBuffers();
280                 check_error();
281
282                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 1);
283                 check_error();
284                 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
285                 check_error();
286                 if (screenshot) {
287                         char filename[256];
288                         sprintf(filename, "frame%05d.ppm", frame);
289                         write_ppm(filename, screenbuf);
290                         printf("Screenshot: %s\n", filename);
291                         screenshot = 0;
292                 }
293                 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
294                 check_error();
295                 glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
296                 check_error();
297
298 #if 1
299 #if _POSIX_C_SOURCE >= 199309L
300                 clock_gettime(CLOCK_MONOTONIC, &now);
301                 double elapsed = now.tv_sec - start.tv_sec +
302                         1e-9 * (now.tv_nsec - start.tv_nsec);
303 #else
304                 gettimeofday(&now, NULL);
305                 double elapsed = now.tv_sec - start.tv_sec +
306                         1e-6 * (now.tv_usec - start.tv_usec);
307 #endif
308                 printf("%d frames in %.3f seconds = %.1f fps (%.1f ms/frame)\n",
309                         frame, elapsed, frame / elapsed,
310                         1e3 * elapsed / frame);
311 #endif
312         }
313         return 0; 
314 }