After some googling and testing this is what I got.
The C++ code in "main.cpp":
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(640, 360), "Test 123");
sf::RenderTexture texture;
texture.create(640, 360);
sf::Texture tex;
tex.loadFromFile("Image.png");
sf::Sprite sprite;
sprite.setTexture(tex);
sf::Shader shader;
shader.loadFromFile("Shockwave.vert", "Shockwave.frag");
shader.setParameter("in_Texture", sf::Shader::CurrentTexture);
shader.setParameter("in_Center", 0.5f, 0.5f);
shader.setParameter("in_ShockParams", 10.0f, 0.8f, 0.1f);
shader.setParameter("in_WindowWidth", 640);
shader.setParameter("in_WindowHeight", 360);
sf::Clock clock;
clock.restart();
// The main loop
while (window.isOpen()) {
// Event processing
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
shader.setParameter("in_Time", clock.getElapsedTime().asSeconds());
texture.clear(sf::Color::Red);
texture.draw(sprite);
texture.display();
sf::Sprite rSprite(texture.getTexture());
window.clear();
window.draw(rSprite, &shader);
window.display();
}
}
The "Shockwave.vert":
#version 110
attribute vec4 in_Color;
varying vec4 var_Color;
varying vec2 var_TexCoord;
void main()
{
gl_Position = ftransform();
var_Color = in_Color;
var_TexCoord = gl_MultiTexCoord0.xy;
}
And the "Shockwave.frag":
#version 110
uniform sampler2D in_Texture; // 0
uniform vec2 in_Center; // Mouse position
uniform float in_Time; // effect elapsed time. Multiply this to affect speed.
// Amplitude?, Refraction?, Width? e.g. 10.0, 0.8, 0.1
uniform vec3 in_ShockParams;
uniform int in_WindowWidth;
uniform int in_WindowHeight;
varying vec2 var_TexCoord;
void main()
{
vec2 uv = var_TexCoord;
vec2 texCoord = uv;
float x = in_Center.x / float(in_WindowWidth);
float y = (float(in_WindowHeight) - in_Center.y) / float(in_WindowHeight);
float distance = distance(uv, vec2(x, y));
if ( (distance <= (in_Time + in_ShockParams.z)) &&
(distance >= (in_Time - in_ShockParams.z)) )
{
float diff = (distance - in_Time);
float powDiff = 1.0 - pow(abs(diff * in_ShockParams.x),
in_ShockParams.y);
float diffTime = diff * powDiff;
vec2 diffUV = normalize(uv - in_Center);
texCoord = uv + (diffUV * diffTime);
}
gl_FragColor = texture2D(in_Texture, texCoord);
}
The program without the shader:
With shader:
It seems to just show this one pixel from the top-left corner, it's like I'm missing something very simple. Ideas?