import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * camera = OrthoCamera(-10,10, -10,10, -10,10) light = Light() material = Material(diffuse=Color.Blue) ballXform = SimpleTransform() ballPos = Vector([0,0,0]) ballVel = Vector([8,4.5,0]) def draw(): Color.Black.clear() camera.apply() light.apply() material.apply() ballXform.pushApply() gluSphere(quadric, 1.0, 32, 16) ballXform.pop() material.disable() glutSwapBuffers() def keyboard(key, x, y): global viewDistance if key == chr(27): sys.exit(0) prevTime = time.time() def checkForBounce(): if ballPos[0] > 10.0: ballVel[0] = -ballVel[0] ballPos[0] = 10.0 - (ballPos[0] - 10.0) elif ballPos[0] < -10.0: ballVel[0] = -ballVel[0] ballPos[0] = -10.0 + (-10.0 - ballPos[0]) if ballPos[1] > 10.0: ballVel[1] = -ballVel[1] ballPos[1] = 10.0 - (ballPos[1] - 10.0) elif ballPos[1] < -10.0: ballVel[1] = -ballVel[1] ballPos[1] = -10.0 + (-10.0 - ballPos[1]) def update(): global prevTime, ballPos, ballVel, ballXform t = time.time() dt = t - prevTime prevTime = t ballPos += ballVel * dt checkForBounce() ballXform.translate = ballPos glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) quadric = gluNewQuadric() glutMainLoop()