我在Mac上使用PyOpenGL。默认情况下,使用OpenGL 2.1。但是,根据我的研究和OpenGL Extension Viewer,我应该可以使用OpenGL 4.1。 我试图将一个QGLFormat传递给我的QGLWidget,正如我在这里看到的许多线程所做的那样,但上下文版本不会改变。如果我试图强制它,它会告诉我上下文无效。 我一直在尝试了很多 - 这里是一个非常简单的例子,说明我的问题:如何在MacOS上使用QGLFormat更改OpenGL版本?
from PyQt4 import QtGui, QtCore, QtOpenGL
import sys
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
glFormat = QtOpenGL.QGLFormat()
glFormat.setVersion(4,1)
glFormat.setProfile(QtOpenGL.QGLFormat.CoreProfile)
glFormat.setSampleBuffers(True)
glFormat.setDefaultFormat(glFormat)
print("Format version: " + str(glFormat.majorVersion()))
myW = QtOpenGL.QGLWidget(glFormat)
print ("Widget context valid: " + str(myW.context().isValid()))
print ("Widget format version: " + str(myW.format().majorVersion()))
print ("Widget context format version: " + str(myW.context().format().majorVersion()))
myW.context().setFormat(glFormat)
print ("Forced format valid: " + str(myW.context().isValid()))
print ("Forced format version: " + str(myW.format().majorVersion()))
myW.show()
sys.exit(app.exec_())
这里是输出:
Format version: 4
Widget context valid: True
Widget format version: 1
Widget context format version: 1
Forced format valid: False
Forced format version: 4
任何想法,我的问题所在?难道我做错了什么? 我正在使用macOS Sierra版本10.12.6。 我使用Qt版本4.8.7,sip版本4.19.3和pyqt版本4.12,以防相关。 这是what OpenGL Extension Viewer tells me。
任何帮助将不胜感激!
编辑:
我发现什么看起来像一个类似的问题与C++在这里。我不知道如何翻译解决方案到PyQt,但我会研究这个: Changing the OpenGL Context Version for QGLWidgets in Qt 4.8.6 on OS X。
编辑3:
尝试(第二版)由@ekhumoro提供的补丁,当我得到以下输出:
patching file qpy/QtOpenGL/core_profile_attributes.mm
patching file qpy/QtOpenGL/qpyopengl.pro
patching file sip/QtOpenGL/qgl.sip
patch unexpectedly ends in middle of line
Hunk #3 FAILED at 493.
1 out of 3 hunks FAILED -- saving rejects to file sip/QtOpenGL/qgl.sip.rej
qpyopengl.pro现在可以正确地打上补丁,但它qgl.sip仍然失败。
更新:
这里是拒绝文件为qgl.sip内容:
***************
*** 478,481 ****
%ModuleHeaderCode
#include <qpyopengl_api.h>
--- 493,498 ----
%ModuleHeaderCode
#include <qpyopengl_api.h>
+ typedef struct GDevice** GDHandle;
+ void* select_3_2_mac_visual();
出于好奇,我跑configure-ng.py并试图编译。我收到以下错误:
[...]/PyQt4_gpl_mac-4.12.1/sip/QtOpenGL/qgl.sip:269:5: error: 'virtual' can only appear on non-static member functions
virtual void* chooseMacVisual(GDHandle handle)
^
[...]/PyQt4_gpl_mac-4.12.1/sip/QtOpenGL/qgl.sip:271:16: error: use of undeclared identifier 'select_3_2_mac_visual'
return select_3_2_mac_visual();
^
[...]/PyQt4_gpl_mac-4.12.1/sip/QtOpenGL/qgl.sip:269:44: warning: unused parameter 'handle' [-Wunused-parameter]
virtual void* chooseMacVisual(GDHandle handle)
你需要创建在OSX上_forward-compatible_上下文。 – derhass
@derhass:你能更具体吗? – Elensil