OpenGL GL_UNPACK_ALIGNMENT

OpenGL GL_UNPACK_ALIGNMENT

在阅读所有这些内容并迷失方向之前,我的主要问题是如何解压缩单个像素数组并解释 open gl 如何读取这些数据。 (我的背景不是 c++,但主要是 python)

非常感谢您的帮助。

我使用 maya(3d 程序),它有一个 class 的 MImage 来获取图像的像素数据。 return 结果是一个数组,每 4 个项目都是 RGBA 这就是我需要解压的东西

pixels = [255, 255, 0, 255, 255, 255, 0, 255] //two yellow pixels and it keeps going with this pattern

以下代码创建了一个 256 x 256 的图像,有 4 个通道

unsigned int size = 256;
unsigned int depth = 4;

float color[3] = {1, 1, 0};

unsigned char *pixels = new unsigned char[size * size * depth];
for(unsigned int i = 0; i < size; i += depth){
    MScriptUtil::setUcharArray(pixels, i+0, (int)floorf(color[0] * 255.0f + 0.5f));
    MScriptUtil::setUcharArray(pixels, i+1, (int)floorf(color[1] * 255.0f + 0.5f));
    MScriptUtil::setUcharArray(pixels, i+2, (int)floorf(color[2] * 255.0f + 0.5f));
    pixels[i + 3] = 255;
    }

在调查这个问题时,我遇到了这个片段(它创建了一个检查器模式),它与最后一段代码一起工作并且显示了我正在尝试做的事情

static GLubyte checkImage[256][256][4];

int i, j, c;

for (i = 0; i < 256; i++) {
    for (j = 0; j < 256; j++) {
        c = ((((i&0x8)==0)^((j&0x8))==0))*255;
        checkImage[i][j][0] = (GLubyte) c;
        checkImage[i][j][1] = (GLubyte) c;
        checkImage[i][j][2] = (GLubyte) c;
        checkImage[i][j][3] = (GLubyte) 255;
    }
}

这个数组变成了(而不是 256x256 我把它变成了 2x2)

[[[255, 255, 255, 255], [255, 255, 255, 255]], [[255, 255, 255, 255], [255, 255, 255, 255]]]// so on and so forth

我将 GL_UNPACK_ALIGNMENT 设置为 1,一切正常

我只是想不通 gl 是如何读取数组以及如何告诉它的

这里是我得到的代码证明

glDisable ( GL_LIGHTING );
glEnable(GL_TEXTURE_2D);


glGenTextures(1, &glTextureObject);

// set the current texture to the one just created
glBindTexture (GL_TEXTURE_2D, glTextureObject);

// repeat the texture in both directions
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// use bi-linear filtering on the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);

// load the texture data into the graphics hardware.
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

GL_UNPACK_ALIGNMENT 值不应在此处发挥作用。默认值为 4。由于使用 RGBA 值时每个像素有 4 个字节,因此行的大小始终是 4 的倍数。

只有当每行的数据量不是4的倍数时才需要更改该值。例如,如果您有RGB数据,每个像素3个字节,则必须将其更改为1,除非这些行实际上与 4 字节的倍数对齐,并带有额外的填充。

我在你的代码中看到的真正问题是循环:

for(unsigned int i = 0; i < size; i += depth){
    MScriptUtil::setUcharArray(pixels, i+0, (int)floorf(color[0] * 255.0f + 0.5f));
    MScriptUtil::setUcharArray(pixels, i+1, (int)floorf(color[1] * 255.0f + 0.5f));
    MScriptUtil::setUcharArray(pixels, i+2, (int)floorf(color[2] * 255.0f + 0.5f));
    pixels[i + 3] = 255;
}

由于size是256,这只会用数据填充前256个字节(对应64个像素)。为了匹配定义和其余代码,此循环应为:

for(unsigned int i = 0; i < size * size * depth; i += depth) {