Пример #1
0
class CrosshairLine(QObject):
    def __init__(self, parent, vertex_data):
        super().__init__(parent)
        self.shader_program = QOpenGLShaderProgram(parent=self)
        self.vertex_attribute_object = None
        self.vertex_data = vertex_data
        self.vertex_position_uniform = None

    def get_uniform_location(self, name):
        location = self.shader_program.uniformLocation(name)
        if location == -1:
            raise ValueError("Uniform {} has no location.".format(name))
        return location

    def initialise_gl(self):
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                    VERTEX)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                    FRAGMENT)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(
                self.shader_program.log()))

        self.vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes,
                        self.vertex_data, GL.GL_STATIC_DRAW)

        self.vertex_position = self.shader_program.attributeLocation(
            "vertexPosition")

        self.inverted_uniform = self.get_uniform_location("inverted")
        self.projection_matrix_uniform = self.get_uniform_location(
            "projection_matrix")
        self.view_matrix_uniform = self.get_uniform_location("view_matrix")

        GL.glVertexAttribPointer(self.vertex_position, 3, GL.GL_FLOAT,
                                 GL.GL_FALSE, 0, None)

        GL.glEnableVertexAttribArray(0)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

    def paint_gl(self, inverted, projection_matrix, view_matrix):
        self.shader_program.bind()
        self.shader_program.setUniformValue(self.inverted_uniform, inverted)
        self.shader_program.setUniformValue(self.projection_matrix_uniform,
                                            projection_matrix)
        self.shader_program.setUniformValue(self.view_matrix_uniform,
                                            view_matrix)
        GL.glBindVertexArray(self.vertex_attribute_object)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.shader_program.release()
Пример #2
0
class TestCanvas(Canvas):

    def __init__(self):

        with open("vertex.glsl","r") as f:
            self.vertexShaderSource = f.read()

        with open("fragment.glsl","r") as f:
            self.fragmentShaderSource = f.read()

        if self.vertexShaderSource is None:
            raise Exception("Could not load the source for the vertex shader")

        if self.fragmentShaderSource is None:
            raise Exception("Could not load the source for the fragment shader")

        super(TestCanvas,self).__init__()

    def setupGL(self):

        self.program = QOpenGLShaderProgram(self.context())
        info("PROGRAM",self.program)

        # addShaderFromSourceCode() only returns a bool telling whether everything went well

        if self.program.addShaderFromSourceCode(QOpenGLShader.Vertex, self.vertexShaderSource):
            info("VERTEX SHADER","Managed to load and parse the vertex shader")

        if self.program.addShaderFromSourceCode(QOpenGLShader.Fragment, self.fragmentShaderSource):
            info("FRAGMENT SHADER","Managed to load and parse the fragment shader")

        # Compile and bind the shader program to the current context
        self.program.link()
        self.program.bind()

        # Bind the shader attribute to a python variable
        self.somePosition = self.gl.glGetAttribLocation(self.program.programId(), "somePosition")

        # Tell GL that whenever we run glClear, this is the color that
        # should be used. We only need to do this once. 
        self.gl.glClearColor(0.1, 0.1, 0.1, 1.0)

        self.dumpGLLogMessages("setupGL()")

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT)
        
        # Assign a value to the shader parameter
        self.gl.glVertexAttrib3f(self.somePosition, 0.5, 0.5, 0.0)

        # Mode, starting at vertex number, number of vertices to draw
        self.gl.glDrawArrays(self.gl.GL_POINTS,0,1)

        self.dumpGLLogMessages("paintGL()")

    def resizeGL(self, width, height):
        pass
Пример #3
0
class TestCanvas(Canvas):
    def __init__(self):

        with open("vertex.glsl", "r") as f:
            self.vertexShaderSource = f.read()

        with open("fragment.glsl", "r") as f:
            self.fragmentShaderSource = f.read()

        if self.vertexShaderSource is None:
            raise Exception("Could not load the source for the vertex shader")

        if self.fragmentShaderSource is None:
            raise Exception(
                "Could not load the source for the fragment shader")

        super(TestCanvas, self).__init__()

    def setupGL(self):

        self.program = QOpenGLShaderProgram(self.context())
        info("PROGRAM", self.program)

        # addShaderFromSourceCode() only returns a bool telling whether everything went well

        if self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                self.vertexShaderSource):
            info("VERTEX SHADER",
                 "Managed to load and parse the vertex shader")

        if self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                self.fragmentShaderSource):
            info("FRAGMENT SHADER",
                 "Managed to load and parse the fragment shader")

        # Compile and bind the shader program to the current context
        self.program.link()
        self.program.bind()

        # Tell GL that whenever we run glClear, this is the color that
        # should be used. We only need to do this once.
        self.gl.glClearColor(0.1, 0.1, 0.1, 1.0)

        self.dumpGLLogMessages("setupGL()")

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT
                        | self.gl.GL_DEPTH_BUFFER_BIT)
        self.dumpGLLogMessages("paintGL()")

    def resizeGL(self, width, height):
        pass
    def linkProgram(self, gl, name, **kwargs):
        ''' Links GLSL program from *name*.vert and *name*.frag shaders '''
        program = QOpenGLShaderProgram()

        vertexShader, fragmentShader = self.loadShaders(name)
        program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                vertexShader)
        program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                fragmentShader)

        assert program.link(), "Can't link ShaderProgram"
        assert program.bind(), "Can't bind ShaderProgram for initialization"

        for k, v in kwargs.items():
            if type(v) in [list, tuple]:
                program.setUniformValue(k, *v)
            else:
                program.setUniformValue(k, v)

        program.release()

        return program
Пример #5
0
class GLSLProgram(Product.Product):
    def __init__(self,
                 parent=None,
                 vertex_shader=None,
                 geometry_shader=None,
                 fragment_shader=None,
                 uniforms={},
                 textures={},
                 outputTextures={}):
        super(GLSLProgram, self).__init__(parent)
        self._vertexShader = None
        self._geometryShader = None
        self._fragmentShader = None
        self._uniforms = {}
        self._textures = {}
        self._outputTextures = {}
        self._linkDirty = False

        self.vertexShader = vertex_shader
        self.geometryShader = geometry_shader
        self.fragmentShader = fragment_shader
        self.uniforms = uniforms
        self.textures = textures
        self.outputTextures = outputTextures

    def _update(self):

        if self._linkDirty:
            self._program = QOpenGLShaderProgram()
            if self._vertexShader is not None:
                self._program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                      self._vertexShader)
            if self._geometryShader is not None:
                self._program.addShaderFromSourceCode(QOpenGLShader.Geometry,
                                                      self._geometryShader)
            if self._fragmentShader is not None:
                self._program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                      self._fragmentShader)
            self._program.link()
            self._linkDirty = False

    Product.InputProperty(vars(), 'QVariant', 'uniforms')
    ''' supports dict of Array, subject to the constraints associated with Image.to_QImage() '''
    Product.InputProperty(vars(), 'QVariant', 'textures')

    Product.InputProperty(vars(), 'QVariant', 'outputTextures')

    def relink(self):
        self._linkDirty = True

    Product.InputProperty(vars(), str, 'geometryShader', relink)

    Product.InputProperty(vars(), str, 'vertexShader', relink)

    Product.InputProperty(vars(), str, 'fragmentShader', relink)
Пример #6
0
class ShaderProgram:
    def __init__(self):
        self._bindings = {}
        self._attribute_bindings = {}

        self._shader_program = None
        self._uniform_indices = {}
        self._attribute_indices = {}
        self._uniform_values = {}
        self._bound = False
        self._textures = {}

        self._debug_shader = False  # Set this to true to enable extra logging concerning shaders

    ##  Load a shader program file.
    #
    #   This method loads shaders from a simple text file, using Python's configparser
    #   as parser.
    #
    #   \note When writing shader program files, please note that configparser expects
    #   indented lines for multiline values. Since the shaders are provided as a single
    #   multiline string, make sure to indent them properly.
    #
    #   \param file_name The shader file to load.
    #   \param version can be used for a special version of the shader. it will be appended
    #          to the keys [vertex, fragment, geometry] in the shader file
    #
    #   \exception{InvalidShaderProgramError} Raised when the file provided does not contain any valid shaders.
    def load(self, file_name, version = ""):
        Logger.log("d", "Loading shader file [%s]...", file_name)

        vertex_key = "vertex" + version
        fragment_key = "fragment" + version
        geometry_key = "geometry" + version

        # Hashtags should not be ignored, they are part of GLSL.
        parser = configparser.ConfigParser(interpolation = None, comment_prefixes = (';', ))
        parser.optionxform = lambda option: option
        parser.read(file_name)

        if "shaders" not in parser:
            raise InvalidShaderProgramError("{0} is missing section [shaders]".format(file_name))

        if vertex_key not in parser["shaders"] or fragment_key not in parser["shaders"]:
            raise InvalidShaderProgramError("{0} is missing a shader [{1}, {2}]".format(file_name, vertex_key, fragment_key))

        vertex_code = parser["shaders"][vertex_key]
        if self._debug_shader:
            vertex_code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(vertex_code.split("\n"))])
            Logger.log("d", "Vertex shader")
            Logger.log("d", vertex_code_str)

        fragment_code = parser["shaders"][fragment_key]
        if self._debug_shader:
            fragment_code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(fragment_code.split("\n"))])
            Logger.log("d", "Fragment shader")
            Logger.log("d", fragment_code_str)

        self.setVertexShader(vertex_code)
        self.setFragmentShader(fragment_code)
        # Geometry shader is optional and only since version OpenGL 3.2 or with extension ARB_geometry_shader4
        if geometry_key in parser["shaders"]:
            code = parser["shaders"][geometry_key]
            if self._debug_shader:
                code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(code.split("\n"))])
                Logger.log("d", "Loading geometry shader... \n")
                Logger.log("d", code_str)
            self.setGeometryShader(code)

        self.build()

        if "defaults" in parser:
            for key, value in parser["defaults"].items():
                self.setUniformValue(key, ast.literal_eval(value), cache = True)

        if "bindings" in parser:
            for key, value in parser["bindings"].items():
                self.addBinding(key, value)

        if "attributes" in parser:
            for key, value in parser["attributes"].items():
                self.addAttributeBinding(key, value)

    ##  Set the vertex shader to use.
    #
    #   \param shader \type{string} The vertex shader to use.
    def setVertexShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, shader):
            Logger.log("e", "Vertex shader failed to compile: %s", self._shader_program.log())

    ##  Set the fragment shader to use.
    #
    #   \param shader \type{string} The fragment shader to use.
    def setFragmentShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, shader):
            Logger.log("e", "Fragment shader failed to compile: %s", self._shader_program.log())

    def setGeometryShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Geometry, shader):
            Logger.log("e", "Geometry shader failed to compile: %s", self._shader_program.log())

    ##  Build the complete shader program out of the separately provided sources.
    def build(self):
        if not self._shader_program:
            Logger.log("e", "No shader sources loaded")
            return

        if not self._shader_program.link():
            Logger.log("e", "Shader failed to link: %s", self._shader_program.log())

    ##  Set a named uniform variable.
    #
    #   Unless otherwise specified as argument, the specified value will be cached so that
    #   it does not matter whether bind() has already been called. Instead, if the shader
    #   is not currently bound, the next call to bind() will update the uniform values.
    #
    #   \param name The name of the uniform variable.
    #   \param value The value to set the variable to.
    #   \param kwargs Keyword arguments.
    #                 Possible keywords:
    #                 - cache: False when the value should not be cached for later calls to bind().
    def setUniformValue(self, name, value, **kwargs):
        if not self._shader_program:
            return

        if name not in self._uniform_indices:
            self._uniform_indices[name] = self._shader_program.uniformLocation(name)

        uniform = self._uniform_indices[name]
        if uniform == -1:
            return

        if kwargs.get("cache", True):
            self._uniform_values[uniform] = value

        if self._bound:
            self._setUniformValueDirect(uniform, value)
    ##  Set a texture that should be bound to a specified texture unit when this shader is bound.
    #
    #   \param texture_unit \type{int} The texture unit to bind the texture to.
    #   \param texture \type{Texture} The texture object to bind to the texture unit.
    def setTexture(self, texture_unit, texture):
        if texture is None:
            if texture_unit in self._textures:
                del self._textures[texture_unit]
        else:
            self._textures[texture_unit] = texture

    ##  Enable a vertex attribute to be used.
    #
    #   \param name The name of the attribute to enable.
    #   \param type The type of the attribute. Should be a python type.
    #   \param offset The offset into a bound buffer where the data for this attribute starts.
    #   \param stride The stride of the attribute.
    #
    #   \note If the shader is not bound, this will bind the shader.
    def enableAttribute(self, name, type, offset, stride = 0):
        if not self._shader_program:
            return

        self.bind()

        if name not in self._attribute_indices:
            self._attribute_indices[name] = self._shader_program.attributeLocation(name)

        attribute = self._attribute_indices[name]
        if attribute == -1:
            return

        if type is "int":
            self._shader_program.setAttributeBuffer(attribute, 0x1404, offset, 1, stride) #GL_INT
        elif type is "float":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 1, stride) #GL_FLOAT
        elif type is "vector2f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 2, stride) #GL_FLOAT
        elif type is "vector3f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 3, stride) #GL_FLOAT
        elif type is "vector4f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 4, stride) #GL_FLOAT

        self._shader_program.enableAttributeArray(attribute)

    ##  Disable a vertex attribute so it is no longer used.
    #
    #   \param name The name of the attribute to use.
    def disableAttribute(self, name):
        if not self._shader_program:
            return

        if name not in self._attribute_indices:
            return

        self._shader_program.disableAttributeArray(self._attribute_indices[name])

    ##  Bind the shader to use it for rendering.
    def bind(self):
        if not self._shader_program or not self._shader_program.isLinked():
            return

        if self._bound:
            return

        self._shader_program.bind()
        self._bound = True

        for uniform in self._uniform_values:
            self._setUniformValueDirect(uniform, self._uniform_values[uniform])

        for texture_unit, texture in self._textures.items():
            texture.bind(texture_unit)

    ##  Release the shader so it will no longer be used for rendering.
    def release(self):
        if not self._shader_program or not self._bound:
            return

        self._shader_program.release()
        self._bound = False

        for texture_unit, texture in self._textures.items():
            texture.release(texture_unit)

    ##  Add a uniform value binding.
    #
    #   Uniform value bindings are used to provide an abstraction between uniforms as set
    #   from code and uniforms as used from shaders. Each binding specifies a uniform name
    #   as key that should be mapped to a string that can be used to look up the value of
    #   the uniform.
    #
    #   \param key The name of the uniform to bind.
    #   \param value The string used to look up values for this uniform.
    def addBinding(self, key, value):
        self._bindings[value] = key

    ##  Remove a uniform value binding.
    #
    #   \param key The uniform to remove.
    def removeBinding(self, key):
        if key not in self._bindings:
            return

        del self._bindings[key]

    ##  Update the values of bindings.
    #
    #   \param kwargs Keyword arguments.
    #                 Each key should correspond to a binding name, with the
    #                 value being the value of the uniform.
    #
    #   \note By default, these values are not cached as they are expected to be continuously
    #         updated.
    def updateBindings(self, **kwargs):
        for key, value in kwargs.items():
            if key in self._bindings and value is not None:
                self.setUniformValue(self._bindings[key], value, cache = False)

    ##  Add an attribute binding.
    #
    #   Attribute bindings are similar to uniform value bindings, except they specify what
    #   what attribute name binds to which attribute in the shader.
    #
    #   TODO: Actually use these bindings. However, that kind of depends on a more freeform
    #   MeshData object as freeform bindings are rather useless when we only have 5 supported
    #   attributes.
    #
    #   \param key The identifier used in the shader for the attribute.
    #   \param value The name to bind to this attribute.
    def addAttributeBinding(self, key, value):
        self._attribute_bindings[key] = value

    ##  Remove an attribute binding.
    #
    #   \param key The name of the attribute binding to remove.
    def removeAttributeBinding(self, key):
        if key not in self._attribute_bindings:
            return

        del self._attribute_bindings[key]

    def _matrixToQMatrix4x4(self, m):
        return QMatrix4x4(m.at(0, 0), m.at(0, 1), m.at(0, 2), m.at(0, 3), m.at(1, 0), m.at(1, 1), m.at(1, 2), m.at(1, 3),
            m.at(2, 0), m.at(2, 1), m.at(2, 2), m.at(2, 3), m.at(3, 0), m.at(3, 1), m.at(3, 2), m.at(3, 3))

    def _setUniformValueDirect(self, uniform, value):
        if type(value) is Vector:
            self._shader_program.setUniformValue(uniform, QVector3D(value.x, value.y, value.z))
        elif type(value) is Matrix:
            self._shader_program.setUniformValue(uniform, self._matrixToQMatrix4x4(value))
        elif type(value) is Color:
            self._shader_program.setUniformValue(uniform,
                QColor(value.r * 255, value.g * 255, value.b * 255, value.a * 255))
        elif type(value) is list and len(value) is 2:
            self._shader_program.setUniformValue(uniform, QVector2D(value[0], value[1]))
        elif type(value) is list and len(value) is 3:
            self._shader_program.setUniformValue(uniform, QVector3D(value[0], value[1], value[2]))
        elif type(value) is list and len(value) is 4:
            self._shader_program.setUniformValue(uniform, QVector4D(value[0], value[1], value[2], value[3]))
        elif type(value) is list and type(value[0]) is list and len(value[0]) is 2:
            self._shader_program.setUniformValueArray(uniform, [QVector2D(i[0], i[1]) for i in value])
        else:
            self._shader_program.setUniformValue(uniform, value)
