2016-04-03 147 views
1

我有一個場景,渲染到屏幕沒有問題,但是當我嘗試渲染紋理時,它將全部爲黑色。我想我在準備紋理時錯過了一些階段。OpenGL ES 2.0 - 渲染到紋理全是黑色

代碼:

int[] FBO_main = new int[1]; 
    int[] textureId = new int[1]; 
    int[] renderBufferId = new int[1]; 

    // create framebuffers 
    GLES20.glGenFramebuffers(FBO_main.length, FBO_main, 0); 

    // create texture object 
    GLES20.glGenTextures(1, textureId, 0); 

    // create render buffer 
    GLES20.glGenRenderbuffers(1, renderBufferId, 0); 

    // Bind Frame buffer 
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]); 

    // Bind texture 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]); 

    int[] buf = new int[800*400]; 
    IntBuffer texBuffer; 
    texBuffer = ByteBuffer.allocateDirect(buf.length*4).order(ByteOrder.nativeOrder()).asIntBuffer(); 

    // Texture parameters 
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, 
      GLES20.GL_RGBA, 800, 400, 0, 
      GLES20.GL_RGBA, 
      GLES20.GL_UNSIGNED_BYTE, texBuffer); 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_MAG_FILTER, 
      GLES20.GL_LINEAR); 
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_MIN_FILTER, 
      GLES20.GL_LINEAR); 
    GLES20.glTexParameterf(
      GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_WRAP_S, 
      GLES20.GL_CLAMP_TO_EDGE); 
    GLES20.glTexParameterf(
      GLES20.GL_TEXTURE_2D, 
      GLES20.GL_TEXTURE_WRAP_T, 
      GLES20.GL_CLAMP_TO_EDGE); 
    GLES20.glTexParameteri(
      GLES20.GL_TEXTURE_2D, 
      GLES20.GL_GENERATE_MIPMAP_HINT, 
      GLES20.GL_TRUE); // automatic mipmap 

    // Bind render buffer and define buffer dimension 
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBufferId[0]); 
    GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, 800, 400); 

    // Attach texture FBO color attachment 
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, textureId[0], 0); 

    // Attach render buffer to depth attachment 
    GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderBufferId[0]); 

    // Reset 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); 
    GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, 0); 
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); 

    // Setup render to texture 
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, FBO_main[0]); 
    GLES20.glViewport(0, 0, 800, 400); 

    // Draw gamespecific 
    onDrawGame(timediff, time); 

    // Draw to buffer 
    GLES20.glFlush(); 

    // Use buffer as input texture 
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]); 
    GLES20.glUniform1i(GLES20.glGetUniformLocation(cube.program, "sampler_prev"), 0); 

    // switch to screen output 
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); 

    // Draw gamespecific 
    onDrawGame(timediff, time); 

    // Draw 
    GLES20.glFlush(); 

我缺少什麼的任何想法?

回答

1

我很久沒有使用過opengl了,但是我認爲你應該使用GL_RGBA作爲內部格式和格式,並使用GL_UNSIGNED_BYTE類型,併爲數據傳遞null。

GLES20.glTexImage2D(
      GLES20.GL_TEXTURE_2D, 0, 
      GLES20.GL_RGBA, 
      800, 600, 0, 
      GLES20.GL_RGBA, 
      GLES20.GL_UNSIGNED_BYTE, null); 
+0

謝謝,我已經更新了這個問題。儘管仍然是全黑色紋理:( – Plarsen

+1

我覺得你的opengl的疼痛eheh ...嘗試刪除texBuffer並傳遞null GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0, GLES20.GL_RGBA,800,400,0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,null)你不應該需要緩衝區。 –

+1

謝謝! – Plarsen