Skip to content

Instantly share code, notes, and snippets.

@davepape
Created September 24, 2013 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/6684206 to your computer and use it in GitHub Desktop.
Save davepape/6684206 to your computer and use it in GitHub Desktop.
another glPushMatrix/glPopMatrix example
import sys, random, time, math
from pyglet.gl import *
window = pyglet.window.Window(500,500)
pos1 = [0,0]
pos2 = [0, 0]
pos3 = [0, 0]
pos4 = [0, 0]
heading1 = 0
heading2 = 0
heading3 = 0
heading4 = 0
@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()
# First orbiter
glPushMatrix()
glColor3f(1,1,1)
glTranslatef(pos3[0], pos3[1], 0)
glRotatef(heading3, 0, 0, 1)
glScalef(0.3, 0.3, 0.3)
drawSquare()
glPopMatrix()
# Second orbiter
glPushMatrix()
glColor3f(1,1,0)
glTranslatef(pos4[0], pos4[1], 0)
glRotatef(heading4, 0, 0, 1)
glScalef(0.3, 0.3, 0.3)
drawSquare()
glPopMatrix()
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()
def drawSquare():
glBegin(GL_QUADS)
glVertex2f(-0.05, -0.05)
glVertex2f(0.05, -0.05)
glVertex2f(0.05, 0.05)
glVertex2f(-0.05, 0.05)
glEnd()
starttime = time.time()
def update(dt):
global pos1, pos2, pos3, pos4, heading1, heading2, heading3, heading4
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
pos3 = [math.sin(t*5)*0.2, math.cos(t*5)*0.2]
pos4 = [-math.sin(t*5+1)*0.2, math.cos(t*5+1)*0.2]
heading3 = math.degrees(t*7)
heading4 = math.degrees(t*8)
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