Пример #7
0
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)
Пример #8
0
class ShaderProgram:
    def __init__(self):
        self._bindings = {}
        self._attribute_bindings = {}

        self._shader_program = None
        self._uniform_indices = {}
        self._attribute_indices = {}
        self._uniform_values = {}
        self._bound = False
        self._textures = {}

        self._debug_shader = False  # Set this to true to enable extra logging concerning shaders

    ##  Load a shader program file.
    #
    #   This method loads shaders from a simple text file, using Python's configparser
    #   as parser.
    #
    #   \note When writing shader program files, please note that configparser expects
    #   indented lines for multiline values. Since the shaders are provided as a single
    #   multiline string, make sure to indent them properly.
    #
    #   \param file_name The shader file to load.
    #   \param version can be used for a special version of the shader. it will be appended
    #          to the keys [vertex, fragment, geometry] in the shader file
    #
    #   \exception{InvalidShaderProgramError} Raised when the file provided does not contain any valid shaders.
    def load(self, file_name, version = ""):
        Logger.log("d", "Loading shader file [%s]...", file_name)

        vertex_key = "vertex" + version
        fragment_key = "fragment" + version
        geometry_key = "geometry" + version

        # Hashtags should not be ignored, they are part of GLSL.
        parser = configparser.ConfigParser(interpolation = None, comment_prefixes = (';', ))
        parser.optionxform = lambda option: option
        parser.read(file_name)

        if "shaders" not in parser:
            raise InvalidShaderProgramError("{0} is missing section [shaders]".format(file_name))

        if vertex_key not in parser["shaders"] or fragment_key not in parser["shaders"]:
            raise InvalidShaderProgramError("{0} is missing a shader [{1}, {2}]".format(file_name, vertex_key, fragment_key))

        vertex_code = parser["shaders"][vertex_key]
        if self._debug_shader:
            vertex_code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(vertex_code.split("\n"))])
            Logger.log("d", "Vertex shader")
            Logger.log("d", vertex_code_str)

        fragment_code = parser["shaders"][fragment_key]
        if self._debug_shader:
            fragment_code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(fragment_code.split("\n"))])
            Logger.log("d", "Fragment shader")
            Logger.log("d", fragment_code_str)

        self.setVertexShader(vertex_code)
        self.setFragmentShader(fragment_code)
        # Geometry shader is optional and only since version OpenGL 3.2 or with extension ARB_geometry_shader4
        if geometry_key in parser["shaders"]:
            code = parser["shaders"][geometry_key]
            if self._debug_shader:
                code_str = "\n".join(["%4i %s" % (i, s) for i, s in enumerate(code.split("\n"))])
                Logger.log("d", "Loading geometry shader... \n")
                Logger.log("d", code_str)
            self.setGeometryShader(code)

        self.build()

        if "defaults" in parser:
            for key, value in parser["defaults"].items():
                self.setUniformValue(key, ast.literal_eval(value), cache = True)

        if "bindings" in parser:
            for key, value in parser["bindings"].items():
                self.addBinding(key, value)

        if "attributes" in parser:
            for key, value in parser["attributes"].items():
                self.addAttributeBinding(key, value)

    ##  Set the vertex shader to use.
    #
    #   \param shader \type{string} The vertex shader to use.
    def setVertexShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, shader):
            Logger.log("e", "Vertex shader failed to compile: %s", self._shader_program.log())

    ##  Set the fragment shader to use.
    #
    #   \param shader \type{string} The fragment shader to use.
    def setFragmentShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, shader):
            Logger.log("e", "Fragment shader failed to compile: %s", self._shader_program.log())

    def setGeometryShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Geometry, shader):
            Logger.log("e", "Geometry shader failed to compile: %s", self._shader_program.log())

    ##  Build the complete shader program out of the separately provided sources.
    def build(self):
        if not self._shader_program:
            Logger.log("e", "No shader sources loaded")
            return

        if not self._shader_program.link():
            Logger.log("e", "Shader failed to link: %s", self._shader_program.log())

    ##  Set a named uniform variable.
    #
    #   Unless otherwise specified as argument, the specified value will be cached so that
    #   it does not matter whether bind() has already been called. Instead, if the shader
    #   is not currently bound, the next call to bind() will update the uniform values.
    #
    #   \param name The name of the uniform variable.
    #   \param value The value to set the variable to.
    #   \param kwargs Keyword arguments.
    #                 Possible keywords:
    #                 - cache: False when the value should not be cached for later calls to bind().
    def setUniformValue(self, name, value, **kwargs):
        if not self._shader_program:
            return

        if name not in self._uniform_indices:
            self._uniform_indices[name] = self._shader_program.uniformLocation(name)

        uniform = self._uniform_indices[name]
        if uniform == -1:
            return

        if kwargs.get("cache", True):
            self._uniform_values[uniform] = value

        if self._bound:
            self._setUniformValueDirect(uniform, value)
    ##  Set a texture that should be bound to a specified texture unit when this shader is bound.
    #
    #   \param texture_unit \type{int} The texture unit to bind the texture to.
    #   \param texture \type{Texture} The texture object to bind to the texture unit.
    def setTexture(self, texture_unit, texture):
        if texture is None:
            if texture_unit in self._textures:
                del self._textures[texture_unit]
        else:
            self._textures[texture_unit] = texture

    ##  Enable a vertex attribute to be used.
    #
    #   \param name The name of the attribute to enable.
    #   \param type The type of the attribute. Should be a python type.
    #   \param offset The offset into a bound buffer where the data for this attribute starts.
    #   \param stride The stride of the attribute.
    #
    #   \note If the shader is not bound, this will bind the shader.
    def enableAttribute(self, name, type, offset, stride = 0):
        if not self._shader_program:
            return

        self.bind()

        if name not in self._attribute_indices:
            self._attribute_indices[name] = self._shader_program.attributeLocation(name)

        attribute = self._attribute_indices[name]
        if attribute == -1:
            return

        if type is "int":
            self._shader_program.setAttributeBuffer(attribute, 0x1404, offset, 1, stride) #GL_INT
        elif type is "float":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 1, stride) #GL_FLOAT
        elif type is "vector2f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 2, stride) #GL_FLOAT
        elif type is "vector3f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 3, stride) #GL_FLOAT
        elif type is "vector4f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 4, stride) #GL_FLOAT

        self._shader_program.enableAttributeArray(attribute)

    ##  Disable a vertex attribute so it is no longer used.
    #
    #   \param name The name of the attribute to use.
    def disableAttribute(self, name):
        if not self._shader_program:
            return

        if name not in self._attribute_indices:
            return

        self._shader_program.disableAttributeArray(self._attribute_indices[name])

    ##  Bind the shader to use it for rendering.
    def bind(self):
        if not self._shader_program or not self._shader_program.isLinked():
            return

        if self._bound:
            return

        self._shader_program.bind()
        self._bound = True

        for uniform in self._uniform_values:
            self._setUniformValueDirect(uniform, self._uniform_values[uniform])

        for texture_unit, texture in self._textures.items():
            texture.bind(texture_unit)

    ##  Release the shader so it will no longer be used for rendering.
    def release(self):
        if not self._shader_program or not self._bound:
            return

        self._shader_program.release()
        self._bound = False

        for texture_unit, texture in self._textures.items():
            texture.release(texture_unit)

    ##  Add a uniform value binding.
    #
    #   Uniform value bindings are used to provide an abstraction between uniforms as set
    #   from code and uniforms as used from shaders. Each binding specifies a uniform name
    #   as key that should be mapped to a string that can be used to look up the value of
    #   the uniform.
    #
    #   \param key The name of the uniform to bind.
    #   \param value The string used to look up values for this uniform.
    def addBinding(self, key, value):
        self._bindings[value] = key

    ##  Remove a uniform value binding.
    #
    #   \param key The uniform to remove.
    def removeBinding(self, key):
        if key not in self._bindings:
            return

        del self._bindings[key]

    ##  Update the values of bindings.
    #
    #   \param kwargs Keyword arguments.
    #                 Each key should correspond to a binding name, with the
    #                 value being the value of the uniform.
    #
    #   \note By default, these values are not cached as they are expected to be continuously
    #         updated.
    def updateBindings(self, **kwargs):
        for key, value in kwargs.items():
            if key in self._bindings and value is not None:
                self.setUniformValue(self._bindings[key], value, cache = False)

    ##  Add an attribute binding.
    #
    #   Attribute bindings are similar to uniform value bindings, except they specify what
    #   what attribute name binds to which attribute in the shader.
    #
    #   TODO: Actually use these bindings. However, that kind of depends on a more freeform
    #   MeshData object as freeform bindings are rather useless when we only have 5 supported
    #   attributes.
    #
    #   \param key The identifier used in the shader for the attribute.
    #   \param value The name to bind to this attribute.
    def addAttributeBinding(self, key, value):
        self._attribute_bindings[key] = value

    ##  Remove an attribute binding.
    #
    #   \param key The name of the attribute binding to remove.
    def removeAttributeBinding(self, key):
        if key not in self._attribute_bindings:
            return

        del self._attribute_bindings[key]

    def _matrixToQMatrix4x4(self, m):
        return QMatrix4x4(m.at(0, 0), m.at(0, 1), m.at(0, 2), m.at(0, 3), m.at(1, 0), m.at(1, 1), m.at(1, 2), m.at(1, 3),
            m.at(2, 0), m.at(2, 1), m.at(2, 2), m.at(2, 3), m.at(3, 0), m.at(3, 1), m.at(3, 2), m.at(3, 3))

    def _setUniformValueDirect(self, uniform, value):
        if type(value) is Vector:
            self._shader_program.setUniformValue(uniform, QVector3D(value.x, value.y, value.z))
        elif type(value) is Matrix:
            self._shader_program.setUniformValue(uniform, self._matrixToQMatrix4x4(value))
        elif type(value) is Color:
            self._shader_program.setUniformValue(uniform,
                QColor(value.r * 255, value.g * 255, value.b * 255, value.a * 255))
        elif type(value) is list and len(value) is 2:
            self._shader_program.setUniformValue(uniform, QVector2D(value[0], value[1]))
        elif type(value) is list and len(value) is 3:
            self._shader_program.setUniformValue(uniform, QVector3D(value[0], value[1], value[2]))
        elif type(value) is list and len(value) is 4:
            self._shader_program.setUniformValue(uniform, QVector4D(value[0], value[1], value[2], value[3]))
        elif type(value) is list and type(value[0]) is list and len(value[0]) is 2:
            self._shader_program.setUniformValueArray(uniform, [QVector2D(i[0], i[1]) for i in value])
        else:
            self._shader_program.setUniformValue(uniform, value)
