使用OpenGL / LibGDX进行后处理问题 - 黑屏(FBO,着色器,四边形)

时间:2014-10-13 21:44:44

标签: android opengl-es libgdx fbo

我在将后处理阶段渲染到屏幕时遇到了一些问题。我写了一些代码可以在屏幕上渲染好,然后决定我需要一些后期处理(棕褐色效果,但之后会出现)所以我决定将所有内容渲染到FrameBuffer对象,然后使用其颜色纹理来渲染纹理,全屏四核。问题是,即使使用一些简单的着色器,我要么得到一个黑屏(或者我用来清除颜色缓冲区的任何颜色)禁用深度测试,或者一些垃圾像素占据屏幕的左下角四分之一,当启用时深度测试(这只是盲测,我不确定我是否需要在2D游戏中进行深度测试,但我的OpenGL底层很差)。

那么,我做错了什么?这是代码:

TRIVIAL VERTEX SHADER:

attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

varying vec4 vColor;
varying vec2 vTexCoord;

void main() {
    vColor = a_color;
    vTexCoord = a_texCoord0;
    gl_Position = a_position;
}

TRIVIAL FRAGMENT SHADER:

#ifdef GL_ES
precision mediump float;
#endif

varying vec4 vColor;
varying vec2 vTexCoord;

uniform sampler2D u_texture;

void main() {
    vec4 texColor = texture2D(u_texture, vTexCoord);
    gl_FragColor = vColor.rgba * vec4(texColor.rgb, 1.0);
}

INITIALIZATION:

FrambeBuffer fbo;
ShaderProgram focusShader;
Mesh focusQuad;

@Override
public void show() {

    camera1 = new OrthographicCamera();
    camera1.setToOrtho(false);
    camera1.zoom = 0.8f;
    viewport = new ExtendViewport(Globals.VIEWPORT_WIDTH, Globals.VIEWPORT_HEIGHT, camera1);
    batch = new SpriteBatch();
    moveCamera(camera1, player.getX(), player.getY());
    fbo = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    ShaderProgram.pedantic = false;
    focusShader = new ShaderProgram(Gdx.files.internal("shaders/trivial_vshader"), Gdx.files.internal("shaders/trivial_fshader"));
    focusQuad = createFullScreenQuad(); //TAKEN FROM https://github.com/manuelbua/libgdx-contribs/blob/master/postprocessing/src/main/java/com/bitfire/postprocessing/utils/FullscreenQuad.java
    // Other game initialization...
}

RENDER LOOP:

@Override
public void render(float delta) {
    // (UPDATE PHYSICS)...
    fbo.begin();
    {
    /* DRAW THE PLAYER */
        batch.setProjectionMatrix(camera1.combined);
        batch.begin();
        player.drawTrail(batch, delta);
        player.draw(batch, delta);
        batch.end();

        Gdx.gl.glEnable(GL20.GL_BLEND);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        shapeRenderer.setProjectionMatrix(camera1.combined);
        shapeRenderer.begin(ShapeType.Filled);
        map.drawEffects(camera1, shapeRenderer);
        //(OTHER GAME STUFF AND EFFECTS...)
        shapeRenderer.end();
        Gdx.gl.glDisable(GL20.GL_BLEND);
    }
    fbo.end();

    //NOW THE PROBLEM
    Gdx.gl.glClearColor(0, 0.4f, 0.3f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    fbo.getColorBufferTexture().bind();
    focusShader.begin();
    focusShader.setUniformi("u_texture", 0);
    focusQuad.render(focusShader, GL20.GL_TRIANGLE_FAN, 0, 4);
    focusShader.end();
}

1 个答案:

答案 0 :(得分:0)

您是否尝试过在fbo.end之前参考颜色纹理?我不确定fbo end是否会自动处理它。