import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * viewRotY = 30 viewRotX = 40 cameraVelX = 0 cameraVelY = 0 prevTime = time.time() def draw(): glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, 1.0, 1.0, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0, 0.0, -15.0) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) glDisable(GL_LIGHTING) glPushMatrix() glTranslatef(0, 4*math.sin(time.time()), 0) glColor3f(1,1,0) glutSolidSphere(0.25, 8, 4) glPopMatrix() glLightfv(GL_LIGHT0, GL_POSITION, [0, 4.0*math.sin(time.time()), 0, 1]) glLightfv(GL_LIGHT1, GL_POSITION, [4, -4, 0, 1]) glLightfv(GL_LIGHT1, GL_DIFFUSE, [1, 1, 1, 1]) glEnable(GL_LIGHTING) # glEnable(GL_LIGHT0) glEnable(GL_LIGHT1) glPushMatrix() glTranslatef(-4.0, 0.0, 0.0) glutSolidSphere(1.5, 16, 8) glPopMatrix() glPushMatrix() glTranslatef(4.0, 0.0, 0.0) glutSolidSphere(1.5, 16, 8) glPopMatrix() glPushMatrix() glTranslatef(0.0, 0.0, -4.0) glutSolidSphere(1.5, 16, 8) glPopMatrix() glPushMatrix() glTranslatef(0.0, 0.0, 4.0) glutSolidSphere(1.5, 16, 8) glPopMatrix() glDisable(GL_LIGHT1) glutSwapBuffers() def keyboard(key, x, y): if key == chr(27): sys.exit(0) def specialkey(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY += 90 elif key == GLUT_KEY_RIGHT: cameraVelY -= 90 elif key == GLUT_KEY_UP: cameraVelX += 90 elif key == GLUT_KEY_DOWN: cameraVelX -= 90 def specialkeyUp(key,x,y): global cameraVelX, cameraVelY if key == GLUT_KEY_LEFT: cameraVelY -= 90 elif key == GLUT_KEY_RIGHT: cameraVelY += 90 elif key == GLUT_KEY_UP: cameraVelX -= 90 elif key == GLUT_KEY_DOWN: cameraVelX += 90 def update(): global viewRotX, viewRotY, cameraVelX, cameraVelY global prevTime t = time.time() dt = t - prevTime prevTime = t viewRotX += cameraVelX * dt viewRotY += cameraVelY * dt glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(700,700) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glutMainLoop()