import sys, time, math, Image from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from cave import * import os startDir = os.getcwd() + os.sep startTime = time.time() teapotOffset = 2 def draw(): glClearColor(0.5, 0.7, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_TEXTURE_2D) drawFloor() glBindTexture(GL_TEXTURE_2D, texture1) wandPos = CAVEGetPosition(CAVE_WAND) wandFront = CAVEGetVector(CAVE_WAND_FRONT) wandOri = CAVEGetOrientation(CAVE_WAND) glTranslatef(wandPos[0]+wandFront[0]*teapotOffset, wandPos[1]+wandFront[1]*teapotOffset, wandPos[2]+wandFront[2]*teapotOffset) glRotatef(wandOri[2], 0, 0, 1) glRotatef(wandOri[0], 1, 0, 0) glRotatef(wandOri[1], 0, 1, 0) glutSolidTeapot(2.0) texture1 = 0 texture2 = 0 angle = 0 def drawFloor(): glMatrixMode(GL_TEXTURE) glPushMatrix() glLoadIdentity() glRotatef(angle, 0, 0, 1) glMatrixMode(GL_MODELVIEW) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, texture2) glColor4f(1,1,1,1) glBegin(GL_QUADS) glTexCoord2f(-1,-1) glVertex3f(-10, 0, -10) glTexCoord2f(1,-1) glVertex3f(10, 0, -10) glTexCoord2f(1,1) glVertex3f(10, 0, 10) glTexCoord2f(-1,1) glVertex3f(-10, 0, 10) glEnd() glMatrixMode(GL_TEXTURE) glPopMatrix() glMatrixMode(GL_MODELVIEW) def initTexture(filename): img = Image.open(startDir + filename) img = img.transpose(Image.FLIP_TOP_BOTTOM) img = img.convert('RGB') 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) return textureID def update(): global angle, startTime, teapotOffset angle = (time.time()-startTime)*10 teapotOffset += CAVEGetWandJoystick()[1] * 0.1 texture1 = initTexture("clouds.jpg") texture2 = initTexture("panda.jpg")