Пример #9
0
class TestCanvas(Canvas):
    def __init__(self):

        with open("vertex.glsl", "r") as f:
            self.vertexShaderSource = f.read()

        with open("fragment.glsl", "r") as f:
            self.fragmentShaderSource = f.read()

        if self.vertexShaderSource is None:
            raise Exception("Could not load the source for the vertex shader")

        if self.fragmentShaderSource is None:
            raise Exception(
                "Could not load the source for the fragment shader")

        # Use an initial scale assuming width = height (should always be overwritten
        # in the resizeGL method below)
        self.currentScaling = QVector4D(1.0, 1.0, 1.0, 1.0)

        super(TestCanvas, self).__init__()

    # Use a non-square initial size
    def sizeHint(self):
        info("TESTCANVAS", "using overridden sizeHint()")
        return QSize(600, 600)

    def setupGL(self):

        self.program = QOpenGLShaderProgram(self.context())
        info("PROGRAM", self.program)

        # addShaderFromSourceCode() only returns a bool telling whether everything went well

        if self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                self.vertexShaderSource):
            info("VERTEX SHADER",
                 "Managed to load and parse the vertex shader")

        if self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                self.fragmentShaderSource):
            info("FRAGMENT SHADER",
                 "Managed to load and parse the fragment shader")

        # Compile and bind the shader program to the current context
        self.program.link()
        self.program.bind()

        # Bind the shader attribute for transfering vertex positions to a python variable
        self.somePosition = self.gl.glGetAttribLocation(
            self.program.programId(), "somePosition")

        # Bind the shader attribute for transfering vertex colors to a python variable
        self.inputColor = self.gl.glGetAttribLocation(self.program.programId(),
                                                      "inputColor")

        # Bind the shader "uniform" for controling the viewport scale of the vertex positions
        self.viewportScaling = self.gl.glGetUniformLocation(
            self.program.programId(), "viewportScaling")

        # Bind the shader "uniform" for controling the rotation of the object
        self.objectRotation = self.gl.glGetUniformLocation(
            self.program.programId(), "objectRotation")

        # Here we mix vertex data (x, y, z) with color data (r, g, b, a).
        self.vertices = array.array(
            'f',
            [
                -0.5,
                -0.5,
                0.0,
                1.0,
                0.0,
                0.0,
                1.0,  # Bottom left is red
                -0.5,
                0.5,
                0.0,
                0.0,
                1.0,
                0.0,
                1.0,  # Top left is green
                0.5,
                0.5,
                0.0,
                0.0,
                0.0,
                1.0,
                1.0,  # Top right is blue
                0.5,
                -0.5,
                0.0,
                1.0,
                1.0,
                0.0,
                1.0  # Bottom right is yellow
            ])

        # Buffer info returns a tuple where the second part is number of elements in the array
        self.verticesLength = self.vertices.buffer_info()[1]

        # Each vertex is specified with with seven values (xyzrgba)
        self.arrayCellsPerVertex = 7

        # Size in bytes for each vertex specification (self.vertices.itemsize is the size in bytes
        # of a single array cell)
        self.vertexSpecificationSize = self.vertices.itemsize * self.arrayCellsPerVertex

        # In bytes, where in a vertex specification does the color data start? (it starts after
        # x, y, z.. i.e after 3 array cells)
        self.colorBytesOffset = self.vertices.itemsize * 3

        # Total size in bytes for entire array
        self.verticesDataLength = self.verticesLength * self.vertices.itemsize

        # Number of vertices in the array
        self.numberOfVertices = int(self.verticesLength /
                                    self.arrayCellsPerVertex)

        # Tell gl to prepare a single buffer
        self.vertexBuffer = self.gl.glGenBuffers(1)

        # Say that the buffer is of type GL_ARRAY_BUFFER
        self.gl.glBindBuffer(self.gl.GL_ARRAY_BUFFER, self.vertexBuffer)

        # Copy data into the buffer
        self.gl.glBufferData(self.gl.GL_ARRAY_BUFFER, self.verticesDataLength,
                             self.vertices.tobytes(), self.gl.GL_STATIC_DRAW)

        # Say that the somePosition attribute should be read from the current array, that it should take three values at a
        # time (x, y, z), and that they are of the type GL_FLOAT. We now also specify a byte offset (vertexSpecificationSize)
        # to say that each new vertex specification is starting this many bytes after the prior one.
        self.gl.glVertexAttribPointer(self.somePosition, 3, self.gl.GL_FLOAT,
                                      False, self.vertexSpecificationSize, 0)

        # Connect the array to the somePosition variable
        self.gl.glEnableVertexAttribArray(self.somePosition)

        # Say that the inputColor attribute should be read from the current array, that it should take four values at a
        # time (r, g, b, a), and that they are of type GL_FLOAT. The same byte offset as for the position info is used, but
        # we now also specify that the color information starts at colorBytesOffset bytes into each vertex specification
        self.gl.glVertexAttribPointer(self.inputColor, 4, self.gl.GL_FLOAT,
                                      False, self.vertexSpecificationSize,
                                      self.colorBytesOffset)

        # Connect the array to the inputColor variable
        self.gl.glEnableVertexAttribArray(self.inputColor)

        # Tell GL that whenever we run glClear, this is the color that
        # should be used. We only need to do this once.
        self.gl.glClearColor(0.1, 0.1, 0.1, 1.0)

        self.dumpGLLogMessages("setupGL()")

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT
                        | self.gl.GL_DEPTH_BUFFER_BIT)

        # Set rotation so that the quad is rotated 45 degrees clockwise
        self.currentRotation = QVector3D(0.0, 0.0, 45.0)
        self.program.setUniformValue(self.objectRotation, self.currentRotation)

        # Before painting we set the scaling "uniform" parameter in the vertex shader.
        # This will be used to scale the vertex positions.
        self.program.setUniformValue(self.viewportScaling, self.currentScaling)

        # Draw a quad starting at vertex number. Since we enabled attribarrays, it will fetch data from the array buffer
        self.gl.glDrawArrays(self.gl.GL_QUADS, 0, self.numberOfVertices)

        self.dumpGLLogMessages("paintGL()")

    def resizeGL(self, width, height):
        scaleX = 1.0
        scaleY = 1.0
        scaleZ = 1.0  # Will always be 1.0 since we don't scale depth
        scaleW = 1.0  # A global scale factor, also always 1.0

        if width > height:
            scaleX = height / width
        else:
            scaleY = width / height

        self.currentScaling = QVector4D(scaleX, scaleY, scaleZ, scaleW)

        # Redraw since we changed the value of the scaling uniform
        self.update()
Пример #10
0
class GLSliceWidget(QtOpenGL.QGLWidget):
    _dataModelChanged = QtCore.pyqtSignal()

    def __init__(self, parent=None, interpolation="linear", **kwargs):
        logger.debug("init")

        super(GLSliceWidget, self).__init__(parent, **kwargs)

        self.renderUpdate = True
        self.parent = parent

        self.setAcceptDrops(True)

        self.texture_LUT = None
        self.setTransform(TransformModel())

        self.renderTimer = QtCore.QTimer(self)
        self.renderTimer.setInterval(50)
        self.renderTimer.timeout.connect(self.onRenderTimer)
        self.renderTimer.start()

        self.dataModel = None

        self.dataPos = 0
        self.slicePos = 0

        self.zoom_fac = 1.
        self.zoom_x = 0.5
        self.zoom_y = 0.5
        # the center in tex coords
        self.zoom_cx = 0.5
        self.zoom_cy = 0.5

        interpolation_vals = ("linear", "nearest")
        if not interpolation in interpolation_vals:
            raise KeyError("interpolation = '%s' not defined ,valid: %s" %
                           (interpolation, str(interpolation_vals)))

        self.interp = (interpolation == "linear")
        # self.refresh()

    def setModel(self, dataModel):
        logger.debug("setModel")

        self.dataModel = dataModel

        if self.dataModel:
            self.transform.setModel(dataModel)

            self.dataModel._dataSourceChanged.connect(self.dataSourceChanged)
            self.dataModel._dataPosChanged.connect(self.dataPosChanged)
            self._dataModelChanged.connect(self.dataModelChanged)
            self._dataModelChanged.emit()

    def setTransform(self, transform):
        self.transform = transform
        self.transform._transformChanged.connect(self.refresh)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        for url in event.mimeData().urls():
            path = url.toLocalFile().toLocal8Bit().data()
            if self.dataModel:
                self.dataModel.loadFromPath(path, prefetchSize=self.N_PREFETCH)
            else:
                self.setModel(
                    DataModel.fromPath(path, prefetchSize=self.N_PREFETCH))

    def set_colormap(self, name):
        """arr should be of shape (N,3) and gives the rgb components of the colormap"""

        try:
            arr = spimagine.config.__COLORMAPDICT__[name]
            self.makeCurrent()
            self.texture_LUT = fillTexture2d(arr.reshape((1, ) + arr.shape),
                                             self.texture_LUT, self.interp)
        except:
            print("could not load colormap %s" % name)

    def set_colormap_rgb(self, color=[1., 1., 1.]):
        self._set_colormap_array(outer(linspace(0, 1., 255), np.array(color)))

    def _set_colormap_array(self, arr):
        """arr should be of shape (N,3) and gives the rgb components of the colormap"""

        self.makeCurrent()
        self.texture_LUT = fillTexture2d(arr.reshape((1, ) + arr.shape),
                                         self.texture_LUT, self.interp)
        self.refresh()

    def initializeGL(self):

        self.resized = True

        self.output = zeros((100, 100))

        logger.debug("initializeGL")

        self.programTex = QOpenGLShaderProgram()
        self.programTex.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                vertShaderTex)
        self.programTex.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                fragShaderTex)
        self.programTex.link()
        self.programTex.bind()
        logger.debug("GLSL programTex log:%s", self.programTex.log())

        glClearColor(0, 0, 0, 1.)

        self.texture = None

        self.quadCoord = np.array([[-1., -1., 0.], [1., -1., 0.], [1., 1., 0.],
                                   [1., 1., 0.], [-1., 1., 0.], [-1., -1.,
                                                                 0.]])

        self.quadCoordTex = np.array([[0, 0], [1., 0.], [1., 1.], [1., 1.],
                                      [0, 1.], [0, 0]])

        self.set_colormap(spimagine.config.__DEFAULTCOLORMAP__)

        glDisable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    def dataModelChanged(self):
        # if self.dataModel:
        #     self.transform.reset(amin(self.dataModel[0]),
        #                          amax(self.dataModel[0]),
        #                          self.dataModel.stackUnits())
        #

        self.refresh()

    def dataSourceChanged(self):
        # self.transform.reset(amin(self.dataModel[0]),
        #                      amax(self.dataModel[0]),
        #                      self.dataModel.stackUnits())
        self.refresh()

    def dataPosChanged(self, pos):
        self.dataPos = pos
        self.refresh()

    def refresh(self):
        # if self.parentWidget() and self.dataModel:
        #     self.parentWidget().setWindowTitle("SpImagine %s"%self.dataModel.name())
        self.renderUpdate = True

    def resizeGL(self, width, height):
        # height = max(10,height)

        self._viewport_width, self._viewport_height = width, height

        self.resized = True
        self.resetViewPort()

    def getDataWidthHeight(self):
        if not self.dataModel:
            return 1, 1

        dim = array(self.dataModel.size()[1:])[::-1].astype(np.float32)

        dim *= array(self.transform.stackUnits)

        if self.transform.sliceDim == 0:
            dim = dim[[2, 1]]
        elif self.transform.sliceDim == 1:
            dim = dim[[0, 2]]
        elif self.transform.sliceDim == 2:
            dim = dim[[0, 1]]

        w, h = dim[0], dim[1]

        fac = min(1. * self._viewport_width / w,
                  1. * self._viewport_height / h)

        return int(fac * w), int(fac * h)

    def resetViewPort(self):
        w, h = self.getDataWidthHeight()
        glViewport((self._viewport_width - w) // 2,
                   (self._viewport_height - h) // 2, w, h)

    def paintGL(self):

        self.makeCurrent()

        if not glCheckFramebufferStatus(
                GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE:
            return

        # hack
        if self.resized:
            self.resetViewPort()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        if self.dataModel:
            # Draw the render texture
            self.programTex.bind()

            self.texture = fillTexture2d(self.output, self.texture,
                                         self.interp)

            glEnable(GL_TEXTURE_2D)
            glDisable(GL_DEPTH_TEST)

            self.programTex.enableAttributeArray("position")
            self.programTex.enableAttributeArray("texcoord")
            self.programTex.setAttributeArray("position", self.quadCoord)
            self.programTex.setAttributeArray(
                "texcoord",
                self.tex_coords_from_xyzoom(self.zoom_x, self.zoom_y,
                                            self.zoom_fac))

            glActiveTexture(GL_TEXTURE0)
            glBindTexture(GL_TEXTURE_2D, self.texture)
            self.programTex.setUniformValue("texture", 0)

            glActiveTexture(GL_TEXTURE1)
            glBindTexture(GL_TEXTURE_2D, self.texture_LUT)
            self.programTex.setUniformValue("texture_LUT", 1)

            glDrawArrays(GL_TRIANGLES, 0, len(self.quadCoord))

    def render(self):
        logger.debug("render")
        if self.dataModel:
            if self.transform.sliceDim == 0:
                out = fliplr(self.dataModel[self.transform.dataPos]
                             [:, :, self.transform.slicePos].T)
            elif self.transform.sliceDim == 1:
                out = self.dataModel[
                    self.transform.dataPos][:, self.transform.slicePos, :]
            elif self.transform.sliceDim == 2:
                out = self.dataModel[self.transform.dataPos][
                    self.transform.slicePos, :, :]

            min_out, max_out = self.transform.minVal, self.transform.maxVal

            if max_out > min_out:
                self.output = np.maximum(
                    0, (1. * (out - min_out) /
                        (max_out - min_out)))**self.transform.gamma
            else:
                self.output = np.zeros_like(out)

            logger.debug(
                "render: output range = %s" %
                ([amin(self.output), amax(self.output)]))

    def saveFrame(self, fName):
        """FIXME: scaling behaviour still hast to be implemented (e.g. after setGamma)"""
        logger.info("saving frame as %s", fName)

        self.render()
        self.paintGL()
        glFlush()
        self.grabFrameBuffer().save(fName)

    def onRenderTimer(self):
        if self.renderUpdate:
            self.render()
            self.renderUpdate = False
            self.updateGL()

    def tex_coords_from_xyzoom(self, x0, y0, zoom):
        """returns array of texccords corners when zoomed
        in onto x0,y0 \in [0,1]
        
        zoom == 1   --> fully zoomed out
        zoom == 0   --> fully zoomed in (to x0,y0)
        """

        q0 = create_quad_coords([0, 1, 0, 1])
        q1 = create_quad_coords([x0, x0, y0, y0])

        return zoom * q0 + (1. - zoom) * q1

    def getRelativeCoords(self, x0, y0):
        w, h = self.getDataWidthHeight()

        w = w * self.width() / self._viewport_width
        h = w * self.height() / self._viewport_height

        x = 2. * (x0 - .5 * (self.width() - w)) / w - 1
        y = 2. * (y0 - .5 * (self.height() - h)) / h - 1

        x = (x0 - .5 * (self.width() - w)) / w
        y = 1 - (y0 - .5 * (self.height() - h)) / h

        return x, y

    def mousePressEvent(self, event):
        super(GLSliceWidget, self).mousePressEvent(event)

        if event.buttons() == QtCore.Qt.LeftButton:
            self._x0, self._y0 = self.getRelativeCoords(event.x(), event.y())

    def mouseMoveEvent(self, event):

        if event.buttons() == QtCore.Qt.LeftButton:
            x, y = self.getRelativeCoords(event.x(), event.y())
            self.zoom_x += self.zoom_fac * (self._x0 - x)
            self.zoom_y += self.zoom_fac * (self._y0 - y)
            self._x0, self._y0 = x, y
            self.zoom_x, self.zoom_y = clip(self.zoom_x, 0,
                                            1), clip(self.zoom_y, 0, 1)

        self.refresh()

    def wheelEvent(self, event):
        # get the zoom factor

        # print self.zoom_x,self.zoom_y
        # x, y = self.getRelativeCoords(event.x(),event.y())
        # self.zoom_x , self.zoom_y = clip(x ,0,1), clip(y,0,1)

        self.zoom_fac *= 1.4**(-event.angleDelta().y() / 1000.)
        self.zoom_fac = clip(self.zoom_fac, 0, 1.)

        self.refresh()
Пример #11
0
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()
Пример #12
0
class ImageOpenGLWidget(RawImageWidget):

    def __init__(self, parent):
        super().__init__(parent)
        self.shader_program = QOpenGLShaderProgram(self)
        self.vertex_data = np.array([
            # X,    Y,   Z     U,   V
            -LOC, LOC, 0.0, 0.0, 1.0,
            LOC, LOC, 0.0, 1.0, 1.0,
            LOC, -LOC, 0.0, 1.0, 0.0,

            LOC, -LOC, 0.0, 1.0, 0.0,
            -LOC, -LOC, 0.0, 0.0, 0.0,
            -LOC, LOC, 0.0, 0.0, 1.0,
        ], dtype=np.float32)

        self._image = None#QImage()
        self.vao = None
        self.vertex_position_uniform = -1
        self.

    @property
    def image(self):
        return self._image

    @image.setter
    def image(self, new_image):
        self._image = new_image
        if self._image is not None:
            self.create_texture()
        self.update()

    def initializeGL(self):
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, VERTEX)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, FRAGMENT)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(self.shader_program.log()))

        self.vao = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vao)

        VBO = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO)

        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes, self.vertex_data,
                        GL.GL_STATIC_DRAW)

        self.vertex_position_uniform = self.get_uniform_location("vertexPosition")

        self.projection_matrix_uniform = self.shader_program.uniformLocation("projection_matrix")
        self.view_matrix_uniform = self.shader_program.uniformLocation("view_matrix")

        GL.glEnableVertexAttribArray(0)
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(self.vertex_position, 3, GL.GL_FLOAT, GL.GL_FALSE, 20,
                                 None)

        GL.glVertexAttribPointer(texture_coords, 2, GL.GL_FLOAT, GL.GL_TRUE, 20,
                                 ctypes.c_void_p(12))

        self.texture_sampler = self.shader_program.uniformLocation("textureSampler")
        self.texture = GL.glGenTextures(1)

        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

    # def paintEvent(self, event):
    #     painter = QPainter(self)
    #     painter.drawImage(0, 0, self._image)

    def paintGL(self):
        self.shader_program.bind()

        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glUniform1i(self.texture_sampler, 0)

        GL.glBindVertexArray(self.VAO)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)

        GL.glBindVertexArray(0)
        self.shader_program.release()
Пример #13
0
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(QOpenGLShader.Vertex,
                self.vertexShaderSource)
        self.m_program.addShaderFromSourceCode(QOpenGLShader.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_matrixUniform, 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
Пример #14
0
class InvertSceneColoursMixin:
    def __init__(self):
        self.initialised = False
        self.shader_program = QOpenGLShaderProgram()
        self.vertex_position = None
        self.texture = 0
        self.frame_buffer = 0
        self.recreate_frame_buffer = True

        x0 = -1
        y0 = -1
        width = 2

        self.vertex_data = np.array([
            x0, y0, 0.0, 0.0, 0.0, x0 + width, y0, 0.0, 1.0, 0.0, x0 + width,
            y0 + width, 0.0, 1.0, 1.0, x0 + width, y0 + width, 0.0, 1.0, 1.0,
            x0, y0 + width, 0.0, 0.0, 1.0, x0, y0, 0.0, 0.0, 0.0
        ],
                                    dtype=np.float32)

    def draw_texture_to_screen(self):
        self.shader_program.bind()
        GL.glBindVertexArray(self.vertex_attribute_object)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.shader_program.release()

    def create_framebuffer(self):
        #  log.debug("create_framebuffer")
        GL.glDeleteTextures([self.texture])
        self.texture = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        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.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, self.width(),
                        self.height(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, None)

        GL.glDeleteFramebuffers([self.frame_buffer])
        self.frame_buffer = GL.glGenFramebuffers(1)
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.frame_buffer)

        GL.glFramebufferTexture(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0,
                                self.texture, 0)

    def initialise_gl(self):
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                    VERTEX)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                    FRAGMENT)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(
                self.shader_program.log()))

        self.vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes,
                        self.vertex_data, GL.GL_STATIC_DRAW)

        self.vertex_position = self.shader_program.attributeLocation(
            "vertexPosition")
        self.texture_coords = self.shader_program.attributeLocation(
            "tex_coords")
        self.texture_framebuffer = self.shader_program.uniformLocation(
            "texture_framebuffer")

        GL.glEnableVertexAttribArray(0)
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(self.vertex_position, 3, GL.GL_FLOAT,
                                 GL.GL_FALSE, 20, None)

        GL.glVertexAttribPointer(self.texture_coords, 2, GL.GL_FLOAT,
                                 GL.GL_TRUE, 20, ctypes.c_void_p(12))

        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

        self.create_framebuffer()
        self.initialised = True

    def resize_texture(self):
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, self.width(),
                        self.height(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, None)
        self.recreate_frame_buffer = False
Пример #15
0
class TestCanvas(Canvas):
    def __init__(self):

        with open("vertex.glsl", "r") as f:
            self.vertexShaderSource = f.read()

        with open("fragment.glsl", "r") as f:
            self.fragmentShaderSource = f.read()

        if self.vertexShaderSource is None:
            raise Exception("Could not load the source for the vertex shader")

        if self.fragmentShaderSource is None:
            raise Exception(
                "Could not load the source for the fragment shader")

        super(TestCanvas, self).__init__()

    def setupGL(self):

        self.program = QOpenGLShaderProgram(self.context())
        info("PROGRAM", self.program)

        # addShaderFromSourceCode() only returns a bool telling whether everything went well

        if self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                self.vertexShaderSource):
            info("VERTEX SHADER",
                 "Managed to load and parse the vertex shader")

        if self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                self.fragmentShaderSource):
            info("FRAGMENT SHADER",
                 "Managed to load and parse the fragment shader")

        # Compile and bind the shader program to the current context
        self.program.link()
        self.program.bind()

        # Bind the shader attribute for transfering vertex positions to a python variable
        self.somePosition = self.gl.glGetAttribLocation(
            self.program.programId(), "somePosition")

        # Bind the shader attribute for transfering vertex colors to a python variable
        self.inputColor = self.gl.glGetAttribLocation(self.program.programId(),
                                                      "inputColor")

        # Here we mix vertex data (x, y, z) with color data (r, g, b, a).
        self.vertices = array.array(
            'f',
            [
                -0.5,
                -0.5,
                0.0,
                1.0,
                0.0,
                0.0,
                1.0,  # Bottom left is red
                -0.5,
                0.5,
                0.0,
                0.0,
                1.0,
                0.0,
                1.0,  # Top left is green
                0.5,
                0.5,
                0.0,
                0.0,
                0.0,
                1.0,
                1.0,  # Top right is blue
                0.5,
                -0.5,
                0.0,
                1.0,
                1.0,
                0.0,
                1.0  # Bottom right is yellow
            ])

        # Buffer info returns a tuple where the second part is number of elements in the array
        self.verticesLength = self.vertices.buffer_info()[1]

        # Each vertex is specified with with seven values (xyzrgba)
        self.arrayCellsPerVertex = 7

        # Size in bytes for each vertex specification (self.vertices.itemsize is the size in bytes
        # of a single array cell)
        self.vertexSpecificationSize = self.vertices.itemsize * self.arrayCellsPerVertex

        # In bytes, where in a vertex specification does the color data start? (it starts after
        # x, y, z.. i.e after 3 array cells)
        self.colorBytesOffset = self.vertices.itemsize * 3

        # Total size in bytes for entire array
        self.verticesDataLength = self.verticesLength * self.vertices.itemsize

        # Number of vertices in the array
        self.numberOfVertices = int(self.verticesLength /
                                    self.arrayCellsPerVertex)

        # Tell gl to prepare a single buffer
        self.vertexBuffer = self.gl.glGenBuffers(1)

        # Say that the buffer is of type GL_ARRAY_BUFFER
        self.gl.glBindBuffer(self.gl.GL_ARRAY_BUFFER, self.vertexBuffer)

        # Copy data into the buffer
        self.gl.glBufferData(self.gl.GL_ARRAY_BUFFER, self.verticesDataLength,
                             self.vertices.tobytes(), self.gl.GL_STATIC_DRAW)

        # Say that the somePosition attribute should be read from the current array, that it should take three values at a
        # time (x, y, z), and that they are of the type GL_FLOAT. We now also specify a byte offset (vertexSpecificationSize)
        # to say that each new vertex specification is starting this many bytes after the prior one.
        self.gl.glVertexAttribPointer(self.somePosition, 3, self.gl.GL_FLOAT,
                                      False, self.vertexSpecificationSize, 0)

        # Connect the array to the somePosition variable
        self.gl.glEnableVertexAttribArray(self.somePosition)

        # Say that the inputColor attribute should be read from the current array, that it should take four values at a
        # time (r, g, b, a), and that they are of type GL_FLOAT. The same byte offset as for the position info is used, but
        # we now also specify that the color information starts at colorBytesOffset bytes into each vertex specification
        self.gl.glVertexAttribPointer(self.inputColor, 4, self.gl.GL_FLOAT,
                                      False, self.vertexSpecificationSize,
                                      self.colorBytesOffset)

        # Connect the array to the inputColor variable
        self.gl.glEnableVertexAttribArray(self.inputColor)

        # Tell GL that whenever we run glClear, this is the color that
        # should be used. We only need to do this once.
        self.gl.glClearColor(0.1, 0.1, 0.1, 1.0)

        self.dumpGLLogMessages("setupGL()")

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT
                        | self.gl.GL_DEPTH_BUFFER_BIT)

        # Draw a quad starting at vertex number. Since we enabled attribarrays, it will fetch data from the array buffer
        self.gl.glDrawArrays(self.gl.GL_QUADS, 0, self.numberOfVertices)

        self.dumpGLLogMessages("paintGL()")

    def resizeGL(self, width, height):
        pass
Пример #16
0
class QtShaderProgram(ShaderProgram):
    def __init__(self):
        super().__init__()

        self._shader_program = None
        self._uniform_indices = {}
        self._attribute_indices = {}
        self._uniform_values = {}
        self._bound = False
        self._textures = {}

    def setVertexShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, shader):
            Logger.log("e", "Vertex shader failed to compile: %s", self._shader_program.log())

    def setFragmentShader(self, shader):
        if not self._shader_program:
            self._shader_program = QOpenGLShaderProgram()

        if not self._shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, shader):
            Logger.log("e", "Fragment shader failed to compile: %s", self._shader_program.log())

    def build(self):
        if not self._shader_program:
            Logger.log("e", "No shader sources loaded")
            return

        if not self._shader_program.link():
            Logger.log("e", "Shader failed to link: %s", self._shader_program.log())

    def setUniformValue(self, name, value, **kwargs):
        if not self._shader_program:
            return

        if name not in self._uniform_indices:
            self._uniform_indices[name] = self._shader_program.uniformLocation(name)

        uniform = self._uniform_indices[name]
        if uniform == -1:
            return

        if kwargs.get("cache", True):
            self._uniform_values[uniform] = value

        if self._bound:
            self._setUniformValueDirect(uniform, value)

    def setTexture(self, texture_unit, texture):
        if texture is None:
            if texture_unit in self._textures:
                del self._textures[texture_unit]
        else:
            self._textures[texture_unit] = texture

    def enableAttribute(self, name, type, offset, stride = 0):
        if not self._shader_program:
            return

        self.bind()

        if name not in self._attribute_indices:
            self._attribute_indices[name] = self._shader_program.attributeLocation(name)

        attribute = self._attribute_indices[name]
        if attribute == -1:
            return

        if type is "int":
            self._shader_program.setAttributeBuffer(attribute, 0x1404, offset, 1, stride) #GL_INT
        elif type is "float":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 1, stride) #GL_FLOAT
        elif type is "vector2f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 2, stride) #GL_FLOAT
        elif type is "vector3f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 3, stride) #GL_FLOAT
        elif type is "vector4f":
            self._shader_program.setAttributeBuffer(attribute, 0x1406, offset, 4, stride) #GL_FLOAT

        self._shader_program.enableAttributeArray(attribute)


    def disableAttribute(self, name):
        if not self._shader_program:
            return

        if name not in self._attribute_indices:
            return

        self._shader_program.disableAttributeArray(self._attribute_indices[name])

    def bind(self):
        if not self._shader_program or not self._shader_program.isLinked():
            return

        if self._bound:
            return

        self._bound = True
        self._shader_program.bind()

        for uniform in self._uniform_values:
            self._setUniformValueDirect(uniform, self._uniform_values[uniform])

        for texture_unit, texture in self._textures.items():
            texture.bind(texture_unit)

    def release(self):
        if not self._shader_program or not self._bound:
            return

        self._bound = False
        self._shader_program.release()

        for texture_unit, texture in self._textures.items():
            texture.release(texture_unit)

    def _matrixToQMatrix4x4(self, m):
        return QMatrix4x4(m.at(0,0), m.at(0, 1), m.at(0, 2), m.at(0, 3),
                          m.at(1,0), m.at(1, 1), m.at(1, 2), m.at(1, 3),
                          m.at(2,0), m.at(2, 1), m.at(2, 2), m.at(2, 3),
                          m.at(3,0), m.at(3, 1), m.at(3, 2), m.at(3, 3))

    def _setUniformValueDirect(self, uniform, value):
        if type(value) is Vector:
            self._shader_program.setUniformValue(uniform, QVector3D(value.x, value.y, value.z))
        elif type(value) is Matrix:
            self._shader_program.setUniformValue(uniform, self._matrixToQMatrix4x4(value))
        elif type(value) is Color:
            self._shader_program.setUniformValue(uniform, QColor(value.r * 255, value.g * 255, value.b * 255, value.a * 255))
        elif type(value) is list and len(value) is 2:
            self._shader_program.setUniformValue(uniform, QVector2D(value[0], value[1]))
        elif type(value) is list and len(value) is 3:
            self._shader_program.setUniformValue(uniform, QVector3D(value[0], value[1], value[2]))
        elif type(value) is list and len(value) is 4:
            self._shader_program.setUniformValue(uniform, QVector4D(value[0], value[1], value[2], value[3]))
        elif type(value) is list and type(value[0]) is list and len(value[0]) is 2:
            self._shader_program.setUniformValueArray(uniform, [QVector2D(i[0], i[1]) for i in value])
        else:
            self._shader_program.setUniformValue(uniform, value)
