用Pyglet绘制基元

时间:2016-03-15 20:39:45

标签: python opengl pyglet

我是pyglet和opengl的新手,我不能让GL_LINES工作。 GL_POINTS没问题。我不断收到此错误:ValueError:只能分配相同大小的序列。显然我好像在定义错误的顶点数但是 伯爵对我来说似乎是正确的,我不知道如何解决它。

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)

    howmany=i=2
    coords=[];colors=[]
    while i>0:
        x=random.randint(0,window.width)
        y=random.randint(0,window.height)
        x2=random.randint(0,window.width)
        y2=random.randint(0,window.height)
        coords.append(x); coords.append(y); coords.append(x2); coords.append(y2)
        #c=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        #c2=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
        #for cx in c: colors.append(cx)
        #for cx in c2: colors.append(cx)
        i=i-1

    coords=tuple(coords)
    colors=tuple(colors)
    vertex_list=pyglet.graphics.vertex_list(howmany,('v2i', coords))#,('c3B', colors))
    vertex_list.draw(pyglet.gl.GL_LINES)

1 个答案:

答案 0 :(得分:4)

如果没问题的话,我会在解决你的问题的过程中先找到你的代码 逐个渲染质量数据将对系统造成极大的负担,因为在尝试渲染每个可能导致绘图过程中出现故障的逻辑操作时,您将执行逻辑运算。

为此,我会考虑尽快完成您的逻辑操作并将一组数据集中到图形卡上,为此,您需要使用名为batched rendering的东西。

@window.event
def on_draw():
    main_batch = pyglet.graphics.Batch()
    glClear(GL_COLOR_BUFFER_BIT)

    # for is less likely to get stuck than while
    for i in range(2):
        x = random.randint(0,window.width)
        y = random.randint(0,window.height)
        dest_x = random.randint(0,window.width)
        dest_y = random.randint(0,window.height)

        main_batch.add(2, gl.GL_LINES, None,
                            ('v2f', (x, y, dest_x, dest_y)),
                            ('c3B', (255, 0, 0, 0, 255, 0)) #c3B is a gradient (0,0,0, 255,255,255) combo
                                                            # this gradient will be per line, not end to end.
                        )
    main_batch.draw()

可能让您感到困惑的是2中的v2i以及color与行数的关系。请参阅示例中的howmany将定义cords中有多少个终点,而不是您有多少行。

意思是,如果你有2 lines,那就是4 endpoints,因为每一行都有一个起始和结束的坐标。解决方案是howmany*2,因为2D线上的位置总是固定不变。

然而,问题在于您还要为每一行确定颜色,这意味着colors列表的长度必须以特定方式与cords列表的长度匹配,因为他们会彼此直接相关。

因此,在上面的示例中,我将一次添加一行作为具有给定颜色的单个对象,它的效果稍差,但可以作为演示。一旦我添加了两行,我就会渲染批次。

如果你想做同样的操作,但有2行,你可以这样做:

main_batch.add(4, gl.GL_LINES, None,
                            ('v2i', (100,100, 120,100,   # start -> end
                                     120,100, 120,150))  # start -> end
                        )

现在4中的x,y -> x,y -> x,y -> x,y sa,4个corindates 现在我省略了这个代码块中的颜色,只是为了向您展示如何添加多线顶点线。此代码默认为白线,可以渲染。

你可以这样做:

 glColor3f(1, 0, 0) # Produces a red line
 main_batch.draw()

要更改该4线绳的颜色,或..您可以在创建线时定义颜色线。 c3B基本上意味着每个“定义”3种颜色,也就是您的标准RGB值。但是,每RGB个坐标需要一个x,y个定义。所以它看起来像这样:

main_batch.add(4, gl.GL_LINES, None,
                            ('v2i', (100,100, 120,100,   # start -> end
                                     120,100, 120,150)),  # start -> end

                             ('c3B', (0,0,0, 255,255,255,
                                      0,0,0, 255,255,255))
                        )

这将创建两条连接线,每条线都有一个黑色到白色的渐变,并将“形状”添加到批次中并一次性渲染整个批次。
您创建的下一个行形状可以添加到同一批次并在同一次迭代中渲染两行。

为了使其更紧凑,并将其转换回您最初设置列表的方式:

cords = (100,100, 120,100, 120,100, 120,150)
howmany = len(cords) / 2 # How many lines, not cordinates

colors = (0,0,0, 255,255,255)*howmany
main_batch.add(howmany, gl.GL_LINES, None, ('v2i', cords), ('c3B', colors))
相关问题