So im trying to create an openGL view (in my window). Im making a Cocoa app. Ive managed to create one through Interface Builder, but for educational purposes i want to go on and make one without it. Just on papers.
And here is the point im telling you im struggling with it. So what ive basically done-tried so far is this: I ve created a new class "MyOpenGLView.h/m" inheriting from NSOpenGLView. I ve added no private vars or methods to it just the class name. The only thing i did, was override initWithFrame: (adding a self = [super initWithFrame:pixelFormat:] inside it.) I read about it on the Web that you have to instantiate it with something like this first before you can use it). here is the code:
- (id) initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]
initWithAttributes:(NSOpenGLPixelFormatAttribute[])
{
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 32,
nil
}];
self = [super initWithFrame:frameRect pixelFormat:pixelFormat];
[[self openGLContext] makeCurrentContext];
}
So i have another class named by "MyViewController.h/m" which handles my View? and there i have my MyOpenGLView *myView. In the .m file i go with something like this:
myView = [[MyOpenGLView alloc] initWithFrame:CGRectMake(0,0,100.0,100.0)];
if (!myView) { NSLog(@"ERROR"); }
and of course, i get error.
There is no openGL view ported in my window application. I would guess its something about the hierarchy on methods being called but then again.. im not sure. Can you aid me with it?
They way I have gotten this to work is I don't implement an
init
method in my view. Then in my controller or app delegate I have.Can can make call
-makeCurrentContext
in your-prepareOpenGl
method. All that I have written below is not necessary but nice for performance reasons. I have started using theCVDisplayLink
to sync frame drawing with the screen refresh rate so my openGLview looks likeWhile the answers above give more information about OpenGL the reason for the specific issue you are having is that your initWithFrame method needs to return self.
Without that initWithFrame will always return Nil. (You should also call super's initWithFrame and take the other OpenGL advice).