Skip to content

Using Shaders Legacy

fallahn edited this page Sep 29, 2017 · 2 revisions

Not only does xygine provide and interface full screen post process effects using shaders, it contains utility classes for easily adding shader effects to custom drawables. To quickly add a shader interface to a custom drawable class simply inherit xy::ShaderProperty. This adds accessors and a shader property to the class, as well as a function for disabling and enabling shader effects at runtime. For example:

class MyDrawable final : public sf::Drawable, public xy::ShaderProperty
{
public:
    MyDrawable();

private:
    void draw(sf::RenderTarget& rt, sf::RenderStates states) const override
    {
        states.shader = getActiveShader();
        rt.draw(myThing, states);
    }
}

MyDrawable drawable;
drawable.setShader(&shaderResource.get(MyShaderID));
drawable.getShader()->setUniform("u_texture", myTexture);
drawable.setShaderActive();

Note the use of getActiveShader() in the draw function rather than getShader(). This ensures that the shader is only applied to the drawable when setShaderActive() has been called with a true parameter. On the other hand getShader() will always return a pointer to any shader which has been set (else nullptr) and should be used when updating shader parameters. Shaders can also be resource managed by xygine, see this page for more details on resource management.

Clone this wiki locally