]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Fix type errors in ColorCurve fragment shader
[libs/gl.git] / source / colorcurve.cpp
1 #include "colorcurve.h"
2 #include "mesh.h"
3 #include "shader.h"
4 #include "texture2d.h"
5
6 using namespace std;
7
8 namespace {
9
10 static const char fragment_src[] =
11         "uniform sampler2D texture;\n"
12         "uniform float peak;\n"
13         "uniform float brightness;\n"
14         "varying vec2 texcoord;\n"
15         "void main()\n"
16         "{\n"
17         "       vec4 sample = texture2D(texture, texcoord);\n"
18         "       float maxc = max(sample.r, max(sample.g, sample.b));\n"
19         "       if(maxc>1.0-peak)\n"
20         "       {\n"
21         "               vec3 saturated = sample.rgb/maxc;\n"
22         "               if(maxc>1.0+peak)\n"
23         "               {\n"
24         "                       gl_FragColor.rgb = mix(vec3(1.0), saturated, 1.0/pow(brightness, maxc-1.0-peak));\n"
25         "               }\n"
26         "               else\n"
27         "               {\n"
28         "                       float x = (1.0+peak-maxc)/(2.0*peak);\n"
29         "                       gl_FragColor.rgb = saturated.rgb*(1.0-peak+(1.0-x*x)*peak);\n"
30         "               }\n"
31         "               gl_FragColor.a = sample.a;\n"
32         "       }\n"
33         "       else\n"
34         "               gl_FragColor = sample;\n"
35         "}";
36
37 }
38
39 namespace Msp {
40 namespace GL {
41
42 ColorCurve::ColorCurve():
43         quad(get_fullscreen_quad())
44 {
45         shprog.attach_shader(get_fullscreen_vertex_shader());
46         shprog.attach_shader_owned(new FragmentShader(fragment_src));
47         shprog.link();
48
49         set_peak(0.2);
50         set_brightness(1.5);
51 }
52
53 void ColorCurve::set_peak(float p)
54 {
55         if(p<0 || p>1)
56                 throw invalid_argument("ColorCurve::set_peak");
57         shdata.uniform("peak", p);
58 }
59
60 void ColorCurve::set_brightness(float b)
61 {
62         if(b<1)
63                 throw invalid_argument("ColorCurve::set_brightness");
64         shdata.uniform("brightness", b);
65 }
66
67 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
68 {
69         Bind _bind_shader(shprog);
70         shdata.apply();
71         Bind _bind_tex(color_buf);
72         quad.draw();
73 }
74
75 } // namespace GL
76 } // namespace Msp