forked from shks/ofkTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofxBlur.cpp
executable file
·233 lines (204 loc) · 6.13 KB
/
ofxBlur.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "ofxBlur.h"
float Gaussian(float x, float mean, float variance) {
x -= mean;
return (1. / sqrt(TWO_PI * variance)) * exp(-(x * x) / (2 * variance));
}
void GaussianRow(int elements, vector<float>& row, float variance = .2) {
row.resize(elements);
for(int i = 0; i < elements; i++) {
float x = ofMap(i, 0, elements - 1, -1, 1);
row[i] = Gaussian(x, 0, variance);
}
}
string generateBlurSource(int radius, float shape) {
int rowSize = 2 * radius + 1;
// generate row
vector<float> row;
GaussianRow(rowSize, row, shape);
// normalize row and coefficients
vector<float> coefficients;
float sum = 0;
for(int i = 0; i < row.size(); i++) {
sum += row[i];
}
for(int i = 0; i < row.size(); i++) {
row[i] /= sum;
}
int center = row.size() / 2;
coefficients.push_back(row[center]);
for(int i = center + 1; i < row.size(); i += 2) {
float weightSum = row[i] + row[i + 1];
coefficients.push_back(weightSum);
}
// generate offsets
vector<float> offsets;
for(int i = center + 1; i < row.size(); i += 2) {
int left = i - center;
int right = left + 1;
float leftVal = row[i];
float rightVal = row[i + 1];
float weightSum = leftVal + rightVal;
float weightedAverage = (left * leftVal + right * rightVal) / weightSum;
offsets.push_back(weightedAverage);
}
stringstream src;
src << "uniform sampler2DRect source;\n";
src << "uniform vec2 direction;\n";
src << "void main(void) {\n";
src << "\tvec2 tc = gl_TexCoord[0].st;\n";
src << "\tgl_FragColor = " << coefficients[0] << " * texture2DRect(source, tc);\n";
for(int i = 1; i < coefficients.size(); i++) {
int curOffset = i - 1;
src << "\tgl_FragColor += " << coefficients[i] << " * \n";
src << "\t\t(texture2DRect(source, tc - (direction * " << offsets[i - 1] << ")) + \n";
src << "\t\ttexture2DRect(source, tc + (direction * " << offsets[i - 1] << ")));\n";
}
src << "}\n";
return src.str();
}
string generateCombineSource(int passes, float downsample) {
vector<string> combineNames;
for(int i = 0; i < passes; i++) {
combineNames.push_back("s" + ofToString(i));
}
stringstream src;
src << "uniform sampler2DRect " << ofJoinString(combineNames, ",") << ";\n";
src << "uniform float brightness;\n";
if(downsample == 1) {
src << "const float scaleFactor = 1.;\n";
} else {
src << "const float scaleFactor = " << downsample << ";\n";
}
src << "void main(void) {\n";
src << "\tvec2 tc = gl_TexCoord[0].st;\n";
for(int i = 0; i < passes; i++) {
src << "\tgl_FragColor " << (i == 0 ? "=" : "+=");
src << " texture2DRect(" << combineNames[i] << ", tc);";
src << (i + 1 != passes ? " tc *= scaleFactor;" : "");
src << "\n";
}
src << "\tgl_FragColor *= brightness / " << passes << ".;\n";
src << "}\n";
return src.str();
}
ofxBlur::ofxBlur()
:scale(1)
,rotation(0)
,brightness(1) {
}
void ofxBlur::setup(int width, int height, int radius, float shape, int passes, float downsample) {
string blurSource = generateBlurSource(radius, shape);
if(ofGetLogLevel() == OF_LOG_VERBOSE) {
cout << "ofxBlur is loading blur shader:" << endl << blurSource << endl;
}
blurShader.setupShaderFromSource(GL_FRAGMENT_SHADER, blurSource);
blurShader.linkProgram();
if(passes > 1) {
string combineSource = generateCombineSource(passes, downsample);
if(ofGetLogLevel() == OF_LOG_VERBOSE) {
cout << "ofxBlur is loading combine shader:" << endl << combineSource << endl;
}
combineShader.setupShaderFromSource(GL_FRAGMENT_SHADER, combineSource);
combineShader.linkProgram();
}
ofFbo::Settings settings;
settings.internalformat = GL_RGBA;
base.allocate(width, height,GL_RGBA);
ping.resize(passes);
pong.resize(passes);
for(int i = 0; i < passes; i++) {
settings.width = width;
settings.height = height;
ping[i] = new ofFbo();
pong[i] = new ofFbo();
ping[i]->allocate(settings);
pong[i]->allocate(settings);
width *= downsample;
height *= downsample;
}
}
void ofxBlur::setScale(float scale) {
this->scale = scale;
}
void ofxBlur::setRotation(float rotation) {
this->rotation = rotation;
}
void ofxBlur::setBrightness(float brightness) {
this->brightness = brightness;
}
void ofxBlur::begin() {
base.begin();
}
void ofxBlur::end() {
base.end();
ofPushStyle();
ofSetColor(255);
ofVec2f xDirection = ofVec2f(scale, 0).getRotatedRad(rotation);
ofVec2f yDirection = ofVec2f(0, scale).getRotatedRad(rotation);
for(int i = 0; i < ping.size(); i++) {
ofFbo* curPing = ping[i];
ofFbo* curPong = pong[i];
// resample previous result into ping
curPing->begin();
int w = curPing->getWidth();
int h = curPing->getHeight();
if(i > 0) {
ping[i - 1]->draw(0, 0, w, h);
} else {
base.draw(0, 0, w, h);
}
curPing->end();
// horizontal blur ping into pong
curPong->begin();
blurShader.begin();
blurShader.setUniformTexture("source", curPing->getTextureReference(), 0);
blurShader.setUniform2f("direction", xDirection.x, xDirection.y);
curPing->draw(0, 0);
blurShader.end();
curPong->end();
// vertical blur pong into ping
curPing->begin();
blurShader.begin();
blurShader.setUniformTexture("source", curPong->getTextureReference(), 0);
blurShader.setUniform2f("direction", yDirection.x, yDirection.y);
curPong->draw(0, 0);
blurShader.end();
curPing->end();
}
// render ping back into base
if(ping.size() > 1) {
int w = base.getWidth();
int h = base.getHeight();
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
mesh.addTexCoord(ofVec2f(0, 0));
mesh.addVertex(ofVec2f(0, 0));
mesh.addTexCoord(ofVec2f(0, h));
mesh.addVertex(ofVec2f(0, h));
mesh.addTexCoord(ofVec2f(w, h));
mesh.addVertex(ofVec2f(w, h));
mesh.addTexCoord(ofVec2f(w, 0));
mesh.addVertex(ofVec2f(w, 0));
base.begin();
combineShader.begin();
for(int i = 0; i < ping.size(); i++) {
string name = "s" + ofToString(i);
combineShader.setUniformTexture(name.c_str(), ping[i]->getTextureReference(), i + 1);
}
combineShader.setUniform1f("brightness", brightness);
mesh.draw();
combineShader.end();
base.end();
} else {
base.begin();
ping[0]->draw(0, 0);
base.end();
}
ofPopStyle();
}
ofTexture& ofxBlur::getTextureReference() {
return base.getTextureReference();
}
void ofxBlur::draw() {
base.draw(0, 0);
}