android openGL ES 2.0使用Frame缓冲区渲染纹理两次,但结果是黑屏

时间:2015-08-28 08:34:33

标签: android opengl-es-2.0 framebuffer

我想用openGL来模糊图片。首先我创建了一个帧缓冲区和一个纹理。然后使用glFramebufferTexture2D。第一个drawElement是模糊垂直方向。代码是:

public boolean renderFrameBuffText(){
    GLES20.glGenFramebuffers(1, fb, 0);
    // Bind the framebuffer
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);
    generateFrameBuffTexture();
    // specify texture as color attachment
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,       
    GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, frameTexture[0], 0);
    // check status
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
    if (status != GLES20.GL_FRAMEBUFFER_COMPLETE)
        return false;
    return true;
}
 private void generateFrameBuffTexture(){
    GLES20.glGenTextures(1, frameTexture, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameTexture[0]);
    // parameters - we have to make sure we clamp the textures to the edges
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20
            .GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, texW, texH, 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5, null);
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D,
            frameTexture[0], 0);
}
public void activeFrameBuffer() {
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);
    GLES20.glViewport(0, 0, this.texW, this.texH);
}

在onDrawFrame方法中,首先我得到系统的默认帧缓冲区,然后生成并渲染我自己的帧缓冲区。绑定将要显示的图片的纹理。 :

 GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
 if (hwFrameBuffer == null){
        GLES20.glGetIntegerv(GLES20.GL_FRAMEBUFFER_BINDING, displayFrameBuf,0);

        IntBuffer dims = IntBuffer.allocate(4);
        GLES20.glGetIntegerv( GLES20.GL_VIEWPORT, dims);
         fbWidth = dims.get(2);
         fbHeight = dims.get(3);

        hwFrameBuffer = new HWFrameBuffer(fbWidth,fbHeight);
        hwFrameBuffer.renderFrameBuffText();

    }
    hwFrameBuffer.activeFrameBuffer();
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureData0);

    //set data of the shader
     .......

   GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,    GLES20.GL_UNSIGNED_SHORT,
            drawListBuffer);

然后我将当前缓冲区更改为系统缓冲区。我生成的水平方向渲染纹理以显示模糊图片。但我有一个黑屏

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, displayFrameBuf[0]);
    GLES20.glViewport(0, 0, fbWidth, fbHeight);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, hwFrameBuffer.frameTexture[0]);

  //set data of the shader
     .......

 GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES20.GL_UNSIGNED_SHORT,
            drawListBuffer);

0 个答案:

没有答案