import sys import time from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * buttonMinX = 10 buttonMinY = 5 buttonMaxX = 100 buttonMaxY = 80 buttonPressed = 0 startTime = time.time() def draw(): global buttonPressed, buttonMinX, buttonMinY, buttonMaxX, buttonMaxY glClear(GL_COLOR_BUFFER_BIT) glLoadIdentity() if buttonPressed: glColor3f(1, 0, 0) else: glColor3f(0, 1, 1) glRectf(buttonMinX, buttonMinY, buttonMaxX, buttonMaxY) if buttonPressed: glColor3f(1, 1, 1) else: glColor3f(0, 0, 0) glPushMatrix() glTranslatef((buttonMinX+buttonMaxX)/2.0, (buttonMinY+buttonMaxY)/2.0, 0) glRotatef(60*(time.time() - startTime), 0, 1, 0) glutWireTeapot(20) glPopMatrix() glutSwapBuffers() def mouseButton(button, state, x, y): global buttonPressed, buttonMinX, buttonMinY, buttonMaxX, buttonMaxY y = glutGet(GLUT_WINDOW_HEIGHT) - y if (button == GLUT_LEFT_BUTTON) and (state == GLUT_DOWN): if (x >= buttonMinX) and (x <= buttonMaxX) and \ (y >= buttonMinY) and (y <= buttonMaxY): buttonPressed = not buttonPressed 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(glutPostRedisplay) glutMainLoop()