import sys, time, math, os import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * startDir = os.getcwd() 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_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 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) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, textureID) glBegin(GL_QUADS) # glTexCoord2f(0.25*math.sin(time.time()), 0.25) # glVertex3f(-5, -5, 0) glTexCoord2f(-2, -2) glVertex3f(-5+math.sin(time.time()), -5, 0) glTexCoord2f(5, -2) glVertex3f(5, -5, 0) glTexCoord2f(5, 3) glVertex3f(5,5,0) glTexCoord2f(-2, 3) glVertex3f(-5, 5, 0) glEnd() glDisable(GL_TEXTURE_2D) glutSwapBuffers() def keyboard(key, x, y): global viewDistance if key == chr(27): sys.exit(0) elif key == '.': viewDistance -= 1 elif key == ',': viewDistance += 1 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.jpg") quadric = gluNewQuadric() gluQuadricTexture(quadric, GL_TRUE) glutMainLoop()