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] class videoButton: def __init__(self, x0, x1, y0, y1): self.x0 = x0 self.x1 = x1 self.y0 = y0 self.y1 = y1 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 > 32 class videoObject: def __init__(self, button, transform, object, sound): self.button = button self.transform = transform self.object = object self.sound = sound self.soundChannel = None self.active = False self.startTime = 0 self.endTime = 0 def update(self, frame, prevFrame): if self.button.check(frame, prevFrame): if not self.active: self.startTime = time.time() self.endTime = time.time() + 0.5 self.active = True self.playSound() elif self.active: if time.time() > self.endTime: self.active = False def playSound(self): if self.soundChannel: if self.soundChannel.get_busy(): return self.soundChannel = self.sound.play() def draw(self): self.transform.pushApply() if self.active: t = time.time() - self.startTime glTranslatef(math.sin(t*30)*6, math.cos(t*10)*6, 0) self.object.draw() self.transform.pop() if 0: glColor3f(1,1,0) glBegin(GL_LINE_LOOP) glVertex2f(self.button.x0, self.button.y0) glVertex2f(self.button.x1, self.button.y0) glVertex2f(self.button.x1, self.button.y1) glVertex2f(self.button.x0, self.button.y1) glEnd() 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() for o in objects: o.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 frame = videoInput.grabVideo() for o in objects: o.update(frame, prevFrame) prevFrame = frame def update(): global prevTime t = time.time() dt = t - prevTime prevTime = t 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) import pygame pygame.mixer.init() cowobj = WFObject('cow.obj') objects = [] objects.append( videoObject(videoButton(50,110,50,90), SimpleTransform(translate=[80,70,0]), cowobj, pygame.mixer.Sound("cow.wav") ) ) objects.append( videoObject(videoButton(130,190,160,220), SimpleTransform(translate=[160,190,0]), cowobj, pygame.mixer.Sound("quack.wav") ) ) objects.append( videoObject(videoButton(200,260,80,120), SimpleTransform(translate=[230,100,0], rotateAxis=[0,1,0], rotateAngle=180), cowobj, pygame.mixer.Sound("cow.wav") ) ) glutMainLoop()