Search code examples
pythonopenglpygamepyopengl

Graphical Glitches in PyOpenGL with PyGame


I am currently programming a game with PyOpenGL with PyGame, and when using Vertex Buffers, graphical glitches occur. The glitches include lines being drawn between each model when it shouldn't. I have found that alternating between Ground() and GroundVBO() does not cause any graphical glitches most of the time. Is there anything that I am missing?

global vbo
vbo = 0
def Ground():
    glBegin(GL_LINES)
    for edge in ground_edges:
        for vertex in edge:
            glVertex3fv(ground_verticies[vertex])
    glEnd()
def GroundVBO():
    for edge in ground_edges:
        for vertex in edge:
            ground_noot = glVertex3fv(ground_verticies[vertex])
    vbo = glGenBuffers(1)
    glBindBuffer (GL_ARRAY_BUFFER, vbo)
    glBufferData (GL_ARRAY_BUFFER, len(ground_verticies)*4, ground_noot, GL_STATIC_DRAW)
    glVertexPointer (3, GL_FLOAT, 0, None)
    glDrawArrays(GL_LINES, 0, 300)


Solution

  • If you want to use fixed function attributes, then you have to enable the client-side capability by glEnableClientState. Specifying the vertices by glVertex3fv in a loop is superfluous. Specifying a vertex outside a glBegin/glEnd sequence results in undefined behavior.
    The last parameter to glDrawArrays is the number of vertex coordinates:

    def GroundVBO():
    
        vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, vbo)
        glBufferData(GL_ARRAY_BUFFER, len(ground_verticies)*4, ground_noot, GL_STATIC_DRAW)
    
        glEnableClientState(GL_VERTEX_ARRAY)
        glVertexPointer(3, GL_FLOAT, 0, None)
        glDrawArrays(GL_LINES, 0, len(ground_verticies))
        glDisableClientState(GL_VERTEX_ARRAY)