【问题标题】:OpenGL 3.2 w/ NSOpenGLView带有 NSOpenGLView 的 OpenGL 3.2
【发布时间】:2012-07-22 17:24:45
【问题描述】:
如何在 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
【问题讨论】:
标签:
objective-c
opengl-3
nsopenglview
nsopengl
【解决方案1】:
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 调用,您的应用将会崩溃。这是 3.2 规范的 PDF document,以便您查看更改。