import sys, time, math, os import Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * startDir = os.getcwd() useTexture = False modulateTexture = False 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 drawSquare(): glBegin(GL_QUADS) glTexCoord2f(0,0) glVertex2f(-3, -3) glTexCoord2f(1,0) glVertex2f(3, -3) glTexCoord2f(1,1) glVertex2f(3, 3) glTexCoord2f(0,1) glVertex2f(-3, 3) glEnd() def draw(): glClearColor(0, 0.3, 0.5, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(-10, 10, -10, 10) glMatrixMode(GL_MODELVIEW) glLoadIdentity() 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) glPushMatrix() glColor3f(1, 1, 1) glTranslatef(-5, 0, 0) drawSquare() glPopMatrix() glPushMatrix() glColor3f(0.9, 0.5, 0.2) glTranslatef(5, 0, 0) drawSquare() glPopMatrix() glDisable(GL_TEXTURE_2D) glColor3f(1, 1, 1) if not useTexture: drawBitmapString("No texture", [-8,-8]) elif modulateTexture: drawBitmapString("GL_MODULATE", [-8,-8]) else: drawBitmapString("GL_REPLACE", [-8,-8]) glutSwapBuffers() def keyboard(key, x, y): global viewDistance, modulateTexture, useTexture if key == chr(27): sys.exit(0) elif key == ' ': modulateTexture = not modulateTexture elif key == 't': useTexture = not useTexture def update(): glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(600,600) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) 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()