import sys, time, math import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * filterNearest = True squareSize = 5 deltaSquareSize = 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) 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 drawSquare(): glBegin(GL_QUADS) glTexCoord2f(0,0) glVertex2f(-squareSize, -squareSize) glTexCoord2f(1,0) glVertex2f(squareSize, -squareSize) glTexCoord2f(1,1) glVertex2f(squareSize, squareSize) glTexCoord2f(0,1) glVertex2f(-squareSize, squareSize) glEnd() def draw(): glClearColor(1, 1, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-10, 10, -10, 10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, textureID) if filterNearest: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) else: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glColor3f(1, 1, 1) drawSquare() glDisable(GL_TEXTURE_2D) if filterNearest: drawLabel("GL_NEAREST") 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.25, 0.05) for c in label: glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, ord(c)) glEnable(GL_DEPTH_TEST) def keyboard(key, x, y): global filterNearest if key == chr(27): sys.exit(0) elif key == ' ': filterNearest = not filterNearest def specialkey(key,x,y): global deltaSquareSize if key == GLUT_KEY_UP: deltaSquareSize = 2 elif key == GLUT_KEY_DOWN: deltaSquareSize = -2 def specialkeyUp(key,x,y): global deltaSquareSize if key == GLUT_KEY_UP: deltaSquareSize = 0 elif key == GLUT_KEY_DOWN: deltaSquareSize = 0 def update(): global squareSize, deltaSquareSize global prevTime t = time.time() dt = t - prevTime prevTime = t squareSize += deltaSquareSize * dt glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(600,600) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) 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()