]> git.tdb.fi Git - libs/gl.git/blob - source/effects/bloom.cpp
Require conditions to be booleans
[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         common_shdata.uniform("source", 0);
31         common_shdata.uniform("blurred", 1);
32
33         combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR), &linear_sampler);
34
35         set_radius(2.0f);
36         set_strength(0.2f);
37 }
38
39 Bloom::~Bloom()
40 {
41         for(unsigned i=0; i<2; ++i)
42                 delete target[i];
43 }
44
45 void Bloom::set_radius(float r)
46 {
47         if(r<=0.0f)
48                 throw invalid_argument("Bloom::set_radius");
49
50         int size = min(static_cast<int>(r*3.0f), 9);
51         common_shdata.uniform("size", size);
52
53         vector<float> factors(size*2+1);
54         float sum = 0.0f;
55         r = 2*r*r;
56         for(int i=-size; i<=size; ++i)
57                 sum += (factors[size+i] = exp(-i*i/r));
58         for(int i=0; i<=size*2; ++i)
59                 factors[i] /= sum;
60
61         common_shdata.uniform1_array("factors", size*2+1, &factors.front());
62 }
63
64 void Bloom::set_strength(float s)
65 {
66         if(s<0.0f || s>1.0f)
67                 throw invalid_argument("Bloom::set_strength");
68         common_shdata.uniform("strength", s);
69 }
70
71 void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
72 {
73         Renderer::Push push(renderer);
74         renderer.set_shader_program(&blur_shader, &common_shdata);
75         for(unsigned i=0; i<2; ++i)
76         {
77                 BindRestore bind_fbo(target[i]->get_framebuffer());
78                 Renderer::Push push2(renderer);
79                 renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src, &nearest_sampler);
80                 renderer.add_shader_data(blur_shdata[i]);
81                 quad.draw(renderer);
82         }
83
84         combine_texturing.attach(0, src, &nearest_sampler);
85         renderer.set_texturing(&combine_texturing);
86         renderer.set_shader_program(&combine_shader);
87         quad.draw(renderer);
88 }
89
90
91 Bloom::Template::Template():
92         radius(2.0f),
93         strength(0.2f)
94 { }
95
96 Bloom *Bloom::Template::create(Resources &res, unsigned width, unsigned height) const
97 {
98         RefPtr<Bloom> bloom = new Bloom(res, width/size_divisor, height/size_divisor);
99         bloom->set_radius(radius);
100         bloom->set_strength(strength);
101         return bloom.release();
102 }
103
104
105 Bloom::Template::Loader::Loader(Template &t):
106         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
107 {
108         add("strength", &Template::strength);
109         add("radius", &Template::radius);
110 }
111
112 } // namespace GL
113 } // namespace Msp