PDA

View Full Version : pyQT5 and openGL - how to specifiy GL version to use?



frankvw
29th September 2017, 16:57
Hi,

I am looking at the openGL examples of pyQT5 and I seem to get display errors when they run (screen grab attached) accompanied by terminal warning :

ImportError: No module named 'PyQt5._QOpenGLFunctions_3_0'

So, looking into it, I see pyQT only support openGL 2.0, 2.1 and 4.0+. My machine is an older laptop running openGL 3.3 via MESA drivers and intel graphics card on ubuntu 16.04. QT vesion is 5.9 and python 3.5.2. All the other examples work no problems.

I then found some older code on a thread here (https://riverbankcomputing.com/pipermail/pyqt/2014-July/034542.html) discussing the issue and the code presented works fine on my machine, specifiying openGL 2.0 in a GlQWindow class using the function: format.setVersion(2, 0).

class GlQWindow(Qt.QWindow):
def __init__(self):
super().__init__()
format = Qt.QSurfaceFormat()
format.setVersion(2, 0)
format.setProfile(Qt.QSurfaceFormat.CompatibilityP rofile)
format.setStereo(False)
format.setSwapBehavior(Qt.QSurfaceFormat.DoubleBuf fer)
self.setSurfaceType(Qt.QWindow.OpenGLSurface)
self.context = Qt.QOpenGLContext()
self.context.setFormat(format)
if not self.context.create():
raise Exception('self.context.create() failed')
self.create()


This is a few years ago now and QT has changed a fair bit over the intervening years so I was wondering, in the example code for current pyQT5, is there a straightforward way to explicitly set the openGL version to use? So, in the openglwindow.py example, where it searches your opengl environment and specifies that (v3.3 in my case), I cannot figure how to specify not to search and just go straight to 2.1. Setting self.m_gl = (2, 1) or PyQt5._QOpenGLFunctions_2_0 doesn't work and I still get the display error.

Cheers,



#!/usr/bin/env python


################################################## ###########################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
## Contact: http://www.qt-project.org/legal
##
## This file is part of the documentation of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:BSD$
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
## * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
## of its contributors may be used to endorse or promote products derived
## from this software without specific prior written permission.
##
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
## $QT_END_LICENSE$
##
################################################## ###########################


import array

from PyQt5.QtCore import QEvent
from PyQt5.QtGui import (QGuiApplication, QMatrix4x4, QOpenGLContext,
QOpenGLShader, QOpenGLShaderProgram, QSurfaceFormat, QWindow)


class OpenGLWindow(QWindow):
def __init__(self, parent=None):
super(OpenGLWindow, self).__init__(parent)

self.m_update_pending = False
self.m_animating = False
self.m_context = None
self.m_gl = None

self.setSurfaceType(QWindow.OpenGLSurface)

def initialize(self):
pass

def setAnimating(self, animating):
self.m_animating = animating

if animating:
self.renderLater()

def renderLater(self):
if not self.m_update_pending:
self.m_update_pending = True
QGuiApplication.postEvent(self, QEvent(QEvent.UpdateRequest))

def renderNow(self):
if not self.isExposed():
return

self.m_update_pending = False

needsInitialize = False

if self.m_context is None:
self.m_context = QOpenGLContext(self)
self.m_context.setFormat(self.requestedFormat())
self.m_context.create()

needsInitialize = True

self.m_context.makeCurrent(self)

if needsInitialize:
self.m_gl = self.m_context.versionFunctions()
self.m_gl.initializeOpenGLFunctions()

self.initialize()

self.render(self.m_gl)

self.m_context.swapBuffers(self)

if self.m_animating:
self.renderLater()

def event(self, event):
if event.type() == QEvent.UpdateRequest:
self.renderNow()
return True

return super(OpenGLWindow, self).event(event)

def exposeEvent(self, event):
self.renderNow()

def resizeEvent(self, event):
self.renderNow()


class TriangleWindow(OpenGLWindow):
vertexShaderSource = '''
attribute highp vec4 posAttr;
attribute lowp vec4 colAttr;
varying lowp vec4 col;
uniform highp mat4 matrix;
void main() {
col = colAttr;
gl_Position = matrix * posAttr;
}
'''

fragmentShaderSource = '''
varying lowp vec4 col;
void main() {
gl_FragColor = col;
}
'''

def __init__(self):
super(TriangleWindow, self).__init__()

self.m_program = 0
self.m_frame = 0

self.m_posAttr = 0
self.m_colAttr = 0
self.m_matrixUniform = 0

def initialize(self):
self.m_program = QOpenGLShaderProgram(self)

self.m_program.addShaderFromSourceCode(QOpenGLShad er.Vertex,
self.vertexShaderSource)
self.m_program.addShaderFromSourceCode(QOpenGLShad er.Fragment,
self.fragmentShaderSource)

self.m_program.link()

self.m_posAttr = self.m_program.attributeLocation('posAttr')
self.m_colAttr = self.m_program.attributeLocation('colAttr')
self.m_matrixUniform = self.m_program.uniformLocation('matrix')

def render(self, gl):
gl.glViewport(0, 0, self.width(), self.height())

gl.glClear(gl.GL_COLOR_BUFFER_BIT)

self.m_program.bind()

matrix = QMatrix4x4()
matrix.perspective(60, 4.0/3.0, 0.1, 100.0)
matrix.translate(0, 0, -2)
matrix.rotate(100.0 * self.m_frame / self.screen().refreshRate(),
0, 1, 0)

self.m_program.setUniformValue(self.m_matrixUnifor m, matrix)

vertices = array.array('f', [
0.0, 0.707,
-0.5, -0.5,
0.5, -0.5])

gl.glVertexAttribPointer(self.m_posAttr, 2, gl.GL_FLOAT, False, 0,
vertices)
gl.glEnableVertexAttribArray(self.m_posAttr)

colors = array.array('f', [
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0])

gl.glVertexAttribPointer(self.m_colAttr, 3, gl.GL_FLOAT, False, 0,
colors)
gl.glEnableVertexAttribArray(self.m_colAttr)

gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3)

self.m_program.release()

self.m_frame += 1


if __name__ == '__main__':

import sys

app = QGuiApplication(sys.argv)

format = QSurfaceFormat()
format.setSamples(4)

window = TriangleWindow()
window.setFormat(format)
window.resize(640, 480)
window.show()

window.setAnimating(True)

sys.exit(app.exec_())


12599