python - Pyglet vertex_list_indexed 异常

标签 python opengl pyglet vertex-buffer

我在 pyglet 中有一些渲染坐标轴的类:

class Axis(object):
    def __init__(self,
                 position=(0.0, 0.0, 0.0),
                 x_color=(1.0, 0.0, 0.0),
                 y_color=(0.0, 1.0, 0.0),
                 z_color=(0.0, 0.0, 1.0),
                 length=1.0):

        self.position = list(position)

        self.x_color = map(float, list(x_color))
        self.y_color = map(float, list(y_color))
        self.z_color = map(float, list(z_color))

        self.length = float(length)

        lines = (
            0, 1,
            0, 2,
            0, 3
        )

        vertices = (
            self.position[0], self.position[1], self.position[2],
            self.length, 0.0, 0.0,
            0.0, self.length, 0.0,
            0.0, 0.0, self.length
        )

        colors = (
            self.x_color[0], self.x_color[1], self.x_color[2],
            self.y_color[0], self.y_color[1], self.y_color[2],
            self.z_color[0], self.z_color[1], self.z_color[2]
        )

        self.vertex_list = pyglet.graphics.vertex_list_indexed(
            len(vertices) / 3,
            lines,
            ('v3f', vertices),
            ('c3f', colors),
        )

    def draw(self):
        self.vertex_list.draw(GL_LINES)

当我在程序中使用这段代码时出现异常:

Traceback (most recent call last):
  File "/home/linch/Project/modelviewer/window.py", line 163, in <module>
    window = ModelViewerWindow()
  File "/home/linch/Project/modelviewer/window.py", line 79, in __init__
    self.axis = Axis()
  File "/home/linch/Project/modelviewer/window.py", line 47, in __init__
    ('c4f', colors),
  File "/usr/local/lib/python2.7/dist-packages/pyglet/graphics/__init__.py", line 301, in vertex_list_indexed
    return _get_default_batch().add_indexed(count, 0, None, indices, *data)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/graphics/__init__.py", line 385, in add_indexed
    vlist._set_attribute_data(i, array)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/graphics/vertexdomain.py", line 413, in _set_attribute_data
    region.array[:] = data
ValueError: Can only assign sequence of same size

如果删除 ('c3f', colors) 一切正常,但没有颜色。我做错了什么?

最佳答案

您使用了 4 个顶点,但您的颜色数组只有 3 个条目。这些数组的大小需要匹配。每个不同的位置和颜色组合都需要一个顶点。所以要用 3 种不同的颜色画 3 条线,你需要 6 个顶点。它们中的 3 个共享相同位置这一事实并没有什么不同,因为您需要在该位置具有 3 种不同颜色的顶点。

此外,如果我正确理解了您要执行的操作,那么顶点位置看起来有一部分是错误的。第2、3、4个顶点看起来是相对于第一个顶点的,但都需要是绝对坐标。

总的来说,这些数组应该能满足您的需求:

vertices = (
    self.position[0], self.position[1], self.position[2],
    self.position[0] + self.length, self.position[1], self.position[2],
    self.position[0], self.position[1], self.position[2],
    self.position[0], self.position[1] + self.length, self.position[2],
    self.position[0], self.position[1], self.position[2],
    self.position[0], self.position[1], self.position[2] + self.length
)

colors = (
    self.x_color[0], self.x_color[1], self.x_color[2],
    self.x_color[0], self.x_color[1], self.x_color[2],
    self.y_color[0], self.y_color[1], self.y_color[2],
    self.y_color[0], self.y_color[1], self.y_color[2],
    self.z_color[0], self.z_color[1], self.z_color[2]
    self.z_color[0], self.z_color[1], self.z_color[2]
)

您还需要相应地调整索引列表以匹配新的顶点定义:

lines = (
    0, 1,
    2, 3,
    4, 5
)

但是,由于没有机会共享顶点,因此在这种情况下使用非索引几何体很可能更容易。

关于python - Pyglet vertex_list_indexed 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24106588/

相关文章:

c++ - glm::lookat,OpenGL 中的透视澄清

python - 在 Pyglet 中将可调整大小的窗口的大小锁定为窗口的原始宽高比

python - Pygame 的当前 GUI 选项

python - 如何创建只读文本 ctrl 但支持复制粘贴事件

python - 并发下载 - Python

OpenGL:什么是矩阵模式?

java - 如何将 3D 树叶的颜色与其下方的纹理融合?

pyglet - pyglet中找不到资源异常

python - jinja2 嵌套变量