]> git.tdb.fi Git - libs/gl.git/blob - source/effects/bloom.cpp
ee7c76c3217bef3befc48e40b794d3f485cb4389
[libs/gl.git] / source / effects / bloom.cpp
1 #include <cmath>
2 #include <msp/strings/format.h>
3 #include "blend.h"
4 #include "bloom.h"
5 #include "misc.h"
6 #include "renderer.h"
7 #include "shader.h"
8 #include "tests.h"
9 #include "texunit.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Bloom::Bloom(unsigned w, unsigned h):
17         blur_shader("bloom_blur.glsl"),
18         combine_shader("bloom_combine.glsl"),
19         quad(get_fullscreen_quad()),
20         nearest_sampler(get_nearest_sampler()),
21         linear_sampler(get_linear_sampler())
22 {
23         blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
24         blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
25
26         for(unsigned i=0; i<2; ++i)
27                 target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
28
29         common_shdata.uniform("source", 0);
30         common_shdata.uniform("blurred", 1);
31
32         combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR), linear_sampler.get());
33
34         set_radius(2.0f);
35         set_strength(0.2f);
36 }
37
38 Bloom::~Bloom()
39 {
40         for(unsigned i=0; i<2; ++i)
41                 delete target[i];
42 }
43
44 void Bloom::set_radius(float r)
45 {
46         if(r<=0.0f)
47                 throw invalid_argument("Bloom::set_radius");
48
49         int size = min(static_cast<int>(r*3.0f), 9);
50         common_shdata.uniform("size", size);
51
52         vector<float> factors(size*2+1);
53         float sum = 0.0f;
54         r = 2*r*r;
55         for(int i=-size; i<=size; ++i)
56                 sum += (factors[size+i] = exp(-i*i/r));
57         for(int i=0; i<=size*2; ++i)
58                 factors[i] /= sum;
59
60         common_shdata.uniform1_array("factors", size*2+1, &factors.front());
61 }
62
63 void Bloom::set_strength(float s)
64 {
65         if(s<0.0f || s>1.0f)
66                 throw invalid_argument("Bloom::set_strength");
67         common_shdata.uniform("strength", s);
68 }
69
70 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
71 {
72         Renderer::Push push(renderer);
73         renderer.set_shader_program(&blur_shader, &common_shdata);
74         for(unsigned i=0; i<2; ++i)
75         {
76                 BindRestore bind_fbo(target[i]->get_framebuffer());
77                 Renderer::Push push2(renderer);
78                 renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src, nearest_sampler.get());
79                 renderer.add_shader_data(blur_shdata[i]);
80                 quad->draw(renderer);
81         }
82
83         combine_texturing.attach(0, src, nearest_sampler.get());
84         renderer.set_texturing(&combine_texturing);
85         renderer.set_shader_program(&combine_shader);
86         quad->draw(renderer);
87 }
88
89
90 Bloom::Template::Template():
91         radius(2.0f),
92         strength(0.2f)
93 { }
94
95 Bloom *Bloom::Template::create(unsigned width, unsigned height) const
96 {
97         RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
98         bloom->set_radius(radius);
99         bloom->set_strength(strength);
100         return bloom.release();
101 }
102
103
104 Bloom::Template::Loader::Loader(Template &t):
105         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
106 {
107         add("strength", &Template::strength);
108         add("radius", &Template::radius);
109 }
110
111 } // namespace GL
112 } // namespace Msp