def setupVertexAttribs(self): self.logoVbo.bind() f = QOpenGLContext.currentContext().functions() f.glEnableVertexAttribArray(0) f.glEnableVertexAttribArray(1) float_size = ctypes.sizeof(ctypes.c_float) null = VoidPtr(0) pointer = VoidPtr(3 * float_size) f.glVertexAttribPointer(0, 3, int(GL.GL_FLOAT), int(GL.GL_FALSE), 6 * float_size, null) f.glVertexAttribPointer(1, 3, int(GL.GL_FLOAT), int(GL.GL_FALSE), 6 * float_size, pointer) self.logoVbo.release()
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()
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