Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android read pixels from GL_TEXTURE_EXTERNAL_OES

I am trying to read pixels/data from an OpenGL texture which is bound to GL_TEXTURE_EXTERNAL_OES.

The reason for binding the texture to that target is because in order get live camera feed on android a SurfaceTexture needs to be created from an OpenGL texture which is bound to GL_TEXTURE_EXTERNAL_OES.

Since android uses OpenGL ES I can't use glGetTexImage() to read the image data.

Therefore I am binding the target to an FBO and then reading it using readPixels(). This is my code:

    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

    //Attach 2D texture to this FBO
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES, cameraTexture, 0);
    status("glFramebufferTexture2D() returned error %d", glGetError());

However I am getting error 1282 (GL_INVALID_OPERATION) for some reason.

like image 625
amirhbp Avatar asked Apr 23 '14 23:04

amirhbp


1 Answers

I think this might be the problem:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_EXTERNAL_OES, cameraTexture, 0);

You should not attach the cameraTexture to the framebuffer, instead you should generate a new texture in the format of GL_TEXTURE_2D

glGenTextures(1, mTextureHandle, 0);

glBindTexture(GL_TEXTURE_2D, mTextureHandle[0]);

...

cameraTexture is the one you get from SurfaceTexture, and it is the source used for rendering. This new texture is the one you should render to (which could be used later in the rendering pipeline). Then do this:

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, mTextureHandle[0], 0);

cameraTexture is drawn to the framebuffer's attached texture, using a simple shader program to draw, bind the cameraTexture when the shader program is in use:

glBindTexture(GL_TEXTURE_EXTERNAL_OES, cameraTexture); 
like image 179
Yu Lu Avatar answered Sep 28 '22 15:09

Yu Lu