用 glReadPixels 读取纹理字节? [英] Read texture bytes with glReadPixels?

查看:28
本文介绍了用 glReadPixels 读取纹理字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将原始纹理数据转储到磁盘(以便稍后读取),但我不确定 glReadPixel 是否会从当前绑定的纹理中读取.

I want to dump raw texture data to disk (to read back later), and I'm not sure about glReadPixel will read from the currently bound texture.

如何从纹理中读取缓冲区?

How can I read the buffer from my texture?

推荐答案

glReadPixels 函数从帧缓冲区读取,而不是从纹理读取.要读取纹理对象,您必须使用 glGetTexImage它在 OpenGL ES 中不可用 :(

glReadPixels function reads from framebuffers, not textures. To read a texture object you must use glGetTexImage but it isn't available in OpenGL ES :(

如果您想从纹理中读取缓冲区,那么您可以将其绑定到 FBO(FrameBuffer 对象)并使用 glReadPixels:

If you want to read the buffer from your texture then you can bind it to an FBO (FrameBuffer Object) and use glReadPixels:

//Generate a new FBO. It will contain your texture.
glGenFramebuffersOES(1, &offscreen_framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreen_framebuffer);

//Create the texture 
glGenTextures(1, &my_texture);
glBindTexture(GL_TEXTURE_2D, my_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

//Bind the texture to your FBO
glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, my_texture, 0);

//Test if everything failed    
GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
if(status != GL_FRAMEBUFFER_COMPLETE_OES) {
    printf("failed to make complete framebuffer object %x", status);
}

然后,您只需在要从纹理中读取数据时调用 glReadPixels:

Then, you only must call to glReadPixels when you want to read from your texture:

//Bind the FBO
glBindFramebufferOES(GL_FRAMEBUFFER_OES, offscreen_framebuffer);
// set the viewport as the FBO won't be the same dimension as the screen
glViewport(0, 0, width, height);

GLubyte* pixels = (GLubyte*) malloc(width * height * sizeof(GLubyte) * 4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

//Bind your main FBO again
glBindFramebufferOES(GL_FRAMEBUFFER_OES, screen_framebuffer);
// set the viewport as the FBO won't be the same dimension as the screen
glViewport(0, 0, screen_width, screen_height);

这篇关于用 glReadPixels 读取纹理字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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