Пример #17
0
class StimulusWidget(QOpenGLWidget):

    shown = pyqtSignal()
    closed = pyqtSignal()
    backgroundColourChanged = pyqtSignal(float)
    invertedChanged = pyqtSignal(bool)
    resized = pyqtSignal(QResizeEvent)

    def __init__(self, homography_transform, parent=None):
        super().__init__(parent)
        self._background_colour = None
        self._drawables = []
        self._image = None
        self._intensity_mask = None
        self.centre_x_uniform = None
        self.centre_y_uniform = None
        self.crosshair = Crosshair(self)
        self.height_uniform = None
        self.image_to_upload = None
        self.gl_initialised = False
        self.shape_uniform = None
        self.max_height_uniform = None
        self.rotation_uniform = None
        self.texture = -1
        self.texture_uploaded = False
        self.width_x_uniform = None
        self.width_y_uniform = None
        self.homography_transform = homography_transform
        self.projection_matrix = QMatrix4x4()
        self.view_matrix = QMatrix4x4()
        self.set_background_colour(0)
        self.shader_program = QOpenGLShaderProgram()
        self._show_crosshair = False
        self._show_scale_bar = False
        self._crosshair_thickness = 1
        self._gaussian_shape = QVector2D()
        self._image_to_render = None
        self._inverted = False
        self._scale_bar_thickness = 1
        self._scale_bar_width = 10
        self._use_homography = False
        self._use_intensity_mask = False

        self.gaussian = Gaussian()

        self.transform = QTransform()
        self.dx = 0
        self.dy = 0
        self.rotation = 0
        self.scale_value = 1

        self.setWindowTitle('Stimulus Window')

        self.homography_transform.matrixChanged.connect(self.update_image)

    def compute_transformation_matrix(self):
        self.view_matrix.setToIdentity()

        dx = self.dx / (0.5 * self.width())
        dy = -self.dy / (0.5 * self.height())

        self.view_matrix.scale(self.scale)
        self.view_matrix.rotate(self.rotation, QVector3D(0, 0, 1))
        self.view_matrix.translate(dx, dy, 0)

    def full_brightness(self):
        return 255.0 if self.inverted else 0.0

    @property
    def background_colour(self):
        return self._background_colour

    @background_colour.setter
    def background_colour(self, new_background):
        new_background = int(new_background)
        self.set_background_colour(new_background)

    def closeEvent(self, event):
        self.closed.emit()

    @property
    def crosshair_thickness(self):
        return self._crosshair_thickness

    @crosshair_thickness.setter
    def crosshair_thickness(self, thickness):
        self._crosshair_thickness = thickness
        self.update()

    @property
    def drawables(self):
        return self._drawables

    @drawables.setter
    def drawables(self, new_drawables):
        self._drawables = new_drawables
        self.gl_initialised = False
        self.update()

    def upload_texture(self):
        self.create_texture(self.image_to_upload)
        self.texture_uploaded = True

    def create_texture(self, image):
        log.info("Creating texture")
        img_data = np.flipud(image)
        height, width = img_data.shape

        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        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_MIPMAP_LINEAR)

        GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S,
                           GL.GL_CLAMP_TO_BORDER)
        GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T,
                           GL.GL_CLAMP_TO_BORDER)

        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, width, height, 0,
                        GL.GL_LUMINANCE, GL.GL_UNSIGNED_BYTE, img_data)
        GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)

    @property
    def homography(self):
        return self._homography

    @homography.setter
    def homography(self, value):
        self._homography = value
        self.update()

    @property
    def image(self):
        return self._image

    @image.setter
    def image(self, img):
        self._image = img
        if self._image is not None:
            self.update_image()
        self.update()

    def initializeGL(self):
        log.info("OpenGL Vendor: {}".format(GL.glGetString(GL.GL_VENDOR)))
        log.info("OpenGL Renderer: {}".format(GL.glGetString(GL.GL_RENDERER)))
        log.info("OpenGL Version: {}".format(GL.glGetString(GL.GL_VERSION)))

        for drawable in self.drawables:
            drawable.initialise_gl()

        return

        self.texture = GL.glGenTextures(1)

        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                    VERTEX)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                    FRAGMENT)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(
                self.shader_program.log()))

        LOC = 1.0

        vertexData = np.array(
            [
                # X,    Y,   Z     U,   V
                -LOC,
                LOC,
                0.0,
                0.0,
                1.0,
                LOC,
                LOC,
                0.0,
                1.0,
                1.0,
                LOC,
                -LOC,
                0.0,
                1.0,
                0.0,
                LOC,
                -LOC,
                0.0,
                1.0,
                0.0,
                -LOC,
                -LOC,
                0.0,
                0.0,
                0.0,
                -LOC,
                LOC,
                0.0,
                0.0,
                1.0,
            ],
            dtype=np.float32)

        self.VAO = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.VAO)

        VBO = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, VBO)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vertexData.nbytes, vertexData,
                        GL.GL_STATIC_DRAW)

        vertex_position = self.shader_program.attributeLocation(
            "vertexPosition")

        self.homography_matrix_uniform = self.shader_program.uniformLocation(
            "homography_matrix")
        self.intensity_mask_uniform = self.shader_program.uniformLocation(
            "intensity_mask")
        self.inverted_uniform = self.shader_program.uniformLocation("inverted")
        self.projection_matrix_uniform = self.shader_program.uniformLocation(
            "projection_matrix")
        self.view_matrix_uniform = self.shader_program.uniformLocation(
            "view_matrix")

        self.centre_x_uniform = self.shader_program.uniformLocation("centre_x")
        self.centre_y_uniform = self.shader_program.uniformLocation("centre_y")
        self.height_uniform = self.shader_program.uniformLocation("height")
        self.shape_uniform = self.shader_program.uniformLocation("shape")
        self.width_x_uniform = self.shader_program.uniformLocation("width_x")
        self.width_y_uniform = self.shader_program.uniformLocation("width_y")
        self.rotation_uniform = self.shader_program.uniformLocation("rotation")

        GL.glEnableVertexAttribArray(0)
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(vertex_position, 3, GL.GL_FLOAT, GL.GL_FALSE,
                                 20, None)

        GL.glVertexAttribPointer(texture_coords, 2, GL.GL_FLOAT, GL.GL_TRUE,
                                 20, ctypes.c_void_p(12))

        self.texture_sampler = self.shader_program.uniformLocation(
            "textureSampler")

        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

        self.crosshair.initialise_gl()

        self.gl_initialised = True

    @property
    def intensity_mask(self):
        return self._intensity_mask

    @intensity_mask.setter
    def intensity_mask(self, new_mask):
        self._intensity_mask = new_mask

    @property
    def inverted(self):
        return self._inverted

    @inverted.setter
    def inverted(self, invert):
        if invert != self._inverted:
            self._inverted = invert
            self.background_colour = ~self._background_colour & 0xFF
            self.invertedChanged.emit(self._inverted)
            self.update()

    def open(self):

        desktop = QApplication.desktop()
        is_fullscreen = desktop.screenCount() > 1

        if is_fullscreen:
            screen_widget_on = desktop.screenNumber(
                QApplication.activeWindow())
            next_screen_not_in_use = (screen_widget_on +
                                      1) % desktop.screenCount()
            other_screen_geometry = desktop.screenGeometry(
                next_screen_not_in_use)
            self.move(other_screen_geometry.x(), other_screen_geometry.y())
            self.showFullScreen()
        else:
            self.resize(1024, 1024)
            self.show()

        self.showFullScreen() if is_fullscreen else self.show()

    def paint_gl_image(self):

        if not self.texture_uploaded:
            self.upload_texture()

        log.debug("paint gl image: texture: {}, sampler: {}".format(
            self.texture, self.texture_sampler))
        self.shader_program.bind()

        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glUniform1i(self.texture_sampler, 0)

        #    homography = self.homography_matrix if self.use_homography else np.identity(3, dtype=np.float32)
        #mat = QMatrix3x3()
        #self.shader_program.setUniformValue(self.homography_matrix_uniform, mat)
        #   GL.glUniformMatrix3fv(self.homography_matrix_uniform, 1, GL.GL_FALSE, homography)
        # self.shader_program.setUniformValue(self.homography_matrix_uniform, homography)

        self.shader_program.setUniformValue(self.inverted_uniform,
                                            self.inverted)
        #   self.shader_program.setUniformValue(self.intensity_mask_uniform, self._intensity_mask)
        self.shader_program.setUniformValue(self.projection_matrix_uniform,
                                            self.projection_matrix)
        self.shader_program.setUniformValue(self.view_matrix_uniform,
                                            self.view_matrix)

        # self.shader_program.setUniformValue(self.height_uniform, self.gaussian.amplitude)
        # self.shader_program.setUniformValue(self.centre_x_uniform, self.gaussian.x0)
        # self.shader_program.setUniformValue(self.centre_y_uniform, self.gaussian.y0)
        # self.shader_program.setUniformValue(self.width_x_uniform, self.gaussian.width_x)
        # self.shader_program.setUniformValue(self.width_y_uniform, self.gaussian.width_y)
        # self.shader_program.setUniformValue(self.rotation_uniform, self.gaussian.rotation)
        # self.shader_program.setUniformValue(self.shape_uniform, self._gaussian_shape)

        GL.glBindVertexArray(self.VAO)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)

        GL.glBindVertexArray(0)
        self.shader_program.release()

    def paint_scale_bar(self, painter, half_width, half_height):
        painter.setPen(QPen(self._pen_colour(), self._scale_bar_thickness))
        half_scale_bar_width = self.scale_bar_width * 0.5

        start = QPointF(half_width - half_scale_bar_width, half_height)
        end = QPointF(half_width + half_scale_bar_width, half_height)

        painter.drawLine(QPointF(start.x(),
                                 start.y() - half_scale_bar_width),
                         QPointF(start.x(),
                                 start.y() + half_scale_bar_width))
        painter.drawLine(start, end)
        painter.drawLine(QPointF(end.x(),
                                 end.y() - half_scale_bar_width),
                         QPointF(end.x(),
                                 end.y() + half_scale_bar_width))

    def paintGL(self):

        if not self.gl_initialised:
            self.initializeGL()

        value = float(self.background_colour) / 255.0
        GL.glClearColor(value, value, value, 1.0)
        # GL.glClearColor(1.0, 0.0, 0.0, 1.0)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)

        self.projection_matrix.setToIdentity()
        self.projection_matrix.ortho(0, self.width(), self.height(), 0, -1, 1)

        for drawable in self.drawables:
            drawable.paint_gl()

    # ar = self.width() / self.height()

    # if ar > 1:
    #     self.projection_matrix.ortho(-ar, ar, -1, 1, -1, 1)
    # else:
    #     self.projection_matrix.ortho(-1, 1, -ar, ar, -1, 1)
    #
    # if self._show_crosshair:
    #     self.crosshair.paint_gl(dx=self.dx,
    #                             dy=self.dy,
    #                             height=self.height(),
    #                             inverted=self.inverted,
    #                             projection_matrix=self.projection_matrix,
    #                             rotation=self.rotation,
    #                             thickness=self._crosshair_thickness,
    #                             width=self.width())
    # elif self._show_scale_bar:
    #     self.paint_scale_bar(painter=painter, half_width=half_x, half_height=half_y)
    # else:
    #     if self.image is not None and self.texture:
    #         self.paint_gl_image()

    def reset_background(self):
        self.set_background_colour(255.0 if self.inverted else 0.0)

    def resizeEvent(self, resize_event):
        super().resizeEvent(resize_event)
        self.resized.emit(resize_event)

    def rotate(self, value):
        self.rotation = value
        self.compute_transformation_matrix()
        self.update()

    def save(self):
        return {
            'dx': self.dx,
            'dy': self.dy,
            'scale': self.scale,
            'rotation': self.rotation
        }

    def set_scale(self, new_scale):
        self.scale = new_scale
        self.compute_transformation_matrix()
        self.update()

    def set_background_colour(self, value):
        value = int(value)

        if value == self._background_colour:
            return

        self._background_colour = value
        self.backgroundColourChanged.emit(value)
        self.update()

    def set_full_brightness(self):
        self.set_background_colour(self.full_brightness())

    def set_gaussian(self, gaussian, shape):
        self.gaussian = gaussian
        self._gaussian_shape.setX(shape[0])
        self._gaussian_shape.setY(shape[1])
        self.update()

    @property
    def scale_bar_thickness(self):
        return self._scale_bar_thickness

    @scale_bar_thickness.setter
    def scale_bar_thickness(self, new_thickness):
        self._scale_bar_thickness = new_thickness
        self.update()

    @property
    def scale_bar_width(self):
        return self._scale_bar_width

    @scale_bar_width.setter
    def scale_bar_width(self, new_width):
        self._scale_bar_width = new_width
        self.update()

    def showEvent(self, event):
        self.shown.emit()

    def hide_crosshair(self):
        self._show_crosshair = False
        self.update()

    def show_crosshair(self):
        self._show_scale_bar = False
        self.set_background_colour(255.0 if self.inverted else 0.0)
        self._show_crosshair = True
        self.update()

    @property
    def show_scale_bar(self):
        return self._show_scale_bar

    @show_scale_bar.setter
    def show_scale_bar(self, new_value):
        self._show_scale_bar = new_value
        if self._show_scale_bar:
            self._show_crosshair = False
        self.update()

    def translate(self, dx, dy):
        self.dx = dx
        self.dy = dy
        self.compute_transformation_matrix()
        self.update()

    def toggle_crosshair(self):
        self.hide_crosshair() if self._show_crosshair else self.show_crosshair(
        )
        return self._show_crosshair

    def toggle_widget(self):
        self.close() if self.isVisible() else self.open()

    @property
    def use_homography(self):
        return self._use_homography

    @use_homography.setter
    def use_homography(self, value):
        self._use_homography = value
        self.update_image()

    @property
    def use_intensity_mask(self):
        return self._use_intensity_mask

    @use_intensity_mask.setter
    def use_intensity_mask(self, new_value):
        self._use_intensity_mask = new_value
        self.update_image()

    def update_image(self):

        img = self._image

        if img is not None:
            if self._use_intensity_mask:
                mask = cv2.resize(self.intensity_mask, img.shape)
                img = img * mask

            if self._use_homography:
                height, width = img.shape
                img = cv2.warpPerspective(img,
                                          self.homography_transform.matrix,
                                          (height, width))

        self.image_to_upload = img
        self.texture_uploaded = False
        self.update()
