使用拉伸时pyglet()而不是事件循环(pyglet when using draw() inst

2019-09-28 17:01发布

我试图绘制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)

Answer 1:

你已经调用window.flip()来更新窗口。

因为你没有设置的投影矩阵,几何形状具有标准化设备坐标,这是在范围[-1,1]的所有3个组分(X,Y,Z)被绘制。 注意,pyglet设置默认的投影矩阵,当应用程序被启动pyglet.app.run()

呼叫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)


文章来源: pyglet when using draw() instead of eventloop