1
General / Re: Help with basic fragment-shadered full-screen sprite
« on: February 13, 2019, 08:55:34 am »
I see, just a minimal drawable and the address of a sf::Shader instanciated object does the trick.
Thanks too.
Thanks too.
hi
no need to use sprite and texture to draw fragment shader. just use sf::RectangleShape to get fullscreen to draw on it. also, no need to provide the vertex shader. like so:#include <SFML/Graphics.hpp>
#define GLSL120(src) "#version 120 core\n" #src
static const char* frag = GLSL120(
uniform float time;
uniform vec2 resolution;
float h(float i) {
return fract(pow(3., sqrt(i / 2.)));
}
void main(void) {
vec2 p = gl_FragCoord.xy*2. - resolution;
float a = floor(degrees(4. + atan(p.y, p.x))*2.) / 4.;
float d = pow(2., -10.*fract(0.1*time*(h(a + .5)*-.1 - .1) - h(a)*1000.));
if (abs(length(p) - d*length(resolution)) < d*35.) {
gl_FragColor = vec4(d*(h(a + .5)*3.));
}
else {
gl_FragColor = vec4(0.);
}
}
);
int main()
{
sf::RenderWindow window({ 800, 600 }, "SFML Shader Example");
window.setMouseCursorVisible(false);
sf::RectangleShape fullscreen({ 800, 600 });
sf::Shader shader;
if (!shader.loadFromMemory(frag, sf::Shader::Fragment))
return -1;
shader.setUniform("resolution", sf::Vector2f({ 800, 600 }));
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
shader.setUniform("time", clock.getElapsedTime().asSeconds());
window.clear();
window.draw(fullscreen, &shader);
window.display();
}
}