johnBuffer / DynamicBlur

Blur header only lib for SFML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DynamicBlur

Configurable realtime blur of any sf::Texture.

Installation

  1. Include blur header
#include "dynamic_blur.hpp"
  1. Instanciate a Blur object with texture's dimensions
// Width and Height are texture's dimensions
Blur blur(tex_width, tex_height);
  1. Apply the filter
sf::Sprite filtered(blur.apply(bloom.getTexture(), intensity);

Where intensity is the number of blur iterations.

Define a region

If you only need to blur a part of your texture you can define a region

1. Define a region

// Retrieve mouse coordinates
sf::Vector2i mousePosition (sf::Mouse::getPosition(window));

// Define region
uint32_t region_width(512);
uint32_t region_height(256);
blur.setRegion(mousePosition.x - region_width/2, mousePosition.y - region_height/2, region_width, region_height);

2. Result

region

Quick bloom example

#include <SFML/Graphics.hpp>
#include "dynamic_blur.hpp"

int main()
{
    constexpr uint32_t  WIN_WIDTH = 1600;
    constexpr uint32_t WIN_HEIGHT = 900;

    sf::RenderWindow window(sf::VideoMode(WIN_WIDTH, WIN_HEIGHT), "TEST", sf::Style::Default);
    window.setVerticalSyncEnabled(false);
    window.setKeyRepeatEnabled(true);

    sf::RenderTexture render_target, bloom;
    render_target.create(WIN_WIDTH, WIN_HEIGHT);
    bloom.create(WIN_WIDTH, WIN_HEIGHT);

    Blur blur(WIN_WIDTH, WIN_HEIGHT);

    float time = 0.0f;
    
    uint8_t intensity = 4;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        time += 0.1;
	
	render_target.clear();
        bloom.clear();
        
	// Draw a rotating rectangle
        sf::RectangleShape rectangle(sf::Vector2f(250, 100));
        rectangle.setPosition(WIN_WIDTH / 2, WIN_HEIGHT / 2);
        rectangle.setRotation(time);
        render_target.draw(rectangle);
        render_target.display();
	
	// Add it to the bloom target
        bloom.draw(rectangle);
        bloom.display();

        window.clear(sf::Color::Black);

        window.draw(sf::Sprite(render_target.getTexture()));
	// Add bloom
        window.draw(blur.apply(bloom.getTexture(), intensity), sf::BlendAdd);

        window.display();
    }

    return 0;
}

Result

bloom

Future work

  • Remove border artifacts
  • Allow region blur
  • Support per pixel regions

About

Blur header only lib for SFML


Languages

Language:C++ 92.8%Language:CMake 7.2%