DrawElements崩溃了Qt App

时间:2015-10-03 18:05:05

标签: qt pyopengl

我已经设置了一个用作opengl查看器的qt小部件。我的目标是将其用作纹理查看器和模型查看器。这就是为什么我创建了两个不同的程序,并附加了不同的着色器以满足我的需求。

我将所有对象存储在我创建的自定义类中,该类还存储了正确的对象程序ID。

在QGLWidget的paintGL函数中,我正在完成为绘制场景而需要完成的所有工作。

问题在于,当模型工作时,当我试图简单地切换程序并绘制一个简单的四边形时,窗口崩溃,并且我在控制台中没有发生任何错误。通过添加打印语句,我发现gldrawElements函数导致崩溃,但我找不到原因。

def paintGL(self):
    GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
    for ob in self.objects:
        activeBuffers = []
        # GL.glLinkProgram(ob.program)
        GL.glLinkProgram(ob.program)
        GL.glUseProgram(ob.program)
        # print ob.vBuffer, ob.uvBuffer, ob.nBuffer, ob.iBuffer

        if ob.program == self.programs[0]:
            # Update Panning
            loc = GL.glGetUniformLocation(ob.program, "panX")
            GL.glUniform1f(loc, self.panX)
            loc = GL.glGetUniformLocation(ob.program, "panY")
            GL.glUniform1f(loc, self.panY)

            # Update Scale in Shader
            loc = GL.glGetUniformLocation(ob.program, "scale")
            GL.glUniform1f(loc, self.scale)
            # Update Theta in Shader
            loc = GL.glGetUniformLocation(ob.program, "theta")
            GL.glUniform3fv(loc, 1, self.theta)
            # Bind Vertex VBO
            ob.vBuffer.bind()
            vpos = GL.glGetAttribLocation(ob.program, "vPosition")
            GL.glVertexAttribPointer(
                vpos, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
            GL.glEnableVertexAttribArray(vpos)

        if ob.uvBuffer:
            # Bind UVs
            ob.uvBuffer.bind()

            uvpos = GL.glGetAttribLocation(ob.program, "uvPosition")
            print ob.program, uvpos
            if not uvpos == -1:
                GL.glVertexAttribPointer(uvpos, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
                GL.glEnableVertexAttribArray(uvpos)
                activeBuffers.append(ob.uvBuffer)
            else:
                ob.uvBuffer.unbind()

        if ob.nBuffer:
            # Bind Normals
            ob.nBuffer.bind()
            npos = GL.glGetAttribLocation(ob.program, "nPosition")
            GL.glVertexAttribPointer(npos, 3, GL.GL_FLOAT, GL.GL_FALSE, 0, None)
            GL.glEnableVertexAttribArray(npos)
            activeBuffers.append(ob.nBuffer)

        # Bind Indices VBO
        ob.iBuffer.bind()
        activeBuffers.append(ob.iBuffer)
        # if ob.program == self.programs[1]:
        #    print 'Continuing'
        #    continue

        GL.glDrawElements(
           GL.GL_TRIANGLES, ob.iLen, GL.GL_UNSIGNED_INT, None)
        # Unbind everything
        for buf in activeBuffers:
            buf.unbind()

我正在创建像这样的对象

def makeTextureQuad(self):
    ob = globject()
    ob.vBuffer = np.array(quadverts, dtype=np.float32)
    ob.vBuffer = glvbo.VBO(ob.vBuffer)
    ob.vLen = len(quadverts)
    ob.iBuffer = np.array(quadfaces, dtype=np.int32)
    ob.iBuffer = glvbo.VBO(ob.iBuffer, target=GL.GL_ELEMENT_ARRAY_BUFFER)
    ob.iLen = len(quadfaces) * 3
    ob.uvBuffer = np.array(quaduvs, dtype=np.float32)
    ob.uvBuffer = glvbo.VBO(ob.uvBuffer)
    # ob.nBuffer = np.array(quadnorms, dtype=np.float32)
    # ob.nBuffer = glvbo.VBO(ob.nBuffer)
    ob.program = self.programs[1]
    return ob

提前致谢

1 个答案:

答案 0 :(得分:0)

我很蠢......由于我的白痴案例结构,我没有绑定任何顶点缓冲区,我要求opengl绘制元素。我把那个部分移出了if块,它起作用了。)