开发者

Taking snapshot of contents in CGL?

I want to create a image out of Core OpenGL context.

I used following code but it creates a black image. So I guess I cannot use glReadPixles there? Any other suggestions please?

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(m开发者_如何学运维yDataLength);
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);


Before you do a glReadPixels call you must

  • set proper packing (see glPixelStorei reference page)
  • select the right buffer to read from with glReadBuffer (front after swapping, back before swapping, I recommend swap and read from front)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