def get_shader_program(name, version): """ Returns a QGLShaderProgram for a specific version. Bound and linked. """ print "name", name, "version", version vert = get_shader_code( name, version, "vert" ) frag = get_shader_code( name, version, "frag" ) geom = get_shader_code( name, version, "geom" ) program = QGLShaderProgram() if frag: print("Frag shader init") fshader = QGLShader(QGLShader.Fragment) fshader.compileSourceCode(frag) program.addShader(fshader) if vert: print("Vert shader init") vshader = QGLShader(QGLShader.Vertex) vshader.compileSourceCode(vert) program.addShader(vshader) if geom: gshader = QGLShader(QGLShader.Geometry) gshader.compileSourceCode(geom) program.addShader(gshader) program.link() return program
def get_shader_program(name, version): """ Returns a QGLShaderProgram for a specific version. Bound and linked. """ print "name", name, "version", version vert = get_shader_code(name, version, "vert") frag = get_shader_code(name, version, "frag") geom = get_shader_code(name, version, "geom") program = QGLShaderProgram() if frag: print("Frag shader init") fshader = QGLShader(QGLShader.Fragment) fshader.compileSourceCode(frag) program.addShader(fshader) if vert: print("Vert shader init") vshader = QGLShader(QGLShader.Vertex) vshader.compileSourceCode(vert) program.addShader(vshader) if geom: gshader = QGLShader(QGLShader.Geometry) gshader.compileSourceCode(geom) program.addShader(gshader) program.link() return program
class OpenGLScene(QGraphicsScene): sliders_changed = QtCore.Signal(tuple) rotation_changed = QtCore.Signal(tuple) def __init__(self, context, *args): super(OpenGLScene, self).__init__(*args) self.model = Model(self) self.sliders_changed.connect(self.model.changeRotation) self.context = context self.initialised = False self.image = None return def loadImage(self, file_name): im = Image.open(file_name) try: ix, iy, self.image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1) except SystemError: ix, iy, self.image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1) ID = GL.glGenTextures(1) GL.glBindTexture(GL.GL_TEXTURE_2D, ID) GL.glTexImage2D( GL.GL_TEXTURE_2D, 0, GL.GL_RGB, ix, iy, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, None) GL.glTexSubImage2D( GL.GL_TEXTURE_2D, 0, 0, 0, ix, iy, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, self.image) return ID def drawBackground(self, painter, rect): if not (painter.paintEngine().type() == QtGui.QPaintEngine.OpenGL or painter.paintEngine().type() == QtGui.QPaintEngine.OpenGL2): QtCore.qWarning('OpenGLScene: drawBackground needs a QGLWidget '\ +'to be set as viewport on the '\ +'graphics view') return painter.beginNativePainting() if not self.initialised: self.vertex_shader = QGLShader(QGLShader.Vertex) self.vertex_shader.compileSourceCode(vertex_shader_code) self.fragment_shader = QGLShader(QGLShader.Fragment) self.fragment_shader.compileSourceCode(frag_shader_code) self.program = QGLShaderProgram(self.context) self.program.addShader(self.vertex_shader) self.program.addShader(self.fragment_shader) self.program.link() print 'initialising' self.tex = self.loadImage('test.jpg') self.textureLocation = self.program.uniformLocation("image") self.initialised = True GL.glMatrixMode(GL.GL_PROJECTION) GL.glPushMatrix() GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPushMatrix() GL.glBindTexture(GL.GL_TEXTURE_2D, self.tex) self.model.render() self.program.bind() GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR) GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR) GL.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glVertexPointer(3, GL.GL_FLOAT, 0, triangle_vertices) GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(triangle_vertices)/3) GL.glDisableClientState(GL.GL_VERTEX_ARRAY) self.program.release() GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPopMatrix() GL.glMatrixMode(GL.GL_PROJECTION) GL.glPopMatrix() painter.endNativePainting() def mouseMoveEvent(self, event): QGraphicsScene.mouseMoveEvent(self, event) if event.isAccepted(): return if (event.buttons() & QtCore.Qt.LeftButton): delta = event.scenePos() - event.lastScenePos() self.model.setXRotation(self.model.xRotation() - delta.y()*4) self.model.setYRotation(self.model.yRotation() + delta.x()*4) event.accept() self.update()
class OpenGLScene(QGraphicsScene): sliders_changed = QtCore.Signal(tuple) rotation_changed = QtCore.Signal(tuple) def __init__(self, context, *args): super(OpenGLScene, self).__init__(*args) self.model = Model(self) self.sliders_changed.connect(self.model.changeRotation) self.context = context self.initialised = False self.image = None return def loadImage(self, file_name): im = Image.open(file_name) try: ix, iy, self.image = im.size[0], im.size[1], im.tostring( "raw", "RGBA", 0, -1) except SystemError: ix, iy, self.image = im.size[0], im.size[1], im.tostring( "raw", "RGBX", 0, -1) ID = GL.glGenTextures(1) GL.glBindTexture(GL.GL_TEXTURE_2D, ID) GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, ix, iy, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, None) GL.glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, ix, iy, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, self.image) return ID def drawBackground(self, painter, rect): if not (painter.paintEngine().type() == QtGui.QPaintEngine.OpenGL or painter.paintEngine().type() == QtGui.QPaintEngine.OpenGL2): QtCore.qWarning('OpenGLScene: drawBackground needs a QGLWidget '\ +'to be set as viewport on the '\ +'graphics view') return painter.beginNativePainting() if not self.initialised: self.vertex_shader = QGLShader(QGLShader.Vertex) self.vertex_shader.compileSourceCode(vertex_shader_code) self.fragment_shader = QGLShader(QGLShader.Fragment) self.fragment_shader.compileSourceCode(frag_shader_code) self.program = QGLShaderProgram(self.context) self.program.addShader(self.vertex_shader) self.program.addShader(self.fragment_shader) self.program.link() print 'initialising' self.tex = self.loadImage('test.jpg') self.textureLocation = self.program.uniformLocation("image") self.initialised = True GL.glMatrixMode(GL.GL_PROJECTION) GL.glPushMatrix() GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPushMatrix() GL.glBindTexture(GL.GL_TEXTURE_2D, self.tex) self.model.render() self.program.bind() GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR) GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR) GL.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE) GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glVertexPointer(3, GL.GL_FLOAT, 0, triangle_vertices) GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(triangle_vertices) / 3) GL.glDisableClientState(GL.GL_VERTEX_ARRAY) self.program.release() GL.glMatrixMode(GL.GL_MODELVIEW) GL.glPopMatrix() GL.glMatrixMode(GL.GL_PROJECTION) GL.glPopMatrix() painter.endNativePainting() def mouseMoveEvent(self, event): QGraphicsScene.mouseMoveEvent(self, event) if event.isAccepted(): return if (event.buttons() & QtCore.Qt.LeftButton): delta = event.scenePos() - event.lastScenePos() self.model.setXRotation(self.model.xRotation() - delta.y() * 4) self.model.setYRotation(self.model.yRotation() + delta.x() * 4) event.accept() self.update()
class GLWidget(QGLWidget): __QUAD_VERTICES = numpy.array(( (-0.5, +0.5, 0.0), (-0.5, -0.5, 0.0), (+0.5, -0.5, 0.0), (+0.5, +0.5, 0.0)), 'float32') def __init__(self, *args): super(GLWidget, self).__init__(*args) self.load_images() self.texture_streamnks = None self.cur_image = 0 def initializeGL(self): self.vertex_shader = QGLShader(QGLShader.Vertex) self.vertex_shader.compileSourceCode(vertex_shader_code) self.fragment_shader = QGLShader(QGLShader.Fragment) self.fragment_shader.compileSourceCode(frag_shader_code) self.program = QGLShaderProgram() self.program.addShader(self.vertex_shader) self.program.addShader(self.fragment_shader) self.program.link() self.texture_location = self.program.uniformLocation('image') self.offset = self.program.uniformLocation('image') # Set up the texture stream # Arguments are (size, gl_format, gl_type, gl_internal_format) self.texture_stream = TextureStream2D((512,512), GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, GL.GL_RGBA, texture_unit=GL.GL_TEXTURE0) self.switch_image() GL.glClearColor(0.0,0.0,0.0,0.0) def load_images(self): im = Image.open('test.jpg').tostring("raw", "RGBX", 0, -1) self.image1 =\ numpy.frombuffer(buffer(im), dtype='uint8').reshape(512,512,4) im = Image.open('test2.jpg').tostring("raw", "RGBX", 0, -1) self.image2 = \ numpy.frombuffer(buffer(im), dtype='uint8').reshape(512,512,4) def switch_image(self): ''' Upload the other texture to be rendered ''' if self.cur_image == 0: self.texture_stream.update_texture(self.image2) self.cur_image = 1 else: self.texture_stream.update_texture(self.image1) self.cur_image = 0 def paintGL(self): GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) print 'after' self.program.bind() with self.texture_stream: # Tell the shader to use texture unit 0 self.program.setUniformValue(self.texture_location, 0) # Draw the quad that is shaded with the image GL.glEnableClientState(GL.GL_VERTEX_ARRAY) GL.glVertexPointer(3, GL.GL_FLOAT, 0, \ self.__QUAD_VERTICES.ravel()) GL.glDrawArrays(GL.GL_QUADS, 0, self.__QUAD_VERTICES.shape[0]) GL.glDisableClientState(GL.GL_VERTEX_ARRAY) self.program.release() def sizeHint(self): return QtCore.QSize(640, 480) def resizeGL(self, width, height): GL.glViewport(0, 0, width, height)