创建pyglet批处理时出现“值解包过多”错误

0 投票
4 回答
770 浏览
提问于 2025-04-16 08:48

我一直在尝试在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 个回答

0

我猜你的 batch.add() 方法的第四个参数格式可能不符合 pyglet 的要求。检查一下这个参数可能会有帮助。

另外,看看 pyglet 中出错的那几行代码,可能会给你更多的信息。问题很可能是你传给 pyglet 函数的参数有问题。

顺便说一下,这个文档关于 add 的内容可能会对你有帮助。

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 代表的是函数调用最后的一个可变参数列表,而不是你尝试使用的元组或列表。

试试看这个改动,告诉我们效果如何。

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指出的),还有就是传递了一个错误的几何数据批次长度;应该是12个顶点,结果我用了6个。

撰写回答