Pyglet Batch error "Value Error: Can only assign sequence of same size"
Pyglet Batch error "Value Error: Can only assign sequence of same size"
所以我正在尝试使用 pyglet 中的批处理 class 来制作这样的面孔
class Block:
def __init__(self, pos):
self.pos = pos
self.top = self.get_tex('img.png')
self.batch = pyglet.graphics.Batch()
tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
self.batch.add(4, GL_QUADS, self.top, ('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0]))), tex_coords)
def add(self, vec, toAdd):
vec = list(vec)
toAdd = list(toAdd)
vec[0] += toAdd[0]
vec[1] += toAdd[1]
vec[2] += toAdd[2]
return vec
def get_tex(self, file):
tex = pyglet.image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
但是我得到错误:
ValueError: Can only assign sequence of same size
参数必须是浮点列表或元组,而不是嵌套列表或带有元组元素的元组。
您的代码生成的是:
('v3f', ([0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]))
它应该生成的是:
('v3f', (0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0))
使用asterisk(*) operator来解压顶点坐标:
('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0])))
('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))
Class Block
class Block:
def __init__(self, pos):
self.pos = pos
self.batch = pyglet.graphics.Batch()
tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
vertices = ('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))
self.batch.add(4, GL_QUADS, self.top, vertices, tex_coords)
def add(self, vec, toAdd):
vec = list(vec)
toAdd = list(toAdd)
vec[0] += toAdd[0]
vec[1] += toAdd[1]
vec[2] += toAdd[2]
return vec
def get_tex(self, file):
tex = pyglet.image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
所以我正在尝试使用 pyglet 中的批处理 class 来制作这样的面孔
class Block:
def __init__(self, pos):
self.pos = pos
self.top = self.get_tex('img.png')
self.batch = pyglet.graphics.Batch()
tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
self.batch.add(4, GL_QUADS, self.top, ('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0]))), tex_coords)
def add(self, vec, toAdd):
vec = list(vec)
toAdd = list(toAdd)
vec[0] += toAdd[0]
vec[1] += toAdd[1]
vec[2] += toAdd[2]
return vec
def get_tex(self, file):
tex = pyglet.image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
但是我得到错误:
ValueError: Can only assign sequence of same size
参数必须是浮点列表或元组,而不是嵌套列表或带有元组元素的元组。
您的代码生成的是:
('v3f', ([0, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 0]))
它应该生成的是:
('v3f', (0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0))
使用asterisk(*) operator来解压顶点坐标:
('v3f', (self.add(pos, [0, 1, 0]), self.add(pos, [0, 1, 1]), self.add(pos, [1, 1, 1]), self.add(pos, [1, 1, 0])))
('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))
Class Block
class Block:
def __init__(self, pos):
self.pos = pos
self.batch = pyglet.graphics.Batch()
tex_coords = ('t2f',(0,0, 1,0, 1,1, 0,1, ))
vertices = ('v3f', (*self.add(pos, [0, 1, 0]), *self.add(pos, [0, 1, 1]), *self.add(pos, [1, 1, 1]), *self.add(pos, [1, 1, 0])))
self.batch.add(4, GL_QUADS, self.top, vertices, tex_coords)
def add(self, vec, toAdd):
vec = list(vec)
toAdd = list(toAdd)
vec[0] += toAdd[0]
vec[1] += toAdd[1]
vec[2] += toAdd[2]
return vec
def get_tex(self, file):
tex = pyglet.image.load(file).get_texture()
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)