0

我一直在尝试让批处理在 pyglet 中工作,但我完全被来自pyglet/graphics/__init__.py文件的错误消息“太多的值无法解包”弄糊涂了。我的猜测是,在将几何图形添加到批处理时,我在语法上做错了。

我将代码缩减为产生错误的基本部分:

from pyglet.gl import *
from pyglet.graphics import *
import pyglet

batch = pyglet.graphics.Batch()
img = pyglet.image.load('pic.png')
texture = img.get_texture()

class TextureEnableGroup(pyglet.graphics.Group):
    def set_state(self):
        glEnable(GL_TEXTURE_2D)
    def unset_state(self):
        glDisable(GL_TEXTURE_2D)

texture_enable_group = TextureEnableGroup()

class TextureBindGroup(pyglet.graphics.Group):
    def __init__(self, texture):
        super(TextureBindGroup, self).__init__(parent=texture_enable_group)
        self.texture = texture
    def set_state(self):
        glBindTexture(GL_TEXTURE_2D, self.texture.id)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    def __eq__(self, other):
        return (self.__class__ is other.__class__ and self.texture == other.__class__)

batch.add(12, GL_TRIANGLES, TextureBindGroup(texture), (('t2f', (0, 0)), ('v3f', (64, 64, 0)), ('t2f', (1, 1)), ('v3f', (-64, -64, 205)), ('t2f', (0, 1)), ('v3f', (-64, 64, 205)), ('t2f', (1, 1)), ('v3f', (64, -64, 205)), ('t2f', (1, 0)), ('v3f', (64, 64, 0)), ('t2f', (0, 1)), ('v3f', (-64, -64, 205))))
4

4 回答 4

1

您的问题出在这一行:

batch.add(12, GL_TRIANGLES, TextureBindGroup(texture), (('t2f', (0, 0)), ('v3f', (64, 64, 0)), ('t2f', (1, 1)), ('v3f', (-64, -64, 205)), ('t2f', (0, 1)), ('v3f', (-64, 64, 205)), ('t2f', (1, 1)), ('v3f', (64, -64, 205)), ('t2f', (1, 0)), ('v3f', (64, 64, 0)), ('t2f', (0, 1)), ('v3f', (-64, -64, 205))))

我认为应该是:

batch.add(12, GL_TRIANGLES, TextureBindGroup(texture), ('t2f', (0, 0)), ('v3f', (64, 64, 0)), ('t2f', (1, 1)), ('v3f', (-64, -64, 205)), ('t2f', (0, 1)), ('v3f', (-64, 64, 205)), ('t2f', (1, 1)), ('v3f', (64, -64, 205)), ('t2f', (1, 0)), ('v3f', (64, 64, 0)), ('t2f', (0, 1)), ('v3f', (-64, -64, 205)))

请注意我如何将最后一个参数从格式更改((tuple), (tuple))(tuple), (tuple)). 我不熟悉 pyglet,但发现这是batch.add()文档中调用的正确方法。请注意,它*data表示函数调用结束时的参数变量列表,而不是您尝试的元组或列表。

尝试一下,让我们知道它对您的效果如何。

于 2010-12-20T22:58:44.150 回答
0

“解包的值太多”是您在执行以下操作时遇到的错误

a, b = "a b c".split(" ")

拆分返回三个值,但您尝试将它们分成两个。我猜你在最后一行的某处有一个括号错误。尝试为此使用一些更清晰的语法。就像现在一样,它非常可怕且难以理解。

于 2010-12-20T22:47:48.680 回答
0

我猜你的第四个参数batch.add()不遵循 pyglet 期望的格式。检查这一点可能很有用。

此外,查看 pyglet 中发生错误的行可以为您提供更多信息。问题很可能是您传递给 pyglet 函数的参数有问题。

PS:可能会感兴趣的文档add

于 2010-12-20T22:50:19.553 回答
0

感谢 marcog,脚本的正确最后一行是:

batch.add(6, GL_TRIANGLES, TextureBindGroup(texture), ('v3i', (64, 64, 0, -64, -64, 205, -64, 64, 205, 64, -64, 205, 64, 64, 0, -64, -64, 205)), ('t2i', (0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1)))

即问题解决了=)

问题部分在于我将所有数据作为单个元组发送(marcog 指出),以及传递了一批几何数据长度的错误值;6 个顶点而不是 12 个。

于 2010-12-21T00:40:36.933 回答