0

I've setup a qt widget that serves as an opengl viewer. My goal is to use it as a texture viewer and as a model viewer as well. That's why i've created two different programs, with different shaders attached to them to fit my needs.

I'm storing all the objects in a custom class that i made, which also stores the correct object program id.

In the paintGL function of the QGLWidget i'm doing all the job that needs to be done in order to draw the scene.

The problem is that while the models work, when i'm trying to simply switch the programs and draw a simple quad, the window crashes, and i get no error in the console of what happened. By adding printing statements, i've located that the gldrawElements function is causing the crash, but i can't find out why.

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

I'm creating the objects like that

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

Thanks in advance

Greg K.
  • 686
  • 1
  • 5
  • 18

1 Answers1

0

I'm stupid... Due to my idiot case structure, i was not binding any vertex buffer, and i demanded opengl to draw the Elements. I moved that section out of the if block and it worked :)

Greg K.
  • 686
  • 1
  • 5
  • 18