]> git.tdb.fi Git - libs/gl.git/blob - source/effects/colorcurve.cpp
Load various built-in things through Resources
[libs/gl.git] / source / effects / colorcurve.cpp
1 #include <cmath>
2 #include "color.h"
3 #include "colorcurve.h"
4 #include "mesh.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "shader.h"
8 #include "texture2d.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 ColorCurve::ColorCurve(Resources &resources):
16         shprog(resources.get<Program>("_colorcurve.glsl")),
17         quad(resources.get<Mesh>("_fullscreen_quad.mesh")),
18         linear_sampler(resources.get<Sampler>("_linear_clamp.samp")),
19         nearest_sampler(resources.get<Sampler>("_nearest_clamp.samp"))
20 {
21         shdata.uniform("source", 0);
22         shdata.uniform("curve", 1);
23
24         curve.storage(LUMINANCE8, 256, 1);
25         texturing.attach(1, curve, &linear_sampler);
26
27         set_exposure_adjust(0.0f);
28         set_brightness_response(0.4f);
29         set_linear();
30 }
31
32 void ColorCurve::set_exposure_adjust(float e)
33 {
34         shdata.uniform("exposure", pow(2.0f, e));
35 }
36
37 void ColorCurve::set_brightness_response(float b)
38 {
39         if(b<=0 || b>1)
40                 throw invalid_argument("ColorCurve::set_brightness_response");
41         float t = (b<1 ? pow(b, 1/(1-b)) : 0.0f);
42         shdata.uniform("brightness_response", b, t, pow(t, b));
43 }
44
45 void ColorCurve::set_gamma(float g)
46 {
47         if(g<0.1 || g>10)
48                 throw invalid_argument("ColorCurve::set_gamma");
49
50         unsigned char curve_data[256];
51         for(unsigned i=0; i<256; ++i)
52                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
53         curve.image(0, curve_data);
54 }
55
56 void ColorCurve::set_srgb()
57 {
58         unsigned char curve_data[256];
59         curve_data[0] = 0;
60         for(unsigned i=1; i<256; ++i)
61                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
62         curve.image(0, curve_data);
63 }
64
65 void ColorCurve::set_linear()
66 {
67         unsigned char curve_data[256];
68         for(unsigned i=0; i<256; ++i)
69                 curve_data[i] = i;
70         curve.image(0, curve_data);
71 }
72
73 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
74 {
75         texturing.attach(0, color_buf, &nearest_sampler);
76
77         Renderer::Push push(renderer);
78         renderer.set_shader_program(&shprog, &shdata);
79         renderer.set_texturing(&texturing);
80         quad.draw(renderer);
81 }
82
83
84 ColorCurve::Template::Template():
85         exposure_adjust(0.0f),
86         brightness_response(0.4f),
87         gamma(1.0f),
88         srgb(false)
89 { }
90
91 ColorCurve *ColorCurve::Template::create(Resources &res, unsigned, unsigned) const
92 {
93         RefPtr<ColorCurve> colorcurve = new ColorCurve(res);
94         colorcurve->set_exposure_adjust(exposure_adjust);
95         colorcurve->set_brightness_response(brightness_response);
96         if(srgb)
97                 colorcurve->set_srgb();
98         else
99                 colorcurve->set_gamma(gamma);
100         return colorcurve.release();
101 }
102
103
104 ColorCurve::Template::Loader::Loader(Template &t):
105         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
106 {
107         add("brightness_response", &Template::brightness_response);
108         add("exposure_adjust", &Template::exposure_adjust);
109         add("gamma", &Loader::gamma);
110         add("srgb", &Loader::srgb);
111 }
112
113 void ColorCurve::Template::Loader::gamma(float g)
114 {
115         obj.gamma = g;
116         obj.srgb = false;
117 }
118
119 void ColorCurve::Template::Loader::srgb()
120 {
121         obj.srgb = true;
122 }
123
124 } // namespace GL
125 } // namespace Msp