2017-01-21 14 views
0

Ich möchte alle RGB-Werte des Fensters auf OpenGL speichern. Und wollen die Werte als 'int' überprüfen (weil ich es verwenden muss) Ich habe versucht, es durch jede Pixel mit For-Schleife zu speichern, und es funktioniert. Wenn ich jedoch versucht, glReadpixels onece, es nicht zu überprüfen. Was ist das Problem?glReadPixels für ganze Fenster (OpenGL)

Dieser Code funktioniert. (Speichert Pixel-RGB richtig, und ich kann es überprüfen cout mit)

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

Aber dieser Code funktioniert nicht. (Es gibt seltsame Werte im Pixel-Array. Mehrere Werte sind korrekt)

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

sicherstellen, dass der [ 'GL_PACK_ALIGNMENT'] (https://www.opengl.org/sdk/docs/man/html/glPixelStore.xhtml) richtig eingestellt ist – derhass

Antwort

0

Ich habe das Problem gelöst. Es sollte

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

Well GL_RGBA und 4-Kanal-Array sein. Die Verwendung von RGB würde auch funktionieren, vorausgesetzt, Sie würden die korrekte Ausrichtung der Pakete einstellen ... – derhass

Verwandte Themen