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

Author Topic: sf::Shader tutorial / example?  (Read 17506 times)

0 Members and 1 Guest are viewing this topic.

Koobazaur

  • Newbie
  • *
  • Posts: 28
    • View Profile
sf::Shader tutorial / example?
« on: November 03, 2012, 08:26:44 pm »
I am trying to use sf::Shader for a simple grayscale filter (on certain sprites) and getting really confused trying to wrap my head around it. I looked at http://en.sfml-dev.org/forums/index.php?topic=1791.0 but it seems to be outdated (the draw() function no longer takes a shader param for instance, and I couldnt set it in a renderstate as its const).

I got it working via bind/unbind() but I want to do it per-sprite - do I just keep binding and unbinding it then? or is there a better way?

secondly, while I've done some HLSL for DX before, I am getting confused with GLSL. I got tutorials that fill color or draw a circle working, but I cant quite figure out how to simply modify the inColor to grayscale it. Or how to even draw it. I tried a simple:

void main()
{
        gl_FragColor = gl_Color;  
}
 

and that just gives me a blank white screen.

Fyi here's my full render loop in case I missed anything:
//snip (setup views and stuff)
                        sf::Shader colorizePostEffect;
                        if (sf::Shader::isAvailable() )
                        {
                                if (colorizePostEffect.loadFromFile( "mypath", sf::Shader::Fragment  ) )
                                {
                                        // Setup the effect parameters                                                                         
                                        colorizePostEffect.bind();
                                }
                               
                        }

mWindow->draw(everything);
mWindow->display();
colorizePostEffect.unbind();
 

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: sf::Shader tutorial / example?
« Reply #1 on: November 03, 2012, 09:16:37 pm »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader tutorial / example?
« Reply #2 on: November 03, 2012, 10:49:53 pm »
And there's the online doc too.
Laurent Gomila - SFML developer

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email



Koobazaur

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: sf::Shader tutorial / example?
« Reply #4 on: November 04, 2012, 11:02:38 pm »
G. - thanks that worked

Laurent and  kaB00M - the documentation isn't quite right. It tells me to do window.draw(sprite, shader) but window.draw() doesn't take a shader param, or sf::RenderStates states; states.shader = shader; which I cant do because the shader param is const.
« Last Edit: November 04, 2012, 11:57:22 pm by Koobazaur »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: sf::Shader tutorial / example?
« Reply #5 on: November 05, 2012, 01:09:48 am »
Laurent and  kaB00M - the documentation isn't quite right. It tells me to do window.draw(sprite, shader) but window.draw() doesn't take a shader param, or sf::RenderStates states; states.shader = shader; which I cant do because the shader param is const.
Yes and no. ;)
The draw() function doesn't take directly a sf::Shader, but it takes a sf::RenderState as argument and that class has a constructor which takes a sf::Shader and thus when you call draw(obj, shader) it implicitly creates a sf::RenderState object with the constructor that takes in the shader. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Shader tutorial / example?
« Reply #6 on: November 05, 2012, 07:55:40 am »
There's an error in the documentation, it takes the address of a shader. Sorry.
Laurent Gomila - SFML developer

Koobazaur

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: sf::Shader tutorial / example?
« Reply #7 on: November 07, 2012, 05:50:40 am »
ah I see now! thanks for the clarification!