Hello forum!
I'm using the normal C++ SFML and tried to use the shader being presented here:
https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5Drawing the sprite without passing the shader renders my normal sprite. Once I add the shader, nothing is being drawn but the cleared coloured background.
Moreover, this applies to a variety of attempts to use other GLSL-shaders - nothing is drawn but the cleared background colour.
When copying a full C++-SFML-shader example from another website, everything works.
The C++/SFML-code:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML Sandbox");
sf::Event event;
// Texture
sf::Texture texture;
texture.loadFromFile("assets/tile.png");
// Sprite
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(500, 500);
// Shader
sf::Shader shader;
if (!shader.loadFromFile("shaders/glsl.vert", "shaders/glsl.frag"))
{
std::cout << "Error. \n";
}
shader.setUniform("u_texture", sf::Shader::CurrentTexture);
shader.setUniform("resolution", 118.0f*77.0f); // width * height of my sprite
shader.setUniform("radius", 1.0f);
shader.setUniform("dir", sf::Vector2f(1.0f, 0.0f));
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color(200, 200, 200));
window.draw(sprite, &shader);
window.display();
}
return 0;
}
My glsl.vert (from previous lessons, as stated in the article
https://github.com/mattdesl/lwjgl-basics/blob/master/test/res/shadertut/lesson4.vert):
//combined projection and view matrix
uniform mat4 u_projView;
//"in" attributes from our SpriteBatch
attribute vec2 Position;
attribute vec2 TexCoord;
attribute vec4 Color;
//"out" varyings to our fragment shader
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = Color;
vTexCoord = TexCoord;
gl_Position = u_projView * vec4(Position, 0.0, 1.0);
}
My glsl.frag:
//"in" attributes from our vertex shader
varying vec4 vColor;
varying vec2 vTexCoord;
//declare uniforms
uniform sampler2D u_texture;
uniform float resolution;
uniform float radius;
uniform vec2 dir;
void main() {
//this will be our RGBA sum
vec4 sum = vec4(0.0);
//our original texcoord for this fragment
vec2 tc = vTexCoord;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
//apply blurring, using a 9-tap filter with predefined gaussian weights
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
//discard alpha for our simple demo, multiply by vertex color and return
gl_FragColor = vColor * vec4(sum.rgb, 1.0);
}
I hope my code-samples did not make this post too verbose, if they did, please inform me : )
Additionally, I tried searching already:
The thread over here did not help me:
http://en.sfml-dev.org/forums/index.php?topic=18191.0Also, my image is a PNG and owns an alpha channel.
Thanks for reading! I would be very thankful if you could help me : )