简体   繁体   中英

How to batch draw primitives in pyglet with opengl?

I'm trying to batch draw primitives with pyglet, but I can't even get a simple example to work.

I was able to draw things individually, but my understanding is that best practice is to put elements into batches.

Here is some very basic code, yet it is not working. I am getting the error: AttributeError: 'tuple' object has no attribute 'parent'

import numpy as np
import pyglet

WIDTH = 640
HEIGHT = 480

game_window = pyglet.window.Window(width=WIDTH, height=HEIGHT)
batch = pyglet.graphics.Batch()

def update(dt):
    global t
    batch = pyglet.graphics.Batch()
    batch.add(2, pyglet.gl.GL_LINES, ('v2f', (100,100,200,200)),
              ('c3B', (255,0,0) * 2))
    batch.add(2, pyglet.gl.GL_LINES, ('v2f', (400,400,50,50)),
              ('c3B', (255,0,0) * 2))

@game_window.event
def on_draw():
    game_window.clear()
    batch.draw()

if __name__ == '__main__':
    pyglet.clock.schedule_interval(update, 1/120)
    pyglet.app.run()

I feel like I must be missing something obvious.

The 3rd parameter of pyglet.graphics.Batch.add is the pyglet.graphics.Group object. If you don't associate the batch to a group, this parameter can be None . See Batched rendering :

def update(dt):

    #batch = pyglet.graphics.Batch()         <---- delete 
    
    batch.add(2, pyglet.gl.GL_LINES, None, # <---- add None
        ('v2f', (100,100,200,200)),
        ('c3B', (255,0,0) * 2)
    )
    batch.add(2, pyglet.gl.GL_LINES, None, # <---- add None
        ('v2f', (400,400,50,50)),
        ('c3B', (255,0,0) * 2)
    )

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