def handle_mouse_move(self, x, screen_y): """ Called when the mouse is moved """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y # invert the y coordinate because OpenGL is inverted if self.pressed is not None: dx = x - self.mouse_loc[0] dy = y - self.mouse_loc[1] if self.pressed == GLUT_RIGHT_BUTTON and self.trackball is not None: # ignore the updated camera loc because we want to always rotate around the origin self.trackball.drag_to(self.mouse_loc[0], self.mouse_loc[1], dx, dy) elif self.pressed == GLUT_LEFT_BUTTON: self.trigger('move', x, y) elif self.pressed == GLUT_MIDDLE_BUTTON: self.translate(dx / 60.0, dy / 60.0, 0) else: pass glutPostRedisplay() self.mouse_loc = (x, y)
def process_events(self): delta = play(self.midi, self.device, wait=False) glutPostRedisplay() if delta > 0: sleep(delta) else: sys.exit(0)
def handle_keystroke(self, key, x, screen_y): """ Called on keyboard input from the user """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y if key == 's': self.trigger('place', 'sphere', x, y) elif key == 'c': self.trigger('place', 'cube', x, y) elif key == 'f': self.trigger('place', 'figure', x, y) elif key == GLUT_KEY_UP: self.trigger('scale', up=True) elif key == GLUT_KEY_DOWN: self.trigger('scale', up=False) elif key == GLUT_KEY_LEFT: self.trigger('rotate_color', forward=True) elif key == GLUT_KEY_RIGHT: self.trigger('rotate_color', forward=False) elif key == '\033': self.trigger('close') elif key == 'm': self.trigger('setCameraMode','LookAt') elif key == ',': self.trigger('setCameraMode','LookAtFollow') elif key == 'n': self.trigger('setCameraMode','Trackball') # embed() glutPostRedisplay()
def handle_mouse_button(self, button, mode, x, y): """ Called when the mouse button is pressed or released """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - y # invert the y coordinate because OpenGL is inverted self.mouse_loc = (x, y) if mode == GLUT_DOWN: self.pressed = button if button == GLUT_RIGHT_BUTTON: pass elif button == GLUT_LEFT_BUTTON: # pick self.trigger('pick', x, y) elif button == 3: # scroll up self.translate(0, 0, 1.0) elif button == 4: # scroll down self.translate(0, 0, -1.0) else: # mouse button release self.pressed = None glutPostRedisplay()
def idle(self): if self.running: if time() - self.time_mark >= self.delay: self.active_index += self.direction self.time_mark = time() if self.active_index <= 0: # self.direction = 1 self.active_index = 0 # elif self.active_index >= len(self.plan)-1: # #self.direction = -1 # self.active_index = len(self.plan)-1 elif self.active_index >= len(self.plan): self.active_index = 0 logger.info("config {} of {}".format(self.active_index, len(self.plan) - 1)) if self.plan: u = (time() - self.time_mark) / self.delay if self.active_index + 1 < len(self.plan): self.knowledge_base.robot_state.sensed_config = vectorops.interpolate( self.plan[self.active_index], self.plan[self.active_index + 1], u ) else: self.knowledge_base.robot_state.sensed_config = self.plan[self.active_index] glutPostRedisplay()
def on_drag(self, cursor_x, cursor_y): """ Mouse cursor is moving Glut calls this function (when mouse button is down) and pases the mouse cursor postion in window coords as the mouse moves. """ from .geometry.rodrigues import rodrigues if (self.isdragging): mouse_pt = Point2fT(cursor_x, cursor_y) ThisQuat = self.arcball.drag( mouse_pt ) # // Update End Vector And Get Rotation As Quaternion self.thisrot = Matrix3fSetRotationFromQuat4f( ThisQuat) # // Convert Quaternion Into Matrix3fT # Use correct Linear Algebra matrix multiplication C = A * B self.thisrot = Matrix3fMulMatrix3f( self.lastrot, self.thisrot) # // Accumulate Last Rotation Into This One # make sure it is a rotation self.thisrot = rodrigues(rodrigues(self.thisrot)[0])[0] self.transform = Matrix4fSetRotationFromMatrix3f( self.transform, self.thisrot ) # // Set Our Final Transform's Rotation From This One glutPostRedisplay() return
def handle_key(self, key, x, y): super().handle_key(key, x, y) key_id = ord(key) if key_id == 27: # ESC sys.exit(0) elif key_id == 32: # SPACE self._is_perspective = not self._is_perspective glutPostRedisplay()
def idleFunc(): # do state stuff. one = time() state = get_state() idle(state) two = time() #print "idleFunc idle time: ", (two - one) glutPostRedisplay()
def redraw(self, callback=None, synchronous=False): """Trigger redisplay and trigger callback after render.""" assert not (callback and synchronous), 'Call should either be asynchronous or process callback' self.__callback = callback self.__synchronous = synchronous glutPostRedisplay() if self.__synchronous: glutMainLoopEvent()
def redraw(self, callback=None, synchronous=False): """Trigger redisplay and trigger callback after render.""" assert not (callback and synchronous ), 'Call should either be asynchronous or process callback' self.__callback = callback self.__synchronous = synchronous glutPostRedisplay() if self.__synchronous: glutMainLoopEvent()
def calc_movement_all_shapes(self, _): #Snake logic, best logic self.snake_instance.game_tick() #Reset our timer glutTimerFunc(TICKRATE_MS, self.calc_movement_all_shapes, 0) #Tell glut to rerun our display function glutPostRedisplay()
def keyupevent(self, c, x, y): try: if c in self.keys: self.keys[c] = False print(self.keys) except Exception as e: print(e) pass glutPostRedisplay()
def special_keypress(self, key, x, y): if key == GLUT_KEY_LEFT: self.rot_z += 0.3 if key == GLUT_KEY_RIGHT: self.rot_z -= 0.3 if key == GLUT_KEY_DOWN: self.rot_x -= 0.3 if key == GLUT_KEY_UP: self.rot_x += 0.3 glutPostRedisplay()
def idle_light_animation_func(self): if self.vertical_rotation: self.theta_light_angle += 1 else: self.phi_light_angle += 1 self.draw_light_source() glLoadIdentity() glutPostRedisplay()
def handle_special_key(self, key, x, y): super().handle_special_key(key, x, y) if key == GLUT_KEY_RIGHT: self._rotate_y += 1 elif key == GLUT_KEY_LEFT: self._rotate_y -= 1 elif key == GLUT_KEY_UP: self._rotate_x += 1 elif key == GLUT_KEY_DOWN: self._rotate_x -= 1 glutPostRedisplay()
def handle_special_key(self, key, x, y): super().handle_special_key(key, x, y) if key == GLUT_KEY_LEFT: self._circle_pos[0] -= self._step elif key == GLUT_KEY_UP: self._circle_pos[1] += self._step elif key == GLUT_KEY_RIGHT: self._circle_pos[0] += self._step elif key == GLUT_KEY_DOWN: self._circle_pos[1] -= self._step glutPostRedisplay()
def keydownevent(self, c, x, y): print( "*******************************************************************************" ) try: self.keys[c] = True print(self.keys) except Exception as e: print(e) pass glutPostRedisplay()
def _glut_redisplay(): evloop = getEventLoop() # hack, glut seem can't handle the leaving on the mainloop # so... leave with sys.exit() :[ try: evloop.idle() except KeyboardInterrupt: evloop.quit = True if evloop.quit: sys.exit(0) glutPostRedisplay()
def display(self): glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(0.0, 240.0, 1000.0, 0.0, 240.0, 0.0, 0.0, 1.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) for i in self.fountains: i.life -= 1 i.init() i.update() glutPostRedisplay() glutSwapBuffers()
def onClick(self, button, button_state, x, y): self.m_IsDragging = False if (button == GLUT_RIGHT_BUTTON and button_state == GLUT_UP): # Right button click self.setRotation(Matrix3f()) glutPostRedisplay() elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_UP): # Left button clicked down self.m_IsDragging = False self.endDrag() elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_DOWN): # Left button clicked down self.m_IsDragging = True self.click(Point2f(x, y))
def handle_mouse_button(self, button, mode, x, y): xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - y # opengl's o is zuoxia, window's o is zuoshang self.mouse_loc = (x, y) if mode == GLUT_DOWN: self.pressed = button if button == GLUT_RIGHT_BUTTON: pass elif button == GLUT_LEFT_BUTTON: self.trigger('pick', x, y) else: self.pressed = None # current window need redrawing glutPostRedisplay()
def spin_display(): global progress global current_mosaic_picture global start_picture_coord global start_orientation duration = args.duration * 1000. old_progress = progress progress = (glutGet(GLUT_ELAPSED_TIME) % duration) / duration if progress < old_progress: current_tile_picture = current_mosaic_picture start_orientation = current_mosaic_picture.orientation current_mosaic_picture = iterator.next() start_picture_coord = find_picture_in_mosaic( current_tile_picture, mosaic_factory.mosaic(current_mosaic_picture, args.tiles, args.reuse)) glutPostRedisplay()
def idle(self): # check for updated state if self.child_pipe.poll(): try: # path = self.child_pipe.recv() # logger.error('remote path: {}'.format(path)) # (self.knowledge_base, self.robot_state) = pickle.load(open(path)) # logger.error('finished loading') (self.knowledge_base, self.robot_state) = self.child_pipe.recv() except EOFError: logger.info("visualization exiting from EOF") sys.exit(0) else: # acknowledge the receipt self.child_pipe.send(True) glutPostRedisplay()
def handle_mouse_button(self, button, mode, x, y): """ 当鼠标按键被点击或者释放的时候调用 """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - y # OpenGL原点在窗口左下角,窗口原点在左上角,所以需要这种转换。 self.mouse_loc = (x, y) if mode == GLUT_DOWN: # 鼠标按键按下的时候 self.pressed = button if button == GLUT_RIGHT_BUTTON: pass elif button == GLUT_LEFT_BUTTON: self.trigger('pick', x, y) else: # 鼠标按键被释放的时候 self.pressed = None # 标记当前窗口需要重新绘制 glutPostRedisplay()
def handle_keystroke(self, key, x, screen_y): """ 键盘输入时调用 """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y if key == 's': self.trigger('place', 'sphere', x, y) elif key == 'c': self.trigger('place', 'cube', x, y) elif key == GLUT_KEY_UP: self.trigger('scale', up=True) elif key == GLUT_KEY_DOWN: self.trigger('scale', up=False) elif key == GLUT_KEY_LEFT: self.trigger('rotate_color', forward=True) elif key == GLUT_KEY_RIGHT: self.trigger('rotate_color', forward=False) glutPostRedisplay()
def handle_keystroke(self, key, x, screen_y): """ Called on keyboard input from the user """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y if key == 's': self.trigger('place', 'sphere', x, y) elif key == 'c': self.trigger('place', 'cube', x, y) elif key == GLUT_KEY_UP: self.trigger('scale', up=True) elif key == GLUT_KEY_DOWN: self.trigger('scale', up=False) elif key == GLUT_KEY_LEFT: self.trigger('rotate_color', forward=True) elif key == GLUT_KEY_RIGHT: self.trigger('rotate_color', forward=False) glutPostRedisplay()
def animate(self, FPS=30): currentTime = time() #key movement for omnicontrol try: if self.keys["x"]: self.cam_focus.x += 1 if self.keys["z"]: self.cam_focus.x -= 1 if self.keys["d"]: self.cam_focus.y += 1 if self.keys["c"]: self.cam_focus.y -= 1 if self.keys["f"]: self.cam_focus.z += 1 if self.keys["v"]: self.cam_focus.z -= 1 except: pass # run calculations for level inc collision etc if not self.level == None: self.level.evaluate(self.joystick, self.keys) glutPostRedisplay() glutTimerFunc(int(1000 / FPS), self.animate, FPS) drawTime = currentTime - self.lastFrameTime self.topFPS = int(1000 / drawTime) # set camera target focus points self.cam_focus_target = XYZ( self.level.solomon.x + 0.2 * self.level.solomon.facing, self.level.solomon.y - 0.5, 3.0) # set camera target position self.cam_pos_target = XYZ( self.level.solomon.x + 1 * self.level.solomon.facing, self.level.solomon.y - 0.2, float(self.level.target_z)) # calculate current focal point and camera position # self.camera_sweep is the "speed" at which transitions are being made self.cam_pos = self.cam_pos.add( self.cam_pos_target.sub(self.cam_pos).mult(1 / self.camera_sweep)) self.cam_focus = self.cam_focus.add( self.cam_focus_target.sub(self.cam_focus).mult(1 / self.camera_sweep)) self.lastFrameTime = time()
def on_click(self, button, button_state, cursor_x, cursor_y): """ Mouse button clicked. Glut calls this function when a mouse button is clicked or released. """ self.isdragging = False # if (button == GLUT_RIGHT_BUTTON and button_state == GLUT_UP): # # Right button click # self.lastrot = Matrix3fSetIdentity (); # // Reset Rotation # self.thisrot = Matrix3fSetIdentity (); # // Reset Rotation # self.transform = Matrix4fSetRotationFromMatrix3f (self.transform, self.thisrot); # // Reset Rotation if (button == GLUT_LEFT_BUTTON and button_state == GLUT_UP): # Left button released self.lastrot = copy.copy( self.thisrot) # Set Last Static Rotation To Last Dynamic One elif (button == GLUT_LEFT_BUTTON and button_state == GLUT_DOWN): # Left button clicked down self.lastrot = copy.copy( self.thisrot) # Set Last Static Rotation To Last Dynamic One self.isdragging = True # // Prepare For Dragging mouse_pt = Point2fT(cursor_x, cursor_y) self.arcball.click( mouse_pt) # Update Start Vector And Prepare For Dragging elif (button == GLUT_RIGHT_BUTTON and button_state == GLUT_DOWN): # If a mouse click location was requested, return it to caller if hasattr(self, 'event_port'): self.mouseclick_port = self.event_port del self.event_port if hasattr(self, 'mouseclick_port'): self.send_mouseclick_to_caller(cursor_x, cursor_y) elif (button == GLUT_MIDDLE_BUTTON and button_state == GLUT_DOWN): # If a mouse click location was requested, return it to caller if hasattr(self, 'event_port'): self.mouseclick_port = self.event_port del self.event_port if hasattr(self, 'mouseclick_port'): self.send_mouseclick_to_caller(cursor_x, cursor_y, button='middle') glutPostRedisplay()
def handle_mouse_move(self, x, screen_y): """ 鼠标移动时调用 """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y if self.pressed is not None: dx = x - self.mouse_loc[0] dy = y - self.mouse_loc[1] if self.pressed == GLUT_RIGHT_BUTTON and self.trackball is not None: # 变化场景的角度 self.trackball.drag_to(self.mouse_loc[0], self.mouse_loc[1], dx, dy) elif self.pressed == GLUT_LEFT_BUTTON: self.trigger('move', x, y) elif self.pressed == GLUT_MIDDLE_BUTTON: self.translate(dx/60.0, dy/60.0, 0) else: pass glutPostRedisplay() self.mouse_loc = (x, y)
def handle_key(self, key, x, y): super().handle_key(key, x, y) key_id = ord(key) if key_id == 27: # ESC sys.exit(0) elif key_id == 91: # [ self._eye_x -= self._eye_step elif key_id == 93: # ] self._eye_x += self._eye_step elif key_id == 39: # ' self._eye_y -= self._eye_step elif key_id == 92: # \ self._eye_y += self._eye_step elif key_id == 46: # . self._eye_z -= self._eye_step elif key_id == 47: # / self._eye_z += self._eye_step glutPostRedisplay()
def handle_mouse_move(self, x, screen_y): """ Called when the mouse is moved """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y # invert the y coordinate because OpenGL is inverted if self.pressed is not None: dx = x - self.mouse_loc[0] dy = y - self.mouse_loc[1] if self.pressed == GLUT_RIGHT_BUTTON and self.trackball is not None: # ignore the updated camera loc because we want to always rotate around the origin self.trackball.drag_to(self.mouse_loc[0], self.mouse_loc[1], dx, dy) elif self.pressed == GLUT_LEFT_BUTTON: self.trigger('move', x, y) elif self.pressed == GLUT_MIDDLE_BUTTON: self.translate(dx/60.0, dy/60.0, 0) else: pass glutPostRedisplay() self.mouse_loc = (x, y)
def handle_mouse_button(self, button, mode, x, y): """ Called when the mouse button is pressed or released """ xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - y # invert the y coordinate because OpenGL is inverted self.mouse_loc = (x, y) if mode == GLUT_DOWN: self.pressed = button if button == GLUT_RIGHT_BUTTON: pass elif button == GLUT_LEFT_BUTTON: # pick self.trigger('pick', x, y) elif button == 3: # scroll up self.translate(0, 0, 1.0) elif button == 4: # scroll up self.translate(0, 0, -1.0) else: # mouse button release self.pressed = None glutPostRedisplay()
def checkQueue(self, unused_timer_id): glutTimerFunc(20, self.checkQueue, 0) # if True: # spinning # w_whole_window = glutGet(GLUT_WINDOW_WIDTH) # h_whole_window = glutGet(GLUT_WINDOW_HEIGHT) # center_x = w_whole_window/2 # center_y = h_whole_window/2 # self.on_click(GLUT_LEFT_BUTTON, GLUT_DOWN, center_x, center_y) # self.on_drag(center_x+2, center_y) try: request = self.server.recv_pyobj(zmq.NOBLOCK) except zmq.ZMQError as e: if e.errno != zmq.EAGAIN: raise # something wrong besides empty queue return # empty queue, no problem if not request: return while (request): task_completion_time = time.time() if not self.handle_request(request): raise Exception('Unknown command string: %s' % (request['label'])) task_completion_time = time.time() - task_completion_time if 'port' in request: # caller wants confirmation port = request['port'] client = zmq.Context.instance().socket(zmq.PUSH) client.connect('tcp://127.0.0.1:%d' % (port)) client.send_pyobj(task_completion_time) try: request = self.server.recv_pyobj(zmq.NOBLOCK) except zmq.ZMQError as e: if e.errno != zmq.EAGAIN: raise request = None if self.need_redraw: glutPostRedisplay()
def main_loop(self): """ Function responsible for main simulation loop """ t = self.dt - (time.time() - self.lasttime) # check if we need to do anything, dt if t > 0 and self.real_time: time.sleep(t) for agent in self.agents_to_add: agent.create_geometry() self.agents.append(agent) self.agents_to_add.remove(agent) for agent in self.agents: if (agent.energy <= 0): self.agents.remove(agent) agent.body.disable() del agent.body del agent.geom del agent else: agent.live() if not self.no_graphics: glutPostRedisplay() n = 4 for i in range(n): self.space.collide((), self.near_callback) self.world.step(self.dt / n) self.contactJoints.empty() self.iter += 1 if self.max_iter != -1: if self.iter > self.max_iter: print self exit(0) self.lasttime = time.time()
def handle_keystroke(self, key, x, screen_y): """ 键盘输入时调用 """ print('进入handle_keystroke()函数') xSize, ySize = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) y = ySize - screen_y print('key值:',key) if key == b'S': self.trigger('place', 'sphere', x, y) elif key == b'C': self.trigger('place', 'cube', x, y) elif key == GLUT_KEY_UP: print('come in :key_up') self.trigger('scale', up=True) elif key == GLUT_KEY_DOWN: self.trigger('scale', up=False) elif key == GLUT_KEY_LEFT: self.trigger('rotate_color', forward=True) elif key == GLUT_KEY_RIGHT: self.trigger('rotate_color', forward=False) glutPostRedisplay()
def spin_display(): global progress global current_mosaic_picture global start_picture_coord global start_orientation duration = args.duration * 1000. old_progress = progress progress = (glutGet(GLUT_ELAPSED_TIME) % duration) / duration if progress < old_progress: current_tile_picture = current_mosaic_picture start_orientation = current_mosaic_picture.orientation current_mosaic_picture = iterator.next() start_picture_coord = find_picture_in_mosaic( current_tile_picture, mosaic_factory.mosaic( current_mosaic_picture, args.tiles, args.reuse ) ) glutPostRedisplay()
def onMouseMove(self, x, y): if self.m_IsDragging: mouse_pt = Point2f(x, y) self.drag(mouse_pt) glutPostRedisplay() return
def handle_idle(self): if self._should_rotate: self._angle -= 1 glutPostRedisplay()
def onIdle(self): if self.idleDelegate: self.idleDelegate.onIdle() return self.scene.step(Environment.dt) glutPostRedisplay()