Re: Snapshot of NSOpenGLView
Re: Snapshot of NSOpenGLView
- Subject: Re: Snapshot of NSOpenGLView
- From: Pierre-Olivier Latour <email@hidden>
- Date: Mon, 02 Sep 2002 12:13:20 +0200
>
I am trying to get a TIFF image of an NSOpenGLView, and save it to file. Here
>
is my code stripped down a bit:
>
>
>
NSBitmapImageRep *bitmap;
>
[openGLView lockFocus];
>
bitmap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[openGLView
>
bounds]] autorelease];
>
[openGLView unlockFocus];
>
>
NSSavePanel *panel = [NSSavePanel savePanel];
>
[panel setRequiredFileType:@"tiff"];
>
if ( NSOKButton == [panel runModalForDirectory:NSHomeDirectory() file:nil] )
>
{
>
NSData *tiffData = [bitmap TIFFRepresentation];
>
[tiffData writeToFile:[panel filename] atomically:YES];
>
}
>
>
>
This produces a blank white tiff file. What am I doing wrong?
Because of hardware acceleration, the image you see in the NSOpenGLView is
not backbuffered in RAM, but in VRAM.
Therefore, you need to copy the content of the OpenGL's backbuffer to a
buffer in RAM, then create a NSBitmapRep from that buffer. Finally, you have
to swap the image vertically, because OpenGL vertical axis is upside-down.
Example dirty code:
unsigned char* planes[1];
NSSize size;
NSBitmapImageRep* representation;
NSImage* image;
size = [openGLView bounds].size;
buffer = [NSMutableData dataWithLength:size.width*size.height*3];
glReadBuffer(GL_BACK);
glReadPixels(0, 0, size.width, size.height, GL_RGB, GL_UNSIGNED_BYTE,
[buffer mutableBytes]);
planes[0] = [buffer mutableBytes];
representation = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:planes
pixelsWide:size.width pixelsHigh:size.height bitsPerSample:8
samplesPerPixel:3 hasAlpha:NO isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:(size.width * 3)
bitsPerPixel:24];
image = [[NSImage alloc] initWithSize:size];
[image setFlipped:YES];
[image lockFocus];
[representation drawInRect:NSMakeRect(0,0,size.width,size.height)];
[image unlockFocus];
Then you can save "image" as a TIFF file.
_____________________________________________________________
Pierre-Olivier Latour email@hidden
Lausanne, Switzerland
http://www.pol-online.net
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.