4

The WGL_EXT_swap_control extension allows doing this on Windows, but I am unable to find anything even remotely cross-platform doing the same, i.e. syncing my buffer swaps with screen refresh. My application uses GLEW, so something offered by that would be preferable. Cross-platform support for Linux, Mac and Windows is necessary, but my application will not break if the sync cannot be set (e.g. the user has forced it off in his graphics drivers).

I will accept program code to do it on many platforms, with GLEW, as a valid answer.

Tronic
  • 10,250
  • 2
  • 41
  • 53
  • 1
    http://stackoverflow.com/questions/589064/how-to-enable-vertical-sync-in-opengl asks the same question, but no GLEW is used and no solution is found. – Tronic Jan 18 '10 at 04:32
  • SDL2 provides a functionality to enable vsync. SDL2 is compatible with many more platform than GLEW and in my opinion a better choice overall. However in SDL2 after created the OpenGL context you can simply call: `SDL_GL_SetSwapInterval(1)` and it will do the job. here the docs if you are interested: https://wiki.libsdl.org/SDL_GL_SetSwapInterval – pappix Feb 15 '18 at 18:52

3 Answers3

6

There is a reason it's not easy to find a cross-platform solution. The platform ultimately owns the display (and the swapping behavior). So it necessarily is part of the platform API (if exposed). There can't really be a cross-platform solution. Even glew has some platform specific bits when it comes down to interaction with the platform.

Now you could argue that all the platforms should use the same API for that specific bit of their interface, but I doubt you'd get any traction from them.

Last, not all framebuffers are displayed directly. If you happen to be using a window management system that actually blends the framebuffer pixels to the desktop (like Aero does when active), then you don't get to control the swap behavior anyways.

For reference, the various APIs to do this on major platforms:

  • wglSwapIntervalEXT
  • glXSwapIntervalSGI
  • AGLSetInteger
Bahbar
  • 17,760
  • 43
  • 62
  • This. "Cross platform", in OpenGL, almost always means multiple code paths. Vsync is hardly unique in this way. – Alan Feb 26 '10 at 17:30
  • 1
    On OSX you want to use Cocoa with a DisplayLink for vSync, not AGL with carbon. – zezba9000 Feb 20 '12 at 21:12
1

From http://www.opengl.org/wiki/Swap_Interval (and indirectly http://www.opengl.org/registry/specs/SGI/swap_control.txt):

In Linux, things are much simpler. If GLX_SGI_swap_control is present in the string returned by glGetString(GL_EXTENSIONS), then you can use glXSwapIntervalSGI(0) to disable vsync or you can use glXSwapIntervalSGI(1) to enable vsync (aka vertical synchronization).

MSN
  • 53,214
  • 7
  • 75
  • 105
0

For OS X, check out http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSOpenGLContext_Class/Reference/Reference.html

NSOpenGLCPSwapInterval

Sets or gets the swap interval. The swap interval is represented as one long. If the swap interval is set to 0 (the default), the flushBuffer method executes as soon as possible, without regard to the vertical refresh rate of the monitor. If the swap interval is set to 1, the buffers are swapped only during the vertical retrace of the monitor. Available in Mac OS X v10.0 and later.

Declared in NSOpenGL.h.

Danny Dulai
  • 1,657
  • 12
  • 14