objective-c – 在没有Interface Builder的情况下创建OpenGL视图

概述所以我试图创建一个openGL视图(在我的窗口中).我正在制作一个 Cocoa应用程序. 我设法通过Interface Builder创建一个,但出于教育目的,我想继续创建一个没有它.就在论文上. 这就是我告诉你即时挣扎的观点. 所以到目前为止基本上已经完成的尝试是: 我创建了一个继承自NSOpenGLView的新类“MyOpenGLView.h / m”. 我没有添加私有变量或方法只是类名.我唯 所以我试图创建一个openGL视图(在我的窗口中).我正在制作一个 Cocoa应用程序.
我设法通过Interface Builder创建一个,但出于教育目的,我想继续创建一个没有它.就在论文上.

这就是我告诉你即时挣扎的观点.
所以到目前为止基本上已经完成的尝试是:
我创建了一个继承自NSOpenGLVIEw的新类“MyOpenGLVIEw.h / m”.
我没有添加私有变量或方法只是类名.我唯一做的是覆盖initWithFrame :(在其中添加self = [super initWithFrame:pixelFormat:].)
我在网上看到它,你必须首先用这样的东西来实例化它然后才能使用它.这是代码:

- (ID) initWithFrame:(NSRect)frameRect{ NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc]                                    initWithAttributes:(NSOpenGLPixelFormatAttribute[])                                    {                                    NSOpenGLPFAWindow,NSOpenGLPFADoubleBuffer,NSOpenGLPFADepthSize,32,nil                                    }]; self = [super initWithFrame:frameRect pixelFormat:pixelFormat]; [[self openGLContext] makeCurrentContext];}

所以我有另一个名为“MyVIEwController.h / m”的类来处理我的VIEw?
我有我的MyOpenGLVIEw * myVIEw.
在.m文件中,我使用以下内容:

myVIEw = [[MyOpenGLVIEw alloc] initWithFrame:CGRectMake(0,100.0,100.0)];if (!myVIEw) { NSLog(@"ERROR"); }

当然,我得到错误.

我的窗口应用程序中没有移植的openGL视图.我猜它有关于被调用的方法的层次结构,但后来再次..我不确定.你可以帮我吗?

解决方法 我这样做的方式是我在我的视图中没有实现init方法.然后在我的控制器或应用程序委托中.

@implementation AppDelegate@synthesize window = _window;@synthesize vIEw = _vIEw;- (voID)applicationDIDFinishLaunching:(NSNotification *)aNotification {    NSRect maindisplayRect = [[NSScreen mainScreen] frame]; // I'm going to make a full screen vIEw.    NSOpenGLPixelFormatAttribute attr[] = {        NSOpenGLPFAOpenGLProfile,NSOpenGLProfiLeversion3_2Core,// Needed if using opengl 3.2 you can comment this line out to use the old version.        NSOpenGLPFAcolorSize,24,NSOpenGLPFAAlphaSize,8,NSOpenGLPFAAccelerated,0    };    NSOpenGLPixelFormat *pix = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];    self.vIEw = [[OpenGLVIEwCoreProfile alloc] initWithFrame:maindisplayRect pixelFormat:pix];    // Below shows how to make the vIEw fullscreen. But you Could just add to the contact vIEw of any window.    self.window = [[NSWindow alloc] initWithContentRect:maindisplayRect                                              styleMask:NSborderlessWindowMask                                                 backing:NSbackingStoreBuffered                                                   defer:YES];    self.window.opaque = YES;    self.window.hIDesOnDeactivate = YES;    self.window.level = NSMainMenuWindowLevel + 1; // Show window above main menu.    self.window.contentVIEw = self.vIEw;    [self.window makeKeyAndOrderFront:self]; // display window.}@end

可以在-prepareOpenGl方法中调用-makeCurrentContext.我在下面写的所有内容都没有必要,但出于性能原因很好.我已经开始使用CVdisplaylink将帧绘制与屏幕刷新率同步,因此我的openGLvIEw看起来像

// This is the callback function for the display link.static CVReturn OpenGLVIEwCoreProfileCallBack(CVdisplaylinkRef displaylink,const CVTimeStamP* Now,const CVTimeStamP* outputTime,CVOptionFlags flagsIn,CVOptionFlags *flagsOut,voID *displaylinkContext) {    @autoreleasepool {        OpenGLVIEwCoreProfile *vIEw = (__brIDge OpenGLVIEwCoreProfile*)displaylinkContext;        [vIEw.openGLContext makeCurrentContext];        CGLLockContext(vIEw.openGLContext.CGLContextObj); // This is needed because this isn't running on the main thread.        [vIEw drawRect:vIEw.bounds]; // Draw the scene. This doesn't need to be in the drawRect method.        CglunlockContext(vIEw.openGLContext.CGLContextObj);        CGLFlushDrawable(vIEw.openGLContext.CGLContextObj); // This does glFlush() for you.        return kCVReturnSuccess;    }}- (voID)reshape {    [super reshape];    CGLLockContext(self.openGLContext.CGLContextObj);    ... // standard opengl reshape stuff goes here.    CglunlockContext(self.openGLContext.CGLContextObj);}- (voID)prepareOpenGL {    [super prepareOpenGL];    [self.openGLContext makeCurrentContext];    Glint swAPInt = 1;    [self.openGLContext setValues:&swAPInt forParameter:NSOpenGLcpswAPInterval];    CGLLockContext(self.openGLContext.CGLContextObj);    ... // all opengl prep goes here    CglunlockContext(self.openGLContext.CGLContextObj);    // Below creates the display link and tell it what function to call when it needs to draw a frame.    CVdisplaylinkCreateWithActiveCGdisplays(&_displaylink);    CVdisplaylinkSetoutputCallback(self.displaylink,&OpenGLVIEwCoreProfileCallBack,(__brIDge voID *)self);    CVdisplaylinkSetCurrentCGdisplayFromOpenGLContext(self.displaylink,self.openGLContext.CGLContextObj,self.pixelFormat.CGLPixelFormatObj);    CVdisplaylinkStart(self.displaylink);}
总结

以上是内存溢出为你收集整理的objective-c – 在没有Interface Builder的情况下创建OpenGL视图全部内容,希望文章能够帮你解决objective-c – 在没有Interface Builder的情况下创建OpenGL视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1006698.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存