我在 PyQt5 中编写了一个应用程序。我想使用可用的最新 OpenGL 版本。我也想要一些向后兼容性。
目前我有:
fmt = QtOpenGL.QGLFormat()
fmt.setVersion(3, 3)
fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)
但是我想尽可能使用最新版本。
我需要类似的东西:
if supported(4, 5):
fmt.setVersion(4, 5)
elif supported(4, 4):
...
这是我的代码:
import struct
import ModernGL
from PyQt5 import QtOpenGL, QtWidgets
class QGLControllerWidget(QtOpenGL.QGLWidget):
def __init__(self):
fmt = QtOpenGL.QGLFormat()
fmt.setVersion(3, 3)
fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)
fmt.setSampleBuffers(True)
super(QGLControllerWidget, self).__init__(fmt, None)
def initializeGL(self):
self.ctx = ModernGL.create_context()
prog = self.ctx.program([
self.ctx.vertex_shader('''
#version 330
in vec2 vert;
void main() {
gl_Position = vec4(vert, 0.0, 1.0);
}
'''),
self.ctx.fragment_shader('''
#version 330
out vec4 color;
void main() {
color = vec4(0.30, 0.50, 1.00, 1.0);
}
'''),
])
vbo = self.ctx.buffer(struct.pack('6f', 0.0, 0.8, -0.6, -0.8, 0.6, -0.8))
self.vao = self.ctx.simple_vertex_array(prog, vbo, ['vert'])
def paintGL(self):
self.ctx.viewport = (0, 0, self.width(), self.height())
self.ctx.clear(0.9, 0.9, 0.9)
self.vao.render()
self.ctx.finish()
app = QtWidgets.QApplication([])
window = QGLControllerWidget()
window.show()
app.exec_()
编辑1:
如何编写像supported()上面这样的函数?
编辑2:
我运行版本查询,窗口要求支持 OpenGL3.3:
GL_VERSION -> 3.3.0 NVIDIA 382.05
GL_VENDOR -> NVIDIA Corporation
GL_MAJOR -> 3
GL_MINOR -> 3