Created
December 7, 2012 13:18
-
-
Save zapsleep/4233229 to your computer and use it in GitHub Desktop.
OpenGL ES Scene Screenshot snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIImage*)snapshot:(UIView*)eaglview | |
{ | |
GLint backingWidth, backingHeight; | |
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer); | |
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, | |
GL_RENDERBUFFER_WIDTH_OES, | |
&backingWidth); | |
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, | |
GL_RENDERBUFFER_HEIGHT_OES, | |
&backingHeight); | |
NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight; | |
NSInteger dataLength = width * height * 4; | |
GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte)); | |
glPixelStorei(GL_PACK_ALIGNMENT, 4); | |
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); | |
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, | |
dataLength, NULL); | |
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); | |
CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, | |
colorspace, | |
kCGBitmapByteOrder32Big | | |
kCGImageAlphaPremultipliedLast, | |
ref, NULL, true, | |
kCGRenderingIntentDefault); | |
NSInteger widthInPoints, heightInPoints; | |
if (NULL != UIGraphicsBeginImageContextWithOptions) { | |
CGFloat scale = eaglview.contentScaleFactor; | |
widthInPoints = width / scale; | |
heightInPoints = height / scale; | |
UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, | |
heightInPoints), | |
NO, scale); | |
} | |
else { | |
widthInPoints = width; | |
heightInPoints = height; | |
UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, | |
heightInPoints)); | |
} | |
CGContextRef cgcontext = UIGraphicsGetCurrentContext(); | |
CGContextSetBlendMode(cgcontext, kCGBlendModeCopy); | |
CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, | |
widthInPoints, | |
heightInPoints), | |
iref); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
free(data); | |
CFRelease(ref); | |
CFRelease(colorspace); | |
CGImageRelease(iref); | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment