Pyglet绘制原始颜色

问题描述 投票:0回答:2

我正在创建一个带网格的元胞自动机,但当我尝试给单元格颜色时,我得到一个错误。如果我删除"('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
python colors primitive pyglet
2个回答
0
投票

由于我的评论显然是有意义的,所以这里有一个详细的解释,为什么它的工作,以及如何正确使用它的一个例子。

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

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

所有这些都基于两件事:v2i意思是vertext信息,每个顶点的2分量(意思是2D空间),所提供的数据是i(整数)类型。

颜色遵循类似的含义/定义。其中c3B表示c的颜色,3表示每个数据blob 3个项目(在这种情况下为RGB),类型为B(unsigned int)。但颜色是“棘手的”,因为它与v2i中早期的顶点定义数量密切相关。意味着颜色与QUAD中的每对(角落)相关联。如果它是一条线,它将与线的起点和终点相关联。在点中,它将是单一颜色定义,因为点只有一个(x,y)元素。但它总是与原语中预期的定义数量相关联 - 所以再次 - 你的四边形有4个定义/角落 - 所以你需要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_***上下文如何期望某些事物/行为的东西。祝你好运。

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()

0
投票

我从来没有使用过Pyglet但是你试过为alpha添加最后的0:(255,0,0,0)?阅读文档似乎需要它。

© www.soinside.com 2019 - 2024. All rights reserved.