glFlush()vs [[self openGLContext] flushBuffer] vs glFinish vs glSwapAPPLE vs aglSwapBuffers

时间:2011-08-11 14:07:56

标签: cocoa macos opengl

使用NSOpenGLView时有几个类似的OpenGL操作:

  • glFlush()
  • [[self openGLContext] flushBuffer]
  • glFinish()
  • glSwapAPPLE
  • aglSwapBuffers

何时应该使用这些?

在示例应用程序中,Apple使用glFlush(),然后使用[[self openGLContext] flushBuffer]。他们为什么要同时使用这两种?

如果我使用的是双缓冲Cocoa NSOpenGLView,那么正确的方法是什么?

3 个答案:

答案 0 :(得分:9)

小心! [[self openGLContext] flushBuffer]不仅仅是gFlush()的Objective-C包装器。此函数(Apple Documentation中的- (void)flushBuffer)仅在您以像素格式设置双缓冲区时才有效

NSOpenGLPixelFormatAttribute attributes [] =
    {
        NSOpenGLPFADoubleBuffer,
        // other presets like
        // NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
        // NSOpenGLPFADepthSize, 32,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] 
                                            initWithAttributes:attributes];

否则你必须使用glFlush();我花了很长时间才看到NSOpenGLContext Class Reference中的基本行。

答案 1 :(得分:2)

你看过this了吗?它解释了何时使用glFlush()和glFinish()。两者都是OpenGL函数,用于控制命令的执行和同步。通常,您希望在进行多线程渲染时使用这些函数,否则不应该有任何需要。

glSwapAPPLE()和aglSwapBuffers()是Apple提供的扩展,用于显示后台缓冲区的内容(在Windows上为wglSwapBuffers())。你应该使用其中一个而不是两个,因为他们真的做同样的事情。我会坚持使用AGL方法,因为它类似于WGL,EGL等。

[[self openGLContext] flushBuffer]可能是glFlush()的目标C包装器。我无法想象它还在做什么。

答案 2 :(得分:2)

正确的方法是调用[[self openGLContext] flushBuffer]