2011-08-29 5 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를 사용하여 설정하고 있습니까? Context를 제대로 바인딩하고 있습니까? glReadPixels를 호출하기 전에 수행중인 작업에 대한 코드를 추가로 표시해야합니다. – moka

답변

1

은 glReadPixels는

  • 설정 적절한 포장
  • 스와핑 후 glReadBuffer (전면에서 읽을 수있는 권리 버퍼를 선택 (glPixelStorei 참조 페이지를 참조하십시오)해야한다 전화 , 스왑 전에 다시, 나는 스왑을 추천하고 앞에서 읽어)