Пример #18
0
class GLGraphicsRectItem(QGraphicsRectItem):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.shader_program = QOpenGLShaderProgram()
        width = 1
        half_width = width / 2
        rect = QRectF(-half_width, -half_width, width, width)
        self.setRect(rect)
        self.vertex_attribute_object = None
        self.vertex_data = np.array([
            -half_width, -half_width, 0.0, -half_width + width, -half_width,
            0.0, -half_width + width, -half_width + width, 0.0,
            -half_width + width, -half_width + width, 0.0, -half_width,
            half_width, 0.0, -half_width, -half_width, 0.0
        ],
                                    dtype=np.float32)
        self.vertex_position_uniform = None

    def get_uniform_location(self, name):
        location = self.shader_program.uniformLocation(name)
        if location == -1:
            raise ValueError("Uniform {} has no location.".format(name))
        return location

    def initialise_gl(self):
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                    VERTEX)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                    FRAGMENT)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(
                self.shader_program.log()))

        self.vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes,
                        self.vertex_data, GL.GL_STATIC_DRAW)

        self.vertex_position = self.shader_program.attributeLocation(
            "vertexPosition")

        self.projection_matrix_uniform = self.get_uniform_location(
            "projection_matrix")
        self.view_matrix_uniform = self.get_uniform_location("view_matrix")
        self.model_matrix_uniform = self.get_uniform_location("model_matrix")

        GL.glVertexAttribPointer(self.vertex_position, 3, GL.GL_FLOAT,
                                 GL.GL_FALSE, 0, None)

        GL.glEnableVertexAttribArray(0)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

    def paint_gl(self):

        model_matrix = QMatrix4x4()
        projection_matrix = QMatrix4x4()
        view_matrix = QMatrix4x4()

        self.shader_program.bind()
        self.shader_program.setUniformValue(self.model_matrix_uniform,
                                            model_matrix)
        self.shader_program.setUniformValue(self.projection_matrix_uniform,
                                            projection_matrix)
        self.shader_program.setUniformValue(self.view_matrix_uniform,
                                            view_matrix)
        GL.glBindVertexArray(self.vertex_attribute_object)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.shader_program.release()
Пример #19
0
class MyWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent = None):
        super(MyWidget, self).__init__(parent)

        self.quadCoord = np.array([[-1., -1., 0.],
                                   [1., -1., 0.],
                                   [1., 1., 0.],
                                   [1., 1., 0.],
                                   [-1., 1., 0.],
                                   [-1., -1., 0.]])

        self.quadCoordTex = np.array([[0, 0],
                                      [1., 0.],
                                      [1., 1.],
                                      [1., 1.],
                                      [0, 1.],
                                      [0, 0]])

    def initializeGL(self):
        GL.glClearColor(1.0, 0.0, 0.0, 1.0)
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)

        print("OpenGL.GL: " + str(GL.glGetString(GL.GL_VERSION)))
        print("GL.GLSL: " + str(GL.glGetString(GL.GL_SHADING_LANGUAGE_VERSION)))
        print("OpenGL ATTRIBUTES:\n",", ".join(d for d in dir(GL) if d.startswith("GL_")))

        self.program = QOpenGLShaderProgram()
        self.program.addShaderFromSourceCode(QOpenGLShader.Vertex, """#version 120
        attribute vec2 position;
        attribute vec2 texcoord;
        varying vec2 mytexcoord;
        void main() {
            gl_Position = vec4(position, 0., 1.0);
            mytexcoord = texcoord;
        }""")

        self.program.addShaderFromSourceCode(QOpenGLShader.Fragment, """#version 120
        uniform sampler2D texture;
        varying vec2 mytexcoord;
        void main() {
            
            gl_FragColor = texture2D(texture,mytexcoord);
        }""")
        print(self.program.log())
        self.program.link()

        self.texture = fillTexture2d(np.outer(np.linspace(0, 1, 128), np.ones(128)))

    def paintGL(self):
        #GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        self.program.bind()

        self.program.enableAttributeArray("position")
        self.program.enableAttributeArray("texcoord")
        self.program.setAttributeArray("position", self.quadCoord)
        self.program.setAttributeArray("texcoord", self.quadCoordTex)

        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        self.program.setUniformValue("texture", 0)

        GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(self.quadCoord))
Пример #20
0
class MyWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)

        self.quadCoord = np.array([[-1., -1., 0.], [1., -1., 0.], [1., 1., 0.],
                                   [1., 1., 0.], [-1., 1., 0.], [-1., -1.,
                                                                 0.]])

        self.quadCoordTex = np.array([[0, 0], [1., 0.], [1., 1.], [1., 1.],
                                      [0, 1.], [0, 0]])

    def initializeGL(self):
        GL.glClearColor(1.0, 0.0, 0.0, 1.0)
        GL.glEnable(GL.GL_BLEND)
        GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)

        print("OpenGL.GL: " + str(GL.glGetString(GL.GL_VERSION)))
        print("GL.GLSL: " +
              str(GL.glGetString(GL.GL_SHADING_LANGUAGE_VERSION)))
        print("OpenGL ATTRIBUTES:\n",
              ", ".join(d for d in dir(GL) if d.startswith("GL_")))

        self.program = QOpenGLShaderProgram()
        self.program.addShaderFromSourceCode(
            QOpenGLShader.Vertex, """#version 120
        attribute vec2 position;
        attribute vec2 texcoord;
        varying vec2 mytexcoord;
        void main() {
            gl_Position = vec4(position, 0., 1.0);
            mytexcoord = texcoord;
        }""")

        self.program.addShaderFromSourceCode(
            QOpenGLShader.Fragment, """#version 120
        uniform sampler2D texture;
        varying vec2 mytexcoord;
        void main() {
            
            gl_FragColor = texture2D(texture,mytexcoord);
        }""")
        print(self.program.log())
        self.program.link()

        self.texture = fillTexture2d(
            np.outer(np.linspace(0, 1, 128), np.ones(128)))

    def paintGL(self):
        #GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        self.program.bind()

        self.program.enableAttributeArray("position")
        self.program.enableAttributeArray("texcoord")
        self.program.setAttributeArray("position", self.quadCoord)
        self.program.setAttributeArray("texcoord", self.quadCoordTex)

        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        self.program.setUniformValue("texture", 0)

        GL.glDrawArrays(GL.GL_TRIANGLES, 0, len(self.quadCoord))
Пример #21
0
class TestCanvas(Canvas):
    def __init__(self):

        with open("vertex.glsl", "r") as f:
            self.vertexShaderSource = f.read()

        with open("fragment.glsl", "r") as f:
            self.fragmentShaderSource = f.read()

        if self.vertexShaderSource is None:
            raise Exception("Could not load the source for the vertex shader")

        if self.fragmentShaderSource is None:
            raise Exception(
                "Could not load the source for the fragment shader")

        super(TestCanvas, self).__init__()

    def setupGL(self):

        self.program = QOpenGLShaderProgram(self.context())
        info("PROGRAM", self.program)

        # addShaderFromSourceCode() only returns a bool telling whether everything went well

        if self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                self.vertexShaderSource):
            info("VERTEX SHADER",
                 "Managed to load and parse the vertex shader")

        if self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                self.fragmentShaderSource):
            info("FRAGMENT SHADER",
                 "Managed to load and parse the fragment shader")

        # Compile and bind the shader program to the current context
        self.program.link()
        self.program.bind()

        # Bind the shader attribute to a python variable
        self.somePosition = self.gl.glGetAttribLocation(
            self.program.programId(), "somePosition")

        # Use array rather than list in order to get control over actual data size
        self.vertices = array.array(
            'f',
            [-0.5, -0.5, 0.0, 0.5, 0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.0, 0.0])

        # Buffer info returns a tuple where the second part is number of elements in the array
        self.verticesLength = self.vertices.buffer_info()[1]

        # Size in bytes for each element
        self.verticesItemSize = self.vertices.itemsize

        # Total size in bytes for entire array
        self.verticesDataLength = self.verticesLength * self.verticesItemSize

        # Number of vertices in the array
        self.numberOfVertices = int(self.verticesLength / 3)

        # Tell gl to prepare a buffer
        self.vertexBuffer = self.gl.glGenBuffers(1)

        # Say that the buffer is of type GL_ARRAY_BUFFER
        self.gl.glBindBuffer(self.gl.GL_ARRAY_BUFFER, self.vertexBuffer)

        # Copy data into the buffer
        self.gl.glBufferData(self.gl.GL_ARRAY_BUFFER, self.verticesDataLength,
                             self.vertices.tobytes(), self.gl.GL_STATIC_DRAW)

        # Say that the somePosition attribute should be read from the current array, that it should take three values at a
        # time (x, y, z), and that they are of the type GL_FLOAT
        self.gl.glVertexAttribPointer(self.somePosition, 3, self.gl.GL_FLOAT,
                                      False, 0, 0)

        # When reading from the current data, start at position 0
        self.gl.glEnableVertexAttribArray(0)

        # Tell GL that whenever we run glClear, this is the color that
        # should be used. We only need to do this once.
        self.gl.glClearColor(0.1, 0.1, 0.1, 1.0)

        self.dumpGLLogMessages("setupGL()")

    def paintGL(self):
        self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT
                        | self.gl.GL_DEPTH_BUFFER_BIT)

        # Draw points, starting at vertex number, number of vertices to draw. Since we enabled attribarrays, it will
        # fetch data from the array buffer
        self.gl.glDrawArrays(self.gl.GL_POINTS, 0, self.numberOfVertices)

        self.dumpGLLogMessages("paintGL()")

    def resizeGL(self, width, height):
        pass
