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