Search code examples
pythonperformanceopenglpyopengl

Efficiency of display lists in OpenGL with Python?


I have been teaching myself OpenGL programming using the Python wrapper PyOpenGL, and am now working on my first project with it. This project is a music visualizer (not dissimilar to whitecap) using many independently moving and independently colored cubes.

My current method is to have a display list for one cube and call it repeatedly changing color and location with glColor and glTranslatef like so (pseudocode):

glPushMatrix()
glPushMatrix() #Why do I need to call this twice?
for x in xrange(...):
    for z in xrange(...):
        y = height[x,z] #numpy array
        glTranslatef(x,y,z)
        glColor((r,g,b))
        glCallList(cube)
        glTranslatef(-x,-y,-z)
glPopMatrix()
glPopMatrix()

In this manner I can render about 10,000 cubes before I start to notice the framerate, this is OK but I would like it to be faster so my program is portable to less able computers, so my question is:

What would be the most efficient way to render many identical but independent objects, and will I get much better performance than I am now using display lists? Should I be using C or learning vertex buffering?

Note: I found disabling the error checking gave a sizable performance boost.


Solution

  • It is like the common sense was wrong. You manually restore the transformation (glTranslatef(-x,-y,-z)) after drawing the cube, this way glPopMatrix is not only called twice for no reason, but it is also useless because you did all the work for it. Correct code would like like so:

    for x in xrange(...):
        for z in xrange(...):
            y = height[x,z] #numpy array
            glPushMatrix() #Remember the matrix
            glTranslatef(x,y,z) #change the matrix
            glColor((r,g,b))
            glCallList(cube)
            glPopMatrix() #Restore tha matrix as it was before glTranslate changed it