June 23, 2015

FBO on Android

private void initFBO(int width, int height) {
  hFBOTex = new int[1];
  GLES20.glGenTextures(1,hFBOTex,0);
  GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, hFBOTex[0]);
  GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D,0,GLES20.GL_RGBA,width,height,0,GLES20.GL_RGBA,GLES20.GL_UNSIGNED_BYTE,null);
  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.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
  GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

  hFBO = new int[1];
  GLES20.glGenFramebuffers(1,hFBO,0);
  GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,hFBO[0]);
  GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER,GLES20.GL_COLOR_ATTACHMENT0,GLES20.GL_TEXTURE_2D,hFBOTex[0],0);

  int[] depth;
  depth = new int[1];
  GLES20.glGenRenderbuffers(1,depth,0);
  GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER,depth[0]);
  GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER,GLES20.GL_DEPTH_COMPONENT16,width,height);
  GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER,GLES20.GL_DEPTH_ATTACHMENT,GLES20.GL_RENDERBUFFER,depth[0]);

  if ( GLES20.glGetError() != GLES20.GL_NO_ERROR) Log.i("mr","initFBO - rbo");

  if(GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER) != GLES20.GL_FRAMEBUFFER_COMPLETE)
    Log.e("mr","initFBO");
}

public void onDrawFrame ( GL10 unused ) {
  GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,hFBO[0]);
  // draw somethings to FBO

  GLES20.glFlush();
  // read a block of pixels from back buffer in double-buffered configurations
  ByteBuffer buf = ByteBuffer.allocateDirect(width*height*4);
  GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);

  GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER,0);
  // draw somethings to frame(back) buffer
  // you can use hFBOTex[0] as a texture
}

No comments:

Post a Comment