如何在OpenGL中启用垂直同步? [英] how to enable vertical sync in opengl?

查看:521
本文介绍了如何在OpenGL中启用垂直同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启用垂直同步?

glEnable(GL_VSYNC)这样简单吗? (尽管glEnable接受的选项中没有GL_VSYNC之类的东西.)

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).

还是在opengl中没有标准的方法?

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

推荐答案

在Windows上,存在OpenGL扩展方法wglSwapIntervalEXT. 来自b2b3的帖子 http://www.gamedev.net/community /forums/topic.asp?topic_id=360862 :

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

如果您在Windows上工作,则必须使用扩展名才能使用 wglSwapIntervalExt功能.它是 在wgle​​xt.h中定义.您还将想要 下载glext.h文件. 在wgle​​xt文件中,Windows特定扩展的所有入口点均为 宣布.所有这些功能都会启动 带有前缀wgl. 要获取有关所有已发布扩展的更多信息,您可以查看 OpenGL扩展注册表.

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来自WGL_EXT_swap_control扩展名.它 让您指定最小数量的 每个缓冲区交换之前的帧. 通常用于垂直 同步(如果您设置了swap 间隔为1).有关整体的更多信息 扩展名可以在这里找到. 使用此功能之前,您需要查询您的卡是否具有 支持WGL_EXT_swap_control和 然后获取指向该函数的指针 使用wglGetProcAddress函数.

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");
}

然后,您可以将这些指针用作其他任何起作用的指针.要启用vync,您可以调用wglSwapIntervalEXT(1),要禁用vync,您可以调用wglSwapIntervalEXT(0).

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).

要获取当前的交换间隔,您需要致电wglGetSwapIntervalEXT().

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

这篇关于如何在OpenGL中启用垂直同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