OpenGL GL_UNPACK_ALIGNMENT

cro*_*ryo 1 c++ opengl maya

在阅读所有这些并迷失之前,我的主要问题是如何解压缩单个像素阵列并解释open gl如何读取这些数据.(我的背景不是c ++,而是python)

非常感谢您的帮助.

我正在使用具有一类MImage的maya(3d程序)来获取图像的像素数据.返回结果是一个单独的数组,每4个项目是RGBA这是我需要解压缩

pixels = [255, 255, 0, 255, 255, 255, 0, 255] //two yellow pixels and it keeps going with this pattern
Run Code Online (Sandbox Code Playgroud)

以下代码创建一个包含4个通道的256 x 256图像

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;
    }
Run Code Online (Sandbox Code Playgroud)

在调查这个问题时,我遇到了这个代码片段(它创建了一个检查器模式),最后一段代码可以正常运行并显示我想要做的事情

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这个数组成为(而不是256x256我使它成为2x2)

[[[255, 255, 255, 255], [255, 255, 255, 255]], [[255, 255, 255, 255], [255, 255, 255, 255]]]// so on and so forth
Run Code Online (Sandbox Code Playgroud)

有了这个,我将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);
Run Code Online (Sandbox Code Playgroud)

Ret*_*adi 5

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;
}
Run Code Online (Sandbox Code Playgroud)

由于size是256,这将仅填充数据的前256个字节(对应于64个像素).要匹配定义和代码的其余部分,此循环应为:

for(unsigned int i = 0; i < size * size * depth; i += depth) {
Run Code Online (Sandbox Code Playgroud)