【问题标题】:NSOpenGLView Won't ClearNSOpenGLView 不会清除
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我过去曾设法通过简单地刷新上下文来做到这一点。在您的绘制代码末尾,opengl 必须绘制任何东西。我没有看到您在 drawrect 代码的末尾使用任何刷新或交换方法,因此为什么添加 glswapapple 有效。尝试添加 glFlush();它也应该可以工作。
  • 您的视图是否设置为使用双缓冲?然后根据我的理解,您需要交换。否则你不会。
【解决方案2】:

你应该使用 CGLFlushDrawable()。喜欢吹:

CGLFlushDrawable([[self openGLContext] CGLContextObj]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-12
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2013-02-04
    • 2013-07-14
    • 2017-12-10
    相关资源
    最近更新 更多