]> git.tdb.fi Git - libs/gl.git/blob - source/bloom.cpp
Use RenderTarget objects to manage FBOs in postprocessors
[libs/gl.git] / source / 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 {
21         blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
22         blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
23
24         blur_shdata_common.uniform("source", 0);
25         for(unsigned i=0; i<2; ++i)
26                 target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
27
28         combine_shdata.uniform("source", 1);
29         combine_shdata.uniform("blurred", 0);
30
31         combine_texturing.attach(0, target[1]->get_target_texture(RENDER_COLOR));
32
33         set_radius(2.0f);
34         set_strength(0.2f);
35 }
36
37 Bloom::~Bloom()
38 {
39         for(unsigned i=0; i<2; ++i)
40                 delete target[i];
41 }
42
43 void Bloom::set_radius(float r)
44 {
45         if(r<=0.0f)
46                 throw invalid_argument("Bloom::set_radius");
47
48         int size = min(static_cast<int>(r*3.0f), 9);
49         blur_shdata_common.uniform("size", size);
50
51         vector<float> factors(size*2+1);
52         float sum = 0.0f;
53         r = 2*r*r;
54         for(int i=-size; i<=size; ++i)
55                 sum += (factors[size+i] = exp(-i*i/r));
56         for(int i=0; i<=size*2; ++i)
57                 factors[i] /= sum;
58
59         blur_shdata_common.uniform1_array("factors", size*2+1, &factors.front());
60 }
61
62 void Bloom::set_strength(float s)
63 {
64         if(s<0.0f || s>1.0f)
65                 throw invalid_argument("Bloom::set_strength");
66         combine_shdata.uniform("strength", s);
67 }
68
69 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
70 {
71         {
72                 Renderer::Push push(renderer);
73                 renderer.set_shader_program(&blur_shader, &blur_shdata_common);
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);
79                         renderer.add_shader_data(blur_shdata[i]);
80                         quad.draw(renderer);
81                 }
82         }
83
84         Renderer::Push push(renderer);
85         combine_texturing.attach(1, src);
86         renderer.set_texturing(&combine_texturing);
87         renderer.set_shader_program(&combine_shader, &combine_shdata);
88         quad.draw(renderer);
89 }
90
91 } // namespace GL
92 } // namespace Msp