Pyglet 绘制带颜色的基元

Pyglet drawing Primitives with color

我正在创建一个带有网格的元胞自动机,但是当我尝试为单元格指定颜色时出现错误。如果我删除 "('c3B', (255,0,0))" 一切正常。

def on_draw(self):
    self.clear()
    self.predatorAndPrey.draw()

这里是函数:

    def draw(self):
    for row in range(0, self.grid_height):
        for col in range(0, self.grid_width):
            square_cords = (
                row * self.cell_size, col * self.cell_size,
                row * self.cell_size, col * self.cell_size + self.cell_size,
                row * self.cell_size + self.cell_size, col * self.cell_size + self.cell_size,
                row * self.cell_size + self.cell_size, col * self.cell_size
            )
            if self.cells[row][col][0] == 1:

                pyglet.graphics.draw_indexed(4, pyglet.gl.GL_QUADS,
                                             [0,1,2,3],
                                             ('v2i', square_cords),
                                             ('c3B', (255,0,0))
                                             )

这是错误:

File "E:/Python/Cellular Automata/Predator and Prey/Cellular Automata.py", line 23, in on_draw
    self.predatorAndPrey.draw()
  File "E:\Python\Cellular Automata\Predator and Prey\predator_and_prey.py", line 39, in draw
    ('c3B', (255,0,0))
  File "E:\Python\Cellular Automata\venv\lib\site-packages\pyglet\graphics\__init__.py", line 230, in draw_indexed
    'Data for %s is incorrect length' % format
AssertionError: Data for c3B is incorrect length

我从未使用过 Pyglet,但您是否尝试过为 alpha 添加最后一个 0:(255,0,0,0)? 阅读文档似乎需要它。

由于我的评论显然是有道理的,这里详细解释了它为什么起作用,并举例说明了如何正确使用它。

函数draw_indexed()是一种按索引顺序绘制图元的方法。原语可以是 pyglet.gl 上下文支持的任何东西,在本例中是 GL_QUADSGL_QUAD 对象至少需要一件事,那就是所有基元四个角的坐标。您在 square_cords 中提供它们,它们成对出现在两个 (X,Y) * 4 角(总共 8 件)。

您可以选择向 GL_QUAD 对象提供颜色信息。这指示渲染过程每个角应该映射到什么颜色。在您的情况下,您选择了发送颜色信息。

所有这些都基于两件事:v2i 表示 vertext 信息,每个顶点 2 个组件(表示 2D space)并且提供的数据是类型i(整数)。

颜色与 meaning/definition 相似。其中 c3B 表示颜色 c3 表示每个数据 blob(在本例中为 RGB)有 3 个项目,类型为 B(unsigned int)。但是颜色是 "tricky",因为它与前面 v2i 中顶点定义的数量密切相关。这意味着颜色与 QUAD 中的每一对(角)相关联。如果它是一条线,它将与线的开始和结束相关联。在一个点中,它将是一个单一的颜色定义,因为一个点只有一个 (x,y) 元素。但它总是与基元中预期的定义数量相关联 - 所以再次 - 你的四边形有 4 definitions/corners - 所以你需要 4 个颜色对。

因此,您需要将颜色定义 (255, 0, 0) 乘以角的次数。 (255, 0, 0) 因为你使用了 3B。你有 4 个角 ,所以 (255, 0, 0) * 4。实现这一目标的最简单方法是 (255,0,0) * int(len(square_cords)/2)/2 是因为 square_cords (x,y) 中每个角有 2 对 - 但您只想计算角的数量而不是位置。我希望这是有道理的。

所以,作为一个实际的例子,这里有一段代码说明了它是如何联系在一起的,希望这会教会你一些关于顶点、颜色以及 GL_*** 上下文如何期望某些东西 be/behave.祝你好运。

from pyglet import *
from pyglet.gl import *

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)
        self.x, self.y = 0, 0

        self.keys = {}

        self.mouse_x = 0
        self.mouse_y = 0

        self.cell_size = 20

        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_mouse_motion(self, x, y, dx, dy):
        self.mouse_x = x

    def on_key_release(self, symbol, modifiers):
        try:
            del self.keys[symbol]
        except:
            pass

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

        self.keys[symbol] = True

    def render(self):
        self.clear()

        square_cords = (
            self.cell_size, self.cell_size,
            self.cell_size, self.cell_size + self.cell_size,
            self.cell_size + self.cell_size, self.cell_size + self.cell_size,
            self.cell_size + self.cell_size, self.cell_size
        )

        color_pairs = (255,0,0) * int(len(square_cords)/2)

        pyglet.graphics.draw_indexed(4, pyglet.gl.GL_QUADS,
                                        [0,1,2,3],
                                        ('v2i', square_cords),
                                        ('c3B', color_pairs)
        )

        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            # -----------> This is key <----------
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()