在我的 OpenGL 程序中,我正在加载一个 24BPP 图像,其宽度为501
. GL_UNPACK_ALINGMENT
参数设置4
为。他们写它不应该工作,因为正在上传的每一行的大小 ( 501*3 = 1503
) 不能除以 4。但是,我可以在显示它时看到没有人工制品的正常纹理。
所以我的代码有效。我正在考虑为什么要充分理解这一点并防止整个项目被窃听。
也许(?)它有效,因为我不只是打电话glTexImage2D
。相反,起初我正在创建一个适当的(尺寸为 2 的幂)空白纹理,然后使用glTexSubImage2D
.
编辑:
但是你觉得写这样的代码有意义吗?
// w - the width of the image
// depth - the depth of the image
bool change_alignment = false;
if (depth != 4 && !is_divisible(w*depth)) // *
{
change_alignment = true;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
// ... now use glTexImage2D
if (change_alingment) glPixelStorei(GL_UNPACK_ALIGNMENT, 4); // set to default
// * - of course we don't even need such a function
// but I wanted to make the code as clear as possible
希望它可以防止应用程序崩溃或故障?