views:

5334

answers:

3

How do you enable vertical sync?

Is it something simple like glEnable(GL_VSYNC)? (though there's no such thing as GL_VSYNC or anything like it in the options that glEnable accepts).

or is there no standard way to do this in opengl?

+3  A: 

Based on an interview with Jake Simpson (Lead Programmer for Raven Software):

"There is no Vsync in OpenGL as a command. Most apps use the GLFlush command, sometimes followed by a GLFinish command. The GLFlush command basically says "Ok, what ever commands you have in your buffer, send 'em to the rendering device now, and get it working." It doesn't care where the raster is in the drawing sync, it just goes out and does it. The GLFinish command will then make the app wait until the rendering device has completed all the commands it has been sent up til then. This gives you the fastest feedback, fairly obviously. Now, depending on whether you are double buffering your video displays (ie rendering to the back one while the front one is being displayed) you might want to use a swapbuffers command. This means that you can afford to slap out commands to the rendering device when ever you feel like it, since it's always going to be rendering to an unseen buffer. The SwapBuffers command does what it says, it swaps the buffers between the front and the back. When it actually does this, ie at Vsync or just randomly whenever it can depends on the card you are using. Sometimes you can set the 'wait for Vsync' in the properties dialog for your card, sometimes it has to be set via registry options. It's messy and highly card dependant. Obviously working in a window you don't get any kind of Vsyncing going on."

You can read the rest here: http://www.d-silence.com/feature.php?id=255

John Rasch
+7  A: 

OpenGL extension function wglSwapIntervalEXT does this, but on Windows only.
Quote from post http://www.gamedev.net/community/forums/topic.asp?topic_id=360862 by b2b3:

quote begin


If you are working on Windows you have to use extensions to use wglSwapIntervalExt function. It is defined in wglext.h. You will also want to download glext.h file. In wglext file all entry points for Windows specific extensions are declared. All such functions start with prefix wgl. To get more info about all published extensions you can look into OpenGL Extension Registry.

wglSwapIntervalEXT is from WGL_EXT_swap_control extension. It lets you specify minimum number of frames before each buffer swap. Usually it is used for vertical synchronization (if you set swap interval to 1). More info about whole extension can be found here. Before using this function you need query whether you card has support for WGL_EXT_swap_control and then obtain pointer to the function using wglGetProcAddress function.

To test for support of given extension you can use function like this:

#include <windows.h>
#include "wglext.h"

bool WGLExtensionSupported(const char *extension_name)
{
    // this is pointer to function which returns pointer to string with list of all wgl extensions
    PFNWGLGETEXTENSIONSSTRINGEXTPROC _wglGetExtensionsStringEXT = NULL;

    // determine pointer to wglGetExtensionsStringEXT function
    _wglGetExtensionsStringEXT = (PFNWGLGETEXTENSIONSSTRINGEXTPROC) wglGetProcAddress("wglGetExtensionsStringEXT");

    if (strstr(_wglGetExtensionsString(), extension_name) == NULL)
    {
        // string was not found
        return false;
    }

    // extension is supported
    return true;
}

To initialize your function pointers you need to:

PFNWGLSWAPINTERVALEXTPROC       wglSwapIntervalEXT = NULL;
PFNWGLGETSWAPINTERVALEXTPROC    wglGetSwapIntervalEXT = NULL;

if (WGLExtensionSupported("WGL_EXT_swap_control"))
{
    // Extension is supported, init pointers.
    wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC) LogGetProcAddress("wglSwapIntervalEXT");

    // this is another function from WGL_EXT_swap_control extension
    wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) LogGetProcAddress("wglGetSwapIntervalEXT");
}

Then you can use these pointers as any other pointer to function. To enable vync you can call wglSwapIntervalEXT(1), to disable it you call wglSwapIntervalEXT(0).

To get current swap interval you need to call wglGetSwapIntervalEXT().


quote end

eugensk00
A: 

Has a matter of providing a cross-platform way of doing this, can anyone give example for GLX?

bohan