Skip to content

Commit 376c7db

Browse files
committed
added new blur effects
1 parent af80034 commit 376c7db

File tree

4 files changed

+144
-6
lines changed

4 files changed

+144
-6
lines changed

DynamicBlur.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "DynamicBlur.h"
2+
3+
int log2(int i)
4+
{
5+
int targetlevel = 0;
6+
while (i >>= 1) ++targetlevel;
7+
return targetlevel;
8+
}
9+
10+
void applyBlur(sf::RenderTexture& texture, sf::Shader& shader)
11+
{
12+
shader.setParameter("DIRECTION", 1.0);
13+
texture.draw(sf::Sprite(texture.getTexture()), &shader);
14+
shader.setParameter("DIRECTION", 0.0);
15+
texture.draw(sf::Sprite(texture.getTexture()), &shader);
16+
}
17+
18+
DynamicBlur::DynamicBlur(unsigned int textureWidth, unsigned int textureHeight) :
19+
__downSizeFactor(8.0),
20+
__WIDTH(textureWidth),
21+
__HEIGHT(textureHeight)
22+
{
23+
__blur.loadFromFile("dblur.frag", sf::Shader::Fragment);
24+
__blur.setParameter("WIDTH", __WIDTH);
25+
__blur.setParameter("HEIGHT", __HEIGHT);
26+
27+
__blurTexture.create(__WIDTH, __HEIGHT);
28+
__lowBlurTexture.create(__WIDTH, __HEIGHT);
29+
}
30+
31+
const sf::Texture& DynamicBlur::operator()(const sf::Texture& inputTexture)
32+
{
33+
sf::Sprite downscaleSprite(inputTexture);
34+
downscaleSprite.setScale(0.5, 0.5);
35+
__blurTexture.draw(downscaleSprite);
36+
37+
__blur.setParameter("SCALE", 2);
38+
for (int i(2); i--;)
39+
applyBlur(__blurTexture, __blur);
40+
41+
__blurTexture.display();
42+
43+
sf::Sprite downscaledSprite1(__blurTexture.getTexture());
44+
downscaledSprite1.setScale(2/float(__downSizeFactor), 2/float(__downSizeFactor));
45+
__lowBlurTexture.draw(downscaledSprite1);
46+
__lowBlurTexture.display();
47+
__blurTexture.draw(sf::Sprite(__lowBlurTexture.getTexture()));
48+
49+
sf::Sprite borderSprite(__lowBlurTexture.getTexture());
50+
borderSprite.setPosition(__WIDTH/__downSizeFactor, 0);
51+
__blurTexture.draw(borderSprite);
52+
borderSprite.setPosition(0, __HEIGHT/__downSizeFactor);
53+
__blurTexture.draw(borderSprite);
54+
55+
int i = __downSizeFactor*2;
56+
while (i >>= 1 > 0.5)
57+
{
58+
__blur.setParameter("SCALE", 1/float(i));
59+
for (int k(log2(i)); k--;)
60+
applyBlur(__blurTexture, __blur);
61+
62+
if (i-1)
63+
{
64+
sf::Sprite upscale(__blurTexture.getTexture());
65+
upscale.scale(2, 2);
66+
__lowBlurTexture.draw(upscale);
67+
__blurTexture.draw(sf::Sprite(__lowBlurTexture.getTexture()));
68+
}
69+
}
70+
__blurTexture.display();
71+
72+
return __blurTexture.getTexture();
73+
}

DynamicBlur.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef DYNAMICBLUR_H_INCLUDED
2+
#define DYNAMICBLUR_H_INCLUDED
3+
4+
#include <SFML/Graphics.hpp>
5+
6+
class DynamicBlur
7+
{
8+
public:
9+
DynamicBlur(unsigned int texureWidth, unsigned int texureHeight);
10+
const sf::Texture& operator()(const sf::Texture&);
11+
12+
void setFactor(unsigned int i) {__downSizeFactor = pow(2, i);}
13+
14+
private:
15+
float __downSizeFactor;
16+
unsigned int __WIDTH, __HEIGHT;
17+
18+
sf::RenderTexture __blurTexture;
19+
sf::RenderTexture __lowBlurTexture;
20+
21+
sf::Shader __blur;
22+
};
23+
24+
#endif // DYNAMICBLUR_H_INCLUDED

dblur.frag

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#version 130
2+
3+
uniform sampler2D texture;
4+
uniform float BLUR_FACTOR;
5+
uniform float MAX_HEIGHT;
6+
7+
uniform float DIRECTION;
8+
9+
uniform float WIDTH;
10+
uniform float HEIGHT;
11+
12+
uniform float SCALE;
13+
14+
vec4 weight = vec4(0.006, 0.061, 0.242, 0.383);
15+
16+
float WIDTH_STEP = 1.0/WIDTH;
17+
float HEIGHT_STEP= 1.0/HEIGHT;
18+
19+
20+
void main()
21+
{
22+
vec2 pos = vec2(gl_FragCoord.x*WIDTH_STEP, gl_FragCoord.y*HEIGHT_STEP);
23+
24+
if (pos.x < SCALE*1.05 && 1-pos.y < SCALE*1.05)
25+
{
26+
vec2 offset = vec2(WIDTH_STEP*DIRECTION, HEIGHT_STEP*(abs(DIRECTION-1)));
27+
28+
float xStep = floor(gl_FragCoord.x);
29+
float yStep = floor(gl_FragCoord.y);
30+
31+
vec4 color = texture2D(texture, pos) * weight[3];
32+
33+
color += texture2D(texture, pos+offset) * weight[2];
34+
color += texture2D(texture, pos+offset*2) * weight[1];
35+
color += texture2D(texture, pos+offset*3) * weight[0];
36+
37+
color += texture2D(texture, pos-offset) * weight[2];
38+
color += texture2D(texture, pos-offset*2) * weight[1];
39+
color += texture2D(texture, pos-offset*3) * weight[0];
40+
41+
gl_FragData[0] = vec4(color.xyz, 1.0);
42+
}
43+
else
44+
{
45+
gl_FragData[0] = texture2D(texture, pos);
46+
}
47+
}

main.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,6 @@ int main()
300300
renderer.draw(stableIndicator);
301301
renderer.display();
302302

303-
drawer.setParameter("MIN_HEIGHT", WIN_HEIGHT-mousePos.y);
304-
drawer.setParameter("ORIGINAL_TEXTURE", renderer.getTexture());
305-
dblur.setFactor(3);
306-
renderer.draw(sf::Sprite(dblur(renderer.getTexture())), &drawer);
307-
renderer.display();
308-
309303
window.draw(sf::Sprite(renderer.getTexture()));
310304
window.display();
311305

0 commit comments

Comments
 (0)