1 #define GL_GLEXT_PROTOTYPES 1
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
18 #include <SDL/SDL_opengl.h>
19 #include <SDL/SDL_image.h>
25 #include "effect_chain.h"
28 #include "texture_enum.h"
30 unsigned char result[WIDTH * HEIGHT * 4];
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;
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;
41 GLhandleARB read_shader(const char* filename, GLenum type)
43 std::string shader_src = read_file(filename);
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);
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);
58 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
59 if (status == GL_FALSE) {
66 void draw_picture_quad(GLint prog, int frame)
68 glUseProgramObjectARB(prog);
71 glActiveTexture(GL_TEXTURE0);
72 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
73 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
75 glActiveTexture(GL_TEXTURE1);
76 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
77 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
79 glActiveTexture(GL_TEXTURE2);
80 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
81 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
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"),
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);
97 glDisable(GL_DEPTH_TEST);
99 glDepthMask(GL_FALSE);
102 glMatrixMode(GL_PROJECTION);
104 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
106 glMatrixMode(GL_MODELVIEW);
109 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
110 // glClear(GL_COLOR_BUFFER_BIT);
115 glTexCoord2f(0.0f, 1.0f);
116 glVertex2f(0.0f, 0.0f);
118 glTexCoord2f(1.0f, 1.0f);
119 glVertex2f(1.0f, 0.0f);
121 glTexCoord2f(1.0f, 0.0f);
122 glVertex2f(1.0f, 1.0f);
124 glTexCoord2f(0.0f, 0.0f);
125 glVertex2f(0.0f, 1.0f);
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);
137 if (saturation < 0.0) {
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);
148 void mouse(int x, int y)
150 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
151 float yf = (HEIGHT - y) / (float)HEIGHT;
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;
166 void load_texture(const char *filename)
168 SDL_Surface *img = IMG_Load(filename);
170 fprintf(stderr, "Load of '%s' failed\n", filename);
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;
182 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
184 temp = pixel & fmt->Rmask;
185 temp = temp >> fmt->Rshift;
186 temp = temp << fmt->Rloss;
189 temp = pixel & fmt->Gmask;
190 temp = temp >> fmt->Gshift;
191 temp = temp << fmt->Gloss;
194 temp = pixel & fmt->Bmask;
195 temp = temp >> fmt->Bshift;
196 temp = temp << fmt->Bloss;
199 dst_pixels[i * 3 + 0] = r;
200 dst_pixels[i * 3 + 1] = g;
201 dst_pixels[i * 3 + 2] = b;
203 SDL_UnlockSurface(img);
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);
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);
215 SDL_FreeSurface(img);
218 void write_ppm(const char *filename, unsigned char *screenbuf)
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);
233 int main(int argc, char **argv)
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);
245 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
246 glPixelStorei(GL_PACK_ALIGNMENT, 1);
248 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
253 load_texture("blg_wheels_woman_1.jpg");
254 //glGenerateMipmap(GL_TEXTURE_2D);
255 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
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");
264 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
266 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
267 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
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;
275 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
278 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
282 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
284 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
285 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
288 for (unsigned i = 0; i < 256; ++i) {
291 srgb_tex[i] = x * (1.0f / 12.92f);
293 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
296 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
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);
305 make_hsv_wheel_texture();
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);
313 glAttachObjectARB(prog, fs_obj);
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);
325 struct timespec start, now;
326 int frame = 0, screenshot = 0;
327 clock_gettime(CLOCK_MONOTONIC, &start);
331 while (SDL_PollEvent(&event)) {
332 if (event.type == SDL_QUIT) {
334 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
336 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
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);
347 draw_picture_quad(prog, frame);
349 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
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);
357 SDL_GL_SwapBuffers();
360 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
364 sprintf(filename, "frame%05d.ppm", frame);
365 write_ppm(filename, screenbuf);
366 printf("Screenshot: %s\n", filename);
369 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
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);