Xcode OpenGL Application Template

Posts: 35
Joined: 2004.07
Post: #16
Well here it is, the long awaited update to the Template Smile

Xcode OpenGL Template Version 3

as said in the Read Me:

Fullscreen is now implemented
Window resizing now works, altho it flickers while resizing
Minimize to the dock now shows the last frame of animation

it took me this long to get it working! Blink
but thanks to OneSadCookie and ThemsAllTook I got it working LOL
as you can probably see Wink

perhaps sometime I'll fix the flashing

regards and hope you like it,
Quote this message in a reply
Posts: 153
Joined: 2004.12
Post: #17
thanks a lot.

There was a long silence...
'I claim them all,' said the Savage at last.
Quote this message in a reply
Post: #18
I downloaded the template, and am using it for my first baby steps on Mac/OpenGL programming. Very much appreciated!

I do have one suggestion, though. There are currently two places where you define the perspective transform: initGL and reshape. Perhaps this could be factored out into its own method? (Or drawFrame call reshape directly on first draw?)
Quote this message in a reply
Posts: 567
Joined: 2004.07
Post: #19
ad this to MyOpenglView.m, and then impliment it in MyOpenGLView.h:

- (IBAction)takeScreenshot:(id)sender
    NSData *imageData;
'self' is your NSOpenGLView

//width needs to be divisible by four
NSSize imageSize = [self bounds].size;                        
imageSize.width = ( imageSize.width - ( ((int)imageSize.width) % 4 ));

int imageWidth = imageSize.width;
int imageHeight = imageSize.height;
int bytesPerPixel = 4;
int bytesPerImage = imageWidth * imageHeight * bytesPerPixel;
char *imageBuffer = ( char * ) malloc( bytesPerImage );

Now, extract image bytes

[[self openGLContext] makeCurrentContext];
glReadPixels(0, 0, imageWidth, imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, imageBuffer);

Now, make 24bit image rep

NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]

Copy RGB over, but not alpha

unsigned char *src, *end, *dest;
src = imageBuffer;
end = src + bytesPerImage;
dest = [rep bitmapData];

while ( src < end )
    *dest = *src; dest++; src++; //R
    *dest = *src; dest++; src++; //G
    *dest = *src; dest++; src++; //B
    ++src;                       //A

NSImage *image = [[NSImage alloc] init];
[image addRepresentation:rep];

Flip image vertically
[image setFlipped:YES];
[image lockFocusOnRepresentation:rep];
[image unlockFocus];

The original NSImage loses its NSBitmapImegreRep when its flipped, and the
only way I can come up with to get an NSBitmapImageRep back is to make a
new NSImage from the original. The horror!
NSImage *flipped = [[NSImage alloc] initWithData: [image TIFFRepresentation]];

Free up temporaries
[rep release];
[image release];
free( imageBuffer );
imageData = [flipped TIFFRepresentation];
[imageData writeToFile:[@"~/Desktop/Screenshot.tiff" stringByExpandingTildeInPath] atomically:YES];


It's not magic, it's Ruby.
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #20
What's up with this comment?
//width needs to be divisible by four

You never actually divide it by four, and I see no reason you'd ever want to. There's no arbitrary requirement that width has to be a multiple of 4 in glReadPixels or -[NSBitmapImageRep initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:has​Alpha:isPlanar:colorSpaceName:bytesPerRow:bitsPerPixel:].

- Alex Diener
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  iPad, OpenGL ES, and XCode Instruments problem! Bandit 0 5,109 Dec 13, 2010 01:21 PM
Last Post: Bandit
  Xcode OpenGL GLUT help please! cloudcrono 11 12,138 Sep 23, 2006 04:58 PM
Last Post: jpalm
  OpenGL problem in Xcode deployment build grandall 6 5,794 Mar 29, 2006 11:12 AM
Last Post: grandall
  Cocoa / Objective-C / OpenGL Template mdavis1982 13 8,658 Feb 28, 2005 01:54 PM
Last Post: arekkusu