How to enable vsync in PyOpengl?

As the name says. I would like to enable vertical sync in PyOpenGL, but how can I do this? A sufficiently comprehensive search on the Internet did not cause anything, but maybe someone has a smart solution? I am in OS X, and I do not mind which package is used to create the window and the application loop. However, I would prefer to avoid developing a full-blown Cocoa application for the reasons discussed below.

I studied the use of pyglet, not PyOpenGL, but the only version of pyglet that runs on a 64-bit OS is the alpha release, which is almost a year ago, so I don't want to use it because I'm afraid it might be refused. This is a shame because it really looked much better than PyOpenGL.

On this page I found the following code. The page talks about this for pygame, but it looks like it should work for Glut as well. However, when I run it (after creating the context), it just causes a segmentation error. If anyone can understand why this is happening, it would be great because something like this is what I'm looking for.

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" 

I could try pygame instead and see if this code works with this, but I found several reports on the Internet that this code also works with pygame, so I assume this is what worked before, but now it doesn’t .

Finally, I know that the solution that will work is to create a Cocoa application with PyObjC. However, there is a rather large learning curve, and I get something that is even close to cross-platform. This code is for my personal use only, but I am very interested in maximizing my likelihood of getting it to work again if I return to it in a few years on another machine. For these reasons, I really don't think that creating a Cocoa application is what I want to get to.

+4
source share
3 answers

I managed to fix your segmentation error. Here is the working code to enable vsync on 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)) 

Just run this enable_vsync () function after creating and setting the context.

+3
source

I solved this using pyglet. Although no code has been tested for a long time, it still seems to be an active community in which the main developer is involved. It is cross platform and it was very easy to go. I just use pyglet to manage windows, and I use PyOpenGL for OpenGL stuff. Later, I can use pyglet faster, but lower levels of OpenGL binding, if I need, and I can take advantage of various additional functions that pyglet offers.

However, the situation looks a bit unstable, because the official release that runs on OS X Mountain Lion (I'm not installed from the last source) is not afraid of me, and I'm afraid that my code may just stop working after one or two major OS X updates Therefore, if someone has a solution that works without using a piglet, I would still appreciate it.

0
source

Try using WinDLL to load the OpenGL DLL and replace the CGL GL or gl (depending on what works) on behalf of the functions. For example: ogl = ctypes.windll.LoadLibrary (ctypes.util.find_library ("OpenGL")) Then your functions: [GL, GL (recommended)] GetCurrentContext ()

0
source

Source: https://habr.com/ru/post/1485977/


All Articles