import sys import time from math import * from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * from buttonClass import * spinbutton = button() spinbutton.setLabel("Spin", 15) spinbutton.setGeometry(10, 300, 80, 50) bouncebutton = button() bouncebutton.setLabel("Bounce", 15) bouncebutton.setGeometry(250, 300, 100, 50) height = 0 angle = 0 def draw(): global spinbutton, bouncebutton, height, angle glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() spinbutton.draw() bouncebutton.draw() glTranslatef(glutGet(GLUT_WINDOW_WIDTH)/2.0-30.0, height, 0) glRotatef(angle, 0, 1, 0) glColor3f(0.5, 1, 0.3) glutWireTeapot(30) glutSwapBuffers() def mouseButton(button, state, x, y): global spinbutton, bouncebutton y = glutGet(GLUT_WINDOW_HEIGHT) - y if (button == GLUT_LEFT_BUTTON) and (state == GLUT_DOWN): bouncebutton.mouseClick(x,y) spinbutton.mouseClick(x,y) startTime = time.time() def update(): global spinbutton, bouncebutton, height, angle t = time.time() - startTime if spinbutton.isOn(): angle = 60*t if bouncebutton.isOn(): height = 200 * abs(sin(t)) glutPostRedisplay() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def reshape(width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, width, 0, height, -100, 100) glMatrixMode(GL_MODELVIEW) glutInit([]) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(400, 400) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutReshapeFunc(reshape) reshape(400,400) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutMouseFunc(mouseButton) glutIdleFunc(update) glutMainLoop()