]> git.tdb.fi Git - libs/gl.git/blob - source/bloom.cpp
Bind ProgramData to a Program upon construction
[libs/gl.git] / source / bloom.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2009-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cmath>
9 #include <msp/strings/formatter.h>
10 #include "blend.h"
11 #include "bloom.h"
12 #include "meshbuilder.h"
13 #include "misc.h"
14 #include "tests.h"
15 #include "texunit.h"
16
17 using namespace std;
18
19 namespace {
20
21 static const char blur_vs[]=
22         "varying vec2 texcoord;\n"
23         "void main()\n"
24         "{\n"
25         "       gl_Position = vec4(gl_Vertex.xy*2.0-1.0, 0.0, 1.0);\n"
26         "       texcoord = gl_Vertex.xy;\n"
27         "}";
28
29 static const char blur_fs[]=
30         "uniform sampler2D source;\n"
31         "uniform vec2 delta;\n"
32         "uniform float factors[19];\n"
33         "uniform int size;\n"
34         "varying vec2 texcoord;\n"
35         "void main()\n"
36         "{\n"
37         "       gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);\n"
38         "       for(int i=0; i<=size*2; ++i)\n"
39         "               gl_FragColor += texture2D(source, texcoord+delta*float(i-size))*factors[i];\n"
40         "}";
41
42 static const char combine_vs[]=
43         "varying vec2 texcoord;\n"
44         "void main()\n"
45         "{\n"
46         "       gl_Position = vec4(gl_Vertex.xy*2.0-1.0, 0.0, 1.0);\n"
47         "       texcoord = gl_Vertex.xy;\n"
48         "}";
49
50 static const char combine_fs[]=
51         "uniform sampler2D source;\n"
52         "uniform sampler2D blurred;\n"
53         "uniform float strength;\n"
54         "varying vec2 texcoord;\n"
55         "void main()\n"
56         "{\n"
57         "       gl_FragColor = mix(texture2D(source, texcoord), texture2D(blurred, texcoord), strength);\n"
58         "}";
59
60 }
61
62 namespace Msp {
63 namespace GL {
64
65 Bloom::Bloom(unsigned w, unsigned h):
66         blur_shader(blur_vs, blur_fs),
67         blur_shdata_common(blur_shader),
68         blur_shdata_x(blur_shader),
69         blur_shdata_y(blur_shader),
70         combine_shader(combine_vs, combine_fs),
71         combine_shdata(combine_shader),
72         quad(VERTEX2)
73 {
74         int loc = blur_shader.get_uniform_location("delta");
75         blur_shdata_x.uniform(loc, 1.0f/w, 0.0f);
76         blur_shdata_y.uniform(loc, 0.0f, 1.0f/h);
77
78         blur_shdata_common.uniform("source", 0);
79         for(unsigned i=0; i<2; ++i)
80         {
81                 tex[i].set_min_filter(NEAREST);
82                 tex[i].storage(RGB16F, w, h);
83         }
84
85         combine_shdata.uniform("source", 1);
86         combine_shdata.uniform("blurred", 0);
87
88         combine_texturing.attach(0, tex[1]);
89
90         set_radius(2.0f);
91         set_strength(0.2f);
92
93         MeshBuilder mbld(quad);
94         mbld.begin(TRIANGLE_STRIP);
95         mbld.vertex(0, 1);
96         mbld.vertex(0, 0);
97         mbld.vertex(1, 1);
98         mbld.vertex(1, 0);
99         mbld.end();
100 }
101
102 void Bloom::set_radius(float r)
103 {
104         if(r<=0.0f)
105                 throw InvalidParameterValue("Radius must be positive");
106
107         int size = min(static_cast<int>(r*3.0f), 9);
108         blur_shdata_common.uniform("size", size);
109
110         vector<float> factors(size*2+1);
111         float sum = 0.0f;
112         r = 2*r*r;
113         for(int i=-size; i<=size; ++i)
114                 sum += (factors[size+i] = exp(-i*i/r));
115
116         for(int i=0; i<=size*2; ++i)
117                 blur_shdata_common.uniform(format("factors[%d]", i), factors[i]/sum);
118 }
119
120 void Bloom::set_strength(float s)
121 {
122         if(s<0.0f || s>1.0f)
123                 throw InvalidParameterValue("Strength must be in the range [0.0, 1.0]");
124         combine_shdata.uniform("strength", s);
125 }
126
127 void Bloom::render(const Texture2D &src, const Texture2D &)
128 {
129         Bind unbind_dtest(static_cast<DepthTest *>(0), true);
130         Bind unbind_blend(static_cast<Blend *>(0), true);
131
132         {
133                 Bind bind_shader(blur_shader);
134                 blur_shdata_common.apply();
135                 Bind bind_fbo(fbo, true);
136                 for(unsigned i=0; i<2; ++i)
137                 {
138                         Bind bind_tex(i ? tex[0] : src);
139                         fbo.attach(COLOR_ATTACHMENT0, tex[i], 0);
140                         (i ? blur_shdata_y : blur_shdata_x).apply();
141                         quad.draw();
142                 }
143         }
144
145         combine_texturing.attach(1, src);
146         Bind bind_texturing(combine_texturing);
147         Bind bind_shader(combine_shader);
148         combine_shdata.apply();
149         quad.draw();
150 }
151
152 } // namespace GL
153 } // namespace Msp