PyQt5选择可用的最新OpenGL版本

2024-06-24 12:51:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我用PyQt5编写了一个应用程序。我想使用最新的OpenGL版本。我也想要一些向后兼容性。在

目前我有:

fmt = QtOpenGL.QGLFormat()
fmt.setVersion(3, 3)
fmt.setProfile(QtOpenGL.QGLFormat.CoreProfile)

但是我想使用最新版本。在

我需要这样的东西:

^{pr2}$

这是我的代码:

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

Tags: importself版本defpyqt5ctxglfmt
1条回答
网友
1楼 · 发布于 2024-06-24 12:51:24

OpenGL实现不会提供您所需的版本。他们为您提供了一个与您所要求的兼容的OpenGL版本。4.5core与3.3core兼容,因此实现可以免费为您提供4.5core上下文,即使您要求3.3core。在

因此,如果您的目的是将3.3作为最低要求,并且您的代码可以利用3.3之后的特性(如果它们可用),那么正确的方法是要求最小值。然后询问OpenGL实际的版本是什么,并使用它来打开这些可选功能。在

但是如果你不打算使用3.3之后的特性,那么就没有理由做任何事情了。如果您的代码没有显式地调用任何3.3后的OpenGL功能,那么基于3.3的代码在4.5实现和3.3实现上的运行方式没有什么不同。在

OpenGL版本并不意味着驱动程序中的bug修复之类的事情。因此,您使用什么版本是API本身的问题:版本提供的特性和功能是您的代码实际使用的。如果你的代码是针对3.3编写的,那么要求3.3并完成它。在

相关问题 更多 >