|
| 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 | +} |
0 commit comments