import sys, time, math import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * useMipmap = False viewDistance = 25.0 viewRotY = 0.0 viewRotX = 0.0 viewDistanceVel = 0.0 viewRotXVel = 0.0 viewRotYVel = 0.0 prevTime = time.time() textureID1 = 0 textureID2 = 0 def initTexture(filename): img = Image.open(filename) img = img.transpose(Image.FLIP_TOP_BOTTOM) global textureID1, textureID2 textureID1 = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, textureID1) 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) textureID2 = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, textureID2) 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_MIPMAP_LINEAR) gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, img.size[0], img.size[1], GL_RGB, GL_UNSIGNED_BYTE, img.tostring()) glBindTexture(GL_TEXTURE_2D, 0) def draw(): glClearColor(1, 1, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, 1.0, 1.0, 1000.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) glEnable(GL_TEXTURE_2D) if useMipmap: glBindTexture(GL_TEXTURE_2D, textureID2) else: glBindTexture(GL_TEXTURE_2D, textureID1) glColor3f(1, 1, 1) glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex3f(-10, -10, 0) glTexCoord2f(2, 0) glVertex3f(10, -10, 0) glTexCoord2f(2, 2) glVertex3f(10, 10, 0) glTexCoord2f(0, 2) glVertex3f(-10, 10, 0) glEnd() glDisable(GL_TEXTURE_2D) if useMipmap: drawLabel("GL_LINEAR_MIPMAP_LINEAR") else: drawLabel("GL_LINEAR") glutSwapBuffers() def drawLabel(label): glDisable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, 1.0, 0.0, 1.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glColor3f(0, 0, 0) glRasterPos2f(0.1, 0.05) for c in label: glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c)) glEnable(GL_DEPTH_TEST) def keyboard(key, x, y): global viewDistanceVel, useMipmap if key == chr(27): sys.exit(0) elif key == '.': viewDistanceVel -= 10 elif key == ',': viewDistanceVel += 10 elif key == ' ': useMipmap = not useMipmap def keyboardUp(key, x, y): global viewDistanceVel if key == '.': viewDistanceVel += 10 elif key == ',': viewDistanceVel -= 10 def specialkey(key,x,y): global viewRotXVel, viewRotYVel if key == GLUT_KEY_LEFT: viewRotYVel += 90 elif key == GLUT_KEY_RIGHT: viewRotYVel -= 90 elif key == GLUT_KEY_UP: viewRotXVel += 90 elif key == GLUT_KEY_DOWN: viewRotXVel -= 90 def specialkeyUp(key,x,y): global viewRotXVel, viewRotYVel if key == GLUT_KEY_LEFT: viewRotYVel -= 90 elif key == GLUT_KEY_RIGHT: viewRotYVel += 90 elif key == GLUT_KEY_UP: viewRotXVel -= 90 elif key == GLUT_KEY_DOWN: viewRotXVel += 90 def update(): global viewRotX, viewRotY, viewRotXVel, viewRotYVel, viewDistance, viewDistanceVel global prevTime t = time.time() dt = t - prevTime prevTime = t viewRotX += viewRotXVel * dt viewRotY += viewRotYVel * dt viewDistance += viewDistanceVel * dt glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(400,400) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutKeyboardUpFunc(keyboardUp) glutSpecialFunc(specialkey) glutSpecialUpFunc(specialkeyUp) glutIgnoreKeyRepeat(1) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) if len(sys.argv) > 1: initTexture(sys.argv[1]) else: initTexture("texture.tiff") glutMainLoop()