import sys, time, math, os import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * startDir = os.getcwd() useLighting = False viewDistance = 6.0 viewRotY = 0.0 viewRotX = 0.0 cameraVelX = 0.0 cameraVelY = 0.0 prevTime = time.time() textureID = 0 def initTexture(filename): img = Image.open(filename) img = img.transpose(Image.FLIP_TOP_BOTTOM) global textureID textureID = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, textureID) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img.tostring()) glBindTexture(GL_TEXTURE_2D, 0) def initLighting(): if useLighting: glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glLightfv(GL_LIGHT0, GL_POSITION, [-1, 0.5, 1, 0]) else: glDisable(GL_LIGHTING) glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [1, 1, 1, 1]) 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, -viewDistance) glRotatef(viewRotX, 1.0, 0.0, 0.0) glRotatef(viewRotY, 0.0, 1.0, 0.0) initLighting() glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, textureID) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) gluSphere(quadric, 2, 32, 24) glDisable(GL_TEXTURE_2D) glutSwapBuffers() def drawBitmapString(text, font=GLUT_BITMAP_TIMES_ROMAN_24): for c in text: glutBitmapCharacter(font, ord(c)) def keyboard(key, x, y): global viewDistance, useLighting if key == chr(27): sys.exit(0) elif key == '.': viewDistance -= 1 elif key == ',': viewDistance += 1 elif key == ' ': useLighting = not useLighting 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(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) os.chdir(startDir) if len(sys.argv) > 1: initTexture(sys.argv[1]) else: initTexture("texture.tiff") quadric = gluNewQuadric() gluQuadricTexture(quadric, GL_TRUE) print "Hit space to switch between GL_REPLACE and GL_MODULATE" glutMainLoop()