def initGl(self):
        self.program = QOpenGLShaderProgram(self)
        self.vao = QOpenGLVertexArrayObject()
        self.vbo = QOpenGLBuffer()

        format = self.context.format()
        useNewStyleShader = format.profile() == QSurfaceFormat.CoreProfile
        # Try to handle 3.0 & 3.1 that do not have the core/compatibility profile
        # concept 3.2+ has. This may still fail since version 150 (3.2) is
        # specified in the sources but it's worth a try.
        if (format.renderableType() == QSurfaceFormat.OpenGL
                and format.majorVersion() == 3 and format.minorVersion() <= 1):
            useNewStyleShader = not format.testOption(
                QSurfaceFormat.DeprecatedFunctions)

        vertexShader = vertexShaderSource if useNewStyleShader else vertexShaderSource110
        fragmentShader = fragmentShaderSource if useNewStyleShader else fragmentShaderSource110
        if not self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                                    vertexShader):
            raise Exception("Vertex shader could not be added: {} ({})".format(
                self.program.log(), vertexShader))
        if not self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                                    fragmentShader):
            raise Exception(
                "Fragment shader could not be added: {} ({})".format(
                    self.program.log(), fragmentShader))
        if not self.program.link():
            raise Exception("Could not link shaders: {}".format(
                self.program.log()))

        self.posAttr = self.program.attributeLocation("posAttr")
        self.colAttr = self.program.attributeLocation("colAttr")
        self.matrixUniform = self.program.uniformLocation("matrix")

        self.vbo.create()
        self.vbo.bind()
        self.verticesData = vertices.tobytes()
        self.colorsData = colors.tobytes()
        verticesSize = 4 * vertices.size
        colorsSize = 4 * colors.size
        self.vbo.allocate(VoidPtr(self.verticesData),
                          verticesSize + colorsSize)
        self.vbo.write(verticesSize, VoidPtr(self.colorsData), colorsSize)
        self.vbo.release()

        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)
        if self.vao.isCreated():  # have VAO support, use it
            self.setupVertexAttribs()
Example #2
0
    def testVoidPtr(self):
        # Creating a VoidPtr object requires an address of
        # a C++ object, a wrapped Shiboken Object type,
        # an object implementing the Python Buffer interface,
        # or another VoidPtr object.

        # Original content
        b = b"Hello world"
        ba = QByteArray(b)
        vp = VoidPtr(ba, ba.size())
        self.assertIsInstance(vp, shiboken.VoidPtr)

        # Create QByteArray from voidptr byte interpretation
        nba = QByteArray.fromRawData(vp.toBytes())
        # Compare original bytes to toBytes()
        self.assertTrue(b, vp.toBytes())
        # Compare original with new QByteArray data
        self.assertTrue(b, nba.data())
        # Convert original and new to str
        self.assertTrue(str(b), str(nba))

        # Modify nba through a memoryview of vp
        mv = memoryview(vp)
        self.assertFalse(mv.readonly)
        mv[6:11] = b'void*'
        self.assertEqual(str(ba), str(b"Hello void*"))
Example #3
0
 def testVoidPtr(self):
     # Creating a VoidPtr object requires an address of
     # a C++ object, a wrapped Shiboken Object type,
     # an object implementing the Python Buffer interface,
     # or another VoidPtr object.
     ba = QByteArray(b"Hello world")
     voidptr = VoidPtr(ba)
     self.assertIsInstance(voidptr, shiboken2.VoidPtr)
Example #4
0
    def draw(self, shaderProgram, glFunctions, camera=None):
        self.__class__.vao.bind()
        shaderProgram.bind()

        #calculate view
        if not camera:
            camera = self.scene.getCamera()
        view = camera.getViewMatrix()
        projection = camera.getProjectionMatrix()

        shaderProgram.setUniformValue("model", self.model)
        shaderProgram.setUniformValue("view", view)
        shaderProgram.setUniformValue("projection", projection)
        shaderProgram.setUniformValue("inColor",
                                      self.material.getEditorColor())

        glFunctions.glDrawElements(GL.GL_TRIANGLES,
                                   self.__class__.elements.size,
                                   GL.GL_UNSIGNED_SHORT, VoidPtr(0))

        self.__class__.vao.release()
        shaderProgram.release()