Re: NSOpenGLView and resizable NSWindow problem
Re: NSOpenGLView and resizable NSWindow problem
- Subject: Re: NSOpenGLView and resizable NSWindow problem
- From: Simone Tellini <email@hidden>
- Date: Tue, 31 Oct 2006 20:50:57 +0100
On Tue, 31 Oct 2006 11:21:48 -0800
"Werner Sharp" <email@hidden> wrote:
WS> I'm trying to programmatically create a NSOpenGLView inside a NSWindow
WS> and I'm having trouble getting the NSOpenGLView to correct resize. This
I had the same problem (plus others) with NSOpenGLView. Eventually, I
ditched it and subclassed NSView with the following code:
//--------------------------------------------------------------------
- (id)initWithFrame: (NSRect)frameRect
{
static NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFANoRecovery,
NSOpenGLPFAWindow,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)4,
(NSOpenGLPixelFormatAttribute)0
};
self = [super initWithFrame: frameRect];
if( self ) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs];
glContext = [[NSOpenGLContext alloc] initWithFormat: pixelFormat shareContext: nil];
[nc addObserver: self
selector: @selector( reshape )
name: NSViewFrameDidChangeNotification
object: self];
ready = NO;
}
return( self );
}
//--------------------------------------------------------------------
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[glContext release];
[pixelFormat release];
[super dealloc];
}
//--------------------------------------------------------------------
- (BOOL)isOpaque
{
return( YES );
}
//--------------------------------------------------------------------
- (BOOL)isFlipped
{
return( YES );
}
//--------------------------------------------------------------------
- (void)reshape
{
NSSize size = [self frame].size;
BOOL setCtx = [NSOpenGLContext currentContext] != glContext;
[glContext update];
if( setCtx )
[glContext makeCurrentContext];
glViewport( 0, 0, (GLint) size.width, (GLint) size.height );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 40.0, size.width / size.height, 1.0f, 1000.0f );
if( setCtx )
[NSOpenGLContext clearCurrentContext];
}
//--------------------------------------------------------------------
- (void)drawRect: (NSRect) rect
{
[glContext setView: self];
[glContext makeCurrentContext];
if( !ready ) {
// here I call a method to create some glLists, etc...
[self reshape];
ready = YES;
}
// render the scene
[glContext flushBuffer];
[NSOpenGLContext clearCurrentContext];
}
//---------------------------------------------------------------------------
So far this solution seems to work well. It also proved a lot easier
than getting NSOpenGLView to work as it should ;-)
Any improvement is welcome.
--
Simone Tellini
http://tellini.info
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden