コード例 #1
0
class TGLViewport(QOpenGLWidget, QOpenGLFunctions):
    def __init__(self, parent=None):
        QOpenGLWidget.__init__(self, parent)
        QOpenGLFunctions.__init__(self)
        self.setMinimumSize(32, 32)

        self.info = ""
        self._supported_images = [
            "TGA", "PNG", "JPG", "JPEG", "TIF", "TIFF", "BMP", "DDS"
        ]

        # indices
        indices = [0, 1, 3, 1, 2, 3]
        self._indices = array('I', indices)

        # vertices
        # 3 position | 2 texture coord
        vertex = [
            1.0,
            1.0,
            0.0,
            1.0,
            1.0,  # top right
            1.0,
            -1.0,
            0.0,
            1.0,
            0.0,  # bottom right
            -1.0,
            -1.0,
            0.0,
            0.0,
            0.0,  # bottom left
            -1.0,
            1.0,
            0.0,
            0.0,
            1.0  # top left
        ]
        self._vertex = array('f', vertex)

        # opengl data related
        self._program = QOpenGLShaderProgram()
        self._program_bg = QOpenGLShaderProgram()
        self._vao = QOpenGLVertexArrayObject()
        self._vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
        self._texture = None
        self._texture_size = (1, 1)
        self._location = ()

        self._colors_default = (QColor.fromRgbF(0.65, 0.65, 0.65, 1.0),
                                QColor.fromRgbF(0.90, 0.90, 0.90, 1.0))
        self._u_colors = self._colors_default
        self._height = QVector4D(0, self.height(), 0, 0)

        self._u_channels = QMatrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
                                      0, 0)

    def initializeGL(self):
        # Set up the rendering context, define display lists etc.
        self.initializeOpenGLFunctions()
        self.glClearColor(0.2, 0.0, 0.2, 0.0)
        self.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)

        # shader code (OpenGL ES)
        # texture
        vs_source_es = """
		attribute highp vec3 Pos;
		attribute highp vec2 UV;
		uniform highp vec2 Scale;
		varying highp vec2 oUV;
		void main() {
			gl_Position = vec4(Pos * vec3(Scale, 1.0), 1.0);
			oUV = UV;
		}
		"""
        ps_source_es = """
		varying highp vec2 oUV;
		uniform sampler2D Texture;
		uniform highp mat4 Channels;
		uniform highp vec4 Add;
		void main() {
			gl_FragColor = texture2D(Texture, oUV * vec2(1.0, -1.0)) * Channels;
			gl_FragColor.a += (1.0 - Channels[3][3]);
		}
		"""
        # background
        vs_grid_es = """
		attribute highp vec3 Pos;
		void main() {
			gl_Position = vec4(Pos, 1.0);
		}
		"""
        ps_grid_es = """
		uniform highp vec4 Color1;
		uniform highp vec4 Color2;
		uniform highp vec4 Height;
		void main() {
			highp vec2 a = floor((Height.xy - gl_FragCoord.xy) / 64.0);
			highp float even = mod(a.x + a.y, 2.0);
			highp vec3 c = mix(Color1.rgb, Color2.rgb, even);
			gl_FragColor = vec4(c, 1);
		}
		"""

        # program - texture
        # shader
        vs = self.__create_shader(QOpenGLShader.Vertex, vs_source_es)
        fs = self.__create_shader(QOpenGLShader.Fragment, ps_source_es)
        # program
        self._program = QOpenGLShaderProgram(self.context())
        self._program.addShader(vs)
        self._program.addShader(fs)
        # attribute location
        self._program.bindAttributeLocation("Pos", 0)
        self._program.bindAttributeLocation("UV", 1)
        # link program
        r = self._program.link()

        # program - background
        # shader
        vs = self.__create_shader(QOpenGLShader.Vertex, vs_grid_es)
        fs = self.__create_shader(QOpenGLShader.Fragment, ps_grid_es)
        # program
        self._program_bg = QOpenGLShaderProgram(self.context())
        self._program_bg.addShader(vs)
        self._program_bg.addShader(fs)
        # attribute location
        self._program_bg.bindAttributeLocation("Pos", 0)
        # link program
        r = self._program_bg.link()

        # uniform locations
        self._location = (
            self._program.uniformLocation("Scale"),
            self._program.uniformLocation("Channels"),
            self._program_bg.uniformLocation("Color1"),
            self._program_bg.uniformLocation("Color2"),
            self._program_bg.uniformLocation("Height"),
        )

        # vao
        r = self._vao.create()
        r = self._vao.bind()
        # vbo
        r = self._vbo.create()
        self._vbo.setUsagePattern(QOpenGLBuffer.StaticDraw)
        r = self._vbo.bind()
        sz_float = ctypes.sizeof(ctypes.c_float)
        self._vbo.allocate(self._vertex.tobytes(),
                           sz_float * len(self._vertex))
        self._vao.release()
        # texture
        self.set_texture(r"C:")

    def resizeGL(self, width, height):
        self.__update_scale(width, height)
        self._height = QVector4D(0, self.height(), 0, 0)
        self.glViewport(0, 0, width, height)

    def paintGL(self):
        SZ_FLOAT = ctypes.sizeof(ctypes.c_float)

        # draw the scene
        self.glClear(GL.GL_COLOR_BUFFER_BIT)

        self._vao.bind()

        # background
        self._program_bg.bind()
        self._program.setAttributeBuffer(0, GL.GL_FLOAT, 0, 3, 5 * SZ_FLOAT)
        self._program.enableAttributeArray(0)
        self._program.setUniformValue(self._location[2], self._u_colors[0])
        self._program.setUniformValue(self._location[3], self._u_colors[1])
        self._program.setUniformValue(self._location[4], self._height)
        self.glDrawElements(GL.GL_TRIANGLES, len(self._indices),
                            GL.GL_UNSIGNED_INT, self._indices.tobytes())

        # texture
        if self._texture is None:
            return

        self.glEnable(GL.GL_BLEND)
        self._program.bind()
        self._program.setAttributeBuffer(1, GL.GL_FLOAT, 3 * SZ_FLOAT, 2,
                                         5 * SZ_FLOAT)
        self._program.enableAttributeArray(1)
        self._program.setUniformValue(self._location[0], *self._scale)
        self._program.setUniformValue(self._location[1], self._u_channels)
        self._texture.bind()
        self.glDrawElements(GL.GL_TRIANGLES, len(self._indices),
                            GL.GL_UNSIGNED_INT, self._indices.tobytes())
        self.glDisable(GL.GL_BLEND)

    @staticmethod
    def __create_shader(type_: QOpenGLShader.ShaderType, source):
        shader = QOpenGLShader(type_)
        r = shader.compileSourceCode(source)
        if not r:
            print(shader.log())
        return shader

    def __update_scale(self, width, height):
        # calc texture scale

        if self._texture_size[0] < width and self._texture_size[1] < height:
            self._scale = (self._texture_size[0] / width,
                           self._texture_size[1] / height)
        else:
            tw = width
            th = self._texture_size[1] / self._texture_size[0] * tw
            if th > height:
                th = height
                tw = th * self._texture_size[0] / self._texture_size[1]
            self._scale = (tw / width, th / height)

    def get_gl_info(self):
        self.makeCurrent()
        info = """
			Vendor: {0}
			Renderer: {1}
			OpenGL Version: {2}
			Shader Version: {3}
			""".format(self.glGetString(GL.GL_VENDOR), self.glGetString(GL.GL_RENDERER),
              self.glGetString(GL.GL_VERSION),
              self.glGetString(GL.GL_SHADING_LANGUAGE_VERSION))
        return info

    def set_channels(self, r, g, b, a):
        r, g, b, a = float(r), float(g), float(b), float(a)
        s = r + g + b
        if s > 1.1:
            self._u_channels = QMatrix4x4(r, 0, 0, 0, 0, g, 0, 0, 0, 0, b, 0,
                                          0, 0, 0, a)
        elif s < 0.1:
            self._u_channels = QMatrix4x4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                          a, a, a, 0)
        elif a:
            self._u_channels = QMatrix4x4(r, 0, 0, 0, 0, g, 0, 0, 0, 0, b, 0,
                                          0, 0, 0, a)
        else:
            self._u_channels = QMatrix4x4(r, r, r, 0, g, g, g, 0, b, b, b, 0,
                                          0, 0, 0, a)
        # redraw
        self.update()

    def set_texture(self, filename):
        # load image
        p = Path(filename)
        suffix = p.suffix[1:].upper()
        try:
            if p.is_file() and suffix in self._supported_images:
                pim = Image.open(filename)
                img = ImageQt(pim)
                self.info = f"{pim.format} - {pim.size} - {pim.mode} "
            else:
                ico = QFileIconProvider().icon(QFileInfo(filename))
                pix = ico.pixmap(256, 256)
                img = pix.toImage()
                self.info = "not an image "
        except UnidentifiedImageError as e:
            print("UnidentifiedImageError:\n", e, flush=True)
            return

        self._texture_size = img.width(), img.height()
        self.__update_scale(self.width(), self.height())

        # create texture
        self.makeCurrent()
        self._texture = QOpenGLTexture(QOpenGLTexture.Target2D)
        self._texture.create()
        self._texture.bind()
        self._texture.setMinMagFilters(QOpenGLTexture.LinearMipMapLinear,
                                       QOpenGLTexture.Linear)
        self._texture.setWrapMode(QOpenGLTexture.DirectionS,
                                  QOpenGLTexture.Repeat)
        self._texture.setWrapMode(QOpenGLTexture.DirectionT,
                                  QOpenGLTexture.Repeat)
        self._texture.setData(img)
        self._texture.release()

        # redraw
        self.update()

    def set_colors(self, checkerboard, color1, color2):
        if checkerboard:
            self._u_colors = self._colors_default
        else:
            self._u_colors = (color1, color2)

        self.update()
