OpenGL 纹理上传:UNSIGNED_BYTE 与 UNSIGNED_INT_8_8_8_8
我正在调用 glTexSubImage2D。如果我的像素格式为 GL_RGBA
,那么像素类型 GL_UNSIGNED_BYTE
和 GL_UNSIGNED_INT_8_8_8_8
完全等效吗?
另外,这两对等价吗?
Format = GL_RGBA, Type = GL_UNSIGNED_INT_8_8_8_8
Format = GL_BGRA, Type = GL_UNSIGNED_INT_8_8_8_8_REV
我已经尝试阅读 OpenGL 规范和 GL_EXT_packed_pixels 规范,但老实说,我无法做出头或他们的尾巴。
I'm calling glTexSubImage2D. If my pixel format is GL_RGBA
, then are the pixel types GL_UNSIGNED_BYTE
and GL_UNSIGNED_INT_8_8_8_8
fully equivalent?
Also, are these two pairs equivalent?
Format = GL_RGBA, Type = GL_UNSIGNED_INT_8_8_8_8
Format = GL_BGRA, Type = GL_UNSIGNED_INT_8_8_8_8_REV
I've tried reading the OpenGL spec and the GL_EXT_packed_pixels spec, but honestly I can't make head or tail of them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案是否定的。您必须考虑计算机中的字节顺序。如果你有 GL_RGBA 和 GL_UNSIGNED_INT_8_8_8_8 ,这意味着像素存储在 32 位整数中,并且颜色按照逻辑顺序 RGBA 在这样的整数中,例如红色位于高位字节,而 alpha 位于低位字节。但如果机器是小尾数法(如 Intel CPU),则内存中的实际顺序是 ABGR。而
GL_RGBA
和GL_UNSIGNED_BYTE
将以 RGBA 顺序存储字节,无论计算机是小端还是大端。GL_BGRA
和GL_UNSIGNED_INT_8_8_8_8_REV
会按照逻辑顺序 ARGB 将颜色存储在整数中,但在小端机器上,您会在内存中获得 BGRA 顺序。The answers are No and No. You have to think about the byte order in your computer. If you have
GL_RGBA
andGL_UNSIGNED_INT_8_8_8_8
, that means that pixels are stored in 32-bit integers, and the colors are in the logical order RGBA in such an integer, e.g. the red is in the high-order byte and the alpha is in the low-order byte. But if the machine is little-endian (as with Intel CPUs), it follows that the actual order in memory is ABGR. Whereas,GL_RGBA
withGL_UNSIGNED_BYTE
will store the bytes in RGBA order regardless whether the computer is little-endian or big-endian.GL_BGRA
withGL_UNSIGNED_INT_8_8_8_8_REV
would store colors in an integer in the logical order ARGB, but then on a little-endian machine, you get BGRA order in memory.首先查看 JWWalker 的回答。这个答案是对其的补充。一个快速的说明。
GL_RGBA
存储在GL_UNSIGNED_INT_8_8_8_8
中:GL_RGBA' 存储在
GL_UNSIGNED_BYTE` 中:在这两种情况下,RGBA 的逻辑顺序都是 r、g、b、a ,但是在小端机器上(这是正常的架构),
0xrrggbbaa
有小字节(最不重要的)首先存储。如果您一次读取uint32_t
一个字节,您将首先得到0xaa
! C++ 示例:first_byte
将等于0x44
。有一点令人困惑的是“第一”这个词。它可以表示“写入时首先出现”,如“红色字节在 0xrrggbbaa 中首先出现”。这与“具有最低的内存/指针地址”不同,如“使用小端编码时,alpha 字节位于 0xrrggbbaa 中的第一个”!当您使用 GL_RGBA 时,看起来红色肯定会排在第一位,但是当以 4 字节整数小端字节序编码时,它在十六进制表示中只是这样。
See JWWalker's answer first. This answer is a supplement to it. A quick illustration.
GL_RGBA
stored inGL_UNSIGNED_INT_8_8_8_8
:GL_RGBA' stored in
GL_UNSIGNED_BYTE`:In both cases, RGBA is in the logical order r, g, b, a, but on a little endian machine (this is the normal architecture),
0xrrggbbaa
has the little byte (least-significant) stored first. If you read theuint32_t
one byte at a time, you'll get0xaa
first! A C++ example:first_byte
will equal0x44
.One thing that's confusing is the word "first". It can mean "appearing first when written" as in "The red byte is first in 0xrrggbbaa". This is different from "having the lowest memory/pointer address" as in "The alpha byte is first in 0xrrggbbaa when encoded using little-endian"! When you use
GL_RGBA
, it sure looks like red will be first, but when encoded in a 4-byte integer little endian, it is only that way in the hex representation.