Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyOpenGL terribly slow

My problem is that my program displaying a simple cube with a 120-fps-restriction only runs at 25 fps. I figured out that all the performance loss lies in the OpenGL part, but I could not figure out where exactly.

My questions are:

  • is it normal for OpenGL to run that slow with Python?
  • is my old notebook part of the problem?

Here is the my code I used for displaying the cube:

def draw(self):
    glBegin(GL_QUADS)#info for OGL: treat following code as surface drawing code
    for surface in self.surfaces:
        x = 0
        for vertex in surface:
            x+=1
            glColor3fv(self.colors[x])
            glVertex3fv(self.verticles[vertex])
    glEnd()

    glBegin(GL_LINES) #info for OGL: treat following code as line drawing code
    for edge in self.edges:
        for vertex in edge:
            glVertex3fv(self.verticles[vertex]) #pass each verticle in the verticles list to glVertex3fv, which creates edges
    glEnd() #info for OGL:no more code incoming
like image 628
xXliolauXx Avatar asked Oct 03 '15 10:10

xXliolauXx


1 Answers

No, it's not normal for OpenGL to run that slow. The slowness here comes from using immediate mode (glBegin(), glEnd()). Basically, every single frame you invoke those python commands one by one and the card has to produce output immediately. This is slow in C, let alone Python which is interpreting line by line.

What you want is to prepare vertex buffers (in what is commonly known as VBO) before-hand, then at render time just submit them for batch rendering.

Have a look at this wikibook for modern OpenGL (>=2.0) approach: https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Introduction. It's in C/C++, but you can follow follow the function invocations and principles.

like image 134
borancar Avatar answered Oct 16 '22 17:10

borancar