2016-04-03 18 views
1

Ekrana dönüştürülecek bir sorunum yok ama dokuya oluşturmaya çalıştığımda tamamen siyah olacak. Doku hazırlarken bir aşamada eksik olduğumu düşünüyorum.OpenGL ES 2.0 - Render to texture all siyah

Kodu:

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(); 

ben eksik ne fikrin var mı?

cevap

1

Uzun zamandır opengl kullanmamıştım, ama GL_RGBA'yı hem internalFormat hem de format için kullanmanız ve GL_UNSIGNED_BYTE türünde kullanmanız ve veri için null değerini geçmeniz gerektiğini düşünüyorum.

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

Teşekkürler, ben sorunu güncelledik. Yine de tüm bir siyah doku :( – Plarsen

+1

Opengl'in acıyı hissederim ... texBuffer'ı çıkarmayı deneyin ve boş bırakın GLES20.glTexImage2D (GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 800, 400, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null) arabelleğe ihtiyacınız yok –

+1

Teşekkürler – Plarsen