2011-08-29 140 views
0

我想創建一個核心OpenGL上下文的圖像。拍攝CGL內容的快照?

我用下面的代碼,但它創建一個黑色的圖像。所以我想我不能在那裏使用glReadPixles?還有其他建議嗎?

int myDataLength = 480 * 480 * 4; 
// allocate array and read pixels into it. 
GLubyte *buffer = (GLubyte *) malloc(myDataLength); 
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 
    // gl renders "upside down" so swap top to bottom into new array. 
// there's gotta be a better way, but this works. 
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength); 
for(int y = 0; y < 480; y++) 
{ 
    for(int x = 0; x < 320 * 4; x++) 
    { 
     buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x]; 
    } 
} 

// make data provider with data. 
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); 

// prep the ingredients 
int bitsPerComponent = 8; 
int bitsPerPixel = 32; 
int bytesPerRow = 4 * 320; 
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; 
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; 

// make the cgimage 
CGImageRef image= CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, false, renderingIntent); 

//PRINT image... Its black!!!!!! 

CGDataProviderRelease(provider); 
free(buffer); 
free(buffer2); 
+0

我不知道C GImage的東西(從未使用過),但glReadPixels當然也可以和CGLContextObj一起使用。你是否在調用glReadPixels之前用glReadBuffer設置任何東西,你是否正確地綁定了上下文,也許你應該在調用glReadPixels之前顯示更多關於你正在做什麼的代碼 – moka

回答

1

你做之前glReadPixels叫你必須

  • 設置適當的包裝(見glPixelStorei參考頁)
  • 選擇正確的緩衝區交換後從glReadBuffer(前閱讀,在交換之前,我推薦交換並從前面讀取)