Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: None  |  size: 3.34 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Capturing image of 3d object in Open GLES
  2. (UIImage*)snapshot:(UIView*)eaglview
  3. {
  4.     GLint backingWidth, backingHeight;
  5.  
  6.     // Bind the color renderbuffer used to render the OpenGL ES view
  7.     // If your application only creates a single color renderbuffer which is already bound at this point,
  8.     // this call is redundant, but it is needed if you're dealing with multiple renderbuffers.
  9.     // Note, replace "_colorRenderbuffer" with the actual name of the renderbuffer object defined in your class.
  10.     glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);
  11.  
  12.     // Get the size of the backing CAEAGLLayer
  13.     glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  14.     glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  15.  
  16.     NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
  17.     NSInteger dataLength = width * height * 4;
  18.     GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
  19.  
  20.     // Read pixel data from the framebuffer
  21.     glPixelStorei(GL_PACK_ALIGNMENT, 4);
  22.     glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
  23.  
  24.     // Create a CGImage with the pixel data
  25.     // If your OpenGL ES content is opaque, use kCGImageAlphaNoneSkipLast to ignore the alpha channel
  26.     // otherwise, use kCGImageAlphaPremultipliedLast
  27.     CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
  28.     CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  29.     CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
  30.                                     ref, NULL, true, kCGRenderingIntentDefault);
  31.  
  32.     // OpenGL ES measures data in PIXELS
  33.     // Create a graphics context with the target size measured in POINTS
  34.     NSInteger widthInPoints, heightInPoints;
  35.     if (NULL != UIGraphicsBeginImageContextWithOptions) {
  36.         // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
  37.         // Set the scale parameter to your OpenGL ES view's contentScaleFactor
  38.         // so that you get a high-resolution snapshot when its value is greater than 1.0
  39.         CGFloat scale = eaglview.contentScaleFactor;
  40.         widthInPoints = width / scale;
  41.         heightInPoints = height / scale;
  42.         UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthInPoints, heightInPoints), NO, scale);
  43.     }
  44.     else {
  45.         // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
  46.         widthInPoints = width;
  47.         heightInPoints = height;
  48.         UIGraphicsBeginImageContext(CGSizeMake(widthInPoints, heightInPoints));
  49.     }
  50.  
  51.     CGContextRef cgcontext = UIGraphicsGetCurrentContext();
  52.  
  53.     // UIKit coordinate system is upside down to GL/Quartz coordinate system
  54.     // Flip the CGImage by rendering it to the flipped bitmap context
  55.     // The size of the destination area is measured in POINTS
  56.     CGContextSetBlendMode(cgcontext, kCGBlendModeCopy);
  57.     CGContextDrawImage(cgcontext, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);
  58.  
  59.     // Retrieve the UIImage from the current context
  60.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  61.  
  62.     UIGraphicsEndImageContext();
  63.  
  64.     // Clean up
  65.     free(data);
  66.     CFRelease(ref);
  67.     CFRelease(colorspace);
  68.     CGImageRelease(iref);
  69.  
  70.     return image;
  71. }
clone this paste RAW Paste Data