import sys, time from math import * from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * viewRotX = 0 viewRotY = 0 light0Pos = [ -5, 4, 0, 1 ] light1Pos = [ 5, 4, 0, 1 ] attenuate = False light0On = True light1On = False currentLight = 0 def draw(): glClearColor(0.5, 0.8, 1.0, 0.0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, 1.75, 1.0, 100.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslatef(0.0, 0.0, -25.0) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) defineLight() glEnable(GL_LIGHTING) global floorMeshDList glCallList(floorMeshDList) drawTeapot() drawPedestal() drawBall() glDisable(GL_LIGHTING) glutSwapBuffers() def defineLight(): global light0On, light1On, light0Pos, light1Pos white = [ 1, 1, 1, 1 ] if light0On: glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0, GL_DIFFUSE, white) glLightfv(GL_LIGHT0, GL_POSITION, light0Pos) if attenuate: glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1) glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.1) glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0) else: glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1) glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0) glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0) glPointSize(12.0) glBegin(GL_POINTS) glVertex3fv(light0Pos) glEnd() else: glDisable(GL_LIGHT0) if light1On: glEnable(GL_LIGHT1) glLightfv(GL_LIGHT1, GL_DIFFUSE, white) glLightfv(GL_LIGHT1, GL_SPECULAR, white) glLightfv(GL_LIGHT1, GL_POSITION, light1Pos) if attenuate: glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1) glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0) glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.1) else: glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1) glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0) glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0) glPointSize(12.0) glBegin(GL_POINTS) glVertex3fv(light1Pos) glEnd() else: glDisable(GL_LIGHT1) def drawFloorMesh(columns,rows): green = [ 0, 0.8, 0.1, 1 ] black = [ 0, 0, 0, 1 ] glMaterialfv(GL_FRONT, GL_AMBIENT, green) glMaterialfv(GL_FRONT, GL_DIFFUSE, green) glMaterialfv(GL_FRONT, GL_SPECULAR, black) glNormal3f(0.0, 1.0, 0.0) for j in range(0,rows): glBegin(GL_TRIANGLE_STRIP) for i in range(0,columns): x = ((float(i)) / columns) * 40.0 - 20.0 z = ((float(j)) / rows) * 40.0 - 20.0 glVertex3f(x, -2.0, z) z = ((float(j)+1) / rows) * 40.0 - 20.0 glVertex3f(x, -2.0, z) glEnd() def drawTeapot(): red = [ 0.8, 0, 0, 1 ] white = [ 1, 1, 1, 1 ] glMaterialfv(GL_FRONT, GL_AMBIENT, red) glMaterialfv(GL_FRONT, GL_DIFFUSE, red) glMaterialfv(GL_FRONT, GL_SPECULAR, white) glMaterialf(GL_FRONT, GL_SHININESS, 90.0) glPushMatrix() glTranslatef(2.0, 4.0, -1.0) glRotatef(currentTime() * 30.0, 0.0, 1.0, 0.0) global teapotDList glCallList(teapotDList) glPopMatrix() quadric = None def drawPedestal(): global quadric grey = [ 0.6, 0.6, 0.6, 1 ] white = [ 1, 1, 1, 1 ] if not quadric: quadric = gluNewQuadric() glMaterialfv(GL_FRONT, GL_AMBIENT, grey) glMaterialfv(GL_FRONT, GL_DIFFUSE, grey) glMaterialfv(GL_FRONT, GL_SPECULAR, white) glMaterialf(GL_FRONT, GL_SHININESS, 30.0) glPushMatrix() glTranslatef(2.0, -2.0, -1.0) glRotatef(-90.0, 1.0, 0.0, 0.0) gluCylinder(quadric, 4.0, 4.0, 4.0, 16, 1) glTranslatef(0.0, 0.0, 4.0) gluDisk(quadric, 0.0, 4.0, 16, 1) glPopMatrix() def drawBall(): cyan = [ 0, 1, 1, 1 ] black = [ 0, 0, 0, 1 ] glMaterialfv(GL_FRONT, GL_AMBIENT, cyan) glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan) glMaterialfv(GL_FRONT, GL_SPECULAR, black) glPushMatrix() glTranslatef(-10.0, fabs(sin(currentTime())) * 3.0, -15.0) glutSolidSphere(2.0, 16, 8) glPopMatrix() def drawBitmapString(text, font=GLUT_BITMAP_TIMES_ROMAN_24): for c in text: glutBitmapCharacter(font, ord(c)) startTime = time.time() def currentTime(): return time.time() - startTime def keyboard(key, x, y): global attenuate, light0On, light1On if key == chr(27): sys.exit(0) elif key == 'a': attenuate = not attenuate elif key == '0': light0On = not light0On elif key == '1': light1On = not light1On def specialkey(k, x, y): global viewRotX, viewRotY if k == GLUT_KEY_LEFT: viewRotY += 3 elif k == GLUT_KEY_RIGHT: viewRotY -= 3 elif k == GLUT_KEY_UP: viewRotX += 3 elif k == GLUT_KEY_DOWN: viewRotX -= 3 def mousebutton(button, state, x, y): global currentLight if (button == GLUT_LEFT_BUTTON) and (state == GLUT_DOWN): currentLight = 0 elif (button == GLUT_RIGHT_BUTTON) and (state == GLUT_DOWN): currentLight = 1 def mousemotion(x, y): global light0Pos, light1Pos, currentLight if currentLight == 0: light0Pos[0] = float(x) / glutGet(GLUT_WINDOW_WIDTH) * 40.0 - 20.0 light0Pos[2] = float(y) / glutGet(GLUT_WINDOW_HEIGHT) * 40.0 - 20.0 elif currentLight == 1: light1Pos[0] = float(x) / glutGet(GLUT_WINDOW_WIDTH) * 40.0 - 20.0 light1Pos[2] = float(y) / glutGet(GLUT_WINDOW_HEIGHT) * 40.0 - 20.0 glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(700,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutMouseFunc(mousebutton) glutMotionFunc(mousemotion) glutIdleFunc(glutPostRedisplay) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) floorMeshDList = glGenLists(1) glNewList(floorMeshDList, GL_COMPILE) drawFloorMesh(32,32) glEndList() teapotDList = glGenLists(1) glNewList(teapotDList, GL_COMPILE) glutSolidTeapot(2) glEndList() glutMainLoop()