如何在 NSOpenGLView 的自定义实现中创建核心配置文件?我应该覆盖哪个方法以及应该在其中放置什么代码?
到目前为止我有这段代码:
// Header File
#import <Cocoa/Cocoa.h>
@interface TCUOpenGLView : NSOpenGLView
@end
// Source File
#import "TCUOpenGLView.h"
#import <OpenGL/gl.h>
@implementation TCUOpenGLView
- (void)drawRect:(NSRect)dirtyRect {
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
@end
最佳答案
Apple 有一个名为 GLEssentials 的示例代码项目它准确地显示了如何执行此操作(请注意,这是一个 Mac OS X 和 iOS 示例代码项目)。
本质上,您需要子类化 NSOpenGLView(示例代码中的 NSGLView 类)并使用以下方法实现 awakeFromNib 方法:
- (void) awakeFromNib
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFADoubleBuffer,
NSOpenGLPFADepthSize, 24,
// Must specify the 3.2 Core Profile to use OpenGL 3.2
NSOpenGLPFAOpenGLProfile,
NSOpenGLProfileVersion3_2Core,
0
};
NSOpenGLPixelFormat *pf = [[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] autorelease];
if (!pf)
{
NSLog(@"No OpenGL pixel format");
}
NSOpenGLContext* context = [[[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] autorelease];
[self setPixelFormat:pf];
[self setOpenGLContext:context];
}
另请记住,如果您使用任何已从 3.2 API 中删除的 OpenGL API 调用,您的应用程序将会崩溃。这是一个 PDF document 3.2 规范,以便您可以看到更改。
关于objective-c - 带 NSOpenGLView 的 OpenGL 3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11602406/