コード例 #2
0
class SquareObject(QObject):
    def __init__(self, parent=None):
        super(SquareObject, self).__init__(parent=parent)
        self.initialize_shader_program()
        self.initialize_geometry()
        self.initialize_geometry_on_gpu()
        self.initialize_texture_on_gpu()

    def destroy(self):
        self.vao.release()
        self.vao.destroy()

        self.vbo.release()
        self.vbo.destroy()

        self.ebo.release()
        self.ebo.destroy()

        self.texture0.release()
        self.texture0.destroy()

        self.texture1.release()
        self.texture1.destroy()

    def initialize_shader_program(self):
        self.vertex_shader = """
            #version 330 core
              layout (location = 0) in vec3 in_coords;
              layout (location = 1) in vec3 in_color;
              layout (location = 2) in vec2 in_tex_coords;

              uniform mat4 transform;

              out vec3 out_color;
              out vec2 out_tex_coords;

              void main()
              {
                gl_Position = transform * vec4(in_coords, 1.0);
                out_color = in_color;
                out_tex_coords = vec2(in_tex_coords.x, in_tex_coords.y);
              }
        """

        self.fragment_shader = """
            #version 330 core
              in vec3 out_color;
              in vec2 out_tex_coords;
              out vec4 frag_color;

              uniform sampler2D texture0;
              uniform sampler2D texture1;

              void main()
              {
                if (out_tex_coords.x > 0.5)
                  frag_color = texture(texture0, out_tex_coords);
                else
                  frag_color = texture(texture1, out_tex_coords);
                //frag_color = mix(texture(texture0, out_tex_coords),
                //                 texture(texture1, out_tex_coords), 0.5)
                //           * vec4(out_color, 1.0);
              }
        """

        self.arg_pos = {
            'in_coords': 0,
            'in_color': 1,
            'in_tex_coords': 2,
            'out_color': 0
        }

        self.program = QOpenGLShaderProgram(parent=self.parent())
        self.program.addCacheableShaderFromSourceCode(QOpenGLShader.Vertex,
                                                      self.vertex_shader)
        self.program.addCacheableShaderFromSourceCode(QOpenGLShader.Fragment,
                                                      self.fragment_shader)
        self.program.link()
        self.program.bind()

    def initialize_geometry(self):
        self.vertices = np.array(
            [
                # coords            color            texture coords
                [0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0],  # bottom-right
                [0.5, 0.5, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0],  # top-right
                [-0.5, 0.5, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0],  # top-let
                [-0.5, -0.5, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0]  # bottom-let
            ],
            dtype=np.float32)

        self.triangles = np.array([[0, 1, 2], [2, 3, 0]], dtype=np.int32)

    def initialize_geometry_on_gpu(self):
        self.vao = QOpenGLVertexArrayObject(self.parent())
        if not self.vao.create():
            raise ValueError('Could not create VAO')
        self.vao.bind()

        self.vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
        if not self.vbo.create():
            raise ValueError('Could not create VBO')
        self.vbo.bind()
        self.vbo.setUsagePattern(QOpenGLBuffer.StaticDraw)
        vertices_data = self.vertices.tostring()
        self.vbo.allocate(len(vertices_data))
        self.vbo.write(0, vertices_data, len(vertices_data))

        self.ebo = QOpenGLBuffer(QOpenGLBuffer.IndexBuffer)
        if not self.ebo.create():
            raise ValueError('Could not create EBO')
        self.ebo.bind()
        self.ebo.setUsagePattern(QOpenGLBuffer.StaticDraw)
        triangles_data = self.triangles.tostring()
        self.ebo.allocate(len(triangles_data))
        self.ebo.write(0, triangles_data, len(triangles_data))

        self.program.enableAttributeArray(self.arg_pos['in_coords'])
        self.program.setAttributeBuffer(
            self.arg_pos['in_coords'], gl.GL_FLOAT, 0, 3,
            self.vertices.shape[1] * self.vertices.dtype.itemsize)

        self.program.enableAttributeArray(self.arg_pos['in_color'])
        self.program.setAttributeBuffer(
            self.arg_pos['in_color'], gl.GL_FLOAT,
            3 * self.vertices.dtype.itemsize, 3,
            self.vertices.shape[1] * self.vertices.dtype.itemsize)

        self.program.enableAttributeArray(self.arg_pos['in_tex_coords'])
        self.program.setAttributeBuffer(
            self.arg_pos['in_tex_coords'], gl.GL_FLOAT,
            6 * self.vertices.dtype.itemsize, 2,
            self.vertices.shape[1] * self.vertices.dtype.itemsize)

        self.vao.release()
        self.vbo.release()
        self.ebo.release()

    def initialize_texture_on_gpu(self):
        # Texture 0.
        image0 = QImage(path.join(DATA_DIR, 'ksmall.jpg')).mirrored()
        self.texture0 = QOpenGLTexture(image0)
        self.texture0.setMinificationFilter(QOpenGLTexture.LinearMipMapLinear)
        self.texture0.setMagnificationFilter(QOpenGLTexture.Linear)
        self.texture0.setWrapMode(QOpenGLTexture.Repeat)
        self.texture0.bind(0)
        self.program.setUniformValue(self.program.uniformLocation('texture0'),
                                     0)

        # Texture 1.
        image1 = QImage(path.join(DATA_DIR, 'sunflowerField.jpg')).mirrored()
        self.texture1 = QOpenGLTexture(image1)
        self.texture1.setMinificationFilter(QOpenGLTexture.LinearMipMapLinear)
        self.texture1.setMagnificationFilter(QOpenGLTexture.Linear)
        self.texture1.setWrapMode(QOpenGLTexture.Repeat)
        self.texture1.bind(1)
        self.program.setUniformValue(self.program.uniformLocation('texture1'),
                                     1)

    def render(self, transform):
        self.program.bind()
        self.program.setUniformValue('transform', transform)
        self.vao.bind()
        gl.glDrawElements(gl.GL_TRIANGLES, self.triangles.size,
                          gl.GL_UNSIGNED_INT, None)
        self.program.release()
