Example #1
0
    def render(self) -> None:
        self.program.bind()
        vao_binder = QOpenGLVertexArrayObject.Binder(self.vao)

        # TODO: switch to Qt functions (doesn't work)
        gl.glUniformMatrix4fv(self.projection_loc, 1, gl.GL_FALSE, self.projection)
        view = self.camera.get_view_matrix()
        gl.glUniformMatrix4fv(self.camera_loc, 1, gl.GL_FALSE, view)

        for model in self.models:
            # TODO: move to entity class ?
            rotation = pyrr.matrix44.create_from_axis_rotation(
                pyrr.vector3.create_from_matrix44_translation(model),
                time.time(),
            )
            scale = pyrr.matrix44.create_from_scale(
                pyrr.vector3.create_from_matrix44_translation(model) * 0.1,
            )
            rotation = pyrr.matrix44.multiply(scale, rotation)
            model = pyrr.matrix44.multiply(rotation, model)

            orbit = pyrr.Matrix44.from_y_rotation(-0.1*time.time())
            model = pyrr.matrix44.multiply(model, orbit)
            gl.glUniformMatrix4fv(self.model_loc, 1, gl.GL_FALSE, model)
            self.glDrawElements(gl.GL_TRIANGLES, len(self.shape.indices), gl.GL_UNSIGNED_INT, VoidPtr(0))

        self.program.release()
        vao_binder = None
    def paintGL(self):
        "drawing loop"
        funcs = self.context.functions()

        # clean up what was drawn
        funcs.glClear(pygl.GL_COLOR_BUFFER_BIT)

        # actual drawing
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao1)
        self.program1.bind()
        funcs.glDrawArrays(pygl.GL_TRIANGLES,  # mode
                           0,  # first
                           3)  # count
        vaoBinder = None
        self.program1.release()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao2)
        self.program2.bind()
        funcs.glDrawArrays(pygl.GL_TRIANGLES,  # mode
                           0,  # first
                           3)  # count
        vaoBinder = None
        self.program2.release()
    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 #4
0
    def paintGL(self):
        "paint gl"
        funcs = self.context.functions()
        # clean up what was drawn
        funcs.glClear(pygl.GL_COLOR_BUFFER_BIT)

        # bind texture
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)
        self.program.bind()

        # draw stuff
        funcs.glDrawElements(pygl.GL_TRIANGLES, self.indices.size,
                             pygl.GL_UNSIGNED_INT, self.indices.tobytes())
        # VoidPtr(self.indices.tobytes() * ctypes.sizeof(ctypes.c_uint)))
        vaoBinder = None
        self.program.release()
Example #5
0
    def initializeGL(self):
        self.context().aboutToBeDestroyed.connect(self.cleanup)
        self.initializeOpenGLFunctions()
        self.glClearColor(0, 0, 0, 1)

        self.program = QOpenGLShaderProgram()

        if self.core:
            self.vertexShader = self.vertexShaderSourceCore()
            self.fragmentShader = self.fragmentShaderSourceCore()
        else:
            self.vertexShader = self.vertexShaderSource()
            self.fragmentShader = self.fragmentShaderSource()

        self.program.addShaderFromSourceCode(QOpenGLShader.Vertex,
                                             self.vertexShader)
        self.program.addShaderFromSourceCode(QOpenGLShader.Fragment,
                                             self.fragmentShader)
        self.program.bindAttributeLocation("vertex", 0)
        self.program.bindAttributeLocation("normal", 1)
        self.program.link()

        self.program.bind()
        self.projMatrixLoc = self.program.uniformLocation("projMatrix")
        self.mvMatrixLoc = self.program.uniformLocation("mvMatrix")
        self.normalMatrixLoc = self.program.uniformLocation("normalMatrix")
        self.lightPosLoc = self.program.uniformLocation("lightPos")

        self.vao.create()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)

        self.logoVbo.create()
        self.logoVbo.bind()
        float_size = ctypes.sizeof(ctypes.c_float)
        self.logoVbo.allocate(self.logo.constData(),
                              self.logo.count() * float_size)

        self.setupVertexAttribs()

        self.camera.setToIdentity()
        self.camera.translate(0, 0, -1)

        self.program.setUniformValue(self.lightPosLoc, QVector3D(0, 0, 70))
        self.program.release()
        vaoBinder = None
Example #6
0
    def paintGL(self):
        self.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        self.glEnable(GL.GL_DEPTH_TEST)
        self.glEnable(GL.GL_CULL_FACE)

        self.world.setToIdentity()
        self.world.rotate(180 - (self.xRot / 16), 1, 0, 0)
        self.world.rotate(self.yRot / 16, 0, 1, 0)
        self.world.rotate(self.zRot / 16, 0, 0, 1)

        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)
        self.program.bind()
        self.program.setUniformValue(self.projMatrixLoc, self.proj)
        self.program.setUniformValue(self.mvMatrixLoc,
                                     self.camera * self.world)
        normalMatrix = self.world.normalMatrix()
        self.program.setUniformValue(self.normalMatrixLoc, normalMatrix)

        self.glDrawArrays(GL.GL_TRIANGLES, 0, self.logo.vertexCount())
        self.program.release()
        vaoBinder = None
Example #7
0
    def create_vbo(self) -> None:
        self.program.bind()  # suspicious behaviour ?

        self.vao.create()
        vao_binder = QOpenGLVertexArrayObject.Binder(self.vao)

        self.vbo.create()
        self.vbo.bind()
        self.vbo.allocate(self.shape.vertices, self.shape.vertices.nbytes)

        self.attrib_loc = self.program.attributeLocation("a_position")
        self.model_loc = self.program.uniformLocation("model")
        self.projection_loc = self.program.uniformLocation("projection")
        self.camera_loc = self.program.uniformLocation("camera")

        for i in range(150):
            x, y, z = random.normalvariate(0, 15), random.normalvariate(0, 15), random.normalvariate(0, 15)
            self.models.append(pyrr.matrix44.create_from_translation(pyrr.Vector3([x, y, z])))

        self.ebo.create()
        self.ebo.bind()
        self.ebo.allocate(self.shape.indices, self.shape.indices.nbytes)

        float_size = ctypes.sizeof(ctypes.c_float)  # (4)
        null = VoidPtr(0)
        pointer = VoidPtr(3 * float_size)
        self.glEnableVertexAttribArray(0)
        self.glVertexAttribPointer(
            0, 3, gl.GL_FLOAT, gl.GL_FALSE, 6 * float_size, null
        )
        self.glEnableVertexAttribArray(1)
        self.glVertexAttribPointer(
            1, 3, gl.GL_FLOAT, gl.GL_FALSE, 6 * float_size, pointer
        )
        self.vao.release()
        self.vbo.release()
        self.ebo.release()

        self.program.release()
        vao_binder = None
Example #8
0
    def paintGL(self):
        self.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        self.glEnable(GL.GL_DEPTH_TEST)
        self.glEnable(GL.GL_CULL_FACE)

        self.world.setToIdentity()
        self.world.rotate(self.xRot / 16, 1, 0, 0)
        self.world.rotate(self.yRot / 16, 0, 1, 0)
        self.world.rotate(self.zRot / 16, 0, 0, 1)
        self.camera.setToIdentity()
        self.camera.translate(0,0,self.zoom)
        self.world.translate(-self.lithophane.width*0.5*0.2,-self.lithophane.height*0.5*0.2 , 0)
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)
        self.program.bind()
        self.program.setUniformValue(self.projMatrixLoc, self.proj)
        self.program.setUniformValue(self.mvMatrixLoc, self.camera * self.world)
        normalMatrix = self.world.normalMatrix()
        self.program.setUniformValue(self.normalMatrixLoc, normalMatrix)

        self.glDrawArrays(GL.GL_QUADS, 0, self.lithophane.vertexCount())
        self.program.release()
        vaoBinder = None
Example #9
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")
    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(1, 1, 1, 1)

        # deal with shaders
        # first shader
        shaderName = "triangle"
        vshader = self.loadVertexShader(shaderName)
        fshader = self.loadFragmentShader(shaderName)

        # creating shader program
        self.program1 = QOpenGLShaderProgram(self.context)
        self.program1.addShader(vshader)  # adding vertex shader
        self.program1.addShader(fshader)  # adding fragment shader

        # bind attribute to a location
        self.program1.bindAttributeLocation("aPos", 0)

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

        # bind the program1
        self.program1.bind()

        # specify uniform value
        colorLoc = self.program1.uniformLocation("color")
        self.program1.setUniformValue(colorLoc,
                                      self.triangleColor1)

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

        #
        self.program2 = QOpenGLShaderProgram(self.context)
        self.program2.addShader(vshader)  # adding vertex shader
        self.program2.addShader(fshader)  # adding fragment shader

        # bind attribute to a location
        self.program2.bindAttributeLocation("aPos", 0)

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

        # bind the program2
        self.program2.bind()

        # specify uniform value
        colorLoc = self.program2.uniformLocation("color")
        self.program2.setUniformValue(colorLoc,
                                      self.triangleColor2)

        # self.useShader("triangle")

        # deal with vao and vbo

        # create vao and vbo

        # vao
        isVao = self.vao1.create()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao1)

        # vbo
        isVbo = self.vbo1.create()
        isBound = self.vbo1.bind()

        # check if vao and vbo are created
        print('vao created: ', isVao)
        print('vbo created: ', isVbo)

        floatSize = ctypes.sizeof(ctypes.c_float)

        # allocate space on buffer
        self.vbo1.allocate(self.vertexData1.tobytes(),
                           floatSize * self.vertexData1.size)
        funcs.glEnableVertexAttribArray(0)
        nullptr = VoidPtr(0)
        funcs.glVertexAttribPointer(0,
                                    3,
                                    int(pygl.GL_FLOAT),
                                    int(pygl.GL_FALSE),
                                    3 * floatSize,
                                    nullptr)
        self.vbo1.release()
        vaoBinder = None

        # second triangle vao vbo
        # vao
        isVao = self.vao2.create()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao2)

        # vbo
        isVbo = self.vbo2.create()
        isBound = self.vbo2.bind()

        # check if vao and vbo are created
        print('vao created: ', isVao)
        print('vbo created: ', isVbo)

        floatSize = ctypes.sizeof(ctypes.c_float)

        # allocate space on buffer
        self.vbo2.allocate(self.vertexData2.tobytes(),
                           floatSize * self.vertexData2.size)
        funcs.glEnableVertexAttribArray(0)
        nullptr = VoidPtr(0)
        funcs.glVertexAttribPointer(0,
                                    3,
                                    int(pygl.GL_FLOAT),
                                    int(pygl.GL_FALSE),
                                    3 * floatSize,
                                    nullptr)
        self.vbo2.release()
        self.program2.release()
Example #11
0
    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, 1, 1, 1)

        # shader
        shaderName = "triangle"
        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)

        # 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()

        # specify uniform value
        colorLoc = self.program.uniformLocation("color")
        self.program.setUniformValue(colorLoc, self.rectColor)

        # vao, vbo, texture
        # vao
        isVao = self.vao.create()
        vaoBinder = QOpenGLVertexArrayObject.Binder(self.vao)

        # 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)

        print("vao created: ", isVao)
        print("vbo created: ", isVbo)
        print("vbo bound: ", isVboBound)

        # dealing with attributes
        # vertex array position
        funcs.glVertexAttribPointer(0, 3, int(pygl.GL_FLOAT),
                                    int(pygl.GL_FALSE), 3 * floatSize,
                                    VoidPtr(0))
        funcs.glEnableVertexAttribArray(0)

        self.vbo.release()
        vaoBinder = None