]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Move postprocessor shaders to the builtin shaderlib
[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 Msp {
11 namespace GL {
12
13 ColorCurve::ColorCurve():
14         shprog("colorcurve.glsl"),
15         quad(get_fullscreen_quad())
16 {
17         shdata.uniform("source", 0);
18         shdata.uniform("curve", 1);
19
20         curve.storage(LUMINANCE, 256);
21         curve.set_min_filter(LINEAR);
22         curve.set_wrap(CLAMP_TO_EDGE);
23
24         set_peak(0.2);
25         set_brightness(1.5);
26         set_linear();
27 }
28
29 void ColorCurve::set_peak(float p)
30 {
31         if(p<0 || p>1)
32                 throw invalid_argument("ColorCurve::set_peak");
33         shdata.uniform("peak", p);
34 }
35
36 void ColorCurve::set_brightness(float b)
37 {
38         if(b<1)
39                 throw invalid_argument("ColorCurve::set_brightness");
40         shdata.uniform("brightness", b);
41 }
42
43 void ColorCurve::set_gamma(float g)
44 {
45         if(g<0.1 || g>10)
46                 throw invalid_argument("ColorCurve::set_gamma");
47
48         unsigned char curve_data[256];
49         for(unsigned i=0; i<256; ++i)
50                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
51         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
52 }
53
54 void ColorCurve::set_srgb()
55 {
56         unsigned char curve_data[256];
57         curve_data[0] = 0;
58         for(unsigned i=1; i<256; ++i)
59                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
60         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
61 }
62
63 void ColorCurve::set_linear()
64 {
65         unsigned char curve_data[256];
66         for(unsigned i=0; i<256; ++i)
67                 curve_data[i] = i;
68         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
69 }
70
71 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
72 {
73         Bind _bind_shader(shprog);
74         shdata.apply();
75         Bind _bind_mesh(quad);
76         Bind _bind_tex(color_buf);
77         Bind _bind_curve(curve, 1);
78         quad.draw();
79 }
80
81 } // namespace GL
82 } // namespace Msp