Пример #22
0
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(QOpenGLShader.Vertex,
                                               self.vertexShaderSource)
        self.m_program.addShaderFromSourceCode(QOpenGLShader.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_matrixUniform, 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
Пример #23
0
class DrawingWindow(GLStandardDrawingWindow3D):

    vertexShaderSource = """
#version 120\n
uniform highp mat4 projectionMatrix;
uniform highp mat4 modelViewMatrix;
uniform highp mat4 normalMatrix; 

attribute highp vec4 vertexAtr; //.posAttr
attribute vec3 normalAttr; // .normals

varying vec3 normal;
varying vec4 vertex;

void main() 
{
    gl_Position =  projectionMatrix * modelViewMatrix * vertexAtr;
    
    normal = normalAttr;
    vertex = vertexAtr;
}

"""

    fragmentShaderSource = """
#version 120\n

uniform highp mat4 projectionMatrix;
uniform highp mat4 modelViewMatrix;
uniform highp mat4 normalMatrix; 


varying vec3 normal;
varying vec4 vertex;

const vec3 lightPos = vec3(0.0, 20.0, 0.0);
const vec3 lightColor = vec3(1.0, 1.0, 1.0);

const vec3 ambientColor = vec3(0.3, 0.0, 0.3);
const vec3 diffuseColor = vec3(0.5, 0.5, 0.5);
const vec3 specularColor = vec3(1.0, 1.0, 1.0);
const vec3 emitColor = vec3(0.5, 0.0, 0.5);

const float shininess = 160.0;

const vec3 eyePos = vec3 (0,0,0);


void main() 
{
    vec4 tempVertex = modelViewMatrix * vertex;
    vec3 vrtx = tempVertex.xyz / tempVertex.w;

    vec4 tempNormal = normalMatrix * vec4(normal,0.0);
    vec3 nrml =  normalize(tempNormal.xyz);



    vec3 eyeDir = normalize(eyePos - vrtx); // V
    vec3 lightDir = normalize(lightPos - vrtx); //L
    vec3 halfVector = normalize(lightDir + eyeDir); //H

    //DIFFUSE 
    float NdotL = dot(nrml, lightDir );
    vec3 lambert = NdotL * diffuseColor * lightColor * max(NdotL ,0.0);

    //SPECULAR
    float NdotH = dot(nrml, halfVector);
    vec3  blinnPhong = lightColor * specularColor * pow( max(NdotH,0.0 ), shininess );


    gl_FragColor = vec4(lambert + blinnPhong + ambientColor , 1.0);
}
"""

    def __init__(self):
        super(DrawingWindow, self).__init__()
        self.history = []
        self.userDefinedPoints = []
        self.setMouseTracking(True)

        #SURFACE setup
        self.ctrPnt = [[
            QVector3D(10, 0, 10),
            QVector3D(5, 0, 10),
            QVector3D(-5, 0, 10),
            QVector3D(-10, 0, 10)
        ],
                       [
                           QVector3D(10, 0, 5),
                           QVector3D(5, 10, 5),
                           QVector3D(-5, 10, 5),
                           QVector3D(-10, 0, 5)
                       ],
                       [
                           QVector3D(10, 0, -5),
                           QVector3D(5, 10, -5),
                           QVector3D(-5, 10, -5),
                           QVector3D(-10, 0, -5)
                       ],
                       [
                           QVector3D(10, 0, -10),
                           QVector3D(5, 0, -10),
                           QVector3D(-5, 0, -10),
                           QVector3D(-10, 0, -10)
                       ]]

        self.vtr = []
        self.vertices = []
        self.indices = []

        self.controlPoints = []
        self.rotationAxis = None

        for row in self.ctrPnt:
            for vector in row:
                self.controlPoints.append(float(vector.x()))
                self.controlPoints.append(float(vector.y()))
                self.controlPoints.append(float(vector.z()))

        self.surface = NURBSSurface(controlNet=self.ctrPnt,
                                    Udegree=3,
                                    Wdegree=3,
                                    Uknots=[0, 0, 0, 0, 1, 1, 1, 1],
                                    Wknots=[0, 0, 0, 0, 1, 1, 1, 1])

        self.surface.setWeights([[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0],
                                 [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]])
        self.surface.compute()
        self.vtr = self.surface.surfacePoints

        self.indices = triangleRenderer(row=12, col=11)
        normalsList = normalsPerTriangle(self.vtr, self.indices)
        self.surfaceNormals = []
        self.normals = normalsPerVertex(normalsList, len(self.vtr))

        for value in self.vtr:
            self.vertices.append(float(value.x()))
            self.vertices.append(float(value.y()))
            self.vertices.append(float(value.z()))

        for value in self.normals:
            self.surfaceNormals.append(float(value.x()))
            self.surfaceNormals.append(float(value.y()))
            self.surfaceNormals.append(float(value.z()))

        #QTsetting
        glFormat = QGLFormat()
        glFormat.setVersion(3, 2)
        glFormat.setProfile(QGLFormat.CoreProfile)
        QGLFormat.setDefaultFormat(glFormat)
        print(glFormat)

        #camera
        self.camera = Camera(position=QVector3D(0, 7, 20),
                             direction=QVector3D(0, 0, 0),
                             up=QVector3D(0, 1, 0))

        self.normalMatrix = QMatrix4x4()

        #interaction
        self.selectedPoint = None
        self.editFlag = False
        self.showControlPoints = False

    def initializeGL(self):
        GL.glClearColor(0.15, 0.15, 0.15, 1.0)
        GL.glEnable(GL.GL_DEPTH_TEST)
        GL.glEnable(GL.GL_LIGHT0)
        GL.glEnable(GL.GL_LIGHTING)

        self.program = QOpenGLShaderProgram(self)
        self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                             self.vertexShaderSource)
        self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                             self.fragmentShaderSource)
        self.program.link()
        self.xRot = 0
        self.yRot = 0
        self.zRot = 0

        self.vertexAtr = self.program.attributeLocation("vertexAtr")
        self.colAttr = self.program.attributeLocation("colAttr")

        self.projectionMatrixAttr = self.program.uniformLocation(
            "projectionMatrix")
        self.modelViewMatrixAttr = self.program.uniformLocation(
            "modelViewMatrix")
        self.normalMatrixAttr = self.program.uniformLocation("normalMatrix")

        self.normalAttr = self.program.attributeLocation("normalAttr")

        self.controlPtsAttr = self.program.attributeLocation("controlPtsAttr")
        self.pressClick = QVector3D(0, 0, 0)
        self.releaseClick = QVector3D(0, 0, 0)
        self.programid = 0

    def paintGL(self):
        '''
        paintGL
        Updates the current object that is being drawn in screen.
        Draws whatever is on the history stack as a set of Vector3Ds
        '''
        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        GL.glViewport(0, 0, self.width, self.height)
        GL.glMatrixMode(GL.GL_PROJECTION)

        ratio = self.width / self.height
        self.camera.setPerspective(self.fov, ratio, 1.0, 50.0)

        self.camera.lookAtCenter()
        self.camera.rotate(self.xRot, 0, 0)
        self.camera.rotate(0, self.yRot, 0)
        self.camera.rotate(0, 0, self.zRot)

        self.normalMatrix = self.camera.modelViewMatrix.inverted(
        )[0].transposed()

        self.program.bind()
        self.program.setUniformValue('modelViewMatrix',
                                     self.camera.modelViewMatrix)
        self.program.setUniformValue('normalMatrix', self.normalMatrix)
        self.program.setUniformValue('projectionMatrix',
                                     self.camera.projectionMatrix)

        GL.glPointSize(5)

        GL.glEnableVertexAttribArray(self.vertexAtr)

        # GL.glPolygonMode( GL.GL_FRONT_AND_BACK ,GL.GL_LINE)
        GL.glVertexAttribPointer(self.vertexAtr, 3, GL.GL_FLOAT, GL.GL_FALSE,
                                 0, self.vertices)
        GL.glDrawElements(GL.GL_TRIANGLE_STRIP, len(self.indices),
                          GL_UNSIGNED_BYTE, self.indices)

        GL.glVertexAttribPointer(self.vertexAtr, 3, GL_FLOAT, GL_FALSE, 0,
                                 self.controlPoints)
        GL.glEnableVertexAttribArray(self.vertexAtr)
        GL.glDrawArrays(GL.GL_LINE_LOOP, 0, (len(self.controlPoints) // 3))

        GL.glVertexAttribPointer(self.vertexAtr, 3, GL_FLOAT, GL_FALSE, 0,
                                 self.controlPoints)
        GL.glEnableVertexAttribArray(self.vertexAtr)
        GL.glDrawArrays(GL.GL_POINTS, 0, (len(self.controlPoints) // 3))

        GL.glEnableVertexAttribArray(self.normalAttr)
        GL.glVertexAttribPointer(self.normalAttr, 3, GL_FLOAT, GL_FALSE, 0,
                                 self.surfaceNormals)

        # GL.glDisableVertexAttribArray(self.colAttr)
        GL.glDisableVertexAttribArray(self.vertexAtr)
        # GL.glDisableVertexAttribArray(self.normalAttr)

        self.program.release()

    def mousePressEvent(self, event):
        if self.editFlag == False:
            self.pressClick = QVector3D(event.x(), self.height - event.y(), 1)
        else:
            begin, end = self.camera.mouseRay(event.x(),
                                              self.height - event.y(),
                                              self.width, self.height)
            t = 0.0
            ray = begin
            while t < 100.0:
                ray = begin + t * (end - begin)
                for row in self.ctrPnt:
                    self.selectedPoint = rayCollision(ray, row, 0.5)
                    if self.selectedPoint:
                        print('collision at {}'.format(self.selectedPoint))
                        break
                else:
                    t += 0.1
                    continue
                break

    def mouseMoveEvent(self, event):
        if self.editFlag == False:
            sensitivity = 0.05
            if event.buttons() == Qt.LeftButton:
                self.releaseClick = QVector3D(event.x(),
                                              self.height - event.y(), 1)

                angle = angleBetweenTwoVectors(self.pressClick,
                                               self.releaseClick)

                if self.rotationAxis is axis.x:
                    if self.pressClick.y() < self.releaseClick.y():
                        angle *= -1
                    self.xRot += angle * sensitivity
                    self.xRot %= 360

                elif self.rotationAxis is axis.z:
                    if self.pressClick.x() < self.releaseClick.x():
                        angle *= -1
                    self.zRot += angle * sensitivity
                    self.zRot %= 360

                elif self.rotationAxis is axis.y:
                    if self.pressClick.x() < self.releaseClick.x():
                        angle *= -1
                    self.yRot += angle * sensitivity
                    self.yRot %= 360
        else:
            if self.selectedPoint and event.buttons() == QtCore.Qt.LeftButton:
                one, two = self.camera.mouseRay(event.x(),
                                                self.height - event.y(),
                                                self.width, self.height)
                two = QVector3D(two.x(), two.y(), two.z()) / two.w()
                self.selectedPoint += (two - self.selectedPoint)
                print(self.selectedPoint)
                self._updateControlPoints()
                self._updateSurface()
        self.update()

    def mouseReleaseEvent(self, event):
        self.pressClick = self.releaseClick

        if self.editFlag == True:
            self.pressClick = QVector3D(0, 0, 1)
            self.releaseClick = QVector3D(0, 0, 1)
            if self.selectedPoint:
                self.selectedPoint = None
        self.update()

    def setRotationAxis(self, rotationAxis=None):
        self.rotationAxis = rotationAxis

    def setEditFlag(self, status=None):
        self.editFlag = status
        self.selectedPoint = None

    def setContontrolPoinsFalg(self, status=None):
        self.showControlPoints = status

    def _updateControlPoints(self):
        for row in self.ctrPnt:
            i = 0
            for vector in row:
                self.controlPoints[i * 3] = (float(vector.x()))  # 0,3,6
                self.controlPoints[(i * 3) + 1] = (float(vector.y()))  # 1,4,7
                self.controlPoints[(i * 3) + 2] = (float(vector.z()))  # 2,5,8
                i += 1

    def _updateSurface(self):
        self.vtr = []
        self.vertices = []
        self.surfaceNormals = []
        self.normals = []
        self.surface.compute()
        self.vtr = self.surface.surfacePoints

        self.indices = triangleRenderer(row=12, col=11)
        normalsList = normalsPerTriangle(self.vtr, self.indices)
        self.normals = normalsPerVertex(normalsList, len(self.vtr))

        for value in self.vtr:
            self.vertices.append(float(value.x()))
            self.vertices.append(float(value.y()))
            self.vertices.append(float(value.z()))

        for value in self.normals:
            self.surfaceNormals.append(float(value.x()))
            self.surfaceNormals.append(float(value.y()))
            self.surfaceNormals.append(float(value.z()))
        pass
class GaussianIntensityMaskRenderer(QObject):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.framebuffer = None
        self.shader_program = QOpenGLShaderProgram()
        self.texture = None
        self.vertex_attribute_object = None

        x0 = -1
        y0 = -1
        width = 2

        self.vertex_data = np.array([
            x0, y0, 0.0,                    0.0, 0.0,
            x0 + width, y0, 0.0,            1.0, 0.0,
            x0 + width, y0 + width, 0.0,    1.0, 1.0,

            x0 + width, y0 + width, 0.0,    1.0, 1.0,
            x0, y0 + width, 0.0,            0.0, 1.0,
            x0, y0, 0.0,                    0.0, 0.0
        ], dtype=np.float32)

        self.initialised = False

    def draw(self):
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.framebuffer)
        self.shader_program.bind()
        GL.glBindVertexArray(self.vertex_attribute_object)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.shader_program.release()

    def initialise_gl(self):
        self.initialise_shader()
        self.initialise_texture()
        self.initialise_framebuffer()
        self.initialised = True

    def initialise_framebuffer(self):
        self.framebuffer = GL.glGenFramebuffers(1)
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.framebuffer)
        GL.glFramebufferTexture(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT1, self.framebuffer, 0)

    def initialise_shader(self):
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, VERTEX_INTENSITY)
        self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, FRAGMENT_INTENSITY)

        if not self.shader_program.link():
            raise Exception("Could not link shaders - {}".format(self.shader_program.log()))

        self.vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes, self.vertex_data,
                        GL.GL_STATIC_DRAW)

        self.vertex_position = self.shader_program.attributeLocation("vertexPosition")
        self.texture_coords = self.shader_program.attributeLocation("vertexTexCoords")
        self.texture_framebuffer = self.shader_program.uniformLocation("texture_framebuffer")

        self.centre_x_uniform = self.shader_program.uniformLocation("centre_x")
        self.centre_y_uniform = self.shader_program.uniformLocation("centre_y")
        self.height_uniform = self.shader_program.uniformLocation("height")
        self.rotation_uniform = self.shader_program.uniformLocation("rotation")
        self.max_height_uniform = self.shader_program.uniformLocation("max_height")
        self.shape_uniform = self.shader_program.uniformLocation("shape")
        self.width_x_uniform = self.shader_program.uniformLocation("width_x")
        self.width_y_uniform = self.shader_program.uniformLocation("width_y")

        GL.glEnableVertexAttribArray(0)
        GL.glEnableVertexAttribArray(1)
        GL.glVertexAttribPointer(self.vertex_position, 3, GL.GL_FLOAT, GL.GL_FALSE, 20,
                                 None)

        GL.glVertexAttribPointer(self.texture_coords, 2, GL.GL_FLOAT, GL.GL_TRUE, 20,
                                 ctypes.c_void_p(12))
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

    def initialise_texture(self):
        self.texture = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.texture)
        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.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, self.parent().width(), self.parent().height(), 0,
                        GL.GL_RGB, GL.GL_UNSIGNED_BYTE, None)
