2017-01-21 74 views
0

我想在OpenGL上保存窗口的所有RGB值。 并想检查值为'int'(因为我必须使用它) 我试图用for循环保存每个像素,它的工作原理。 但是,如果我试图glReadpixels onece,它无法检查。什么问题?整个窗口的glReadPixels(OpenGL)

此代码有效。 (正确保存像素RGB,我可以使用cout来检查它)

int width = 50; 
int height = 50; 
for(int i=0; i<height; i++) 
{ 
    for(int j=0; j<width; j++) 
    { 
     unsigned char pick_col[3]; 
     glReadPixels(j , i , 1 , 1 , GL_RGB , GL_UNSIGNED_BYTE , pick_col); 
     cout << (int)pick_col[0] << " " << (int)pick_col[1] << " " << (int)pick_col[2] << endl; 
    } 
} 

但是这段代码不起作用。 (在像素阵列中有奇怪的值,几个值是正确的)

GLubyte pixelarray[width*height*3]; 
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixelarray); 

for(int i=0; i<height; i++) 
{ 
    for(int j=0; j<width; j++) 
    { 
     cout << (int)pixelarray[i*width*3 + j*3] << " " (int)pixelarray[i*width*3 + j*3 +1] << " " << (int)pixelarray[i*width*3 + j*3+2] << endl; 
    } 
    cout << endl; 
} 
+3

确保['GL_PACK_ALIGNMENT'(https://www.opengl.org/sdk/docs/man/html/glPixelStore.xhtml)是否设置正确 – derhass

回答

0

我解决了问题。它应该是GL_RGBA和4通道阵列

GLubyte pixelarray[width*height*4]; 
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixelarray); 

for(int i=0; i<height; i++) 
{ 
    for(int j=0; j<width; j++) 
    { 
     cout << (int)pixelarray[i*width*4 + j*4] << " "  (int)pixelarray[i*width*4 + j*4 +1] << " " << (int)pixelarray[i*width*4 + j*4+2]  << endl; 
    } 
    cout << endl; 
} 
+0

嘛。使用RGB也可以,因为你可以设置正确的包装对齐... – derhass