2015-11-24 22 views
0

在閱讀所有這些並迷路之前,我的主要問題是如何解開單個像素數組並解釋open gl是如何讀取這些數據的。 (我的背景是不是在C + +但大多是蟒蛇)OpenGL GL_UNPACK_ALIGNMENT

你的幫助是非常感謝。

即時通訊使用具有一類MImage的maya(3d程序)來獲取圖像的像素數據。 返回結果是一個單一的陣列,每4項RGBA 這就是我需要解壓

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

下面的代碼創建了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; 
    } 

尋找到256×256的圖像這個問題我遇到了這個代碼片段(它創建了一個檢查器模式),它與最後一塊代碼一起工作,並顯示出我試圖做的事情

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; 
    } 
} 

這個數組成爲(而不是256×256我做到了2×2)

[[[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); 
+0

您僅環爲256個字節(64個像素)寫入數據,因爲'size'是循環計數器的上限。 –

+0

Wowww我一直在移動這麼多,我一定錯過了。謝謝 – cronicryo

回答

5

GL_UNPACK_ALIGNMENT值不應該來這裏發揮作用。默認值爲4.由於使用RGBA值時每個像素有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) { 
+0

感謝我自己的愚蠢哈哈 – cronicryo