Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSOpenGLView:openGLContext在DisplayLink回调中返回nil_Macos_Opengl - Fatal编程技术网

Macos NSOpenGLView:openGLContext在DisplayLink回调中返回nil

Macos NSOpenGLView:openGLContext在DisplayLink回调中返回nil,macos,opengl,Macos,Opengl,我有一个环境问题。我有一个简单的NSOpenGLView子类。我在initWithFrame中定义了像素格式,并在之后验证我是否有有效的上下文。我在awakeFromNib中进行了一些额外的gl设置,并验证我仍然具有有效的上下文。我使用DisplayLink进行主渲染。在显示链接回调中,[self-openGLContext]返回nil。在调试器中,我可以看到_openGLContext实例变量仍然有效-但是访问器返回nil。我错过了什么 ------编辑:包含代码------------- M

我有一个环境问题。我有一个简单的NSOpenGLView子类。我在initWithFrame中定义了像素格式,并在之后验证我是否有有效的上下文。我在awakeFromNib中进行了一些额外的gl设置,并验证我仍然具有有效的上下文。我使用DisplayLink进行主渲染。在显示链接回调中,[self-openGLContext]返回nil。在调试器中,我可以看到_openGLContext实例变量仍然有效-但是访问器返回nil。我错过了什么

------编辑:包含代码-------------

MyGLView.h

@interface MyGLView : NSOpenGLView

- (void)displayLinkDraw;

@end
MyGLView.m

- (id)initWithFrame:(NSRect)frameRect
{
    NSOpenGLPixelFormat        *windowedPixelFormat;
    NSOpenGLPixelFormatAttribute    attribs[] = {
        NSOpenGLPFAWindow,
        NSOpenGLPFAColorSize, 32,
        NSOpenGLPFAAccelerated,
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFASingleRenderer,
        0 };

    windowedPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
    self = [super initWithFrame:frameRect pixelFormat:windowedPixelFormat];

    // set synch to VBL to eliminate tearing
    GLint    vblSynch = 1;
    [[self openGLContext] setValues:&vblSynch forParameter:NSOpenGLCPSwapInterval];

    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns valid context here. ***
    return self;
}

- (void)awakeFromNib
{
    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns valid context here. ***

    [self.openGLContext makeCurrentContext];

    glDisable(GL_DEPTH_TEST);

    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);

    glEnableVertexAttribArray(ATTRIB_TEXCOORD);
    glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);

    glGenFramebuffers(1, &_frameBufferID);
    glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferID);

    glGenRenderbuffers(1, &_colorBufferID);
    glBindRenderbuffer(GL_RENDERBUFFER, _colorBufferID);

    glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, self.frame.size.width, self.frame.size.height);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorBufferID);
    if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

- (void)displayLinkDraw
{
    NSOpenGLContext* context = self.openGLContext;
    // *** self.openGLContext returns nil here??? ***
    [context makeCurrentContext];

    CGLLockContext([context CGLContextObj]);

    glClearColor(0.9f, 0.9f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    // ...

    [context flushBuffer];

    CGLUnlockContext([context CGLContextObj]);
}
MyViewController.m

static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext)
{
    MyViewController* self = (__bridge MyViewController *)displayLinkContext;
    MyGLView* glView = self->_myGLView;

    [glView displayLinkDraw];

    return kCVReturnSuccess;
}
来自Apple的示例应用程序在
NSOpenGLView
子类中有一个运行正常的
CGDisplayLink
循环。此外,还有一个来自苹果的显示链接设置

我已经在自己的基本OpenGL动画中成功地使用了这种方法。 您的
CGDisplayLink
设置代码应该如下所示(我的代码是从my
NSOpenGLView
子类运行的,您的代码似乎是从视图控制器设置的)。
如果您没有使用设置的上下文和像素格式调用
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext()
,则可能选择了无法访问该上下文的显示链接

// Create a display link capable of being used with all active displays
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);

// Set the renderer output callback function
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void *)(self));

// Set the display link for the current renderer
CGLContextObj cglContext = [[self openGLContext] CGLContextObj];
CGLPixelFormatObj cglPixelFormat = [[self pixelFormat] CGLPixelFormatObj];
CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, cglContext, cglPixelFormat);

// Activate the display link
CVDisplayLinkStart(displayLink);

请阅读“如何提问”部分并发布您的代码。它发现了一些链接,使其听起来像NSOpenGLView上下文是线程特定的。既然DisplayLink肯定位于不同的线程上,那么共享上下文是解决这个问题的方法吗?还是应该在第一次DisplayLink回调时进行初始化?