Szabolcs Dombi
Szabolcs Dombi

Reputation: 5783

PyQt5 choose the latest OpenGL version available

I write an application in PyQt5. I want to use the latest OpenGL version available. I want some backward compatibility as well.

Currently I have:

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

However I want to use the latest version possible.

I need something like:

if supported(4, 5):
    fmt.setVersion(4, 5)

elif supported(4, 4):
    ...

Here is my code:

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_()

EDIT 1:

How to write a function like supported() above?

EDIT 2:

I run a version query with a window asking for OpenGL3.3 support:

GL_VERSION -> 3.3.0 NVIDIA 382.05
GL_VENDOR  -> NVIDIA Corporation
GL_MAJOR   -> 3
GL_MINOR   -> 3

Upvotes: 1

Views: 1389

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473262

OpenGL implementations do not give you the version you ask for. They give you a version of OpenGL that is compatible with what you ask for. 4.5 core is compatible with 3.3 core, so an implementation is free to give you a 4.5 core context, even if you asked for 3.3 core.

So if your intent is to have 3.3 be a minimum, with your code taking advantage of post-3.3 features if they're available, the right way to do it is to ask for the minimum. Then ask OpenGL what the actual version is, and use that to turn on those optional features.

But if you don't intend to ever use post-3.3 features, then there's no reason to do anything. If your code doesn't explicitly invoke any post-3.3 OpenGL functionality, then your 3.3-based code will run no differently on a 4.5 implementation than a 3.3 one.

OpenGL versions don't signify things like bug fixes in drivers and the like. So what version you use is a matter of the API itself: the features and functionality that version provides which your code actually uses. If you wrote your code against 3.3, then ask for 3.3 and be done with it.

Upvotes: 1

Related Questions