簡體   English   中英

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

[英]pyglet when using draw() instead of eventloop

我正在嘗試用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)

您必須調用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM