opengl-esopengl-es-2.0opengl-es-3.0

opengl es 2.0 android c++ glGetTexImage alternative


when testing on windows the code works as expected, but on android the glGetTexImage api doesn't exist, is there an other way of getting all the pixels from OpenGL without caching them before creating the texture?

this is the code:

void Texture::Bind(int unit)
{
    glActiveTexture(GL_TEXTURE0 + unit);
    glBindTexture(GL_TEXTURE_2D, mTextureID);
}

GLubyte* Texture::GetPixels()
{
    Bind();

    int data_size = mWidth * mHeight * 4;

    GLubyte* pixels = new GLubyte[mWidth * mHeight * 4];

    glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

    return pixels;
}

Solution

  • glGetTexImage doesn't exist in OpenGL ES.
    In OpenGL ES, you have to attach the texture to a framebuffer and read the color plane from the framebuffer by glReadPixels

    Bind();
    int data_size = mWidth * mHeight * 4;
    GLubyte* pixels = new GLubyte[mWidth * mHeight * 4];
    
    GLuint textureObj = ...; // the texture object - glGenTextures  
    
    GLuint fbo;
    glGenFramebuffers(1, &fbo); 
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureObj, 0);
    
    glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glDeleteFramebuffers(1, &fbo);