在阅读所有这些和迷路之前,我的主要问题是如何解压一个pixles数组,并解释如何打开gl来读取这些数据。(我的背景不是c++,主要是python)
非常感谢你的帮助。
我使用maya(3d程序),它有一类MImage来获取图像的像素数据。返回的结果是一个数组,每4项都是RGBA,这是我需要解压的
pixels = [255, 255, 0, 255, 255, 255, 0, 255] //two yellow pixels and it keeps going with this pattern下面的代码创建带有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;
}查看这个问题,我看到了一个片段(它创建了一个检查器模式),它使用最后一个代码块工作,并显示出我试图做的事情。
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 i使它成为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);发布于 2015-11-24 07:29:57
这里不应该使用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) {https://stackoverflow.com/questions/33887344
复制相似问题