]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Use Renderer for rendering PostProcessors
[libs/gl.git] / source / colorcurve.cpp
1 #include <cmath>
2 #include "color.h"
3 #include "colorcurve.h"
4 #include "mesh.h"
5 #include "renderer.h"
6 #include "shader.h"
7 #include "texture2d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 ColorCurve::ColorCurve():
15         shprog("colorcurve.glsl"),
16         quad(get_fullscreen_quad())
17 {
18         shdata.uniform("source", 0);
19         shdata.uniform("curve", 1);
20
21         curve.storage(LUMINANCE, 256);
22         curve.set_min_filter(LINEAR);
23         curve.set_wrap(CLAMP_TO_EDGE);
24         texturing.attach(1, curve);
25
26         set_peak(0.2);
27         set_brightness(1.5);
28         set_linear();
29 }
30
31 void ColorCurve::set_peak(float p)
32 {
33         if(p<0 || p>1)
34                 throw invalid_argument("ColorCurve::set_peak");
35         shdata.uniform("peak", p);
36 }
37
38 void ColorCurve::set_brightness(float b)
39 {
40         if(b<1)
41                 throw invalid_argument("ColorCurve::set_brightness");
42         shdata.uniform("brightness", 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, LUMINANCE, UNSIGNED_BYTE, 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, LUMINANCE, UNSIGNED_BYTE, 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, LUMINANCE, UNSIGNED_BYTE, curve_data);
71 }
72
73 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
74 {
75         texturing.attach(0, color_buf);
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 } // namespace GL
84 } // namespace Msp