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, 30, 80, 50) bouncebutton = button() bouncebutton.setLabel("Bounce", 15) bouncebutton.setGeometry(120, 30, 100, 50) height = 0 angle = 0 def drawControls(): global spinbutton, bouncebutton glClearColor(0.2, 0.3, 0.6, 0) glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() spinbutton.draw() bouncebutton.draw() glutSwapBuffers() def draw(): global height, angle glClearColor(0,0,0,0) glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() glTranslatef(0, height, -8) glRotatef(angle, 0, 1, 0) glColor3f(0.5, 1, 0.3) glutWireTeapot(1) 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) glutSetWindow(controlwindow) glutPostRedisplay() startTime = time.time() def update(): global spinbutton, bouncebutton, height, angle t = time.time() - startTime if spinbutton.isOn(): angle = 60*t if bouncebutton.isOn(): height = 4 * abs(sin(t)) glutSetWindow(mainwindow) 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) mainwindow = glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, 1, 0.1, 100) glMatrixMode(GL_MODELVIEW) glutInitWindowSize(240, 100) glutInitWindowPosition(500,0) controlwindow = glutCreateWindow("controls") glutDisplayFunc(drawControls) glutReshapeFunc(reshape) glutKeyboardFunc(keyboard) glutMouseFunc(mouseButton) reshape(240,100) glutIdleFunc(update) glutMainLoop()