import sys import time from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * def drawBitmapString(text, font=GLUT_BITMAP_TIMES_ROMAN_24): for c in text: glutBitmapCharacter(font, ord(c)) buttonMinX = 10 buttonMinY = 25 buttonMaxX = 170 buttonMaxY = 80 buttonPressed = 0 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) glColor3f(0.4, 0.2, 0.5) glRasterPos2f(buttonMinX+10, buttonMinY+10) drawBitmapString("Click Me") 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) glutMainLoop()