2015-02-10 67 views
0

我試圖渲染到紋理,然後將紋理繪製到正方形中。在創建幀緩衝區時,我在glFramebufferRenderbufferglFramebufferTexture2D調用後得到無效操作。這裏是應該創建幀緩衝區的功能:OpenGL ES 2.0創建幀緩衝區無效操作

private void createFrameBuffer() 
{ 
    final int[] texture = new int[1]; 
    frameTextureID = texture[0]; 
    GLES20.glGenTextures(1,texture,0); 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, frameTextureID); 

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER,GLES20.GL_NEAREST); 
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,GLES20.GL_NEAREST); 
    GLES20.glTexParameteri(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_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null); 

    final int[] depthBuffer = new int[1]; 
    GLES20.glGenRenderbuffers(1, depthBuffer, 0); 
    renderBufferID = depthBuffer[0]; 

    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, depthBuffer[0]); 
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, width, height); 

    final int[] frameBuffer = new int[1]; 
    GLES20.glGenFramebuffers(1,frameBuffer,0); 
    frameBufferID = frameBuffer[0]; 
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,frameBufferID); 

    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, frameTextureID, 0); 
    error("glFramebufferTexture2D"); 
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT , GLES20.GL_RENDERBUFFER, renderBufferID); 
    error("glFramebufferRenderbuffer"); 

    frameBufferComplete("createFrameBuffer"); 

    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0); 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 
} 

我不明白這是什麼問題。

ps:當我檢查函數結束時,幀緩衝區已完成。

回答

0

這些前幾個語句順序錯誤:

final int[] texture = new int[1]; 
frameTextureID = texture[0]; 
GLES20.glGenTextures(1,texture,0); 

才把它是由glGenTextures()設定的texture[0]值。所以frameTextureID將始終爲0

相反,這應該是:

final int[] texture = new int[1]; 
GLES20.glGenTextures(1,texture,0); 
frameTextureID = texture[0]; 

還要注意的是RGBA8(RGBA與8位分量)不能保證在ES 2.0渲染格式。這是你的紋理使用GL_RGBAGL_UNSIGNED_BYTE的組合。許多實現通過OES_rgb8_rgba8 extension來支持它。但是,如果您想完全確定您的代碼適用於所有設備,則需要使用RGB565等支持的格式之一:

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, 
        GLES20.GL_RGBA, GLES20.GL_UNSIGNED_SHORT_5_6_5, null); 
相關問題