opengl - 如何在opengl中启用垂直同步?

标签 opengl vsync vertical-sync

如何启用垂直同步?

它是像glEnable(GL_VSYNC)这样简单的东西吗? (尽管 glEnable 接受的选项中没有 GL_VSYNC 或类似的东西)。

或者在opengl中没有标准的方法来做到这一点?

最佳答案

在 Windows 上有 OpenGL 扩展方法 wglSwapIntervalEXT。 来自 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().

关于opengl - 如何在opengl中启用垂直同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589064/

相关文章:

c++ - 有没有办法取消 GLFW 中的 60 fps 上限?

c# - 如何使用Application.targetFrameRate设置目标帧速率以修复不良帧速率(vsync)

c++ - 如何在 OpenGL 中启用 VSYNC

sdl - 如何在窗口 SDL 应用程序中同步翻页和垂直回扫?

c++ - OpenGL - 加载多面的正确方法

c++ - 在 OpenGL 中动态创建规则网格的互补三角形

java - 如何在 LibGDX 中正确旋转和移动 3D 透视相机

opengl - TBO/VBO/其他用于带有压缩和 mipmap 的 opengl 纹理

wpf - 我可以在 CompositionTarget.Rendering 中做什么?

javascript - 需要。 JavaScript 3D 框架,具有帧速率和键盘事件的精确时间戳