2 #include <msp/strings/format.h>
16 Bloom::Bloom(unsigned w, unsigned h):
17 blur_shader("bloom_blur.glsl"),
18 combine_shader("bloom_combine.glsl"),
19 quad(get_fullscreen_quad())
21 blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
22 blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
24 for(unsigned i=0; i<2; ++i)
25 target[i] = new RenderTarget(w, h, (RENDER_COLOR,RGB16F));
26 target[1]->set_texture_filter(LINEAR);
28 common_shdata.uniform("source", 0);
29 common_shdata.uniform("blurred", 1);
31 combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR));
39 for(unsigned i=0; i<2; ++i)
43 void Bloom::set_radius(float r)
46 throw invalid_argument("Bloom::set_radius");
48 int size = min(static_cast<int>(r*3.0f), 9);
49 common_shdata.uniform("size", size);
51 vector<float> factors(size*2+1);
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)
59 common_shdata.uniform1_array("factors", size*2+1, &factors.front());
62 void Bloom::set_strength(float s)
65 throw invalid_argument("Bloom::set_strength");
66 common_shdata.uniform("strength", s);
69 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
71 Renderer::Push push(renderer);
72 renderer.set_shader_program(&blur_shader, &common_shdata);
73 for(unsigned i=0; i<2; ++i)
75 BindRestore bind_fbo(target[i]->get_framebuffer());
76 Renderer::Push push2(renderer);
77 renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src);
78 renderer.add_shader_data(blur_shdata[i]);
82 combine_texturing.attach(0, src);
83 renderer.set_texturing(&combine_texturing);
84 renderer.set_shader_program(&combine_shader);
89 Bloom::Template::Template():
94 Bloom *Bloom::Template::create(unsigned width, unsigned height) const
96 RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
97 bloom->set_radius(radius);
98 bloom->set_strength(strength);
99 return bloom.release();
103 Bloom::Template::Loader::Loader(Template &t):
104 DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
106 add("strength", &Template::strength);
107 add("radius", &Template::radius);