opengl es 2.0 android c ++ glGetTexImage替代

时间:2019-01-01 07:39:27

标签: opengl-es opengl-es-2.0

在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 个答案:

答案 0 :(得分:4)

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);