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