iPhone 上的 BGRA glTexImage2D 和 glReadPixels

发布于 2024-10-18 11:16:43 字数 852 浏览 6 评论 0原文

查看文档,我应该能够使用 BGRA 作为纹理的内部格式。我为纹理提供 BGRA 数据(使用 GL_RGBA8_OES 进行 glRenderbufferStorage,因为 BGRA 似乎是不允许的)。但是,以下内容不起作用:

glTexImage2D(GL_TEXTURE_2D, 0, **GL_BGRA**, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, GL_BGRA, GL_UNSIGNED_BYTE,buffer, 0);

虽然这给了我一个黑框:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, **GL_BGRA**, GL_UNSIGNED_BYTE,buffer, 0);

这确实有效,但蓝色/红色是反转的(我向纹理提供 BGRA 数据):

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, **GL_RGBA**, GL_UNSIGNED_BYTE,buffer, 0);

...为什么我不能在整个过程中只使用 BGRA ?我确实注意到 glRenderbufferStorage 似乎不接受任何 BGRA 格式......我真的很困惑。 BGRA 是我的数据唯一合适的格式,因为它来自 iPhone 的相机。

Looking at the docs, I should be able to use BGRA for the internal format of a texture. I am supplying the texture with BGRA data (using GL_RGBA8_OES for glRenderbufferStorage as it seems BGRA there is not allowed). However, the following does not work:

glTexImage2D(GL_TEXTURE_2D, 0, **GL_BGRA**, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, GL_BGRA, GL_UNSIGNED_BYTE,buffer, 0);

While this gives me a black frame:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, **GL_BGRA**, GL_UNSIGNED_BYTE,buffer, 0);

And this does work, but the blues/reds are inverted (I supply BGRA data to the texture):

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
...
glReadPixels(0, 0, w,h, **GL_RGBA**, GL_UNSIGNED_BYTE,buffer, 0);

...why can't I just use BGRA throughout? I do notice that glRenderbufferStorage does not seem to accept any BGRA formats...I'm really confused. BGRA is the only suitable format my data is in, as it comes from the iphone's camera.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骷髅 2024-10-25 11:16:43

glTexImage2D() 的第三个参数是纹理中颜色分量的数量,而不是纹理的像素顺序。您想在此处使用GL_RGBA,否则它将无法工作。

我不相信 iOS 设备上的 glReadPixels() 支持 GL_BGRA 作为颜色格式。虽然 Apple 在处理视频图像帧时建议以 BGRA 格式向纹理提供像素数据,但我认为您可以以 RGBA 格式读回该数据,然后将其编码到磁盘,正如您在其他地方所描述的那样。

如果您想查看一个示例项目,该项目在 BGRA 中获取相机视频帧,将它们发送到纹理,使用着色器处理它们,然后读回生成的像素,您可以查看我构建的一个 此处

The third parameter to glTexImage2D() is the number of color components in the texture, not the pixel ordering of the texture. You want to use GL_RGBA here, or it just won't work.

I don't believe GL_BGRA is supported by glReadPixels() on iOS devices as a color format. While providing pixel data to the textures in BGRA format is recommended by Apple when processing video image frames, I think you're fine in reading that back in RGBA format and then encoding that to disk, as you've described elsewhere.

If you want to see a sample project that takes camera video frames in BGRA, sends them to a texture, processes them using shaders, and then reads the resulting pixels back, you can check out the one I built here.

尝蛊 2024-10-25 11:16:43

我自己对 OpenGL ES2 的研究表明,这适用于 iOS 下的 BGRA 小端本机格式。这里的“像素”指向 32 位 BGRA 的缓冲区,该缓冲区来自 CGBitmapContextCreate() 或从 CGImageRef 中抓取图像数据。

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);

请在此处查看此 Apple 扩展。
此处对此问题进行了很好的讨论

My own looking around for OpenGL ES2 indicates that this works for the BGRA little endian native format under iOS. Here "pixels" points to a buffer of 32 bit BGRA that would come from a CGBitmapContextCreate() or grabbing the image data from out of a CGImageRef.

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);

See the apple ext for this here.
Good discussion of this issue here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文