Search code examples
pythonopenglpyglet

Fastest way to draw a ton of squares?


I have a collection of Square objects in a list called squares. I iterate over the list and draw each square individually, I'm wondering if there is a faster way to do this? I don't think GL_QUAD will work because that fills in the square when I just want the outline.

Here's my code

    for sq in squares:
            x1, y1 = sq.point
            x2, y2 = x1 + sq.length - 1, y1 + sq.length - 1
            batch.add(2, pyglet.gl.GL_LINES, None, ('v2i', (x1, y1, x1, y2)))
            batch.add(2, pyglet.gl.GL_LINES, None, ('v2i', (x1, y1, x2, y1)))
            batch.add(2, pyglet.gl.GL_LINES, None, ('v2i', (x2, y2, x1, y2)))
            batch.add(2, pyglet.gl.GL_LINES, None, ('v2i', (x2, y2, x2, y1)))

    batch.draw()

(x1,y1) would be the bottom left point of the square and (x2,y2) is the top right.


Solution

  • In OpenGL you can avoid polygon filling by calling:

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    

    To get filled polygons again use:

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    

    This way, you could use quads or 2 triangles. Using this(glPolygonMode) and vertexs arrays should greatly improve performance.