]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Make postprocessor shaders compatible with modern interface
[libs/gl.git] / source / colorcurve.cpp
1 #include <cmath>
2 #include "color.h"
3 #include "colorcurve.h"
4 #include "mesh.h"
5 #include "shader.h"
6 #include "texture2d.h"
7
8 using namespace std;
9
10 namespace {
11
12 static const char fragment_src[] =
13         "uniform sampler2D texture;\n"
14         "uniform sampler1D curve;\n"
15         "uniform float peak;\n"
16         "uniform float brightness;\n"
17         "varying vec2 texcoord;\n"
18         "void main()\n"
19         "{\n"
20         "       vec4 sample = texture2D(texture, texcoord);\n"
21         "       float maxc = max(sample.r, max(sample.g, sample.b));\n"
22         "       if(maxc>1.0-peak)\n"
23         "       {\n"
24         "               vec3 saturated = sample.rgb/maxc;\n"
25         "               if(maxc>1.0+peak)\n"
26         "               {\n"
27         "                       sample.rgb = mix(vec3(1.0), saturated, 1.0/pow(brightness, maxc-1.0-peak));\n"
28         "               }\n"
29         "               else\n"
30         "               {\n"
31         "                       float x = (1.0+peak-maxc)/(2.0*peak);\n"
32         "                       sample.rgb = saturated.rgb*(1.0-peak+(1.0-x*x)*peak);\n"
33         "               }\n"
34         "       }\n"
35         "       gl_FragColor = vec4(texture1D(curve, sample.r).r, texture1D(curve, sample.g).r, texture1D(curve, sample.b).r, sample.a);\n"
36         "}";
37
38 }
39
40 namespace Msp {
41 namespace GL {
42
43 ColorCurve::ColorCurve():
44         quad(get_fullscreen_quad())
45 {
46         shprog.attach_shader(get_fullscreen_vertex_shader());
47         shprog.attach_shader_owned(new FragmentShader(fragment_src));
48         shprog.bind_attribute(get_component_type(VERTEX2), "vertex");
49         shprog.link();
50
51         shdata.uniform("texture", 0);
52         shdata.uniform("curve", 1);
53
54         curve.storage(LUMINANCE, 256);
55         curve.set_min_filter(LINEAR);
56         curve.set_wrap(CLAMP_TO_EDGE);
57
58         set_peak(0.2);
59         set_brightness(1.5);
60         set_linear();
61 }
62
63 void ColorCurve::set_peak(float p)
64 {
65         if(p<0 || p>1)
66                 throw invalid_argument("ColorCurve::set_peak");
67         shdata.uniform("peak", p);
68 }
69
70 void ColorCurve::set_brightness(float b)
71 {
72         if(b<1)
73                 throw invalid_argument("ColorCurve::set_brightness");
74         shdata.uniform("brightness", b);
75 }
76
77 void ColorCurve::set_gamma(float g)
78 {
79         if(g<0.1 || g>10)
80                 throw invalid_argument("ColorCurve::set_gamma");
81
82         unsigned char curve_data[256];
83         for(unsigned i=0; i<256; ++i)
84                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
85         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
86 }
87
88 void ColorCurve::set_srgb()
89 {
90         unsigned char curve_data[256];
91         curve_data[0] = 0;
92         for(unsigned i=1; i<256; ++i)
93                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
94         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
95 }
96
97 void ColorCurve::set_linear()
98 {
99         unsigned char curve_data[256];
100         for(unsigned i=0; i<256; ++i)
101                 curve_data[i] = i;
102         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
103 }
104
105 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
106 {
107         Bind _bind_shader(shprog);
108         shdata.apply();
109         Bind _bind_mesh(quad);
110         Bind _bind_tex(color_buf);
111         Bind _bind_curve(curve, 1);
112         quad.draw();
113 }
114
115 } // namespace GL
116 } // namespace Msp