我用 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
最佳答案
OpenGL 实现不会为您提供您所要求的版本。他们为您提供与您要求的兼容的 OpenGL 版本。 4.5 核心与 3.3 核心兼容,因此即使您要求 3.3 核心,也可以免费实现为您提供 4.5 核心上下文。
因此,如果您的目的是让 3.3 成为最低限度,并且您的代码利用 3.3 后的功能(如果可用),那么正确的方法是要求最低限度。然后询问 OpenGL 实际版本是什么,并使用它来打开这些可选功能。
但是如果您不打算使用 3.3 后的功能,那么就没有理由做任何事情。如果您的代码没有显式调用任何 3.3 后的 OpenGL 功能,那么基于 3.3 的代码在 4.5 实现上的运行与在 3.3 实现上没有什么不同。
OpenGL 版本并不意味着驱动程序中的错误修复等。因此,您使用哪个版本取决于 API 本身:该版本提供的特性和功能是您的代码实际使用的。如果您根据 3.3 编写代码,那么请使用 3.3 并完成它。
关于python - PyQt5选择可用的最新OpenGL版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44277975/