Created
September 24, 2013 12:49
-
-
Save davepape/6684218 to your computer and use it in GitHub Desktop.
simple animated car using push/pop matrix for hierarchical transformations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This program draws a moving "car" with rotating wheels | |
# It demonstrates the use of the glPushMatrix and glPopMatrix to | |
# create a hierarchy of transformations. | |
from pyglet.gl import * | |
window = pyglet.window.Window(500,500) | |
angle = 0 | |
carPosition = [100, 100, 0] | |
carDirection = 1 | |
carbody = pyglet.graphics.vertex_list(4, ('v2f', [-75,0, 75,0, -75,50, 75,50])) | |
wheel = pyglet.graphics.vertex_list(4, ('v2f', [-20,-20, 20,-20, -20,20, 20,20])) | |
def drawCar(): | |
glPushMatrix() | |
glTranslatef(carPosition[0], carPosition[1], carPosition[2]) | |
carbody.draw(GL_TRIANGLE_STRIP) | |
glColor3f(1, 1, 0) | |
glPushMatrix() | |
glTranslatef(-50, 0.0, 0.0) | |
glRotatef(angle, 0, 0, 1) | |
wheel.draw(GL_TRIANGLE_STRIP) | |
glPopMatrix() | |
glColor3f(1, 0, 0) | |
glPushMatrix() | |
glTranslatef(50, 0.0, 0.0) | |
glRotatef(angle, 0, 0, 1) | |
wheel.draw(GL_TRIANGLE_STRIP) | |
glPopMatrix() | |
glPopMatrix() | |
@window.event | |
def on_draw(): | |
glClear(GL_COLOR_BUFFER_BIT) | |
glLoadIdentity() | |
glColor3f(0, 0, 1) | |
drawCar() | |
def update(dt): | |
global angle, carPosition, carDirection | |
angle = angle - 180 * dt * carDirection | |
carPosition[0] = carPosition[0] + dt * carDirection * 200 | |
if carPosition[0] > 400: | |
carDirection = -1 | |
elif carPosition[0] < 100: | |
carDirection = 1 | |
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