Пример #25
0
class OpenGLWidget(QOpenGLWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.scene_shader_program = QOpenGLShaderProgram()
        self.screen_shader_program = QOpenGLShaderProgram()
        x0 = -0.5
        y0 = -0.5
        width = 1

        self.vertex_data = np.array([
            x0, y0, 0.0,
            x0 + width, y0, 0.0,
            x0 + width, y0 + width, 0.0,

            x0 + width, y0 + width, 0.0,
            x0, y0 + width, 0.0,
            x0, y0, 0.0,
        ], dtype=np.float32)

    def create_render_target(self):
        render_texture = GL.glGenTextures(1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, render_texture)
        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, self.width(), self.height(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, None)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST)
        GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST)

        fbo = GL.glGenFramebuffers(1)
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, fbo)

        GL.glFramebufferTexture(GL.GL_FRAMEBUFFER, GL.GL_COLOR_ATTACHMENT0, render_texture, 0)

        if GL.glCheckFramebufferStatus(GL.GL_FRAMEBUFFER) != GL.GL_FRAMEBUFFER_COMPLETE:
            raise Exception("Framebuffer creation failed")

        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0)
        return fbo, render_texture

    def create_screen_shader_program(self):
        self.screen_shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, VERTEX_SCREEN)
        self.screen_shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, FRAGMENT_SCREEN)

        if not self.screen_shader_program.link():
            raise Exception("Could not link shaders - {}".format(self.screen_shader_program.log()))

        self.screen_vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.screen_vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes, self.vertex_data,
                        GL.GL_STATIC_DRAW)

        self.screen_vertex_position = self.screen_shader_program.attributeLocation("vertexPosition")
        self.screen_texture_coords = self.screen_shader_program.attributeLocation("texture_coords")
        self.texture_framebuffer = self.screen_shader_program.uniformLocation("texture_framebuffer")

        GL.glVertexAttribPointer(self.screen_vertex_position, 3, GL.GL_FLOAT, GL.GL_FALSE, 0,
                                 None)

        GL.glEnableVertexAttribArray(0)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

        self.fbo, self.screen_texture = self.create_render_target()

    def create_scene_shader_program(self):
        self.scene_shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, VERTEX)
        self.scene_shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, FRAGMENT)

        if not self.scene_shader_program.link():
            raise Exception("Could not link shaders - {}".format(self.scene_shader_program.log()))

        self.scene_vertex_attribute_object = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self.scene_vertex_attribute_object)

        vertex_buffer_object = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer_object)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, self.vertex_data.nbytes, self.vertex_data,
                        GL.GL_STATIC_DRAW)

        self.scene_vertex_position = self.scene_shader_program.attributeLocation("vertexPosition")
        assert self.scene_vertex_position != -1
        # self.texture_coords = self.shader_program.attributeLocation("texture_coords")
        # self.texture_framebuffer = self.shader_program.uniformLocation("texture_framebuffer")

        GL.glVertexAttribPointer(self.scene_vertex_position, 3, GL.GL_FLOAT, GL.GL_FALSE, 0,
                                 None)

        GL.glEnableVertexAttribArray(0)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
        GL.glBindVertexArray(0)

       # self.scene_texture = self.texture_from_image(imread("image-2018-01-31_15-40-12.png"))


    def create_texture(self):
       # GL.glActiveTexture(GL.GL_TEXTURE0)

        texture = GL.glGenTextures(1)

       # GL.glActiveTexture(GL.GL_TEXTURE0)
        #GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
        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_MIPMAP_LINEAR)

        #GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_BORDER)
        #GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_BORDER)

        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, self.width(), self.height(), 0,
                        GL.GL_RGB, GL.GL_UNSIGNED_BYTE, None)
       # GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        return texture

    def initializeGL(self):
        print("Create scene shader")
        self.create_scene_shader_program()
        print("Create screen shader")
        self.create_screen_shader_program()
        print("InitGL complete")
       # self.create_render_target()

    def paintGL(self):
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.fbo)
        self.paint_quad()
        GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.defaultFramebufferObject())

        GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        self.paint_to_screen()

    def paint_to_screen(self):
        print("Paint to screen")
        self.screen_shader_program.bind()
        GL.glBindVertexArray(self.screen_vertex_attribute_object)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glBindTexture(GL.GL_TEXTURE_2D, self.screen_texture)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.screen_shader_program.release()
        print("Paint to screen complete")
       # GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, 0)

    def paint_quad(self):
        self.scene_shader_program.bind()
       # GL.glActiveTexture(GL.GL_TEXTURE0)
        #GL.glBindTexture(GL.GL_TEXTURE_2D, self.scene_texture)
        GL.glBindVertexArray(self.scene_vertex_attribute_object)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
        GL.glBindVertexArray(0)
        self.scene_shader_program.release()

    def texture_from_image(self, image):
        img_data = np.flipud(image)
        height, width = img_data.shape
        texture = GL.glGenTextures(1)
        GL.glActiveTexture(GL.GL_TEXTURE0)
        GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1)
        GL.glBindTexture(GL.GL_TEXTURE_2D, texture)
        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_MIPMAP_LINEAR)

        GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_BORDER)
        GL.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_BORDER)

        GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, width, height, 0,
                        GL.GL_LUMINANCE, GL.GL_UNSIGNED_BYTE, img_data)
        GL.glGenerateMipmap(GL.GL_TEXTURE_2D)
        GL.glBindTexture(GL.GL_TEXTURE_2D, 0)
        return texture
Пример #26
0
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()
Пример #27
0
class GLSliceWidget(QtOpenGL.QGLWidget):
    _dataModelChanged = QtCore.pyqtSignal()

    def __init__(self, parent=None, interpolation="linear", **kwargs):
        logger.debug("init")

        super(GLSliceWidget, self).__init__(parent, **kwargs)

        self.renderUpdate = True
        self.parent = parent

        self.setAcceptDrops(True)

        self.texture_LUT = None
        self.setTransform(TransformModel())

        self.renderTimer = QtCore.QTimer(self)
        self.renderTimer.setInterval(50)
        self.renderTimer.timeout.connect(self.onRenderTimer)
        self.renderTimer.start()

        self.dataModel = None

        self.dataPos = 0
        self.slicePos = 0

        self.zoom_fac = 1.
        self.zoom_x = 0.5
        self.zoom_y = 0.5
        # the center in tex coords
        self.zoom_cx = 0.5
        self.zoom_cy = 0.5

        interpolation_vals = ("linear", "nearest")
        if not interpolation in interpolation_vals:
            raise KeyError("interpolation = '%s' not defined ,valid: %s" % (interpolation, str(interpolation_vals)))

        self.interp = (interpolation =="linear")
        # self.refresh()

    def setModel(self, dataModel):
        logger.debug("setModel")

        self.dataModel = dataModel

        if self.dataModel:
            self.transform.setModel(dataModel)

            self.dataModel._dataSourceChanged.connect(self.dataSourceChanged)
            self.dataModel._dataPosChanged.connect(self.dataPosChanged)
            self._dataModelChanged.connect(self.dataModelChanged)
            self._dataModelChanged.emit()

    def setTransform(self, transform):
        self.transform = transform
        self.transform._transformChanged.connect(self.refresh)

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        for url in event.mimeData().urls():
            path = url.toLocalFile().toLocal8Bit().data()
            if self.dataModel:
                self.dataModel.loadFromPath(path, prefetchSize=self.N_PREFETCH)
            else:
                self.setModel(DataModel.fromPath(path, prefetchSize=self.N_PREFETCH))

    def set_colormap(self, name):
        """arr should be of shape (N,3) and gives the rgb components of the colormap"""

        try:
            arr = spimagine.config.__COLORMAPDICT__[name]
            self.makeCurrent()
            self.texture_LUT = fillTexture2d(arr.reshape((1,) + arr.shape), self.texture_LUT, self.interp)
        except:
            print("could not load colormap %s" % name)

    def set_colormap_rgb(self, color=[1., 1., 1.]):
        self._set_colormap_array(outer(linspace(0, 1., 255), np.array(color)))

    def _set_colormap_array(self, arr):
        """arr should be of shape (N,3) and gives the rgb components of the colormap"""

        self.makeCurrent()
        self.texture_LUT = fillTexture2d(arr.reshape((1,) + arr.shape), self.texture_LUT, self.interp)
        self.refresh()

    def initializeGL(self):

        self.resized = True

        self.output = zeros((100, 100))

        logger.debug("initializeGL")

        self.programTex = QOpenGLShaderProgram()
        self.programTex.addShaderFromSourceCode(QOpenGLShader.Vertex, vertShaderTex)
        self.programTex.addShaderFromSourceCode(QOpenGLShader.Fragment, fragShaderTex)
        self.programTex.link()
        self.programTex.bind()
        logger.debug("GLSL programTex log:%s", self.programTex.log())

        glClearColor(0, 0, 0, 1.)

        self.texture = None

        self.quadCoord = np.array([[-1., -1., 0.],
                                   [1., -1., 0.],
                                   [1., 1., 0.],
                                   [1., 1., 0.],
                                   [-1., 1., 0.],
                                   [-1., -1., 0.]])

        self.quadCoordTex = np.array([[0, 0],
                                      [1., 0.],
                                      [1., 1.],
                                      [1., 1.],
                                      [0, 1.],
                                      [0, 0]])

        self.set_colormap(spimagine.config.__DEFAULTCOLORMAP__)

        glDisable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    def dataModelChanged(self):
        # if self.dataModel:
        #     self.transform.reset(amin(self.dataModel[0]),
        #                          amax(self.dataModel[0]),
        #                          self.dataModel.stackUnits())
        #

        self.refresh()

    def dataSourceChanged(self):
        # self.transform.reset(amin(self.dataModel[0]),
        #                      amax(self.dataModel[0]),
        #                      self.dataModel.stackUnits())
        self.refresh()

    def dataPosChanged(self, pos):
        self.dataPos = pos
        self.refresh()

    def refresh(self):
        # if self.parentWidget() and self.dataModel:
        #     self.parentWidget().setWindowTitle("SpImagine %s"%self.dataModel.name())
        self.renderUpdate = True

    def resizeGL(self, width, height):
        # height = max(10,height)

        self._viewport_width, self._viewport_height = width, height

        self.resized = True
        self.resetViewPort()

    def getDataWidthHeight(self):
        if not self.dataModel:
            return 1, 1

        dim = array(self.dataModel.size()[1:])[::-1].astype(np.float32)

        dim *= array(self.transform.stackUnits)

        if self.transform.sliceDim == 0:
            dim = dim[[2, 1]]
        elif self.transform.sliceDim == 1:
            dim = dim[[0, 2]]
        elif self.transform.sliceDim == 2:
            dim = dim[[0, 1]]

        w, h = dim[0], dim[1]

        fac = min(1. * self._viewport_width / w, 1. * self._viewport_height / h)

        return int(fac * w), int(fac * h)

    def resetViewPort(self):
        w, h = self.getDataWidthHeight()
        glViewport((self._viewport_width - w) // 2, (self._viewport_height - h) // 2, w, h)

    def paintGL(self):

        self.makeCurrent()

        if not glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE:
            return

        # hack
        if self.resized:
            self.resetViewPort()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        if self.dataModel:
            # Draw the render texture
            self.programTex.bind()

            self.texture = fillTexture2d(self.output, self.texture, self.interp)

            glEnable(GL_TEXTURE_2D)
            glDisable(GL_DEPTH_TEST)

            self.programTex.enableAttributeArray("position")
            self.programTex.enableAttributeArray("texcoord")
            self.programTex.setAttributeArray("position", self.quadCoord)
            self.programTex.setAttributeArray("texcoord",
                                              self.tex_coords_from_xyzoom(self.zoom_x, self.zoom_y, self.zoom_fac))

            glActiveTexture(GL_TEXTURE0)
            glBindTexture(GL_TEXTURE_2D, self.texture)
            self.programTex.setUniformValue("texture", 0)

            glActiveTexture(GL_TEXTURE1)
            glBindTexture(GL_TEXTURE_2D, self.texture_LUT)
            self.programTex.setUniformValue("texture_LUT", 1)

            glDrawArrays(GL_TRIANGLES, 0, len(self.quadCoord))

    def render(self):
        logger.debug("render")
        if self.dataModel:
            if self.transform.sliceDim == 0:
                out = fliplr(self.dataModel[self.transform.dataPos][:, :, self.transform.slicePos].T)
            elif self.transform.sliceDim == 1:
                out = self.dataModel[self.transform.dataPos][:, self.transform.slicePos, :]
            elif self.transform.sliceDim == 2:
                out = self.dataModel[self.transform.dataPos][self.transform.slicePos, :, :]

            min_out, max_out = self.transform.minVal, self.transform.maxVal

            if max_out > min_out:
                self.output = np.maximum(0,(1. * (out - min_out) / (max_out - min_out))) ** self.transform.gamma
            else:
                self.output = np.zeros_like(out)

            logger.debug("render: output range = %s" % ([amin(self.output), amax(self.output)]))

    def saveFrame(self, fName):
        """FIXME: scaling behaviour still hast to be implemented (e.g. after setGamma)"""
        logger.info("saving frame as %s", fName)

        self.render()
        self.paintGL()
        glFlush()
        self.grabFrameBuffer().save(fName)

    def onRenderTimer(self):
        if self.renderUpdate:
            self.render()
            self.renderUpdate = False
            self.updateGL()

    def tex_coords_from_xyzoom(self, x0, y0, zoom):
        """returns array of texccords corners when zoomed
        in onto x0,y0 \in [0,1]
        
        zoom == 1   --> fully zoomed out
        zoom == 0   --> fully zoomed in (to x0,y0)
        """

        q0 = create_quad_coords([0, 1, 0, 1])
        q1 = create_quad_coords([x0, x0, y0, y0])

        return zoom * q0 + (1. - zoom) * q1

    def getRelativeCoords(self, x0, y0):
        w, h = self.getDataWidthHeight()

        w = w * self.width() / self._viewport_width
        h = w * self.height() / self._viewport_height

        x = 2. * (x0 - .5 * (self.width() - w)) / w - 1
        y = 2. * (y0 - .5 * (self.height() - h)) / h - 1

        x = (x0 - .5 * (self.width() - w)) / w
        y = 1 - (y0 - .5 * (self.height() - h)) / h

        return x, y

    def mousePressEvent(self, event):
        super(GLSliceWidget, self).mousePressEvent(event)

        if event.buttons() == QtCore.Qt.LeftButton:
            self._x0, self._y0 = self.getRelativeCoords(event.x(), event.y())

    def mouseMoveEvent(self, event):

        if event.buttons() == QtCore.Qt.LeftButton:
            x, y = self.getRelativeCoords(event.x(), event.y())
            self.zoom_x += self.zoom_fac * (self._x0 - x)
            self.zoom_y += self.zoom_fac * (self._y0 - y)
            self._x0, self._y0 = x, y
            self.zoom_x, self.zoom_y = clip(self.zoom_x, 0, 1), clip(self.zoom_y, 0, 1)

        self.refresh()

    def wheelEvent(self, event):
        # get the zoom factor

        # print self.zoom_x,self.zoom_y
        # x, y = self.getRelativeCoords(event.x(),event.y())
        # self.zoom_x , self.zoom_y = clip(x ,0,1), clip(y,0,1)



        self.zoom_fac *= 1.4 ** (-event.angleDelta().y() / 1000.)
        self.zoom_fac = clip(self.zoom_fac, 0, 1.)

        self.refresh()