Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable vertical sync in opengl?

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?

like image 690
hasen Avatar asked Feb 26 '09 04:02

hasen


People also ask

Does VSync lower FPS?

VSync is far from a perfect solution and can negatively affect your gaming experience, even if it is useful and working as intended. If a monitor and a game are having trouble syncing up, then VSync can lower your frame rate significantly to try to find a point where they can.

Should I enable VSync?

You may experience input lag and a decrease in the frame rate if your VSync isn't appropriate for your PC. So, the question is whether you should turn VSync ON or OFF. The answer is simple. If screen tearing interferes with your gaming experience and causes frequent screen tears, you should enable VSync.

How do I turn off VSync on Glfw?

Once you've made your GL context current via glfwMakeContextCurrent() you can use glfwSwapInterval(0) to request that vsync be disabled: int main( int, char** ) { GLFWwindow* window; glfwInit(); window = glfwCreateWindow( 640, 480, "GLFW", NULL, NULL ); glfwMakeContextCurrent( window ); glfwSwapInterval( 0 ); ...


1 Answers

On Windows there is OpenGL extension method wglSwapIntervalEXT. From the post by b2b3 http://www.gamedev.net/community/forums/topic.asp?topic_id=360862:

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(_wglGetExtensionsStringEXT(), 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) wglGetProcAddress("wglSwapIntervalEXT");      // this is another function from WGL_EXT_swap_control extension     wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC) wglGetProcAddress("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().

like image 115
eugensk Avatar answered Sep 18 '22 12:09

eugensk