简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM