OpenGL:将图像加载到纹理数组中
我正在尝试通过以下代码片段将多个纹理(从 8x8 *.bmp 图像)加载到多个 FBO 中:
glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8I_EXT, TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_NUM, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
for( int i = 0; i < TEXTURE_NUM; i++ )
{
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBO_ID[i]);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
glFramebufferTextureLayerEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, TexArray_ID, 0, i);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, RBO_ID[i]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
checkGLErrors("begin check");
for ( int i = 0; i < TEXTURE_NUM; i++ )
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
char fileName[32];
sprintf(fileName, "./img/%02d.bmp", i);
unsigned char* imgData;
imgData = loadBMP(fileName);
if (imgData)
printf("imgData %s is successfully loaded\n",fileName);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
checkGLErrors("end check");
free(imgData);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, NULL);
}
但是,我无法使用 glTexSubImage3D 函数附加这些纹理。 CheckGLErrors() 指示错误出现在以下行中,指出“无效值”错误。
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
阅读 手册 后,我认为该错误与以下声明:
如果 xoffset
则生成 GL_INVALID_VALUE - b , xoffset + 宽度 > w - b , yoffset < - b ,或 yoffset + height > h - b ,或 zoffset < - b ,或 zoffset + 深度 > d - b ,其中 w 是 GL_TEXTURE_WIDTH,h 是 GL_TEXTURE_HEIGHT,d 是 GL_TEXTURE_DEPTH,b 是要修改的纹理图像的边框宽度。请注意,w、h 和 d 包括两倍的边框宽度。
这让我不知道如何让这些纹理发挥作用。有什么想法吗?提前致谢。
I'm trying to load multiple textures (from 8x8 *.bmp images) into multiple FBOs, by the following snippet:
glTexImage3D(GL_TEXTURE_2D_ARRAY_EXT, 0, GL_RGB8I_EXT, TEXTURE_WIDTH, TEXTURE_HEIGHT, TEXTURE_NUM, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
for( int i = 0; i < TEXTURE_NUM; i++ )
{
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, RBO_ID[i]);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
glFramebufferTextureLayerEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, TexArray_ID, 0, i);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, RBO_ID[i]);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
checkGLErrors("begin check");
for ( int i = 0; i < TEXTURE_NUM; i++ )
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBO_ID[i]);
char fileName[32];
sprintf(fileName, "./img/%02d.bmp", i);
unsigned char* imgData;
imgData = loadBMP(fileName);
if (imgData)
printf("imgData %s is successfully loaded\n",fileName);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
checkGLErrors("end check");
free(imgData);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, NULL);
}
However, I cannot get these textures attached using glTexSubImage3D function. The CheckGLErrors() indicates that the error is in the following line, stating "Invalid Value" error.
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 8, 8, 1, GL_RGB, GL_UNSIGNED_BYTE, imgData);
After reading the manual , i think the error is associated with the following statement:
GL_INVALID_VALUE is generated if xoffset < - b , xoffset + width > w - b , yoffset < - b , or yoffset + height > h - b , or zoffset < - b , or zoffset + depth > d - b , where w is the GL_TEXTURE_WIDTH, h is the GL_TEXTURE_HEIGHT, d is the GL_TEXTURE_DEPTH and b is the border width of the texture image being modified. Note that w, h, and d include twice the border width.
Which left me no clue on how to get these textures work. Any Ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您想要将新图像渲染到纹理中时,将纹理附加到 FBO 才有意义。然而,您已经拥有图像文件形式的图像,因此绝对没有理由将纹理附加到 FBO(除非您想在加载将“牺牲给 FBO”的图像时浪费内存和 CPU 周期)。
摆脱对 FBO 的滥用,你应该没问题。
也许您将 FBO(帧缓冲区对象)与 PBO(像素缓冲区对象)混淆了,它们是不同类型的事物,如果您愿意,实际上使用它是有意义的加速纹理生成。
由于评论而编辑
你真正需要的是一个纹理对象
Attaching textures to an FBO only makes sense if you want to render a new image into the texture. However you already have the images in form of image files, so there's absolutely no reason to attach the textures to an FBO (unless you want to waste memory and CPU cycles on loading images that are going to be "sacrificed to the FBO").
Get rid of that misuse of FBOs and you should be fine.
Maybe you confused FBOs (Frame Buffer Objects) with PBOs (Pixel Buffer Objects), which are are different kind of thing and actually make sense to use if you want to accelerate texture generation.
EDIT due to comment
What you actually need is a Texture Object