Question

I'm trying to take a screenshot of a GLView on the iPhone. I wrote the following code:

[self setContext];

GLint backWidth, backHeight;

glGetRenderbufferParameterivOES( GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backWidth );
glGetRenderbufferParameterivOES( GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backHeight );

int         dataLength  = backWidth * backHeight * 4;
uint32_t*   pData       = (uint32_t*)malloc( dataLength * sizeof( char ) );
memset( pData, 0xff, dataLength ); // This is here to confirm some writing occurs in glReadPixels

// Read pixel data from the framebuffer
//glPixelStorei( GL_PACK_ALIGNMENT, 4 );
glReadPixels( 0, 0, backWidth, backHeight, GL_RGBA, GL_UNSIGNED_BYTE, pData );
fprintf( stderr, "%d\n", glGetError() );

CGDataProviderRef   cgDataProvider  = CGDataProviderCreateWithData( NULL, pData, dataLength, DataProviderReleaseDataCallback );
CGColorSpaceRef     cgColorSpace    = CGColorSpaceCreateDeviceRGB();
CGImageRef          cgImage         = CGImageCreate(    backWidth, backHeight, 8, 32, backWidth * 4, cgColorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
                                                        cgDataProvider, NULL, true, kCGRenderingIntentDefault );

//NSData*       pNSData         = [[NSData alloc] initWithBytes: pData length: dataLength];
UIImage*    pRetImage       = [UIImage imageWithCGImage: cgImage scale: 1.0f orientation: UIImageOrientationDownMirrored];
CFRelease( cgDataProvider );
CFRelease( cgColorSpace );
CGImageRelease( cgImage );
//free( pData );

return pRetImage;

Which works perfectly in the simulator. Unfortunately the moment I tried to run it on the iPhone 4S glReadPixels does nothing. I deliberately memset the array to 0xff to see if I could see if it was doing anything and it doesn't matter what I set the array too glReadPixels does nothing. It also reports no errors.

I'm not using a multisample buffer unless the iPhone does the set up for me.

glGenRenderbuffers( 1, &mGlRenderBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, mGlRenderBuffer );

[mGlContext renderbufferStorage: GL_RENDERBUFFER fromDrawable: mGlLayer];

GLuint frameBuffer;
glGenFramebuffers( 1, &frameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mGlRenderBuffer );

Has anyone any ideas whats going on? Its driving me mad :(

Was it helpful?

Solution

Turns out it was that iOS6 is a bit more picky about setting kEAGLDrawablePropertyRetainedBacking ...

eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:YES],
                                    kEAGLDrawablePropertyRetainedBacking,
                                    kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                    nil];

See here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top