Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 24, 2013 12:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save davepape/6684189 to your computer and use it in GitHub Desktop.
glPushMatrix / glPopMatrix example
import sys, random, time, math
from pyglet.gl import *
window = pyglet.window.Window()
pos1 = [0,0]
pos2 = [-1, 0]
heading1 = 0
heading2 = 90
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
drawBackground()
# First triangle
glPushMatrix()
glColor3f(1,0,0)
glTranslatef(pos1[0], pos1[1], 0)
glRotatef(heading1, 0, 0, 1)
drawTriangle()
glPopMatrix()
# Second triangle
glPushMatrix()
glColor3f(0,1,0)
glTranslatef(pos2[0], pos2[1], 0)
glRotatef(heading2, 0, 0, 1)
drawTriangle()
glPopMatrix()
def drawBackground():
random.seed(1)
glColor3f(1,1,1)
glBegin(GL_POINTS)
for i in range(0,200):
glVertex2f(random.uniform(-1,1), random.uniform(-1,1))
glEnd()
def drawTriangle():
glBegin(GL_TRIANGLES)
glVertex2f(-0.05, 0.0)
glVertex2f(0.05, 0.0)
glVertex2f(0.0, 0.15)
glEnd()
starttime = time.time()
def update(dt):
global pos1, pos2, heading1, heading2
t = time.time() - starttime
pos1 = [math.sin(t), math.cos(t)]
heading1 = -math.degrees(t) - 90
pos2 = [-math.sin(t*1.5)*0.75, math.cos(t*1.5)*0.5]
heading2 = math.degrees(t*1.5) + 90
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment