import sys, time, math, os import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * startDir = os.getcwd() useTexture = False modulateTexture = False viewDistance = 15.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 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) if useTexture: glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, textureID) else: glDisable(GL_TEXTURE_2D) if modulateTexture: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) else: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) glColor3f(1, 1, 1) glTranslatef(-3, 0, 0) gluSphere(quadric, 2, 32, 24) glColor3f(0.9, 0.5, 0.2) glTranslatef(6, 0, 0) gluSphere(quadric, 2, 32, 24) glDisable(GL_TEXTURE_2D) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, 1.0, 0.0, 1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glColor3f(1, 1, 1) glRasterPos2f(0.25, 0.05) if not useTexture: drawBitmapString("No texture") elif modulateTexture: drawBitmapString("GL_MODULATE") else: drawBitmapString("GL_REPLACE") 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, modulateTexture, useTexture if key == chr(27): sys.exit(0) elif key == '.': viewDistance -= 1 elif key == ',': viewDistance += 1 elif key == ' ': modulateTexture = not modulateTexture elif key == 't': useTexture = not useTexture 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 't' to enable/disable texturing Hit space to switch between GL_REPLACE and GL_MODULATE""" glutMainLoop()