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

Author Topic: Shader in SFML  (Read 2199 times)

0 Members and 1 Guest are viewing this topic.

quochuy0703

  • Newbie
  • *
  • Posts: 1
    • View Profile
Shader in SFML
« on: January 02, 2021, 06:11:16 am »
Hi everybody!
I learn shader from this website: https://thebookofshaders.com/05/
I complete to compiling my project and it's run not error, but not show line green in window. :-[
I use cocos2dx then app run good.


file fragment shader:
Quote
uniform vec2 u_resolution;
uniform float u_time;

uniform sampler2D texture;

float plot(vec2 st, float pct){
  return  smoothstep( pct-0.02, pct, st.y) -
          smoothstep( pct, pct+0.02, st.y);
}

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution;

    // Smooth interpolation between 0.1 and 0.9
    float y = smoothstep(0.1,0.9,st.x);

    vec3 color = vec3(y);

    float pct = plot(st,y);
    color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0);

    gl_FragColor = vec4(color,1.0);
}


file main:
Quote
sf::RectangleShape shape(sf::Vector2f(200.0f,200.0f));

   sf::Texture texture;
    if (!texture.loadFromFile("container.jpg"))
    {
        // error...
    }


    shape.setTexture(&texture);


   if (!sf::Shader::isAvailable())
    {
        std::cout<< "Not available" <<std::endl;

    }

   sf::Shader shader;
   if (!shader.loadFromFile("cube.vs", "cube.fs"))
    {

    }


    sf::Clock clock;


     // run the program as long as the window is open
    while (window.isOpen())
    {
        World.Step(1/60.f, 8, 3);
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
            if(event.type == sf::Event::MouseButtonPressed){
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                   int MouseX = event.mouseButton.x;
                   int MouseY = event.mouseButton.y;
                }
            }

        }

        window.clear(sf::Color::Black);

        float sec  = clock.getElapsedTime().asSeconds();

        shader.setUniform("u_time",sec);
        shader.setUniform("u_resolution",sf::Vector2f(200.0f, 200.0f));
        shader.setUniform("texture", sf::Shader::CurrentTexture);

        window.draw(shape,&shader);

        World.DrawDebugData();
         //at global scop
        window.display();



    }
anyone help me this problem?. Thanks advance. :'(
Sorry for my English.

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Shader in SFML
« Reply #1 on: January 02, 2021, 12:07:53 pm »
Some thoughts:

You should include a version directive: https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Version

SFML uses GLSL 1.2 so you'd need
#version 120

GLSL 1.2 doesn't support smoothstep() https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/smoothstep.xhtml
This is easy to implement it yourself, the link even gives an example.

GLSL 1.2 provides gl_TexCoord so you can do this with SFML:
vec2 st = gl_TexCoord[0].xy;

although this is removed in GLSL 1.3 and higher.