gpt4 book ai didi

ios - 使用 glReadPixels 读取纹理字节?

转载 作者:技术小花猫 更新时间:2023-10-29 10:19:38 26 4
gpt4 key购买 nike

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

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

最佳答案

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

如果你想从你的纹理中读取缓冲区,那么你可以将它绑定(bind)到一个 FBO(FrameBuffer 对象)并使用 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:

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

关于ios - 使用 glReadPixels 读取纹理字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com