opengl es 2.0 android c ++ glGetTexImage替代 [英] opengl es 2.0 android c++ glGetTexImage alternative

查看:596
本文介绍了opengl es 2.0 android c ++ glGetTexImage替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上进行测试时,代码可以按预期工作,但是在android上,不存在glGetTexImage api,是否还有另一种方法可以从OpenGL获取所有像素,而无需在创建纹理之前对其进行缓存?

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?

这是代码:

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

推荐答案

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

这篇关于opengl es 2.0 android c ++ glGetTexImage替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