NSOpenGLView无法清除

时间:2013-07-22 19:38:51

标签: macos cocoa opengl opengl-3 nsopenglview

我正在尝试使用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);

 }

代码应该只是将视图清除为红色。没有错误或警告 - 只是一个白色的窗口。

2 个答案:

答案 0 :(得分:1)

我需要在调用glClear()之后使用glSwapAPPLE()交换缓冲区

我在某处读到NSOpenGLViews不需要交换,因为它是自动完成的。根据我的经验,我可以说这完全是不真实的。

答案 1 :(得分:0)

您应该使用CGLFlushDrawable()。像打击一样:

CGLFlushDrawable([[self openGLContext] CGLContextObj]);