我在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