Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Using shaders with sfml  (Read 4244 times)

0 Members and 1 Guest are viewing this topic.

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Using shaders with sfml
« on: February 16, 2022, 10:49:40 am »
Hi!

I want to use this shader but all i get is a black image. I know the texture is loaded correctly because i can render it without the shader.

This is how i load and use the shader, which is probably not correct.

void init() {
        shader.loadFromFile("glsl-shaders-master/xbr/shaders/xbr-mlv4-multipass/xbr-mlv4-pass1.glsl", sf::Shader::Vertex);
        shader.loadFromFile("glsl-shaders-master/xbr/shaders/xbr-mlv4-multipass/xbr-mlv4-pass1.glsl", sf::Shader::Fragment);
        shader.setUniform("MVPmatrix", sf::Glsl::Mat4(bg_sprite.getTransform()));
        shader.setUniform("FrameDirection", 1);
        shader.setUniform("FrameCount", 100);
        shader.setUniform("OutputSize", sf::Vector2f(500, 500));
        shader.setUniform("TextureSize", sf::Vector2f(500, 500));
        shader.setUniform("InputSize", sf::Vector2f(500, 500));
        shader.setUniform("Texture", sf::Shader::CurrentTexture);

        //more code
}

void draw() {
        window.draw(bg_sprite, &shader);

        //more code
}

This are the errors i get:

Uniform "MVPmatrix" not found in shader
Uniform "FrameDirection" not found in shader
Uniform "FrameCount" not found in shader
Uniform "OutputSize" not found in shader
Uniform "TextureSize" not found in shader
Uniform "InputSize" not found in shader
Uniform "Texture" not found in shader
« Last Edit: February 19, 2022, 02:10:56 pm by taifun93 »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Using shaders with sfml
« Reply #1 on: February 16, 2022, 11:32:05 am »
I'm a complete ignorant in shaders, but have you tried anothe one, just for testing? I get this error:

Quote
Failed to compile vertex shader:
0:368(1): error: syntax error, unexpected end of file
Visit my game site (and hopefully help funding it? )
Website | IndieDB

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Using shaders with sfml
« Reply #2 on: February 16, 2022, 12:44:50 pm »
I'm a complete ignorant in shaders, but have you tried anothe one, just for testing? I get this error:

Quote
Failed to compile vertex shader:
0:368(1): error: syntax error, unexpected end of file

I could use this simple shader which is not useful to me, it only paints some colors on top of everything. But other shaders that i downloaded do not seem to work.


uniform vec2 u_resolution;

void main() {
    vec2 st = gl_FragCoord.st/u_resolution;
    gl_FragColor = vec4(st.x, st.y, 0.0, 1);
}
 

        shader.loadFromFile("rainbow.glsl", sf::Shader::Fragment);
        shader.setUniform("u_resolution", sf::Glsl::Vec2{ 100, 100 });*/
 


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Using shaders with sfml
« Reply #3 on: February 16, 2022, 01:19:29 pm »
Since the linked shader has a big #if around the main body, I wonder whether the version check fails and thus you get no main() function which the compiler expects and as it doesn't find it until the end of the file, it errors with said message.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Using shaders with sfml
« Reply #4 on: February 16, 2022, 01:49:26 pm »
Since the linked shader has a big #if around the main body, I wonder whether the version check fails and thus you get no main() function which the compiler expects and as it doesn't find it until the end of the file, it errors with said message.

agreed. thats also why its not finding the variables.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Using shaders with sfml
« Reply #5 on: February 18, 2022, 03:03:49 pm »
I made some progress. If i add this line the the shader file i get a green texture. I also dropped all the setUniform() calls from my program as they seem to do nothing.

#define FRAGMENT

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Using shaders with sfml
« Reply #6 on: February 18, 2022, 04:08:56 pm »
well, 'setuniform' is used to pass variables from SFML to the shader...  :P
Visit my game site (and hopefully help funding it? )
Website | IndieDB

taifun93

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Using shaders with sfml
« Reply #7 on: February 19, 2022, 02:09:54 pm »
I finally got it working! I had to separate the shader in two different files, one for the vertex shader and one for the fragment shader.

I also had to feed OutputSize, TextureSize, and InputSize uniform variables to the verex shader. Then in the main() function of the vertex shader i added these lines and delete their previous declarations.

        mat4 MVPMatrix = gl_ModelViewProjectionMatrix;
        vec4 VertexCoord = gl_Vertex;
        vec4 TexCoord = gl_TextureMatrix[0] * gl_MultiTexCoord0;