• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: OpenGL printing/image
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OpenGL printing/image


  • Subject: Re: OpenGL printing/image
  • From: Christopher Holland <email@hidden>
  • Date: Wed, 15 Jan 2003 08:17:01 -0600

I've got some code that creates an image from an NSOpenGLView. I put
the code together with comments from this list and some Apple sample
code, I think. The code actually opens a save dialog and saves the
image as a TIFF.

The first method can be put in your controller as long as it has a
connection to the NSOpenGLView (I use 'theOpenGLView').

The second method needs to go into either a category of NSOpenGLView or
a subclass of the same. I use a subclass, so I can't vouch for the
accuracy of using a category.

- (IBAction)saveScreenshot:(id)sender
{
NSSavePanel *sp = [NSSavePanel savePanel];
unsigned char *pixie;
NSData *outData;
int bWidth, bHeight;
NSImage *theImage;
NSBitmapImageRep *theImageRep;

NSRect bounds = [[[tabView selectedTabViewItem] view] bounds];
bWidth = (int)(bounds.size.width);
bHeight = (int)(bounds.size.height);
pixie = (unsigned char *)malloc(bWidth * bHeight * 4);

[theOpenGLView readPixels:pixie];

// Try to create the BitmapImageRep
theImageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:&pixie
pixelsWide:bounds.size.width
pixelsHigh:bounds.size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:32];

theImage = [[NSImage alloc] initWithSize:bounds.size];
[theImage setBackgroundColor:[NSColor blackColor]];
[theImage setFlipped:YES];
[theImage addRepresentation:theImageRep];
[theImage lockFocusOnRepresentation:theImageRep];
outData = [theImage TIFFRepresentation];

// Let's found out a little bit about this monster we've created....
// THis should spit out the bytes of the image.....yep, it does.
Don't do that. It hurts.
//NSLog(@"NSBitmapImageRep -> %@",[theImage TIFFRepresentation]);


// Now handle the save-as GUI sheet
[outData retain];
[sp setRequiredFileType:@"tif"];
[sp setCanSelectHiddenExtension:YES];
[sp beginSheetForDirectory:NSHomeDirectory()
file:nil
modalForWindow:mainWindow
modalDelegate:self

didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
contextInfo:(void *)outData];
[theImage unlockFocus];
[theImage release];
}

// This should be a method of the NSOpenGLView.
- (void) readPixels:(unsigned char *)pixie
{
NSRect bounds = [self bounds];
glReadBuffer(GL_FRONT_LEFT);
[[self openGLContext] makeCurrentContext];
glClearColor(0.0, 0.0, 0.0, 0.0);
glReadPixels(0, 0, (GLsizei)(bounds.size.width),
(GLsizei)(bounds.size.height), GL_RGBA, GL_UNSIGNED_BYTE, pixie);
[NSOpenGLContext clearCurrentContext];
}


On Wednesday, January 15, 2003, at 06:42 AM, Simon Bovet wrote:

> Has anyone tried to print an NSOpenGLView, or make an NSImage from it?
> It's surely easy, but if someone has a ready-made solution, it could
> spare me some time!
>
> So thanks for any help!
> Simon
> _______________________________________________
> 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.
_______________________________________________
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.

  • Prev by Date: Re: coding struct with keyed archivers
  • Next by Date: Re: Atomic operations in Obj-C?
  • Previous by thread: Re: coding struct with keyed archivers
  • Next by thread: Re: OpenGL printing/image
  • Index(es):
    • Date
    • Thread