LibGDX着色器不适用于FBO纹理

时间:2017-07-28 14:44:19

标签: java opengl opengl-es libgdx

我有一个着色器,我希望在最后渲染的图像上运行着色器,而不是我在屏幕上渲染的每个单独的精灵。为此我创建了一个FBO,我将所有图像渲染到FBO,然后渲染到FBO生成的colorBufferTexture。只有一个问题,当我渲染FBO纹理时着色器不起作用。 withoutFbo()方法使用着色器正常工作渲染图像,但withFbo()方法仅渲染没有着色器的图像。另外我知道withFbo()中的着色器正在工作,因为如果我尝试渲染其他东西,那么来自FBO的colorBufferTexture,着色器将起作用。那么为什么着色器不能处理FBO生成的纹理?

注意:着色器正在使用此代码,问题是当我使用fboTexture时它不起作用。如果我调用batch.draw(sprite,...),那么着色器将在withFbo()和withoutFbo()方法中工作,但我调用batch.draw(fboTexture,...)然后它将只是渲染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();
    }
}

0 个答案:

没有答案