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.
#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;
}
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
The vertex shader doesn't seem to be setting gl_TexCoord[0] or gl_FrontColor
http://www.sfml-dev.org/tutorials/2.3/graphics-shader.php#minimal-shaders
Nor, are you setting any of the uniforms.
What are the values of:
uniform mat4 projection_mat;
uniform mat4 modelview_mat;
uniform vec2 viewport;
Your setting of gl_Position relies on projection_mat.
Thank you, I believe my lack of knowledge about GLSL got me stumped here.
I tried replacing the
mat4 proj_modelview_mat = projection_mat * modelview_mat;
with
mat4 proj_modelview_mat = gl_ModelViewProjectionMatrix;
And added accordingly:
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
But didn't have much success.
What I was trying to accomplish was a trailing effect like the image bellow:
(http://pptheaven.mvps.org/images/snake1.jpg)
However I didn't know where to start. I thought learning per object motion blur with GLSL would be a good start, but not knowing the language is a barrier. Could anyone guide me on how to achieve this?
Also, some examples suggest acquiring the previous framebuffer or the previous view-projection matrix. How could I do this with or without SFML? I hope this isn't a stupid question...