OpenGL ES 2.0 Android C++ glGetTexImage替代方案

7

在Windows上测试时,代码按预期工作,但在Android上,glGetTexImage API不存在,是否有其他方法从OpenGL获取所有像素而不是在创建纹理之前将它们缓存?

以下是代码:

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;
}
1个回答

16

glGetTexImage在OpenGL ES中不存在。
在OpenGL ES中,您需要将纹理附加到帧缓冲区,并通过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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接