【发布时间】:2013-07-22 19:38:50
【问题描述】:
我正在尝试使用 Cocoa 窗口和 NSOpenGLView 的 OpenGL 3.2。但是我无法让 NSOpenGLView 绘制红色。我只是得到一个白色的窗口。这是我的代码(在 NSOpenGLView 的子类中):
-(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];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
代码应该简单地将视图清除为红色。没有错误或警告——只有一个白色窗口。
【问题讨论】:
-
尝试插入一些 NSLog 到
drawRect:。可能根本没有被调用?也许你应该告诉 OpenGL 视图重绘自己? -
我添加了一个 NSLog() 到 drawRect: 并且它最终被调用了,所以我仍然不确定是什么损坏了。
标签: macos cocoa opengl opengl-3 nsopenglview