1
\$\begingroup\$

I have a shader and I want to run the shader on the whole rendered image at the end instead of each individual sprite that I render on screen. For this purpose I created an FBO, I render the all my images to the FBO and then I render to colorBufferTexture generated by the FBO. There is just one problem, the shader does not work when I render the FBO texture. The withoutFbo() method render the image with the shader working just fine, but the withFbo() method only renders the image without the shader. Also I know that the shader in withFbo() is working because if I try to render something other then the colorBufferTexture from the FBO the shader will work. So why isn't the shader working with the texture generated by the FBO?

public class Application3 extends ApplicationAdapter
{
    float time;
    Sprite sprite;
    FrameBuffer fbo;
    SpriteBatch batch;
    Texture noiseTexture;
    TextureRegion fboTexture;
    ShaderProgram shaderProgram;

    @Override public void create()
    {
        ShaderProgram.pedantic = false;
        batch = new SpriteBatch();

        sprite = new Sprite(new Texture("game.png"));
        sprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        shaderProgram = new ShaderProgram(Gdx.files.internal(Shader.GLITCH2.getVertex()), Gdx.files.internal(Shader.GLITCH2.getFragment()));

        noiseTexture = new Texture(Gdx.files.internal(Shader.GLITCH2.getPath() + "noise.png"));
        noiseTexture.bind(0);

        fbo = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
        fboTexture = new TextureRegion(fbo.getColorBufferTexture());
        fboTexture.flip(false, true);
    }

    @Override public void render()
    {
        //withFbo();
        withoutFbo();
    }

    void withoutFbo()
    {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        time += Gdx.graphics.getDeltaTime();

        shaderProgram.setUniformi("u_noise", 0);
        shaderProgram.setUniformf("u_time", time);
        batch.setShader(shaderProgram);
        batch.begin();

        batch.draw(sprite, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());

        batch.setShader(null);
        batch.end();
    }

    void withFbo()
    {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        time += Gdx.graphics.getDeltaTime();

        fbo.begin();
        batch.begin();

        batch.draw(sprite, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());

        batch.end();
        fbo.end();

        shaderProgram.setUniformi("u_noise", 0);
        shaderProgram.setUniformf("u_time", time);
        batch.setShader(shaderProgram);
        batch.begin();

        batch.draw(fboTexture, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());

        batch.setShader(null);
        batch.end();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.