Re: Cocoa newbie - document-based Cocoa app with OpenGL
Re: Cocoa newbie - document-based Cocoa app with OpenGL
- Subject: Re: Cocoa newbie - document-based Cocoa app with OpenGL
- From: Raphael Sebbe <email@hidden>
- Date: Fri, 22 Feb 2002 11:22:25 +0100
On Friday, February 22, 2002, at 01:58 AM, John M Coggi wrote:
Hi all. I'm new to Cocoa so please excuse my ignorance. I recently
signed
up to port a Windoze MFC app to OS X. This MFC app can have multiple
OpenGL windows representing different wireframe models. By clicking a
toolbar button, animation occurs in each OpenGL window. The problem
is I
can't seem to get any OpenGL graphics to appear in my windows (I have an
NSOpenGL view in the window). I can write to other outlets in the
windows,
but nothing in the OpenGL view. I'm pretty sure I've got my outlets
set up
right. Also, I have a non-document-based version which works fine. Is
there something I'm missing with document-based Cocoa apps? In the
awakeFromNib() routine in the document code, I do all my OpenGL
initialization so every window is initialized when it is summoned from
the
New menu. Is this correct?
The document instance is not in a NIB, but is instanciated when the
document is created (New...). And thus awakeFromNib is not invoked.
But this is tricky... GL initialization must be done once the window
containing the gl view is on screen. In document-based apps, this is not
straightforward (as it is with simple apps, using awakeFromNib and
initially visible windows).
My solution was to implement that method in my document class, (the
document must be window's delegate - connected in IB)
- (void)windowDidBecomeMain:(NSNotification *)aNotification
{
//NSLog(@"expose");
if(_glInited == NO && [aNotification object] ==
[_mainWindowController window])
{
//NSLog(@"Initializing GL");
// This is a tricky issue : OpenGL can be inited only after the
view is brought on-screen.
// So, we track the first time the document window become main
and prepare gl at that
// time.
[[_mainWindowController glView] prepareGL]; // this must be done
when the window is on screen, and before the first use of OpenGL...
//NSLog(@"Initializing GL 2");
[_glPreviewer setIsReadyForDrawing:YES];
[_toolsController changeTools:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:
NOTIF_SELECTION_CHANGED
object:_scene];
[[NSNotificationCenter defaultCenter] postNotificationName:
NOTIF_SCENE_NEED_DISPLAY
object:_scene];
//[_mainWindowController drawGLView:nil];
//NSLog(@"Initializing GL 3");
_glInited = YES;
}
[[TDAppController sceneController] setCurrentScene:_scene];
}
I can't give you the code for prepareGL, as I am not using NSOpenGLView
but a custom class instead. At least you see where to do it !
Note this is not a perfect solution, especially if the app is
immediately put in the background, as the OpenGL view will only be drawn
when the app becomes active again. If anyone knows of a better solution,
please tell !
Raphael
_______________________________________________
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.