问题描述
我正在尝试用pyglet画一个圆.但是当我使用app.run()循环的draw()函数instad时,它不可见.有什么建议我可以做什么?谢谢
I'm trying to draw a circle with pyglet. But it is not visible when I use the draw() function instad of the app.run() loop. Any suggestions what I can do? thanks
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)
推荐答案
您必须致电来更新窗口.
You've to call window.flip()
to update the window.
由于未设置投影矩阵,因此必须在归一化的设备坐标中绘制几何图形,该坐标在所有3个分量(x,y,z)的[-1,1]范围内.注意,在pyglet.app.run()
启动应用程序时,pyglet默认设置投影矩阵.
Since you don't have set a projection matrix, the geometry has to be draw in normalized device coordinates, which are in range [-1, 1] for all 3 components (x, y, z). Note, pyglet set a projection matrix by default when the application is started by 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
.例如:
Alternatively you can set an orthographic projection on your own, by glOrtho
. e.g.:
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)
这篇关于使用draw()而不是eventloop时的pyglet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!