NSOpenGLView newbie question.
NSOpenGLView newbie question.
- Subject: NSOpenGLView newbie question.
- From: JoanBa <email@hidden>
- Date: Tue, 30 Mar 2004 21:55:56 +0200
Hi,
I'm developing a threaded application ( POSIX threads ) which uses
Cocoa and OpenGL.
The application main thread is a GUI developed using Interface Builder
and Xcode, based on Cocoa classes. The interface is simple: a checkbox
(NSButton), a text field (NSTextField), a popup menu (NSComboBox), a
text view used for messaging output (NSScrollView) and several buttons
(NSButton).
When the user press the start button the main application starts two
threads, one acting as a network server ( just an UDP listener ) which
is fully functional, and the other thread acting as a worker, which
must open a window ( NSWindow + NSOpenGLView ) and display a 3D model
moved by the network data, but it still doesn't work.
I'm porting to Cocoa the graphical thread, from an stand-alone C
program which uses OpenGL an glut. Because I want to move the code to a
POSIX threaded application I decided to not to use the glut library, so
I've created a secondary window (NSWindow) in Interface Builder and
I've added an OpenGLView which fills the window, and subclasses
NSOpenGLView, the new class is named Fly3DView.
The problem is that when I create an instance of Fly3DView I cannot see
the window. Also I want to know how can I process events in this
window, from mouse and keyboard.
Just a source snipets to clarify my question:
From the main class, FlyController, which shows the main window, the
code that handles the start button:
- (IBAction)Start:(id)sender
// User has pressed the 'start' button in the GUI.
(...)
// Start OpenGL tasks.
w3D = new Fly3D(gBuf, wtrace, self);
w3D->start();
(...)
}
Fly3D is a C++ class derived from POSIX threads. The constructor for
this class creates an instance of the Fly3DView class:
Fly3D::Fly3D(packetBuf *theBuf, short trace, id controller) {
(...)
g3DView = [[Fly3DView alloc] init];
(...)
}
The init method of Objective-C class Fly3DView is the following, and
seems to be fine:
// global variable...
id g3DView;
- (id) init {
NSOpenGLPixelFormatAttribute attrib[] = { NSOpenGLPFAMaximumPolicy,
NSOpenGLPFAClosestPolicy,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) 32,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) 32,
NSOpenGLPFAWindow,
(NSOpenGLPixelFormatAttribute) nil };
NSRect winRect = {{100, 100},{SCREEN_WIDTH, SCREEN_HEIGHT}};
NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc]
initWithAttributes: attrib];
self = [super initWithFrame: winRect pixelFormat: pf];
return self;
}
The run method for class Fly3D contains an infinite loop, in order to
render the scene:
void Fly3D::run(void) {
gthread_pool = [[NSAutoreleasePool alloc] init];
// Loop infinit. runLoop autodestrueix el thread si hi ha una
notificacis per a fer-ho.
for (;;) runLoop();
return;
}
runLoop() reads network packets and calls the drawRect method with:
(...)
[g3DView drawRect:[g3DView bounds]];
(...)
Finally, drawRect is a typical and simple method from an Apple sample (
just for testing ):
- (void) drawRect:(NSRect)rect
{
if (NO == fGLInit)
[self prepareOpenGL];
fGLInit = YES;
[[self openGLContext] makeCurrentContext];
// setup viewport and prespective
[self resizeGL]; // forces projection matrix update (does test for
size changes)
[self updateModelView]; // update model view matrix for object
// clear our drawable
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// model view and projection matricies already set
//renderScene(); // draw scene
drawCube (1.5f);
if ([self inLiveResize])
glFlush ();
else
[[self openGLContext] flushBuffer];
}
Why I cannot see the window with the OpenGL view ? What I need to
implement event processing for this window ? keyboard and trackball, as
in Apple sample...
Best regards,
Joan B. Altadill
_______________________________________________
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.