SFML community forums

General => General discussions => Topic started by: Koobazaur on November 03, 2012, 08:26:44 pm

Title: sf::Shader tutorial / example?
Post by: Koobazaur 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();
 
Title: Re: sf::Shader tutorial / example?
Post by: G. on November 03, 2012, 09:16:37 pm
Here are some shader examples (https://github.com/SFML/SFML/tree/master/examples/shader).
Title: Re: sf::Shader tutorial / example?
Post by: Laurent on November 03, 2012, 10:49:53 pm
And there's the online doc too.
Title: Re: sf::Shader tutorial / example?
Post by: kaB00M on November 04, 2012, 10:43:43 pm
http://www.sfml-dev.org/documentation/2.0/classsf_1_1Shader.php

Title: Re: sf::Shader tutorial / example?
Post by: Koobazaur 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.
Title: Re: sf::Shader tutorial / example?
Post by: eXpl0it3r 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. :)
Title: Re: sf::Shader tutorial / example?
Post by: Laurent on November 05, 2012, 07:55:40 am
There's an error in the documentation, it takes the address of a shader. Sorry.
Title: Re: sf::Shader tutorial / example?
Post by: Koobazaur on November 07, 2012, 05:50:40 am
ah I see now! thanks for the clarification!