python - Pyglet vertex_list_indexed exception -
i have class render coordinate axis in 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)
when use code in program exception:
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 assign sequence of same size
if remove ('c3f', colors)
works, without color. doing wrong?
you use 4 vertices, color array has 3 entries. sizes of arrays need match. need vertex each distinct combination of position , color. draw 3 lines in 3 different colors, need 6 vertices. fact 3 of them share same position doesn't make difference, because need vertices 3 different colors @ position.
also, if correctly understand you're trying do, vertex positions partly wrong. 2nd, 3rd, , 4th vertex relative 1st vertex, need absolute coordinates.
overall, these arrays should you're looking for:
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] )
you need adjust index list accordingly match new vertex definitions:
lines = ( 0, 1, 2, 3, 4, 5 )
however, since there's no opportunity share vertices, it's easier use non-indexed geometry in case.
Comments
Post a Comment