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