使用draw()而不是eventloop时的pyglet

时间:2019-06-25 07:08:44

标签: python opengl pyglet

我正在尝试用pyglet画一个圆。但是当我使用app.run()循环的draw()函数instad时,它不可见。有什么建议我可以做什么?谢谢

from math import *
from pyglet.gl import *

window = pyglet.window.Window()

def makeCircle(x_pos, y_pos, radius, numPoints):
    verts = []
    glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
    glColor3f(1,1,0)
    for i in range(numPoints):
        angle = radians(float(i)/numPoints * 360.0)
        x = radius *cos(angle) + x_pos
        y = radius *sin(angle) + y_pos
        verts += [x,y]
    circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
    circle.draw(GL_LINE_LOOP)
    input()

makeCircle(5,5, 100, 10)

1 个答案:

答案 0 :(得分:1)

您必须致电window.flip()来更新窗口。

由于未设置投影矩阵,因此必须在归一化的设备坐标中绘制几何图形,该坐标在所有3个分量(x,y,z)的[-1,1]范围内。请注意,在pyglet.app.run()启动应用程序时,pyglet默认设置投影矩阵。

调用window.flip()并更改几何形状:

from math import *
from pyglet.gl import *

window = pyglet.window.Window()

def makeCircle(x_pos, y_pos, radius, numPoints):
    verts = []
    glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
    glColor3f(1,1,0)
    for i in range(numPoints):
        angle = radians(float(i)/numPoints * 360.0)
        x = radius *cos(angle) + x_pos
        y = radius *sin(angle) + y_pos
        verts += [x,y]
    circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
    circle.draw(GL_LINE_LOOP)

    window.flip()           # <--------

    input()

makeCircle(0, 0, 0.5, 10)   # <--------

或者,您可以通过glOrtho自行设置正交投影。例如:

from math import *
from pyglet.gl import *

window = pyglet.window.Window()

def makeCircle(x_pos, y_pos, radius, numPoints):
    verts = []

    glMatrixMode(GL_PROJECTION)
    glOrtho(0, 640, 0, 480, -1, 1)
    glMatrixMode(GL_MODELVIEW)

    glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
    glColor3f(1,1,0)
    for i in range(numPoints):
        angle = radians(float(i)/numPoints * 360.0)
        x = radius *cos(angle) + x_pos
        y = radius *sin(angle) + y_pos
        verts += [x,y]
    circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
    circle.draw(GL_LINE_LOOP)

    text = 'This is a test but it is not visible'
    label = pyglet.text.Label(text, font_size=36,
                          x=10, y=10, anchor_x='left', anchor_y='bottom',
                          color=(255, 123, 255, 255))
    label.draw()

    window.flip()
    input()

makeCircle(5,5, 100, 10)