1

XCode 能够从 iPad 捕获 Opengl ES 帧,这很棒!我想扩展此功能并捕获我的应用程序的整个 Opengl ES 电影。有办法吗?如果无法使用 XCode,我怎么能不费力气地对我的代码进行重大更改?非常感谢!

4

1 回答 1

2

我使用了一种非常简单的技术,只需要几行代码。

您可以使用以下代码将每个 OGL 帧捕获到 UIImage 中:

- (UIImage*)captureScreen {

    NSInteger dataLength = framebufferWidth * framebufferHeight * 4;

    // Allocate array.
    GLuint *buffer = (GLuint *) malloc(dataLength);
    GLuint *resultsBuffer = (GLuint *)malloc(dataLength);
    // Read data
    glReadPixels(0, 0, framebufferWidth, framebufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // Flip vertical
    for(int y = 0; y < framebufferHeight; y++) {
        for(int x = 0; x < framebufferWidth; x++) {
            resultsBuffer[x + y * framebufferWidth] = buffer[x + (framebufferHeight - 1 - y) * framebufferWidth];
        }
    }

    free(buffer);

    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, resultsBuffer, dataLength, releaseScreenshotData);

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

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(framebufferWidth, framebufferHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);

    // then make the UIImage from that
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return image;
}

然后您将捕获主循环中的每一帧:

- (void)onTimer {

    // Compute and render new frame
    [self update];

    // Recording
    if (recordingMode == RecordingModeMovie) {

        recordingFrameNum++;

        // Save frame
        UIImage *image = [self captureScreen];
        NSString *fileName = [NSString stringWithFormat:@"%d.jpg", (int)recordingFrameNum];
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[basePath stringByAppendingPathComponent:fileName] atomically:NO];
    }
}

最后,您将拥有大量的 JPEG 文件,这些文件可以通过 Time Lapse Assembler轻松转换为电影

如果您想拥有漂亮的 30FPS 电影,请将您的计算步骤硬固定为每帧 1 / 30.0 秒。

于 2012-10-30T21:56:41.277 回答