Why is my Python OpenGL render2DTexture function so slow?

Posted by Barakat on Stack Overflow See other posts from Stack Overflow or by Barakat
Published on 2010-03-07T21:18:38Z Indexed on 2010/03/08 6:36 UTC
Read the original article Hit count: 358

Filed under:
|
|
|
|

SOLVED: The problem was actually using time.time() every CPU cycle to see whether the next frame should be drawn or not. The time it takes to execute time.time() was having an impact on the FPS.

I made this function for drawing 2D textures as images in a 2D view in my OpenGL application. After doing some testing I found that it takes up 1-2 fps per texture. I know I am probably doing something wrong in this code. Any ideas? I am limiting the FPS to 60.

Edit: When I disable the texture rendering it adds about 15% fps back. When I disabled text rendering it adds about 15% fps back. When i disable both barely any fps is consumed anymore. IE: 20 out of 60 fps with both on. 30 out of 60 when one is disabled. 58 out of 60 when both are disabled.

When rendering the text on a button ( the control I'm using to test this ), it only "prepares" the text when the button label is set.

Updated code, still running at the same speed but still works the same:

def render2DTexture( self, texture, rect, texrect ):
    glEnable( GL_TEXTURE_2D )
    glBindTexture( GL_TEXTURE_2D, texture )
    glBegin( GL_QUADS )
    glTexCoord2f( texrect.left, texrect.bottom )
    glVertex2i( rect.left, self.windowSize[1] - rect.top )
    glTexCoord2f( texrect.right, texrect.bottom )
    glVertex2i( rect.left + rect.right, self.windowSize[1] - rect.top )
    glTexCoord2f( texrect.right, texrect.top )
    glVertex2i( rect.left + rect.right, self.windowSize[1] - ( rect.top + rect.bottom ) )
    glTexCoord2f( texrect.left, texrect.top )
    glVertex2i( rect.left, self.windowSize[1] - ( rect.top + rect.bottom ) )
    glEnd()
    glDisable( GL_TEXTURE_2D )

def prepareText( self, text, fontFace, color ):
    self.loadFont( fontFace )
    bmp = self.fonts[ fontFace ].render( text, 1, color )
    return ( pygame.image.tostring( bmp, 'RGBA', 1 ), bmp.get_width(), bmp.get_height() )

def renderText( self, pText, position ):
    glRasterPos2i( position[0], self.windowSize[1] - ( position[1] + pText[2] ) )
    glDrawPixels( pText[1], pText[2], GL_RGBA, GL_UNSIGNED_BYTE, pText[0] )

© Stack Overflow or respective owner

Related posts about python

Related posts about opengl