import sys, time, math from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * from dmsgl import * import videoInput videoWidth, videoHeight = 320,240 videoScale = 640/videoWidth videoInput.openVideo("/dev/video", videoWidth, videoHeight) prevFrame = videoInput.grabVideo() orthoCam = OrthoCamera(-0.5, videoWidth-0.5, -0.5, videoHeight-0.5, -1000, 1000) light = Light() light.position = [-1, 1, 2, 0] material = Material(diffuse=Color.White*1.5) texture1 = Texture2D('glitter.jpg') texture2 = Texture2D('panda.jpg') texture3 = Texture2D('aludiplt.jpg') texture = texture1 ballXform = SimpleTransform(translate=[videoWidth/2,videoHeight/4,0], rotateAxis=[0,1,0]) class videoButton: def __init__(self, x0, x1, y0, y1, texture): self.x0 = x0 self.x1 = x1 self.y0 = y0 self.y1 = y1 self.texture = texture def draw(self): self.texture.apply() glColor3f(1,1,1) glBegin(GL_QUADS) glTexCoord2f(0, 0) glVertex2f(self.x0, self.y0) glTexCoord2f(1, 0) glVertex2f(self.x1, self.y0) glTexCoord2f(1, 1) glVertex2f(self.x1, self.y1) glTexCoord2f(0, 1) glVertex2f(self.x0, self.y1) glEnd() self.texture.disable() def check(self, frame, prevFrame): count = 0 for x in range(self.x0, self.x1+1): for y in range(self.y0, self.y1+1): index = (x + (videoHeight-1-y)*videoWidth)*3 if abs(ord(frame[index]) - ord(prevFrame[index])) > 32: count += 1 return count > 64 buttons = [] buttons.append( videoButton(30,70,180,220,texture1) ) buttons.append( videoButton(140,180,180,220,texture2) ) buttons.append( videoButton(250,290,180,220,texture3) ) def draw(): orthoCam.apply() glDisable(GL_DEPTH_TEST) glRasterPos2f(0, videoHeight-1) glPixelZoom(videoScale, -videoScale) glDrawPixels(videoWidth, videoHeight, GL_RGB, GL_UNSIGNED_BYTE, prevFrame) glEnable(GL_DEPTH_TEST) glClear(GL_DEPTH_BUFFER_BIT) light.apply() material.apply() texture.apply() ballXform.pushApply() gluSphere(quadric, 40.0, 32, 16) ballXform.pop() texture.disable() material.disable() for b in buttons: b.draw() glutSwapBuffers() def keyboard(key, x, y): global texture, texture1, texture2, texture3 if key == chr(27): sys.exit(0) elif key == '1': texture = texture1 elif key == '2': texture = texture2 elif key == '3': texture = texture3 prevTime = time.time() def checkVideo(): global prevFrame, buttons, texture frame = videoInput.grabVideo() for b in buttons: if b.check(frame, prevFrame): texture = b.texture prevFrame = frame def update(): global prevTime t = time.time() dt = t - prevTime prevTime = t ballXform.rotateAngle += dt * 50 checkVideo() glutPostRedisplay() glutInit([]) glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(videoWidth*videoScale, videoHeight*videoScale) glutCreateWindow(sys.argv[0]) glutDisplayFunc(draw) glutKeyboardFunc(keyboard) glutIdleFunc(update) glEnable(GL_DEPTH_TEST) quadric = gluNewQuadric() gluQuadricTexture(quadric, GL_TRUE) glutMainLoop()