View Full Version : How to get SwapBuffers to wait for vsync?

02-03-2010, 08:45 AM
My app is running faster than the framerate of my monitor and giving a tearing effect on the frames because of that. I'd like to wait for vertical sync before calling glutSwapBuffers, but I can't seem to find the correct function to do this. Any ideas? Thanks!

02-03-2010, 10:27 AM
With GLX (X11), uses extension GLX_SGI_video_sync
With Mac (CGL, Carbon and Cocoa), set swap internal to a none zero value:
http://developer.apple.com/mac/library/d...1987-CH216-SW12 (http://developer.apple.com/mac/library/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_contexts/opengl_contexts.html#//apple_ref/doc/uid/TP40001987-CH216-SW12)

With WGL (Windows), use extension WGL_EXT_swap_control

On Linux with nVidia, you can also set some environment variable for force sync blank (or force it in the control panel):
export __GL_SYNC_TO_VBLANK=1

02-03-2010, 12:03 PM
Thanks that is helpful! I'm on Mac OSX so I think I can use
CGLSetParameter(context, kCGLCPSwapInterval, &sync);

However, I am using GLUT so I don't have a context, only a Window. Any idea how to get the context from the window so I call CGLSetParameter? Or does GLUT just not allow this kind of thing and I need to use OpenGL instead of GLUT?

Thanks again!

02-03-2010, 01:44 PM
CGLGetCurrentContext() returns the current rendering context for the calling thread.

http://devworld.apple.com/mac/library/do...tCurrentContext (http://devworld.apple.com/mac/library/documentation/GraphicsImaging/Reference/CGL_OpenGL/Reference/reference.html#//apple_ref/c/func/CGLGetCurrentContext)

02-05-2010, 07:31 AM
Thanks, it works!! Thanks also for the CGL manual link...
Much appreciated.

Here is the actual code, in case this help others, for Mac OS 10.6:

// a swap interval of 1 causes SwapBuffers to wait until the
// next vertical sync, avoiding possible tears in your images.
// Note that this limits your framerate to 60 fps or so,
// so don't forget to turn it OFF if you are doing render timing.

int swap_interval = 1;
CGLContextObj cgl_context = CGLGetCurrentContext();
CGLSetParameter(cgl_context, kCGLCPSwapInterval, &swap_interval);