如何使用 opengl 在 pyglet 中批量绘制图元?
How to batch draw primitives in pyglet with opengl?
我正在尝试使用 pyglet 批量绘制图元,但我连一个简单的示例都无法运行。
我能够单独绘制事物,但我的理解是最好的做法是将元素放入批次中。
这是一些非常基本的代码,但它不起作用。我收到错误: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()
我觉得我肯定遗漏了一些明显的东西。
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)
)
我正在尝试使用 pyglet 批量绘制图元,但我连一个简单的示例都无法运行。
我能够单独绘制事物,但我的理解是最好的做法是将元素放入批次中。
这是一些非常基本的代码,但它不起作用。我收到错误: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()
我觉得我肯定遗漏了一些明显的东西。
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)
)