简体   繁体   中英

OpenGL ES snapshot for multi-sampling giving odd colors in iOS

When trying to get opengl view snapshot as UIImage for multi-sampling the image colors are different.

When multi-sampling is off, it is proper. This is how I am taking the snapshot:

- (UIImage*)snapshot
{

    GLint backingWidth, backingHeight;

    backingWidth = framebufferWidth;
    backingHeight = framebufferHeight;

    NSInteger myDataLength = backingWidth * backingHeight * 4;

    GLubyte *buffer = (GLubyte *) malloc(myDataLength);
    glReadPixels(0, 0, backingWidth, backingHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // gl renders "upside down" so swap top to bottom into new array.
    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);

    for(int y = 0; y < backingHeight; y++) {
        for(int x = 0; x < backingWidth * 4; x++) {
            buffer2[y*4*backingWidth + x] = buffer[(backingHeight - y -1 ) * backingWidth * 4 + x];
        }
    }
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, myProviderReleaseData);

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

    // make the cgimage
    CGImageRef imageRef = CGImageCreate(backingWidth, backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

    // then make the uiimage from that
    UIImage *image1 = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(provider);
    free(buffer);

    return image1;
}

Here are the result of taking the snapshot :

OpenGL视图

同一视图的快照

First one is the opengl view I am drawing and second image is the snapshot of the image I am getting for the above mentioned code.

I am not using GLKit framework. want to know why multisampling is messing up the snapshot.

在拍摄opengl视图的快照之前,请检查是否正在调用[EAGLContext:presentRenderBuffer]方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM