import sys, math from OpenGL.GLUT import * from OpenGL.GL import * from OpenGL.GLU import * cameraDist = 15 cameraXrot = 0 cameraYrot = 0 def draw(): global cameraDist, cameraXrot, cameraYrot glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, 1, 0.1, 1000) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0, 0, -cameraDist) glRotatef(-cameraXrot, 1, 0, 0) glRotatef(-cameraYrot, 0, 1, 0) glLightfv(GL_LIGHT0, GL_POSITION, [-1, 0, 1, 0]) glMaterialfv(GL_FRONT, GL_DIFFUSE, [1, 0.7, 0, 1]) glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1]) glMaterialf(GL_FRONT, GL_SHININESS, 80) glShadeModel(GL_FLAT) drawRing() glutSwapBuffers() def drawRing(): glBegin(GL_TRIANGLE_STRIP) for theta in range(0,370,10): x = 5 * math.sin(theta * math.pi/180.0) z = 5 * math.cos(theta * math.pi/180.0) nx = math.sin(theta*math.pi/180.0) nz = math.cos(theta*math.pi/180.0) glNormal3f(nx, 0, nz) glVertex3f(x, -1, z) glVertex3f(x, 1, z) glEnd() def keyboard(key, x, y): global cameraDist if key == chr(27): sys.exit(0) elif key == '.': cameraDist = cameraDist - 1 elif key == ',': cameraDist = cameraDist + 1 glutPostRedisplay() def specialkey(key,x,y): global cameraXrot, cameraYrot if key == GLUT_KEY_LEFT: cameraYrot = cameraYrot - 3 elif key == GLUT_KEY_RIGHT: cameraYrot = cameraYrot + 3 elif key == GLUT_KEY_UP: cameraXrot = cameraXrot - 3 elif key == GLUT_KEY_DOWN: cameraXrot = cameraXrot + 3 glutPostRedisplay() glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(700,700) glutInitWindowPosition(0,0) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glutMainLoop()