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 static char buf[131072];
44 FILE *fp = fopen(filename, "r");
50 int len = fread(buf, 1, sizeof(buf), fp);
53 GLhandleARB obj = glCreateShaderObjectARB(type);
54 const GLchar* source[] = { buf };
55 const GLint length[] = { len };
56 glShaderSource(obj, 1, source, length);
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);
66 glGetShaderiv(obj, GL_COMPILE_STATUS, &status);
67 if (status == GL_FALSE) {
74 void draw_picture_quad(GLint prog, int frame)
76 glUseProgramObjectARB(prog);
79 glActiveTexture(GL_TEXTURE0);
80 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
81 glUniform1i(glGetUniformLocation(prog, "tex"), 0);
83 glActiveTexture(GL_TEXTURE1);
84 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
85 glUniform1i(glGetUniformLocation(prog, "srgb_tex"), 1);
87 glActiveTexture(GL_TEXTURE2);
88 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
89 glUniform1i(glGetUniformLocation(prog, "srgb_reverse_tex"), 2);
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"),
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);
105 glDisable(GL_DEPTH_TEST);
107 glDepthMask(GL_FALSE);
110 glMatrixMode(GL_PROJECTION);
112 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
114 glMatrixMode(GL_MODELVIEW);
117 glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
118 // glClear(GL_COLOR_BUFFER_BIT);
123 glTexCoord2f(0.0f, 1.0f);
124 glVertex2f(0.0f, 0.0f);
126 glTexCoord2f(1.0f, 1.0f);
127 glVertex2f(1.0f, 0.0f);
129 glTexCoord2f(1.0f, 0.0f);
130 glVertex2f(1.0f, 1.0f);
132 glTexCoord2f(0.0f, 0.0f);
133 glVertex2f(0.0f, 1.0f);
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);
145 if (saturation < 0.0) {
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);
156 void mouse(int x, int y)
158 float xf = (x / (float)WIDTH) * 16.0f / 9.0f;
159 float yf = (HEIGHT - y) / (float)HEIGHT;
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;
174 void load_texture(const char *filename)
176 SDL_Surface *img = IMG_Load(filename);
178 fprintf(stderr, "Load of '%s' failed\n", filename);
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;
190 unsigned int pixel = *(unsigned int *)(src_pixels + i * fmt->BytesPerPixel);
192 temp = pixel & fmt->Rmask;
193 temp = temp >> fmt->Rshift;
194 temp = temp << fmt->Rloss;
197 temp = pixel & fmt->Gmask;
198 temp = temp >> fmt->Gshift;
199 temp = temp << fmt->Gloss;
202 temp = pixel & fmt->Bmask;
203 temp = temp >> fmt->Bshift;
204 temp = temp << fmt->Bloss;
207 dst_pixels[i * 3 + 0] = r;
208 dst_pixels[i * 3 + 1] = g;
209 dst_pixels[i * 3 + 2] = b;
211 SDL_UnlockSurface(img);
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);
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);
223 SDL_FreeSurface(img);
226 void write_ppm(const char *filename, unsigned char *screenbuf)
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);
241 int main(int argc, char **argv)
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);
253 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
254 glPixelStorei(GL_PACK_ALIGNMENT, 1);
256 glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE);
258 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
261 load_texture("blg_wheels_woman_1.jpg");
262 //glGenerateMipmap(GL_TEXTURE_2D);
263 //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 4);
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");
272 glBindTexture(GL_TEXTURE_1D, SRGB_REVERSE_LUT);
274 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
275 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
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;
283 srgb_reverse_tex[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f;
286 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 4096, 0, GL_LUMINANCE, GL_FLOAT, srgb_reverse_tex);
290 glBindTexture(GL_TEXTURE_1D, SRGB_LUT);
292 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
293 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
296 for (unsigned i = 0; i < 256; ++i) {
299 srgb_tex[i] = x * (1.0f / 12.92f);
301 srgb_tex[i] = pow((x + 0.055) * (1.0 / 1.055f), 2.4);
304 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, 256, 0, GL_LUMINANCE, GL_FLOAT, srgb_tex);
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);
313 make_hsv_wheel_texture();
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);
321 glAttachObjectARB(prog, fs_obj);
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);
333 struct timespec start, now;
334 int frame = 0, screenshot = 0;
335 clock_gettime(CLOCK_MONOTONIC, &start);
339 while (SDL_PollEvent(&event)) {
340 if (event.type == SDL_QUIT) {
342 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
344 } else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_F1) {
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);
355 draw_picture_quad(prog, frame);
357 glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, BUFFER_OFFSET(0));
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);
365 SDL_GL_SwapBuffers();
368 unsigned char *screenbuf = (unsigned char *)glMapBuffer(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
372 sprintf(filename, "frame%05d.ppm", frame);
373 write_ppm(filename, screenbuf);
374 printf("Screenshot: %s\n", filename);
377 glUnmapBuffer(GL_PIXEL_PACK_BUFFER_ARB);
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);