コード例 #3
0
ファイル: hello_square.py プロジェクト: stjordanis/sara
class SquareObject(QObject):
    def __init__(self, parent=None):
        super(SquareObject, self).__init__(parent=parent)
        self.initialize_shader_program()
        self.initialize_geometry()
        self.initialize_geometry_on_gpu()

    def initialize_shader_program(self):
        self.vertex_shader = """
            #version 330 core
              layout (location = 0) in vec3 in_coords;
              layout (location = 1) in vec3 in_color;

              out vec3 out_color;

              void main()
              {
                gl_Position = vec4(in_coords, 1.0);
                gl_PointSize = 200.0;
                out_color = in_color;
              }
        """

        self.fragment_shader = """
            #version 330 core
              in vec3 out_color;
              out vec4 frag_color;

              void main()
              {
                frag_color = vec4(out_color, 1.0);
              }
        """

        self.arg_pos = {'in_coords': 0, 'in_color': 1, 'out_color': 0}

        self.program = QOpenGLShaderProgram(parent=self.parent())
        self.program.addCacheableShaderFromSourceCode(QOpenGLShader.Vertex,
                                                      self.vertex_shader)
        self.program.addCacheableShaderFromSourceCode(QOpenGLShader.Fragment,
                                                      self.fragment_shader)
        self.program.link()

    def initialize_geometry(self):
        self.vertices = np.array(
            [
                # coords           color
                [0.5, 0.5, 0.0, 1.0, 0.0, 0.0],  # top-right
                [0.5, -0.5, 0.0, 0.0, 1.0, 0.0],  # bottom-right
                [-0.5, -0.5, 0.0, 0.0, 0.0, 1.0],  # bottom-left
                [-0.5, 0.5, 0.0, 1.0, 1.0, 0.0]  # top-left
            ],
            dtype=np.float32)

        self.triangles = np.array([[0, 1, 2], [2, 3, 0]], dtype=np.int32)

    def initialize_geometry_on_gpu(self):
        self.vao = QOpenGLVertexArrayObject(self.parent())
        if not self.vao.create():
            raise ValueError('Could not create VAO')
        self.vao.bind()

        self.vbo = QOpenGLBuffer(QOpenGLBuffer.VertexBuffer)
        if not self.vbo.create():
            raise ValueError('Could not create VBO')
        self.vbo.bind()
        self.vbo.setUsagePattern(QOpenGLBuffer.StaticDraw)
        vertices_data = self.vertices.tostring()
        self.vbo.allocate(len(vertices_data))
        self.vbo.write(0, vertices_data, len(vertices_data))

        self.ebo = QOpenGLBuffer(QOpenGLBuffer.IndexBuffer)
        if not self.ebo.create():
            raise ValueError('Could not create EBO')
        self.ebo.bind()
        self.ebo.setUsagePattern(QOpenGLBuffer.StaticDraw)
        triangles_data = self.triangles.tostring()
        self.ebo.allocate(len(triangles_data))
        self.ebo.write(0, triangles_data, len(triangles_data))

        self.program.enableAttributeArray(self.arg_pos['in_coords'])
        self.program.setAttributeBuffer(
            self.arg_pos['in_coords'], gl.GL_FLOAT, 0, 3,
            self.vertices.shape[1] * self.vertices.dtype.itemsize)

        self.program.enableAttributeArray(self.arg_pos['in_color'])
        self.program.setAttributeBuffer(
            self.arg_pos['in_color'], gl.GL_FLOAT,
            3 * self.vertices.dtype.itemsize, 3,
            self.vertices.shape[1] * self.vertices.dtype.itemsize)

        self.vao.release()
        self.vbo.release()
        self.ebo.release()

    def render(self):
        self.program.bind()
        self.vao.bind()
        gl.glDrawElements(gl.GL_TRIANGLES, self.triangles.size,
                          gl.GL_UNSIGNED_INT, None)
        self.program.release()