python - PyQt5选择可用的最新OpenGL版本

标签 python python-3.x opengl pyqt5 python-moderngl

我用 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/

相关文章:

c++ - 如何在现代 Opengl 中添加 View 矩阵

Python通过python生成xml文件

Python-存在多个同名元素属性时如何编辑特定的XML元素内容?

python-3.x - 如何解码文件中base64格式的编码值?

python - 如何将 tkinter 按钮与标签和输入框保持在同一行

c++ - 如何在 Win32(C++) 静态控件中正确呈现 OpenGL?

python - `MonitoredTrainingSession()`如何与 "restore"和 "testing mode"一起使用?

python - ValueError : Cannot set tensor: Got value of type NOTYPE but expected type FLOAT64 for input 0, 名称:serving_default_z_raw_min:0

Python 扩展在处理大型列表时创建无效指针

多边形太少的 OpenGL 扭曲效果