gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:04:03 24 4
gpt4 key购买 nike

正如标题所言。我想在 PyOpenGL 中打开垂直同步,但我该怎么做呢?一个相当详尽的网络搜索没有发现任何东西,但也许有人有一个聪明的解决方案?我在 OS X 上,我不介意使用哪个包来创建窗口和应用程序循环。但是,出于下面讨论的原因,我宁愿远离开发成熟的 Cocoa 应用程序。

我研究过使用 pyglet 而不是 PyOpenGL,但是在 64 位操作系统上运行的唯一版本的 pyglet 是一个近一年的 alpha 版本,所以我不想使用它,因为我害怕它可能是废弃软件。真可惜,因为它实际上看起来比 PyOpenGL 好得多。

关于 this page我找到了以下代码。该页面说它适用于 pygame,但它看起来应该也适用于 Glut。但是,当我运行它时(在创建上下文之后)它只会导致段错误。如果有人能深入了解为什么会发生这种情况,那就太好了,因为我正在寻找这样的东西。

import sys

def enable_vsync():
if sys.platform != 'darwin':
return
try:
import ctypes
import ctypes.util
ogl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("OpenGL"))
# set v to 1 to enable vsync, 0 to disable vsync
v = ctypes.c_int(1)
ogl.CGLSetParameter(ogl.CGLGetCurrentContext(), ctypes.c_int(222), ctypes.pointer(v))
except:
print "Unable to set vsync mode, using driver defaults"

我可以试试 pygame,看看这段代码是否适用于它,但我在网上发现一些报告说这段代码也与 pygame 一起崩溃,所以我猜它以前可以工作,但现在不行了。

最后,我知道一个可行的解决方案是使用 PyObjC 构建 Cocoa 应用程序。然而,那里有一个相当大的学习曲线,我最终会得到一些甚至不接近跨平台的东西。此代码仅供我个人使用,但我非常关心如果我在几年后在另一台机器上重新使用它,我会最大限度地提高它再次工作的可能性。出于这些原因,我真的不认为构建 Cocoa 应用程序是我想要涉足的领域。

最佳答案

我设法修复了您的段错误。这是在 Mac 上启用垂直同步的工作代码:-

import sys

def enable_vsync():
if sys.platform != 'darwin':
return
try:
import ctypes
import ctypes.util
ogl = ctypes.cdll.LoadLibrary(ctypes.util.find_library("OpenGL"))
v = ctypes.c_int(1)

ogl.CGLGetCurrentContext.argtypes = []
ogl.CGLGetCurrentContext.restype = ctypes.c_void_p

ogl.CGLSetParameter.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
ogl.CGLSetParameter.restype = ctypes.c_int

context = ogl.CGLGetCurrentContext()

ogl.CGLSetParameter(context, 222, ctypes.pointer(v))
except Exception as e:
print("Unable to set vsync mode, using driver defaults: {}".format(e))

只需在创建和设置上下文后调用此 enable_vsync() 函数即可。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com