5

だから私は(私のウィンドウで)openGLビューを作成しようとしています。ココアアプリを作っています。Interface Builder を使用して作成することができましたが、教育目的でそれなしで作成したいと考えています。紙の上だけ。

そして、これが私がそれに苦労しているとあなたに言っているポイントです。これまでに基本的に行ったことは次のとおりです。NSOpenGLView から継承する新しいクラス「MyOpenGLView.h/m」を作成しました。クラス名だけにプライベート変数やメソッドを追加しませんでした。私がした唯一のことは、initWithFrame をオーバーライドすることでした: (その中に self = [super initWithFrame:pixelFormat:] を追加します。) 使用する前に、まずこのようなものでインスタンス化する必要があることを Web で読みました) . コードは次のとおりです。

- (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」という名前の別のクラスがありますか? そこに私の MyOpenGLView *myView があります。.m ファイルでは、次のようなものを使用します。

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

もちろん、エラーが発生します。

ウィンドウ アプリケーションに移植された openGL ビューがありません。呼び出されるメソッドの階層について何かを推測しますが、もう一度..よくわかりません。それを手伝ってくれませんか?

4

2 に答える 2

8

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,
        NSOpenGLPFADoubleBuffer,
        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

メソッドで呼び出すことができ-makeCurrentContextます-prepareOpenGl。以下に書いたことはすべて必須ではありませんが、パフォーマンス上の理由からは良いことです。フレーム描画を画面のリフレッシュ レートと同期するためにを使用し始めた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);
}
于 2012-01-10T22:28:34.553 に答える
2

上記の回答は OpenGL に関する詳細情報を提供しますが、あなたが抱えている特定の問題の理由は、initWithFrame メソッドが self を返す必要があるためです。

それがなければ、initWithFrame は常に Nil を返します。(また、スーパーの initWithFrame を呼び出して、他の OpenGL のアドバイスに従う必要があります)。

于 2012-06-19T00:41:40.600 に答える