OpenGL ES | Access mipmap array

819 views Asked by At

In my android application I have texture with mipmap array

final int[] textureHandle = new int[1]; 
GLES20.glGenTextures(1, textureHandle, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureHandle);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, filter);
GLES20.glGenerateMipmap(GLES20.GL_TEXTURE_2D);

Initial size of texture is 1024x1024. I need get textures:

  • 512x512 (level 1)
  • 256x256 (level 2)
  • 128x128 (level 3)

Has OpenGL any api to access textures from mipmap array?

1

There are 1 answers

0
Reto Koradi On BEST ANSWER

Full OpenGL has a glGetTexImage() call for this purpose. But it's not available in any version of OpenGL ES, up to the current ES 3.1.

Without having this call, you can set the texture as an FBO attachment, and then read the content with glReadPixels(). To read level lev:

int[] fboIds = new int[1];
GLES20.glGenFramebuffers(1, fboIds, 0);
int fboId = fboIds[0];
glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                       GLES20.GL_TEXTURE_2D, mTextureHandle, lev);
glReadPixels(...);
glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

This approach is somewhat restricted because it will only work for texture formats that are color-renderable. Other formats will not be valid as FBO attachments. The only formats guaranteed to be color-renderable in ES 2.0 are RGBA4444, RGB5_A1 and RGB565. Many devices have extensions that add support for formats like RGB8 and RGBA8.

The next best approach is to render a quad with the texture, and then read the result with glReadPixels(). But you would have the same restrictions on the format of the render target. For example if your texture is RGBA8, and your device does not support rendering to RGBA8 surfaces, you would lose precision with this approach.