问题描述
大多数图像库都给出图像的宽度,高度,间距和数组像素.但是OpenGL并不直接推销,而是期望:
Most image library give an image as width, height, pitch and then an array pixels. OpenGL however does not take a pitch directly, instead it expects:
glPixelStorei(GL_UNPACK_ALIGNMENT, align);
glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
row_length
是pitch / bytes_per_pixel
,但是如何计算align
的正确值?
row_length
is pitch / bytes_per_pixel
, but how does one calculate the proper value for align
?
推荐答案
对于您将看到的绝大多数图像,您不需要GL_ROW_LENGTH
.将此值设置为非零值可能会严重降低上传性能;仅在绝对需要时使用它.
For the vast majority of images that you'll see, you shouldn't need GL_ROW_LENGTH
. Setting this to a non-zero value can degrade upload performance severely; only employ it when you absolutely need it.
对于大多数图像,间距将只是(width
* bpp
),与某个边界对齐.例如,如果bpp
为3(每像素字节),并且宽度为9,则得到27.对于许多计算机而言,这不是一个很好的行号,因此它们通常会将其对齐为最接近4的倍数. .因此,您在大多数图像中看到的螺距是28.
For most images, the pitch will simply be (width
* bpp
), aligned to some boundary. For example, if the bpp
is 3 (bytes per pixel), and the width is 9, you get 27. That's not a nice row number for many computers, so they'll often align that up to the nearest multiple of 4. Thus the pitch you would see in most images is 28.
因此,要计算对齐方式,只需计算分为螺距的2的最大乘方(最多8).如果图像中的音高为28,则2的最大幂为4.将其设置为GL_UNPACK_ALIGNMENT
.通常,您可以停在4,因为这是大多数图像使用的最大对齐方式.
So to compute the alignment, simply compute the largest power of two (up to 8) that divides into the pitch. If the pitch in the image is 28, the largest power of two is 4. Set that to GL_UNPACK_ALIGNMENT
. Generally, you can stop at 4, because that's the largest alignment most images will use.
需要使用GL_ROW_LENGTH
的时间是计算的音高(align(width * bpp, alignment)
)与实际音高不相等时.如果发生这种情况,则需要同时使用对齐方式和行长.但这是一种罕见的情况,您可能应该只丢弃任何使用这种极其错误的间距的图像.
The times when you need to use GL_ROW_LENGTH
are when the computed pitch (align(width * bpp, alignment)
) does not equal the actual pitch. If that happens, then you need to use both the alignment and the row length. But this is such a rare occurrence that you should probably just discard any image that uses such a wildly incorrect pitch.
这篇关于给定图像的间距,如何计算GL_UNPACK_ALIGNMENT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!