1
Graphics / Motion Blur GLSL
« on: March 13, 2016, 05:57:37 pm »
Hello,
I'm trying to load a motion blur shader in GLSL using sf::Shader, however I'm having some trouble applying it to a sprite, as it seems to be completely hide it (i.e. the sprite isn't even drawn, maybe it's loosing its alpha value?) instead of blurring on movement. I've implemented a minimal snippet that reproduces this problem, but I'm inclined to say the problem relies with the GLSL shaders. I've also attached the shaders and resources to this post.
Sadly I'm not too experienced in GLSL, so ended up using this implementation (slide 29): http://www.stevenlu.net/files/motion_blur_2d/Fragment_shader_dynamic_blur.pdf .
Am I missing something really simple?
Best regards
I'm trying to load a motion blur shader in GLSL using sf::Shader, however I'm having some trouble applying it to a sprite, as it seems to be completely hide it (i.e. the sprite isn't even drawn, maybe it's loosing its alpha value?) instead of blurring on movement. I've implemented a minimal snippet that reproduces this problem, but I'm inclined to say the problem relies with the GLSL shaders. I've also attached the shaders and resources to this post.
#include <cmath>
#include <iostream>
#include <SFML/Graphics.hpp>
const double PI = std::atan(1.0) * 4;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader",
sf::Style::Titlebar | sf::Style::Close);
window.setFramerateLimit(60.f);
sf::Texture m_texture;
if (!m_texture.loadFromFile("eff08b.png"))
return EXIT_FAILURE;
sf::Sprite m_sprite;
m_sprite.setTexture(m_texture);
m_sprite.setOrigin(m_sprite.getGlobalBounds().width / 2.f, m_sprite.getGlobalBounds().height / 2.f);
sf::Shader m_shader;
if (!m_shader.loadFromFile("motionblur.vert", "motionblur.frag"))
return EXIT_FAILURE;
m_shader.setParameter("scene", sf::Shader::CurrentTexture);
sf::Clock time;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
float amplitude = 100.f;
m_sprite.setPosition(amplitude * std::sin(PI * 0.5 * time.getElapsedTime().asSeconds()) + 400.f, 300.f);
m_sprite.rotate(5.f);
window.clear(sf::Color(255, 255, 255));
window.draw(m_sprite, &m_shader);
window.display();
}
return EXIT_SUCCESS;
}
#include <iostream>
#include <SFML/Graphics.hpp>
const double PI = std::atan(1.0) * 4;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Shader",
sf::Style::Titlebar | sf::Style::Close);
window.setFramerateLimit(60.f);
sf::Texture m_texture;
if (!m_texture.loadFromFile("eff08b.png"))
return EXIT_FAILURE;
sf::Sprite m_sprite;
m_sprite.setTexture(m_texture);
m_sprite.setOrigin(m_sprite.getGlobalBounds().width / 2.f, m_sprite.getGlobalBounds().height / 2.f);
sf::Shader m_shader;
if (!m_shader.loadFromFile("motionblur.vert", "motionblur.frag"))
return EXIT_FAILURE;
m_shader.setParameter("scene", sf::Shader::CurrentTexture);
sf::Clock time;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
float amplitude = 100.f;
m_sprite.setPosition(amplitude * std::sin(PI * 0.5 * time.getElapsedTime().asSeconds()) + 400.f, 300.f);
m_sprite.rotate(5.f);
window.clear(sf::Color(255, 255, 255));
window.draw(m_sprite, &m_shader);
window.display();
}
return EXIT_SUCCESS;
}
Sadly I'm not too experienced in GLSL, so ended up using this implementation (slide 29): http://www.stevenlu.net/files/motion_blur_2d/Fragment_shader_dynamic_blur.pdf .
Am I missing something really simple?
Best regards