我有一个消失的上下文问题。我有一个简单的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;
}
答案 0 :(得分:0)
Apple的GLEssentials示例应用程序在CGDisplayLink
子类中有一个正常运行的NSOpenGLView
循环。还有来自Apple的Technical Q&A显示链接设置。
我已经在我自己的基本OpenGL动画中成功使用了这种方法。
您的CGDisplayLink
设置代码应如下所示(我的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);