import sys from OpenGL import GL from PySide2.QtCore import QLibraryInfo from PySide2.QtGui import QOpenGLFunctions, QColor, QOpenGLContext, QOpenGLShaderProgram, QOpenGLShader from PySide2.QtWidgets import QOpenGLWidget, QApplication basic_vertex_shader_source = \ """ attribute highp vec4 vPosition; void main() { gl_Position = vPosition; } """ basic_fragment_shader_source = \ """ uniform mediump vec4 color; void main() { gl_FragColor = color; // WILL DRAW A BLACK TRIANGLE EVEN THOUGH THE UNIFORM IS SET //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // WILL DRAW A RED TRIANGLE } """ class TestWidget(QOpenGLWidget): def initializeGL(self): context = QOpenGLContext.currentContext() self.GLF = QOpenGLFunctions(context) self.shader_program = QOpenGLShaderProgram(context) self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, basic_vertex_shader_source) self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, basic_fragment_shader_source) self.shader_program.bind() self.GLF.glClear(GL.GL_DEPTH_BUFFER_BIT) self.GLF.glClearColor(255, 255, 255, 255) self.GLF.glEnable(GL.GL_DEPTH_TEST) self.GLF.glEnable(GL.GL_CULL_FACE) def paintGL(self): self.GLF.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) vertex_position_location = self.shader_program.attributeLocation("vPosition") color_location = self.shader_program.attributeLocation("color") vertices = [ 0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0 ] color = QColor(255, 0, 0, 255) # <== THIS SHOULD BE A RED TRIANGLE AS WELL self.shader_program.enableAttributeArray(vertex_position_location) self.shader_program.setAttributeArray(vertex_position_location, vertices, 3) self.shader_program.setUniformValue(color_location, color) # <= RUNTIME WARNING, COLOR IS NOT SET self.GLF.glDrawArrays(GL.GL_TRIANGLES, 0, 3) self.shader_program.disableAttributeArray(vertex_position_location) def resizeGL(self, w, h): self.GLF.glViewport(0, 0, w, h) if __name__ == "__main__": print('Python {}.{}'.format(sys.version_info[0], sys.version_info[1])) print(QLibraryInfo.build()) app = QApplication() widget = TestWidget() widget.show() app.exec_()