def initializeGL(self):
        "Initialize opengl "
        print('gl initial')
        print(self.getGlInfo())
        # create context and make it current
        self.context.create()
        self.context.aboutToBeDestroyed.connect(self.cleanUpGl)

        # initialize functions
        funcs = self.context.functions()
        funcs.initializeOpenGLFunctions()
        funcs.glClearColor(1, 0, 1, 1)

        # shader
        shaderName = "texture"
        vshader = self.loadVertexShader(shaderName)
        fshader = self.loadFragmentShader(shaderName)

        # create shader program
        self.program = QOpenGLShaderProgram(self.context)
        self.program.addShader(vshader)
        self.program.addShader(fshader)

        # bind attribute location
        self.program.bindAttributeLocation("aPos", 0)
        self.program.bindAttributeLocation("aTexCoord", 1)

        # link shader program
        isLinked = self.program.link()
        print("shader program is linked: ", isLinked)

        # activate shader program to set uniform an attribute values
        self.program.bind()
        self.program.setUniformValue('myTexture', 0)

        # vbo
        isVbo = self.vbo.create()
        isVboBound = self.vbo.bind()

        floatSize = ctypes.sizeof(ctypes.c_float)

        # allocate vbo
        self.vbo.allocate(self.vertexData.tobytes(),
                          floatSize * self.vertexData.size)

        # texture new school
        self.texture = QOpenGLTexture(QOpenGLTexture.Target2D)
        self.texture.create()
        # new school
        self.texture.bind()
        self.texture.setData(self.image)
        self.texture.setMinMagFilters(QOpenGLTexture.Linear,
                                      QOpenGLTexture.Linear)
        self.texture.setWrapMode(QOpenGLTexture.DirectionS,
                                 QOpenGLTexture.Repeat)
        self.texture.setWrapMode(QOpenGLTexture.DirectionT,
                                 QOpenGLTexture.Repeat)
Esempio n. 2
0
    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)
Esempio n. 3
0
 def resizeGL(self, w, h):
     self.scene.getCamera().setAspect(w, h)
     if self.viewportTexture and self.context().isValid():
         self.viewportTexture.destroy()
     if self.context().isValid():
         self.renderImage = QImage(w, h, QImage.Format_RGB32)
         self.renderImage.fill(Qt.black)
         self.viewportTexture = QOpenGLTexture(QOpenGLTexture.Target2D)
         self.viewportTexture.setSize(w, h)
         self.viewportTexture.setBorderColor(0, 0, 0, 255)
         self.viewportTexture.setWrapMode(QOpenGLTexture.ClampToBorder)
         self.viewportTexture.setMinificationFilter(QOpenGLTexture.Nearest)
         self.viewportTexture.setMagnificationFilter(QOpenGLTexture.Nearest)
         self.viewportTexture.setData(self.renderImage,
                                      QOpenGLTexture.DontGenerateMipMaps)
         self.updateRenderResolution(w, h)
Esempio n. 4
0
 def checkRender(self):
     if not self.isRendering:
         return
     imageArray, numIterations = self.renderer.getData(
         self.width(), self.height())
     if numIterations < 0 or imageArray.shape != (self.height(),
                                                  self.width()):
         self.timer.start()
         return
     if self.viewportTexture and self.context().isValid():
         self.viewportTexture.destroy()
     if self.context().isValid():
         self.renderImage = QImage(self.width(), self.height(),
                                   QImage.Format_RGB32)
         copyArray = np.ndarray(shape=(self.height(), self.width()),
                                dtype=np.uint32,
                                buffer=self.renderImage.bits())
         copyArray[0:, 0:] = imageArray[0:, 0:]
         self.viewportTexture = QOpenGLTexture(QOpenGLTexture.Target2D)
         self.viewportTexture.setSize(self.width(), self.height())
         self.viewportTexture.setBorderColor(0, 0, 0, 255)
         self.viewportTexture.setWrapMode(QOpenGLTexture.ClampToBorder)
         self.viewportTexture.setMinificationFilter(QOpenGLTexture.Nearest)
         self.viewportTexture.setMagnificationFilter(QOpenGLTexture.Nearest)
         self.viewportTexture.setData(self.renderImage,
                                      QOpenGLTexture.DontGenerateMipMaps)
         if len(self.timestamps) == self.fpsWindow:
             self.timestamps.pop()
         self.timestamps.insert(0, [time.time(), numIterations])
         diffs = [(self.timestamps[i][1] - self.timestamps[i + 1][1]) /
                  (self.timestamps[i][0] - self.timestamps[i + 1][0])
                  for i in range(len(self.timestamps) - 1)]
         secs = int(time.time() - self.renderStartTime)
         self.updateFpsDisplay(
             '{: 4.1f} iterations/sec     {:02.0f}:{:02.0f} min    {:7d} iterations    {:4d} x {:4d} pixel'
             .format(
                 sum(diffs) / len(diffs), secs // 60, secs % 60,
                 numIterations, self.width(), self.height()))
     self.update()
     self.timer.start()
Esempio n. 5
0
    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()
Esempio n. 6
0
 def initializeGL(self):
     self.m_functions = self.context().functions()
     self.m_functions.initializeOpenGLFunctions()
     image = QImage(QSize(200, 200), QImage.Format_RGBA8888)
     image.fill(QColor(Qt.red))
     self.m_texture = QOpenGLTexture(image)
Esempio n. 7
0
    def initializeGL(self):
        print('gl initial')
        print(self.getGlInfo())

        # create context and make it current
        self.context.create()
        self.context.aboutToBeDestroyed.connect(self.cleanUpGl)

        # initialize functions
        funcs = self.context.functions()
        funcs.initializeOpenGLFunctions()
        funcs.glClearColor(0.0, 0.4, 0.4, 0)
        funcs.glEnable(pygl.GL_DEPTH_TEST)
        funcs.glEnable(pygl.GL_TEXTURE_2D)

        # create uniform values for shaders
        # deal with shaders

        # cube shader
        self.program = QOpenGLShaderProgram(self.context)
        vshader = self.loadVertexShader("cube")
        fshader = self.loadFragmentShader("cube")
        self.program.addShader(vshader)  # adding vertex shader
        self.program.addShader(fshader)  # adding fragment shader
        self.program.bindAttributeLocation("aPos", 0)
        self.program.bindAttributeLocation("aTexCoord", 1)

        isLinked = self.program.link()
        print("cube shader program is linked: ", isLinked)
        # bind the program
        self.program.bind()

        self.program.setUniformValue('myTexture1', self.texUnit1)
        self.program.setUniformValue('myTexture2', self.texUnit2)
        #
        # deal with vaos and vbo
        # vbo
        isVbo = self.vbo.create()
        isVboBound = self.vbo.bind()

        floatSize = ctypes.sizeof(ctypes.c_float)

        # allocate space on vbo buffer
        self.vbo.allocate(self.cubeVertices.tobytes(),
                          floatSize * self.cubeVertices.size)
        self.vao.create()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)
        funcs.glEnableVertexAttribArray(0)  # viewport
        funcs.glVertexAttribPointer(0, 3, int(pygl.GL_FLOAT),
                                    int(pygl.GL_FALSE), 5 * floatSize,
                                    VoidPtr(0))
        funcs.glEnableVertexAttribArray(1)
        funcs.glVertexAttribPointer(1, 2, int(pygl.GL_FLOAT),
                                    int(pygl.GL_FALSE), 5 * floatSize,
                                    VoidPtr(3 * floatSize))
        # deal with textures
        # first texture
        self.texture1 = QOpenGLTexture(QOpenGLTexture.Target2D)
        self.texture1.create()
        self.texture1.bind(self.texUnit1)
        self.texture1.setData(self.image1)
        self.texture1.setMinMagFilters(QOpenGLTexture.Nearest,
                                       QOpenGLTexture.Nearest)
        self.texture1.setWrapMode(QOpenGLTexture.DirectionS,
                                  QOpenGLTexture.Repeat)
        self.texture1.setWrapMode(QOpenGLTexture.DirectionT,
                                  QOpenGLTexture.Repeat)

        # second texture
        self.texture2 = QOpenGLTexture(QOpenGLTexture.Target2D)
        self.texture2.create()
        self.texture2.bind(self.texUnit2)
        self.texture2.setData(self.image2)
        self.texture2.setMinMagFilters(QOpenGLTexture.Linear,
                                       QOpenGLTexture.Linear)
        self.texture2.setWrapMode(QOpenGLTexture.DirectionS,
                                  QOpenGLTexture.Repeat)
        self.texture2.setWrapMode(QOpenGLTexture.DirectionT,
                                  QOpenGLTexture.Repeat)

        self.vbo.release()
        vaoBinder = None
        print("gl initialized")
Esempio n. 8
0
 def createimpl(self, gl):
   self.texture = QOpenGLTexture(QImage(self.filename))
   self.width = self.texture.width()
   self.height = self.texture.height()