Reputation: 13
Okay, so I'm a complete novice to both pyglet and OpenGL generally, I'm wondering why this doesn't work:
def DrawCircle(cx, cy, r, segments):
theta = 2*3.1415926/segments
c = math.cos(theta)
s = math.sin(theta)
x = r
y = 0
pyglet.gl.glBegin(pyglet.gl.GL_POLYGON)
pyglet.gl.glColor3f(0.05,0.2,0.9)
for counter in range(segments):
pyglet.gl.glVertex2f(x+cx, y+cy)
t = x
x = c * x - s * y
y = s * t + c * y
pyglet.gl.glEnd()
if __name__ == '__main__':
window = pyglet.window.Window(960,1010)
window.set_location(959,20)
@window.event
def on_draw():
window.clear()
def update(dt):
print(dt)
DrawCircle(500,500,20,30)
pyglet.clock.schedule_once(update,0.6)
pyglet.app.run()
Put simply, if I call DrawCircle from on_draw it creates a circle exactly as desired. If I schedule it nothing gets drawn. The scheduler itself is working because dt prints to console. I'm guessing that on_draw is setting the context or something which allows it to draw whereas when I schedule it the context or perhaps the buffer is wrong. Any help would be greatly appreciated.
Upvotes: 1
Views: 1504
Reputation: 435
Try adding window.switch_to()
above the draw call and window.flip()
after in the update function.
Although, as mentioned in msarch's answer, you should avoid drawing in an update funcion and let the on_draw function handle it.
Upvotes: 0
Reputation: 345
Here's the correct code :
import pyglet
import math
window = pyglet.window.Window(960,1010)
window.set_location( 50,50)
def DrawCircle(cx, cy, r, segments):
theta = 2*3.1415926/segments
c = math.cos(theta)
s = math.sin(theta)
x = r
y = 0
pyglet.gl.glColor3f(0.05,0.2,0.9)
pyglet.gl.glBegin(pyglet.gl.GL_POLYGON)
for counter in range(segments):
pyglet.gl.glVertex2f(x+cx, y+cy)
t = x
x = c * x - s * y
y = s * t + c * y
pyglet.gl.glEnd()
@window.event
def on_draw():
window.clear()
DrawCircle(500,500,20,30)
if __name__ == '__main__':
pyglet.app.run()
With Pyglet, all the drawings is usually done in the on_draw method. Since you clear the window at every redraw, you also have to draw your shape every time.
An update function isn't necessarily related to display
Scheduling once or at regular interval is generally used to modify various parameters on a regular interval basis (ie: x,y += dx,dy *dt for animation).
see :http://pyglet.org/doc/api/pyglet.clock-module.html
Upvotes: 1