def main(): # * Initialize graphics subsystem initGraphics() # * Open camera and create GLCameraViewer instance camera = cv2.VideoCapture(0) # NOTE: Live camera can be substituted with recorded video here cameraViewer = GLCameraViewer(camera) # * Main GLFW loop while glfw.GetWindowParam(glfw.OPENED): # ** Handle events glfw.PollEvents() if glfw.GetKey(glfw.KEY_ESC): break # ** Run cameraViewer through one iteration of processing cameraViewer.capture() cameraViewer.process() # ** Clear current output and render glClear(GL_COLOR_BUFFER_BIT) cameraViewer.render() # ** Present rendered output glfw.SwapBuffers() # * Clean up cameraViewer.cleanUp() camera.release() glfw.Terminate()
def RenderHead(folderPath, headMeshName, textureName, capturePath=None): global orthoSize, currentHandle print folderPath if capturePath != None: capturePath = os.path.join(capturePath, os.path.basename(folderPath)) mesh = LoadObj(os.path.join(folderPath, headMeshName)) orthoSize = mesh.radius * 1.5 print 'mesh center = ', mesh.center print 'mesh radius = ', mesh.radius print 'len texturecoord = ', len(mesh.textureCoords) print 'len faces = ', len(mesh.faces) print 'len vertex = ', len(mesh.vertexs) # PrintList(mesh.vertexs) textureImg = Image.Image() textureImg = Image.open(os.path.join(folderPath, textureName)) textureHandle = None arr = PIL2array(textureImg) # tempImage = array2PIL(arr, textureImg.size) # textureImg.save(os.path.join(folderPath, 'temp_texture.jpg')) # print arr.size # print textureImg.format print 'texture size = ', textureImg.size # print textureImg.size[0], textureImg.size[1] # print textureImg.info print list(textureImg.getdata())[0] print textureImg.mode angle = 0 textureHandle = init(textureImg) currentHandle = textureHandle Render(mesh, textureImg, angle) glfw.SwapBuffers() print 'handle = ', textureHandle endFlag = 361 angleChange = {0: 30, 30: -30, -30: endFlag} while (True): Render(mesh, textureImg, textureHandle, angle) if (glfw.GetKey(glfw.KEY_ESC) == glfw.GLFW_PRESS): break # angle += 1; if (angleChange.get(angle) != None): captureImage = CaptureOpenGLImage() captureImage.save(folderPath + '/capture_' + str(angle) + '.jpg') if capturePath != None: captureImage.save(capturePath + '_capture_' + str(angle) + '.jpg') angle = angleChange[angle] else: print 'end' glfw.Terminate() glfw.CloseWindow() break glfw.SwapBuffers() time.sleep(0.02)
def run(self): self.running = True last_time = time() while self.running: if glfw.GetKey(glfw.KEY_ESC) == glfw.GLFW_PRESS: self.running = False break # get time current_time = time() delta = current_time - last_time last_time = current_time self.step(delta) self.render() glfw.SwapBuffers()
def init(): width = 640 height = 480 glfw.Init() glfw.OpenWindow(width, height, 8, 8, 8, 0, 24, 0, glfw.WINDOW) glfw.SetWindowTitle("glfw line") glfw.SetWindowSizeCallback(Reshape) glEnable(GL_DEPTH_TEST) glPolygonMode(GL_BACK, GL_LINE) # set eht projection # mouse glfw.SetMouseButtonCallback(MouseHandler) glfw.SetKeyCallback(KeyboardHandler) # init() while (True): Display() glfw.SwapBuffers() if (glfw.GetKey(glfw.KEY_ESC) == glfw.GLFW_PRESS): break time.sleep(0.02) xAxisAngle += 1 yAxisAngle += 1 glfw.Terminate()
def pollInput(self): currentTime = time.clock() elapsedTime = currentTime - self.timer self.timer = currentTime #tempWheelPosition = glfw.GetMouseWheel() #if tempWheelPosition != self.wheelPosition: #self.wheelPosition = tempWheelPosition #self.setView(hm.lookat(hm.identity(), np.array([0.0, 0.0, 55.0 - self.wheelPosition, 1.0], dtype = np.float32), np.array([0.0, 0.0, 0.0, 1.0], dtype = np.float32))) if glfw.GetKey('M'): print "Initializing manual control" self.manualControl = True self.context.scene.transform = hm.translation( hm.identity(), [0, 0, 60]) mouseX, mouseY = glfw.GetMousePos() self.calcArcBallVector(mouseX, mouseY) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('P'): print "Stopping manual control" self.manualControl = False time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('A') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 1, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('D') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 1, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('W') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [1, 0, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('S') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [1, 0, 0]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('Q') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), -60 * elapsedTime, [0, 0, 1]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('E') and self.manualControl: self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, -60]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.rotation(hm.identity(), 60 * elapsedTime, [0, 0, 1]), self.context.scene.transform) self.context.scene.transform = np.dot( hm.translation(hm.identity(), [0, 0, 60]), self.context.scene.transform) if glfw.GetKey('1'): print "1 pressed" if glfw.GetKey('2'): print "2 pressed" if glfw.GetKey('3'): print "3 pressed" if glfw.GetKey('X'): self.context.scene.dump() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('T'): self.context.task.toggle() time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey('I'): inputSnapshot = self.context.cubeTracker.imageIn # grab current input image as snapshot cv2.imshow("Input snapshot", inputSnapshot) # show snapshot in a window #cv2.imwrite(self.input_snapshot_file, inputSnapshot) # write snapshot to file (NOTE doesn't work!) #print "Input snapshot saved to {}".format(self.input_snapshot_file) time.sleep(0.5) # TODO prevent multiple key-presses properly if glfw.GetKey(glfw.KEY_ESC): if not self.quitting: self.doQuit = True self.quitting = True if not self.leftPressed and glfw.GetMouseButton( glfw.MOUSE_BUTTON_LEFT): self.leftPressed = True self.context.scene.hideCube = not self.context.scene.hideCube if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_LEFT): self.leftPressed = False if not self.rightPressed and glfw.GetMouseButton( glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = True self.oldMouseX, self.oldMouseY = glfw.GetMousePos() self.curMouseX = self.oldMouseX self.curMouseY = self.oldMouseY if not glfw.GetMouseButton(glfw.MOUSE_BUTTON_RIGHT): self.rightPressed = False if self.rightPressed: #OK self.curMouseX, self.curMouseY = glfw.GetMousePos() #OK if self.curMouseX != self.oldMouseX or self.curMouseY != self.oldMouseY: #OK oldVec = self.calcArcBallVector(self.oldMouseX, self.oldMouseY) #OK curVec = self.calcArcBallVector(self.curMouseX, self.curMouseY) #OK angle = math.acos(min(1.0, np.dot(oldVec, curVec))) #OK cameraAxis = np.cross(oldVec, curVec) #OK cameraAxis /= np.linalg.norm( cameraAxis, ord=2) # normalize cameraAxis to be a unit vector cameraToObjectCoords = np.linalg.inv( np.dot(self.context.renderer.view[:-1, :-1], self.context.scene.transform[:-1, :-1])) #??? cameraAxisObjectCoords = np.dot(cameraToObjectCoords, cameraAxis) #OK self.context.scene.transform = hm.rotation( self.context.scene.transform, math.degrees(angle), cameraAxisObjectCoords) #OK self.oldMouseX = self.curMouseX #OK self.oldMouseY = self.curMouseY #OK
glfw.ext.set_icons([(icon_data, icon_width, icon_height)]) glfw.SetWindowTitle("pyglfw test") glfw.Disable(glfw.AUTO_POLL_EVENTS) glfw.Enable(glfw.KEY_REPEAT) center_x = glfw.GetDesktopMode().Width / 2 - glfw.GetWindowSize()[0] / 2 center_y = glfw.GetDesktopMode().Height / 2 - glfw.GetWindowSize()[1] / 2 glfw.SetWindowPos(center_x, center_y) glfw.SetWindowSizeCallback(on_resize) glfw.SetWindowCloseCallback(on_close) glfw.SetWindowRefreshCallback(on_refresh) glfw.SetKeyCallback(on_key) glfw.SetCharCallback(on_char) glfw.SetMouseButtonCallback(on_button) glfw.SetMousePosCallback(on_pos) glfw.SetMouseWheelCallback(on_scroll) while glfw.GetWindowParam(glfw.OPENED): glfw.PollEvents() if glfw.GetKey(glfw.KEY_ESC): break glClear(GL_COLOR_BUFFER_BIT) glfw.SwapBuffers() glfw.CloseWindow() glfw.Terminate()