# Example of all GLUT geometry functions, with lighting enabled # # Dave Pape # 27 October 2003 import sys from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * viewRotY = 0 viewRotX = 0 useLighting = False def draw(): 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) if useLighting: glEnable(GL_LIGHTING) else: glDisable(GL_LIGHTING) glLoadIdentity() glTranslatef(0.0, 0.0, -30.0) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) glColor3f(1.0, 1.0, 1.0) glPushMatrix() glTranslatef(-12.0, 0.0, 0.0) glutSolidSphere(1.5, 16, 8) glTranslatef(0.0, 4.0, 0.0) glutWireSphere(1.5, 40, 8) glPopMatrix() glColor3f(1.0, 1.0, 0.0) glPushMatrix() glTranslatef(-8.0, 0.0, 0.0) glutSolidCube(2.0) glTranslatef(0.0, 4.0, 0.0) glutWireCube(2.0) glPopMatrix() glColor3f(0.0, 1.0, 1.0) glPushMatrix() glTranslatef(-4.0, 0.0, 0.0) glutSolidCone(1.0, 1.5, 16, 4) glTranslatef(0.0, 4.0, 0.0) glutWireCone(1.0, 1.5, 16, 4) glPopMatrix() glColor3f(0.0, 1.0, 0.0) glPushMatrix() glTranslatef(0.0, 0.0, 0.0) glutSolidTorus(0.5, 1.5, 8, 16) glTranslatef(0.0, 4.0, 0.0) glutWireTorus(0.5, 1.5, 8, 16) glPopMatrix() glColor3f(1.0, 0.0, 1.0) glPushMatrix() glTranslatef(4.0, 0.0, 0.0) glutSolidDodecahedron() glTranslatef(0.0, 4.0, 0.0) glutWireDodecahedron() glPopMatrix() glColor3f(1.0, 0.0, 0.0) glPushMatrix() glTranslatef(8.0, 0.0, 0.0) glutSolidOctahedron() glTranslatef(0.0, 4.0, 0.0) glutWireOctahedron() glPopMatrix() glColor3f(0.0, 0.0, 1.0) glPushMatrix() glTranslatef(12.0, 0.0, 0.0) glutSolidTetrahedron() glTranslatef(0.0, 4.0, 0.0) glutWireTetrahedron() glPopMatrix() glColor3f(1.0, 1.0, 0.0) glPushMatrix() glTranslatef(-6.0, -4.0, 0.0) glutSolidIcosahedron() glTranslatef(0.0, -4.0, 0.0) glutWireIcosahedron() glPopMatrix() glColor3f(1.0, 0.0, 0.0) glPushMatrix() glTranslatef(6.0, -4.0, 0.0) glutSolidTeapot(2.0) glTranslatef(0.0, -4.0, 0.0) glutWireTeapot(2.0) glPopMatrix() glutSwapBuffers() def keyboard(key, x, y): global useLighting if key == chr(27): sys.exit(0) elif key == ' ': useLighting = not useLighting glutPostRedisplay() def specialkey(key,x,y): global viewRotX, viewRotY if key == GLUT_KEY_LEFT: viewRotY = viewRotY + 3 elif key == GLUT_KEY_RIGHT: viewRotY = viewRotY - 3 elif key == GLUT_KEY_UP: viewRotX = viewRotX + 3 elif key == GLUT_KEY_DOWN: viewRotX = viewRotX - 3 glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(700,700) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glutMainLoop()