class TriangleUnderlayRenderer(QObject): def __init__(self, parent=None): super(TriangleUnderlayRenderer, self).__init__(parent) self._shader_program = None self._viewport_size = QSize() self._window = None @pyqtSlot() def paint(self): # TODO test on Ubuntu # for Darwin, it's a must gl = self._window.openglContext().versionFunctions() if self._shader_program is None: self._shader_program = QOpenGLShaderProgram() self._shader_program.addShaderFromSourceFile( QOpenGLShader.Vertex, 'shaders/OpenGL_2_1/vertex.glsl') self._shader_program.addShaderFromSourceFile( QOpenGLShader.Fragment, 'shaders/OpenGL_2_1/fragment.glsl') self._shader_program.bindAttributeLocation('position', 0) self._shader_program.bindAttributeLocation('color', 1) self._shader_program.link() self._shader_program.bind() self._shader_program.enableAttributeArray(0) self._shader_program.enableAttributeArray(1) self._shader_program.setAttributeArray(0, positions) self._shader_program.setAttributeArray(1, colors) gl.glViewport(0, 0, self._viewport_size.width(), self._viewport_size.height()) gl.glClearColor(0.5, 0.5, 0.5, 1) gl.glDisable(gl.GL_DEPTH_TEST) gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 3) self._shader_program.disableAttributeArray(0) self._shader_program.disableAttributeArray(1) self._shader_program.release() # Restore the OpenGL state for QtQuick rendering self._window.resetOpenGLState() self._window.update() def set_viewport_size(self, size): self._viewport_size = size def set_window(self, window): self._window = window
class SquircleRenderer(QObject): # QOpenGLFunctions """docstring for SquircleRenderer""" def __init__(self, parent=None): super(SquircleRenderer, self).__init__(parent) self.m_t = 0.0 self.m_program = None self.m_viewportSize = QSize() def setT(self, t): self.m_t = t def setViewportSize(self, size): self.m_viewportSize = size def setWin(self, win): self.win = win ver = QOpenGLVersionProfile() ver.setVersion(2, 1) self.m_context = self.win.openglContext() self.gl = self.m_context.versionFunctions(ver) @pyqtSlot() def paint(self): if not self.m_program: self.gl.initializeOpenGLFunctions() self.m_program = QOpenGLShaderProgram(self) self.m_program.addShaderFromSourceCode( QOpenGLShader.Vertex, "attribute highp vec4 vertices;" "varying highp vec2 coords;" "void main() {" " gl_Position = vertices;" " coords = vertices.xy;" "}", ) self.m_program.addShaderFromSourceCode( QOpenGLShader.Fragment, "uniform lowp float t;" "varying highp vec2 coords;" "void main() {" " lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));" " i = smoothstep(t - 0.8, t + 0.8, i);" " i = floor(i * 20.) / 20.;" " gl_FragColor = vec4(coords * .5 + .5, i, i);" "}", ) self.m_program.bindAttributeLocation("vertices", 0) self.m_program.link() self.m_program.bind() self.m_program.enableAttributeArray(0) values = [(-1, -1), (1, -1), (-1, 1), (1, 1)] self.m_program.setAttributeArray(0, values) self.m_program.setUniformValue("t", self.m_t) # print("DATA:",self.m_viewportSize.width(), self.m_viewportSize.height(), self.m_t)#, self.gl.glViewport) self.gl.glViewport(0, 0, self.m_viewportSize.width(), self.m_viewportSize.height()) self.gl.glDisable(self.gl.GL_DEPTH_TEST) self.gl.glClearColor(0, 0, 0, 1) self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT) self.gl.glEnable(self.gl.GL_BLEND) self.gl.glBlendFunc(self.gl.GL_SRC_ALPHA, self.gl.GL_ONE) self.gl.glDrawArrays(self.gl.GL_TRIANGLE_STRIP, 0, 4) self.m_program.disableAttributeArray(0) self.m_program.release()
class GLWidget(QGLWidget): clicked = pyqtSignal() PROGRAM_VERTEX_ATTRIBUTE, PROGRAM_TEXCOORD_ATTRIBUTE = range(2) vsrc = """ attribute highp vec4 vertex; attribute mediump vec4 texCoord; varying mediump vec4 texc; uniform mediump mat4 matrix; void main(void) { gl_Position = matrix * vertex; texc = texCoord; } """ fsrc = """ uniform sampler2D texture; varying mediump vec4 texc; void main(void) { gl_FragColor = texture2D(texture, texc.st); } """ coords = (((+1, -1, -1), (-1, -1, -1), (-1, +1, -1), (+1, +1, -1)), ((+1, +1, -1), (-1, +1, -1), (-1, +1, +1), (+1, +1, +1)), ((+1, -1, +1), (+1, -1, -1), (+1, +1, -1), (+1, +1, +1)), ((-1, -1, -1), (-1, -1, +1), (-1, +1, +1), (-1, +1, -1)), ((+1, -1, +1), (-1, -1, +1), (-1, -1, -1), (+1, -1, -1)), ((-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1))) def __init__(self, parent=None, shareWidget=None): super(GLWidget, self).__init__(parent, shareWidget) self.clearColor = Qt.black self.xRot = 0 self.yRot = 0 self.zRot = 0 self.clearColor = QColor() self.lastPos = QPoint() self.program = None def minimumSizeHint(self): return QSize(50, 50) def sizeHint(self): return QSize(200, 200) def rotateBy(self, xAngle, yAngle, zAngle): self.xRot += xAngle self.yRot += yAngle self.zRot += zAngle self.updateGL() def setClearColor(self, color): self.clearColor = color self.updateGL() def initializeGL(self): self.makeObject() glEnable(GL_DEPTH_TEST) glEnable(GL_CULL_FACE) vshader = QOpenGLShader(QOpenGLShader.Vertex, self) vshader.compileSourceCode(self.vsrc) fshader = QOpenGLShader(QOpenGLShader.Fragment, self) fshader.compileSourceCode(self.fsrc) self.program = QOpenGLShaderProgram(self) self.program.addShader(vshader) self.program.addShader(fshader) self.program.bindAttributeLocation('vertex', self.PROGRAM_VERTEX_ATTRIBUTE) self.program.bindAttributeLocation('texCoord', self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.link() self.program.bind() self.program.setUniformValue('texture', 0) self.program.enableAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE) self.program.enableAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.setAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE, self.vertices) self.program.setAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE, self.texCoords) def paintGL(self): self.qglClearColor(self.clearColor) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) m = QMatrix4x4() m.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, 15.0) m.translate(0.0, 0.0, -10.0) m.rotate(self.xRot / 16.0, 1.0, 0.0, 0.0) m.rotate(self.yRot / 16.0, 0.0, 1.0, 0.0) m.rotate(self.zRot / 16.0, 0.0, 0.0, 1.0) self.program.setUniformValue('matrix', m) for i in range(6): glBindTexture(GL_TEXTURE_2D, self.textures[i]) glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4) def resizeGL(self, width, height): side = min(width, height) glViewport((width - side) // 2, (height - side) // 2, side, side) def mousePressEvent(self, event): self.lastPos = event.pos() def mouseMoveEvent(self, event): dx = event.x() - self.lastPos.x() dy = event.y() - self.lastPos.y() if event.buttons() & Qt.LeftButton: self.rotateBy(8 * dy, 8 * dx, 0) elif event.buttons() & Qt.RightButton: self.rotateBy(8 * dy, 0, 8 * dx) self.lastPos = event.pos() def mouseReleaseEvent(self, event): self.clicked.emit() def makeObject(self): self.textures = [] self.texCoords = [] self.vertices = [] for i in range(6): self.textures.append( self.bindTexture(QPixmap(':/images/side%d.png' % (i + 1)))) for j in range(4): self.texCoords.append(((j == 0 or j == 3), (j == 0 or j == 1))) x, y, z = self.coords[i][j] self.vertices.append((0.2 * x, 0.2 * y, 0.2 * z))
class DDSWidget(QOpenGLWidget): def __init__(self, ddsFile, debugContext = False, parent = None, f = Qt.WindowFlags()): super(DDSWidget, self).__init__(parent, f) self.ddsFile = ddsFile self.clean = True self.logger = None self.program = None self.transparecyProgram = None self.texture = None self.vbo = None self.vao = None self.backgroundColour = None if debugContext: format = QSurfaceFormat() format.setOption(QSurfaceFormat.DebugContext) self.setFormat(format) self.logger = QOpenGLDebugLogger(self) qDebug("__init__()") def __del__(self): qDebug("__del__()") self.cleanup() def __dtor__(self): qDebug("__dtor__()") self.cleanup() def initializeGL(self): qDebug("initializeGL()") if self.logger: self.logger.initialize() self.logger.messageLogged.connect(lambda message: qDebug(self.__tr("OpenGL debug message: {0}").fomat(message.message()))) self.logger.startLogging() gl = QOpenGLContext.currentContext().versionFunctions(glVersionProfile) QOpenGLContext.currentContext().aboutToBeDestroyed.connect(self.cleanup) self.clean = False fragmentShader = None vertexShader = vertexShader2D if self.ddsFile.isCubemap: fragmentShader = fragmentShaderCube vertexShader = vertexShaderCube if QOpenGLContext.currentContext().hasExtension(b"GL_ARB_seamless_cube_map"): GL_TEXTURE_CUBE_MAP_SEAMLESS = 0x884F gl.glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) elif self.ddsFile.glFormat.samplerType == "F": fragmentShader = fragmentShaderFloat elif self.ddsFile.glFormat.samplerType == "UI": fragmentShader = fragmentShaderUInt else: fragmentShader = fragmentShaderSInt self.program = QOpenGLShaderProgram(self) self.program.addShaderFromSourceCode(QOpenGLShader.Vertex, vertexShader) self.program.addShaderFromSourceCode(QOpenGLShader.Fragment, fragmentShader) self.program.bindAttributeLocation("position", 0) self.program.bindAttributeLocation("texCoordIn", 1) self.program.link() self.transparecyProgram = QOpenGLShaderProgram(self) self.transparecyProgram.addShaderFromSourceCode(QOpenGLShader.Vertex, transparencyVS) self.transparecyProgram.addShaderFromSourceCode(QOpenGLShader.Fragment, transparencyFS) self.transparecyProgram.bindAttributeLocation("position", 0) self.transparecyProgram.link() self.vao = QOpenGLVertexArrayObject(self) vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao) self.vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer) self.vbo.create() self.vbo.bind() theBytes = struct.pack("%sf" % len(vertices), *vertices) self.vbo.allocate(theBytes, len(theBytes)) gl.glEnableVertexAttribArray(0) gl.glEnableVertexAttribArray(1) gl.glVertexAttribPointer(0, 4, gl.GL_FLOAT, False, 6 * 4, 0) gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT, False, 6 * 4, 4 * 4) self.texture = self.ddsFile.asQOpenGLTexture(gl, QOpenGLContext.currentContext()) def resizeGL(self, w, h): qDebug("resizeGL(" + str(w) + ", " + str(h) + ")") aspectRatioTex = self.texture.width() / self.texture.height() if self.texture else 1.0 aspectRatioWidget = w / h ratioRatio = aspectRatioTex / aspectRatioWidget self.program.bind() self.program.setUniformValue("aspectRatioRatio", ratioRatio) self.program.release() def paintGL(self): qDebug("paintGL()") gl = QOpenGLContext.currentContext().versionFunctions(glVersionProfile) vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao) # Draw checkerboard so transparency is obvious self.transparecyProgram.bind() if self.backgroundColour and self.backgroundColour.isValid(): self.transparecyProgram.setUniformValue("backgroundColour", self.backgroundColour) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6) self.transparecyProgram.release() self.program.bind() if self.texture: self.texture.bind() gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6) if self.texture: self.texture.release() self.program.release() def cleanup(self): qDebug("cleanup()") if not self.clean: self.makeCurrent() self.program = None self.transparecyProgram = None if self.texture: self.texture.destroy() self.texture = None self.vbo.destroy() self.vbo = None self.vao.destroy() self.vao = None self.doneCurrent() self.clean = True def setBackgroundColour(self, colour): self.backgroundColour = colour def getBackgroundColour(self): return self.backgroundColour def __tr(self, str): return QCoreApplication.translate("DDSWidget", str)
class GLWidget(QOpenGLWidget): clicked = pyqtSignal() PROGRAM_VERTEX_ATTRIBUTE, PROGRAM_TEXCOORD_ATTRIBUTE = range(2) vsrc = """ attribute highp vec4 vertex; attribute mediump vec4 texCoord; varying mediump vec4 texc; uniform mediump mat4 matrix; void main(void) { gl_Position = matrix * vertex; texc = texCoord; } """ fsrc = """ uniform sampler2D texture; varying mediump vec4 texc; void main(void) { gl_FragColor = texture2D(texture, texc.st); } """ coords = ( ((+1, -1, -1), (-1, -1, -1), (-1, +1, -1), (+1, +1, -1)), ((+1, +1, -1), (-1, +1, -1), (-1, +1, +1), (+1, +1, +1)), ((+1, -1, +1), (+1, -1, -1), (+1, +1, -1), (+1, +1, +1)), ((-1, -1, -1), (-1, -1, +1), (-1, +1, +1), (-1, +1, -1)), ((+1, -1, +1), (-1, -1, +1), (-1, -1, -1), (+1, -1, -1)), ((-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1)), ) def __init__(self, parent=None): super(GLWidget, self).__init__(parent) self.clearColor = QColor(Qt.black) self.xRot = 0 self.yRot = 0 self.zRot = 0 self.program = None self.lastPos = QPoint() def minimumSizeHint(self): return QSize(50, 50) def sizeHint(self): return QSize(200, 200) def rotateBy(self, xAngle, yAngle, zAngle): self.xRot += xAngle self.yRot += yAngle self.zRot += zAngle self.update() def setClearColor(self, color): self.clearColor = color self.update() def initializeGL(self): version_profile = QOpenGLVersionProfile() version_profile.setVersion(2, 0) self.gl = self.context().versionFunctions(version_profile) self.gl.initializeOpenGLFunctions() self.makeObject() self.gl.glEnable(self.gl.GL_DEPTH_TEST) self.gl.glEnable(self.gl.GL_CULL_FACE) vshader = QOpenGLShader(QOpenGLShader.Vertex, self) vshader.compileSourceCode(self.vsrc) fshader = QOpenGLShader(QOpenGLShader.Fragment, self) fshader.compileSourceCode(self.fsrc) self.program = QOpenGLShaderProgram() self.program.addShader(vshader) self.program.addShader(fshader) self.program.bindAttributeLocation("vertex", self.PROGRAM_VERTEX_ATTRIBUTE) self.program.bindAttributeLocation("texCoord", self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.link() self.program.bind() self.program.setUniformValue("texture", 0) self.program.enableAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE) self.program.enableAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.setAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE, self.vertices) self.program.setAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE, self.texCoords) def paintGL(self): self.gl.glClearColor( self.clearColor.redF(), self.clearColor.greenF(), self.clearColor.blueF(), self.clearColor.alphaF(), ) self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT) m = QMatrix4x4() m.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, 15.0) m.translate(0.0, 0.0, -10.0) m.rotate(self.xRot / 16.0, 1.0, 0.0, 0.0) m.rotate(self.yRot / 16.0, 0.0, 1.0, 0.0) m.rotate(self.zRot / 16.0, 0.0, 0.0, 1.0) self.program.setUniformValue("matrix", m) for i, texture in enumerate(self.textures): texture.bind() self.gl.glDrawArrays(self.gl.GL_TRIANGLE_FAN, i * 4, 4) def resizeGL(self, width, height): side = min(width, height) self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side) def mousePressEvent(self, event): self.lastPos = event.pos() def mouseMoveEvent(self, event): dx = event.x() - self.lastPos.x() dy = event.y() - self.lastPos.y() if event.buttons() & Qt.LeftButton: self.rotateBy(8 * dy, 8 * dx, 0) elif event.buttons() & Qt.RightButton: self.rotateBy(8 * dy, 0, 8 * dx) self.lastPos = event.pos() def mouseReleaseEvent(self, event): self.clicked.emit() def makeObject(self): self.textures = [] self.texCoords = [] self.vertices = [] root = QFileInfo(__file__).absolutePath() for i in range(6): self.textures.append( QOpenGLTexture( QImage(root + ("/images/side%d.png" % (i + 1))).mirrored())) for j in range(4): self.texCoords.append(((j == 0 or j == 3), (j == 0 or j == 1))) x, y, z = self.coords[i][j] self.vertices.append((0.2 * x, 0.2 * y, 0.2 * z))
class GLWidget(QOpenGLWidget): clicked = pyqtSignal() PROGRAM_VERTEX_ATTRIBUTE, PROGRAM_TEXCOORD_ATTRIBUTE = range(2) vsrc = """ attribute highp vec4 vertex; attribute mediump vec4 texCoord; varying mediump vec4 texc; uniform mediump mat4 matrix; void main(void) { gl_Position = matrix * vertex; texc = texCoord; } """ fsrc = """ uniform sampler2D texture; varying mediump vec4 texc; void main(void) { gl_FragColor = texture2D(texture, texc.st); } """ coords = (((+1, -1, -1), (-1, -1, -1), (-1, +1, -1), (+1, +1, -1)), ((+1, +1, -1), (-1, +1, -1), (-1, +1, +1), (+1, +1, +1)), ((+1, -1, +1), (+1, -1, -1), (+1, +1, -1), (+1, +1, +1)), ((-1, -1, -1), (-1, -1, +1), (-1, +1, +1), (-1, +1, -1)), ((+1, -1, +1), (-1, -1, +1), (-1, -1, -1), (+1, -1, -1)), ((-1, -1, +1), (+1, -1, +1), (+1, +1, +1), (-1, +1, +1))) def __init__(self, parent=None): super(GLWidget, self).__init__(parent) self.clearColor = QColor(Qt.black) self.xRot = 0 self.yRot = 0 self.zRot = 0 self.program = None self.lastPos = QPoint() def minimumSizeHint(self): """ Define the minimum size of the widget """ return QSize(50, 50) def sizeHint(self): """ Define a default size for the widget """ return QSize(200, 200) def rotateBy(self, xAngle, yAngle, zAngle): self.xRot += xAngle self.yRot += yAngle self.zRot += zAngle self.update() def setClearColor(self, color): self.clearColor = color self.update() def initializeGL(self): self.gl = self.context().versionFunctions() self.gl.initializeOpenGLFunctions() self.makeObject() self.gl.glEnable(self.gl.GL_DEPTH_TEST) self.gl.glEnable(self.gl.GL_CULL_FACE) vshader = QOpenGLShader(QOpenGLShader.Vertex, self) vshader.compileSourceCode(self.vsrc) fshader = QOpenGLShader(QOpenGLShader.Fragment, self) fshader.compileSourceCode(self.fsrc) self.program = QOpenGLShaderProgram() self.program.addShader(vshader) self.program.addShader(fshader) self.program.bindAttributeLocation('vertex', self.PROGRAM_VERTEX_ATTRIBUTE) self.program.bindAttributeLocation('texCoord', self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.link() self.program.bind() self.program.setUniformValue('texture', 0) self.program.enableAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE) self.program.enableAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.setAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE, self.vertices) self.program.setAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE, self.texCoords) def paintGL(self): self.gl.glClearColor(self.clearColor.redF(), self.clearColor.greenF(), self.clearColor.blueF(), self.clearColor.alphaF()) self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT) m = QMatrix4x4() m.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, 15.0) m.translate(0.0, 0.0, -10.0) self.program.setUniformValue('matrix', m) self.texture.bind() self.gl.glDrawArrays(self.gl.GL_TRIANGLE_FAN, 0, 4) def resizeGL(self, width, height): side = min(width, height) self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side) def makeObject(self): self.texCoords = [(True, True), (False, True), (False, False), (True, False)] self.vertices = [(0.5, -0.5, -0.5), (-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5)] my_movie = QImage('/Users/reno/Dropbox/media/cloudy.png') self.texture = QOpenGLTexture(my_movie.mirrored())
class TriangleUnderlayRenderer(QObject): def __init__(self, parent=None): super(TriangleUnderlayRenderer, self).__init__(parent) self._shader_program = None self._viewport_size = QSize() self._window = None self._camera = Camera() self._perspective_projection_matrix = perspective_projection( 45.0, 4.0 / 3.0, 0.001, 100.0) self._orthographic_projection_matrix = orthographic_projection( 640.0, 480.0, 0.001, 100.0) self._model_matrix = np.identity(4) self._projection_type = 0 self._projection_matrix = self._perspective_projection_matrix self._theta = 0.0 def set_theta(self, theta): self._theta = theta # around y axis def build_rotation_matrix(self): m = np.identity(4) m[0][0] = np.cos(np.radians(self._theta)) m[0][2] = np.sin(np.radians(self._theta)) m[2][0] = -np.sin(np.radians(self._theta)) m[2][2] = np.cos(np.radians(self._theta)) return m @pyqtSlot(int) def setProjectionType(self, t): if t != self._projection_type: self._projection_type = t @pyqtSlot() def paint(self): # for Darwin, it's a must if pf.uname().system == 'Darwin': global GL GL = self._window.openglContext().versionFunctions() w = self._viewport_size.width() h = self._viewport_size.height() GL.glViewport(0, 0, int(w), int(h)) if self._shader_program is None: self._shader_program = QOpenGLShaderProgram() self._shader_program.addShaderFromSourceFile( QOpenGLShader.Vertex, 'shaders/OpenGL_2_1/vertex.glsl') self._shader_program.addShaderFromSourceFile( QOpenGLShader.Fragment, 'shaders/OpenGL_2_1/fragment.glsl') self._shader_program.bindAttributeLocation('position', 0) self._shader_program.bindAttributeLocation('color', 1) self._shader_program.link() self._shader_program.bind() self._shader_program.enableAttributeArray(0) self._shader_program.enableAttributeArray(1) self._shader_program.setAttributeArray(0, positions) self._shader_program.setAttributeArray(1, colors) if self._projection_type == 0: self._projection_matrix = self._perspective_projection_matrix elif self._projection_type == 1: self._projection_matrix = self._orthographic_projection_matrix self._model_matrix = self.build_rotation_matrix() self._shader_program.setUniformValue( 'model_matrix', QMatrix4x4(self._model_matrix.flatten().tolist())) self._shader_program.setUniformValue( 'view_matrix', QMatrix4x4(self._camera.get_view_matrix().flatten().tolist())) self._shader_program.setUniformValue( 'projection_matrix', QMatrix4x4(self._projection_matrix.flatten().tolist())) GL.glClearColor(0.2, 0.2, 0.2, 1) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClear(GL.GL_COLOR_BUFFER_BIT) GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3) self._shader_program.disableAttributeArray(0) self._shader_program.disableAttributeArray(1) self._shader_program.release() # Restore the OpenGL state for QtQuick rendering self._window.resetOpenGLState() self._window.update() def set_viewport_size(self, size): self._viewport_size = size def set_window(self, window): self._window = window def set_projection_matrix(self): # Need to be set every time we change the size of the window self._perspective_projection_matrix = perspective_projection( 45.0, self._viewport_size.width() / self._viewport_size.height(), 0.001, 100.0) self._orthographic_projection_matrix = orthographic_projection( self._viewport_size.width(), self._viewport_size.height(), 0.001, 100.0)
class ModelUnderlayRenderer(QObject): def __init__(self, parent=None): super(ModelUnderlayRenderer, self).__init__(parent) self._cube_shader = None self._sphere_shader = None self._viewport_size = QSize() self._window = None self._camera = Camera() self._perspective_projection_matrix = None self._orthographic_projection_matrix = None self._model_matrix = np.identity(4) self._projection_type = 0 self._projection_matrix = perspective_projection( 45.0, 640.0 / 480.0, 0.001, 1000.0) self._index_buffer = -1 # keep track of the objects in the scene self._cube_model = Cube() self._sphere_model = Sphere() self._models = dict() self._models[self._cube_model] = [] self._models[self._sphere_model] = [] @pyqtSlot() def paint(self): # for Darwin, it's a must if pf.uname().system == 'Darwin': global GL GL = self._window.openglContext().versionFunctions() w = self._viewport_size.width() h = self._viewport_size.height() GL.glViewport(0, 0, int(w), int(h)) GL.glClearColor(0.1, 0.1, 0.1, 1) GL.glEnable(GL.GL_DEPTH_TEST) GL.glClear(GL.GL_COLOR_BUFFER_BIT) GL.glClear(GL.GL_DEPTH_BUFFER_BIT) # # vertices_block = np.vstack((self._cube_model.vertices, self._sphere_model.vertices)) # colors_block = np.vstack((self._cube_model.colors, self._sphere_model)) # # if len(self._objects) > 1: # for v in self._vertices[1:]: # vertices_block = np.vstack((vertices_block, v)) # for idx, c in enumerate(self._colors[1:]): # if not c: # c = [[0.6, 0.6, 0.7] for i in range(len(self._vertices[idx]))] # colors_block = np.vstack((colors_block, c)) # for i in self._indices[1:]: # indices_block = np.hstack((indices_block, i)) view_matrix = np.identity(4) view_matrix[2][3] = -30 if self._cube_shader is None: self._cube_shader = QOpenGLShaderProgram() self._cube_shader.addShaderFromSourceFile( QOpenGLShader.Vertex, 'shaders/OpenGL_2_1/vertex.glsl') self._cube_shader.addShaderFromSourceFile( QOpenGLShader.Fragment, 'shaders/OpenGL_2_1/fragment.glsl') self._cube_shader.bindAttributeLocation('position', 0) self._cube_shader.bindAttributeLocation('color', 1) self._cube_shader.link() self._cube_shader.bind() self._cube_shader.enableAttributeArray(0) self._cube_shader.enableAttributeArray(1) self._cube_shader.setAttributeArray(0, self._cube_model.vertices.tolist()) self._cube_shader.setAttributeArray(1, self._cube_model.colors.tolist()) # view_matrix = self._camera.get_view_matrix() self._cube_shader.setUniformValue( 'view_matrix', QMatrix4x4(view_matrix.flatten().tolist()).transposed()) self._cube_shader.setUniformValue( 'projection_matrix', QMatrix4x4( self._projection_matrix.flatten().tolist()).transposed()) if self._cube_model in self._models.keys(): for entity in self._models[self._cube_model]: m = create_transformation_matrix(entity.position, entity.rotation, entity.scale) self._cube_shader.setUniformValue( 'model_matrix', QMatrix4x4(m.flatten().tolist())) GL.glDrawElements(GL.GL_TRIANGLES, len(self._cube_model.indices), GL.GL_UNSIGNED_INT, self._cube_model.indices.tolist()) self._cube_shader.disableAttributeArray(0) self._cube_shader.disableAttributeArray(1) self._cube_shader.release() if self._sphere_shader is None: self._sphere_shader = QOpenGLShaderProgram() self._sphere_shader.addShaderFromSourceFile( QOpenGLShader.Vertex, 'shaders/OpenGL_2_1/vertex.glsl') self._sphere_shader.addShaderFromSourceFile( QOpenGLShader.Fragment, 'shaders/OpenGL_2_1/fragment.glsl') self._sphere_shader.bindAttributeLocation('position', 0) self._sphere_shader.bindAttributeLocation('color', 1) self._sphere_shader.link() self._sphere_shader.bind() self._sphere_shader.enableAttributeArray(0) self._sphere_shader.enableAttributeArray(1) self._sphere_shader.setAttributeArray( 0, self._sphere_model.vertices.tolist()) self._sphere_shader.setAttributeArray( 1, self._sphere_model.colors.tolist()) self._sphere_shader.setUniformValue( 'view_matrix', QMatrix4x4(view_matrix.flatten().tolist()).transposed()) self._sphere_shader.setUniformValue( 'projection_matrix', QMatrix4x4( self._projection_matrix.flatten().tolist()).transposed()) if self._sphere_model in self._models.keys(): for entity in self._models[self._sphere_model]: m = create_transformation_matrix(entity.position, entity.rotation, entity.scale) self._cube_shader.setUniformValue( 'model_matrix', QMatrix4x4(m.flatten().tolist())) GL.glDrawElements(GL.GL_TRIANGLES, len(self._sphere_model.indices), GL.GL_UNSIGNED_INT, self._sphere_model.indices.tolist()) self._sphere_shader.disableAttributeArray(0) self._sphere_shader.disableAttributeArray(1) self._sphere_shader.release() # def build_rotation_matrix (t): # m = np.identity(4) # m[0][0] = np.cos(np.radians(t)) # m[0][2] = np.sin(np.radians(t)) # m[2][0] = -np.sin(np.radians(t)) # m[2][2] = np.cos(np.radians(t)) # return m # # global theta # theta += 1 # self._model_matrix = build_rotation_matrix(theta) # self._model_matrix[2][3] = -3 # Restore the OpenGL state for QtQuick rendering self._window.resetOpenGLState() self._window.update() def set_viewport_size(self, size): self._viewport_size = size def set_window(self, window): self._window = window def set_projection_matrix(self): # Need to be set every time we change the size of the window self._projection_matrix = perspective_projection( 45.0, self._window.width() / self._window.height(), 0.001, 1000.0) def move_model(self, val): self._model_matrix[2][3] += val def move_camera(self): pass def add_geometry(self, geo_enum): if geo_enum == 0: self._models[self._cube_model].append( Entity( self._cube_model, np.array([ random.uniform(-3.0, 3.0), random.uniform(-3.0, 3.0), random.uniform(-20.0, -10.0) ]), np.array([ random.uniform(-45.0, 45.0), random.uniform(-45.0, 45.0), random.uniform(-45.0, 45.0) ]), np.array([1.0, 1.0, 1.0]))) elif geo_enum == 1: self._models[self._sphere_model].append( Entity( self._sphere_model, np.array([ random.uniform(-3.0, 3.0), random.uniform(-3.0, 3.0), random.uniform(-20.0, -10.0) ]), np.array([ random.uniform(-30.0, 30.0), random.uniform(-30.0, 30.0), random.uniform(-30.0, 30.0) ]), np.array([1.0, 1.0, 1.0]))) else: return def delete_geometry(self, geo_enum): if geo_enum == 0: if self._models[self._cube_model]: self._models[self._cube_model].pop() elif geo_enum == 1: if self._models[self._sphere_model]: self._models[self._sphere_model].pop() def change_random_cube_color(self): tmp = self._models[self._cube_model] self._models.pop(self._cube_model) self._cube_model = Cube() self._models[self._cube_model] = tmp def change_random_sphere_color(self): tmp = self._models[self._sphere_model] self._models.pop(self._sphere_model) self._sphere_model = Sphere() self._models[self._sphere_model] = tmp
class GLWidget(QOpenGLWidget): clicked = pyqtSignal() PROGRAM_VERTEX_ATTRIBUTE, PROGRAM_TEXCOORD_ATTRIBUTE = range(2) vsrc = """ attribute highp vec4 vertex; attribute mediump vec4 texCoord; varying mediump vec4 texc; uniform mediump mat4 matrix; void main(void) { gl_Position = matrix * vertex; texc = texCoord; } """ fsrc = """ uniform sampler2D texture; varying mediump vec4 texc; void main(void) { gl_FragColor = texture2D(texture, texc.st); } """ coords = ( (( +1, -1, -1 ), ( -1, -1, -1 ), ( -1, +1, -1 ), ( +1, +1, -1 )), (( +1, +1, -1 ), ( -1, +1, -1 ), ( -1, +1, +1 ), ( +1, +1, +1 )), (( +1, -1, +1 ), ( +1, -1, -1 ), ( +1, +1, -1 ), ( +1, +1, +1 )), (( -1, -1, -1 ), ( -1, -1, +1 ), ( -1, +1, +1 ), ( -1, +1, -1 )), (( +1, -1, +1 ), ( -1, -1, +1 ), ( -1, -1, -1 ), ( +1, -1, -1 )), (( -1, -1, +1 ), ( +1, -1, +1 ), ( +1, +1, +1 ), ( -1, +1, +1 )) ) def __init__(self, parent=None): super(GLWidget, self).__init__(parent) self.clearColor = QColor(Qt.black) self.xRot = 0 self.yRot = 0 self.zRot = 0 self.program = None self.lastPos = QPoint() def minimumSizeHint(self): return QSize(50, 50) def sizeHint(self): return QSize(200, 200) def rotateBy(self, xAngle, yAngle, zAngle): self.xRot += xAngle self.yRot += yAngle self.zRot += zAngle self.update() def setClearColor(self, color): self.clearColor = color self.update() def initializeGL(self): self.gl = self.context().versionFunctions() self.gl.initializeOpenGLFunctions() self.makeObject() self.gl.glEnable(self.gl.GL_DEPTH_TEST) self.gl.glEnable(self.gl.GL_CULL_FACE) vshader = QOpenGLShader(QOpenGLShader.Vertex, self) vshader.compileSourceCode(self.vsrc) fshader = QOpenGLShader(QOpenGLShader.Fragment, self) fshader.compileSourceCode(self.fsrc) self.program = QOpenGLShaderProgram() self.program.addShader(vshader) self.program.addShader(fshader) self.program.bindAttributeLocation('vertex', self.PROGRAM_VERTEX_ATTRIBUTE) self.program.bindAttributeLocation('texCoord', self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.link() self.program.bind() self.program.setUniformValue('texture', 0) self.program.enableAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE) self.program.enableAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE) self.program.setAttributeArray(self.PROGRAM_VERTEX_ATTRIBUTE, self.vertices) self.program.setAttributeArray(self.PROGRAM_TEXCOORD_ATTRIBUTE, self.texCoords) def paintGL(self): self.gl.glClearColor(self.clearColor.redF(), self.clearColor.greenF(), self.clearColor.blueF(), self.clearColor.alphaF()) self.gl.glClear( self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT) m = QMatrix4x4() m.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, 15.0) m.translate(0.0, 0.0, -10.0) m.rotate(self.xRot / 16.0, 1.0, 0.0, 0.0) m.rotate(self.yRot / 16.0, 0.0, 1.0, 0.0) m.rotate(self.zRot / 16.0, 0.0, 0.0, 1.0) self.program.setUniformValue('matrix', m) for i, texture in enumerate(self.textures): texture.bind() self.gl.glDrawArrays(self.gl.GL_TRIANGLE_FAN, i * 4, 4) def resizeGL(self, width, height): side = min(width, height) self.gl.glViewport((width - side) // 2, (height - side) // 2, side, side) def mousePressEvent(self, event): self.lastPos = event.pos() def mouseMoveEvent(self, event): dx = event.x() - self.lastPos.x() dy = event.y() - self.lastPos.y() if event.buttons() & Qt.LeftButton: self.rotateBy(8 * dy, 8 * dx, 0) elif event.buttons() & Qt.RightButton: self.rotateBy(8 * dy, 0, 8 * dx) self.lastPos = event.pos() def mouseReleaseEvent(self, event): self.clicked.emit() def makeObject(self): self.textures = [] self.texCoords = [] self.vertices = [] root = QFileInfo(__file__).absolutePath() for i in range(6): self.textures.append( QOpenGLTexture( QImage(root + ('/images/side%d.png' % (i + 1))).mirrored())) for j in range(4): self.texCoords.append(((j == 0 or j == 3), (j == 0 or j == 1))) x, y, z = self.coords[i][j] self.vertices.append((0.2 * x, 0.2 * y, 0.2 * z))
class SquircleRenderer(QObject): #QOpenGLFunctions """docstring for SquircleRenderer""" def __init__(self, parent=None): super(SquircleRenderer, self).__init__(parent) self.m_t = 0.0 self.m_program = None self.m_viewportSize = QSize() def setT(self, t): self.m_t = t def setViewportSize(self, size): self.m_viewportSize = size def setWin(self, win): self.win = win ver = QOpenGLVersionProfile() ver.setVersion(2, 1) self.m_context = self.win.openglContext() self.gl = self.m_context.versionFunctions(ver) @pyqtSlot() def paint(self): if not self.m_program: self.gl.initializeOpenGLFunctions() self.m_program = QOpenGLShaderProgram(self) self.m_program.addShaderFromSourceCode( QOpenGLShader.Vertex, "attribute highp vec4 vertices;" "varying highp vec2 coords;" "void main() {" " gl_Position = vertices;" " coords = vertices.xy;" "}") self.m_program.addShaderFromSourceCode( QOpenGLShader.Fragment, "uniform lowp float t;" "varying highp vec2 coords;" "void main() {" " lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));" " i = smoothstep(t - 0.8, t + 0.8, i);" " i = floor(i * 20.) / 20.;" " gl_FragColor = vec4(coords * .5 + .5, i, i);" "}") self.m_program.bindAttributeLocation("vertices", 0) self.m_program.link() self.m_program.bind() self.m_program.enableAttributeArray(0) values = [(-1, -1), (1, -1), (-1, 1), (1, 1)] self.m_program.setAttributeArray(0, values) self.m_program.setUniformValue("t", self.m_t) #print("DATA:",self.m_viewportSize.width(), self.m_viewportSize.height(), self.m_t)#, self.gl.glViewport) self.gl.glViewport(0, 0, self.m_viewportSize.width(), self.m_viewportSize.height()) self.gl.glDisable(self.gl.GL_DEPTH_TEST) self.gl.glClearColor(0, 0, 0, 1) self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT) self.gl.glEnable(self.gl.GL_BLEND) self.gl.glBlendFunc(self.gl.GL_SRC_ALPHA, self.gl.GL_ONE) self.gl.glDrawArrays(self.gl.GL_TRIANGLE_STRIP, 0, 4) self.m_program.disableAttributeArray(0) self.m_program.release()