Efficiency of display lists in OpenGL with Python?

南笙酒味 提交于 2019-12-11 06:39:45

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/8469034/efficiency-of-display-lists-in-opengl-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!