def mouse_move(x, y): global alpha, beta, down_x, down_y, delta_x, delta_y, delta_alpha if left_button: delta_alpha = 0 alpha += ((x - down_x) / 4.0) * -1 if alpha >= 360: alpha -= 360 if alpha <= 0: alpha += 360 if alpha >= 180: beta -= (y - down_y) / 4.0 * -1 else: beta += (y - down_y) / 4.0 * -1 if beta >= 360: beta -= 360 if beta <= 0: beta += 360 # Translate if right_button: delta_x += -1 * (x - down_x) / 100.0 delta_y += (y - down_y) / 100.0 down_x, down_y = x, y GLUT.glutPostRedisplay()
def mouse(self,button, state, x, y): viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) (win_w,win_h) = viewport[2:4] self.ratio_x1 = (2.0 * x - win_w) / win_w self.ratio_y1 = (win_h - 2.0 * y) / win_h self.modifier = glut.glutGetModifiers() glut.glutPostRedisplay()
def reshape(width,height): global win_width, win_height win_width = width win_height = height gl.glViewport(0, 0, width, height) glut.glutPostRedisplay()
def draw(): global _angle, _val, _rotation, _time_dif, _test_object, _shader_program,\ _child1, _child2, _child3, _child4 # tdelta = (datetime.now() - _time_dif).total_seconds() tdelta = time.time() - _time_dif if tdelta > 0.006944444: gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glEnable(gl.GL_DEPTH_TEST) # use shader program _shader.use() # load GLSL transformation matrix uniform locations # FIXME: obsolete documentation _shader.set_projection(_projection.value) _shader.set_lookat(_view_lookat.value) _rotation = glm.tquat(glm.vec3(0.0, 2.0 * tdelta, 0.0)) * _rotation a = 2.0 * tdelta _test_object.set_rotation(_rotation) _child1.rotate((0.0, 0.0, a)) _child2.rotate_local((a, 0.0, 0.0)) _child1.rotate_local((a, 0.0, 0.0)) _child2.rotate((0.0, 0.0, a)) _child3.rotate_local((a, 0, 0)) _child3.rotate((0.0, 0.0, a)) _child4.rotate_local((a, 0, 0)) _child4.rotate((0.0, 0.0, a)) # render object _test_object.render() _time_dif = time.time() print('fps: ' + '{:.2f}'.format(1 / tdelta) + '\r') glut.glutSwapBuffers() glut.glutPostRedisplay()
def update_figure(spikes): logging.debug("Updating figure") global window, frame, ax, fig, bar, stim # draw in matplotlib ax.cla() ax.hist(spikes, bins=np.linspace(-0.1, 0.5, 25), color='k') ax.vlines(0, 0, ax.get_ylim()[1], color='b') ax.set_title("%s" % str(stim)) fig.canvas.draw() buffer = fig.canvas.buffer_rgba(0, 0) x, y, w, h = fig.bbox.bounds F = np.fromstring(buffer, np.uint8).copy() F.shape = h, w, 4 # Replace 'transparent' color with a real transparent color v = np.array(np.array([1, 2, 3, 255]), dtype=np.uint8).view(dtype=np.int32)[0] Ft = np.where(F.view(dtype=np.int32) == v, 0, F) # Create main frame frame = glumpy.Image(Ft, interpolation='nearest') frame.update() bar.update() glut.glutPostRedisplay()
def timer(fps): global clock global data global phi, theta clock += 0.005 * 1000.0/fps # for scaling #loc = gl.glGetUniformLocation(program, "scale") #gl.glUniform1f(loc, (1+np.cos(clock))/2.0) loc = gl.glGetUniformLocation(program, "transform") # transform['transform'] = [ # (np.cos(clock),np.sin(clock),0,0), # (-np.sin(clock),np.cos(clock),0,0), # (0,0,1,0), (0,0,0,1) ] # gl.glUniformMatrix4fv(loc, 1, False, transform['transform']) # Rotate cube theta += 0.5 # degrees phi += 0.5 # degrees model = np.eye(4, dtype=np.float32) rotate(model, theta, 0, 0, 1) rotate(model, phi, 0, 1, 0) gl.glUniformMatrix4fv(loc, 1, False, model) glut.glutTimerFunc(1000/fps, timer, fps) glut.glutPostRedisplay()
def switch_view(line): if line: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE) else: gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL) glut.glutPostRedisplay()
def mouseWheel(self, wheel, direction, x, y): if direction > 0: self.camera.rad = max(0.1, self.camera.rad - 0.1) elif direction < 0: self.camera.rad = min(100, self.camera.rad + 0.1) GLUT.glutPostRedisplay()
def animate(self): d_time_millis = GLUT.glutGet( GLUT.GLUT_ELAPSED_TIME) - self._data.prev_time self._data.prev_time += d_time_millis d_time = d_time_millis / 1000.0 if self._data.key_states[ord(b'q')] or self._data.key_states[ord( b'e')]: if self._data.key_states[ord(b'q')]: self._tracked_cam_track_pos_float -= \ d_time * self._change_rates.track_pos if self._data.key_states[ord(b'e')]: self._tracked_cam_track_pos_float += \ d_time * self._change_rates.track_pos self._tracked_cam_track_pos_float = np.clip( self._tracked_cam_track_pos_float, 0, self._tracked_cam_track_len - 1) self._tracked_cam_track_pos_float = np.clip( self._tracked_cam_track_pos_float, 0, self._tracked_cam_track_len - 1) if self._data.key_states[ord(b'a')]: self.move_camera(d_time * np.array([-self._change_rates.tr_xz, 0, 0])) if self._data.key_states[ord(b'd')]: self.move_camera(d_time * np.array([+self._change_rates.tr_xz, 0, 0])) if self._data.key_states[ord(b's')]: self.move_camera(d_time * np.array([0, 0, +self._change_rates.tr_xz])) if self._data.key_states[ord(b'w')]: self.move_camera(d_time * np.array([0, 0, -self._change_rates.tr_xz])) GLUT.glutPostRedisplay()
def mouse_motion(self, x, y): glut.glutSetWindow(self.window_handle) old_x = self.mouse_x old_y = self.mouse_y dx = 2 * (x - old_x) / self.window_width dy = 2 * (old_y - y) / self.window_height aspect_ratio = self.window_width / self.window_height if self.mouse_button == glut.GLUT_LEFT_BUTTON: if self.mouse_state == glut.GLUT_DOWN: self.trans['x'] += dx * ZOOM_FACTOR * aspect_ratio self.trans['y'] += dy * ZOOM_FACTOR glut.glutPostRedisplay() if self.mouse_state == glut.GLUT_UP: pass if self.mouse_button == glut.GLUT_RIGHT_BUTTON: if self.mouse_state == glut.GLUT_DOWN: self.rot['y'] -= 90 * dx self.rot['x'] -= 90 * dy # self.rotate['y'] -= 5 * dx # self.rotate['x'] -= 5 * dy glut.glutPostRedisplay() if self.mouse_state == glut.GLUT_UP: pass if self.mouse_button == glut.GLUT_MIDDLE_BUTTON: if self.mouse_state == glut.GLUT_DOWN: pass if self.mouse_state == glut.GLUT_UP: pass self.mouse_x = x self.mouse_y = y
def display(): # called every frame to render frame gl.glClear(gl.GL_COLOR_BUFFER_BIT) for obj in gameobjects: obj.draw() gl.glFlush() glut.glutPostRedisplay()
def display(self): with self.mainfbo: self.clear() self.render() gl.glMemoryBarrier(gl.GL_FRAMEBUFFER_BARRIER_BIT) if self.emulate: gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, int(self.screenWidth / 2), self.screenHeight) self.tree.render(0) gl.glViewport(int(self.screenWidth / 2), 0, int(self.screenWidth / 2), self.screenHeight) self.texquad.render() else: gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) if self.preview: self.texquad.render() else: self.signalgenerator.render() glut.glutSwapBuffers() glut.glutPostRedisplay()
def motion(self, x, y): """ On movement when a button is pressed """ position = np.array([x, y], dtype="int") move = position - self.cursor yaw = 0 pitch = 0 dolly_horiz = 0 dolly_vert = 0 if self.button[0]: yaw = float(move[0]) * 0.5 pitch = float(move[1]) * 0.5 if self.button[2]: dolly_horiz = -float(move[0]) dolly_vert = float(move[1]) self.move_camera( yaw=yaw, pitch=pitch, dolly_horiz=dolly_horiz, dolly_vert=dolly_vert, ) self.update_cursor(x, y) GLUT.glutPostRedisplay()
def mouseButton( self, button, mode, x, y ): """Callback function for mouse button. http://stackoverflow.com/questions/14378/using-the-mouse-scrollwheel-in-glut """ #print button, mode if button == 3 and mode == 1: #scroll up #tZ = deltaY * self.scalingFactorTranslation tZ=5*self.scalingFactorTranslation self.translationMatrix.addTranslation(0, 0, -tZ) if button == 4 and mode == 1: #scroll down #tZ = deltaY * self.scalingFactorTranslation tZ=5 * self.scalingFactorTranslation self.translationMatrix.addTranslation(0, 0, tZ) if mode == glut.GLUT_DOWN: self.mouseButtonPressed = button else: self.mouseButtonPressed = None self.oldMousePos[0], self.oldMousePos[1] = x, y glut.glutPostRedisplay( )
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.GLUT_LEFT_BUTTON and button_state == glut.GLUT_UP: # Left button released self.lastrot = copy.copy(self.thisrot) # Set Last Static Rotation To Last Dynamic One elif button == glut.GLUT_LEFT_BUTTON and button_state == glut.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 = arcball.Point2fT(cursor_x, cursor_y) self.arcball.click(mouse_pt) # Update Start Vector And Prepare For Dragging elif button == glut.GLUT_RIGHT_BUTTON and button_state == glut.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.GLUT_MIDDLE_BUTTON and button_state == glut.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') glut.glutPostRedisplay()
def checkQueue(self, unused_timer_id): # pylint: disable=unused-argument glut.glutTimerFunc(20, self.checkQueue, 0) 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: glut.glutPostRedisplay()
def motion(x,y): global lastX global lastY global firstMouse global model if firstMouse: lastX = x lastY = y firstMouse = False else: xoffset = x - lastX yoffset = lastY - y lastX = x lastY = y angle_inc = 0.5 if xoffset > 0: angleY = xoffset*angle_inc Ry = ut.matRotateY(math.radians(angleY)) model = np.matmul(Ry,model) if xoffset < 0: angleY = xoffset*angle_inc Ry = ut.matRotateY(math.radians(angleY)) model = np.matmul(Ry,model) if yoffset > 0: angleX = yoffset*angle_inc Rx = ut.matRotateX(math.radians(angleX)) model = np.matmul(Rx,model) if yoffset < 0: angleX = yoffset*angle_inc Rx = ut.matRotateX(math.radians(angleX)) model = np.matmul(Rx,model) glut.glutPostRedisplay()
def event(value): global iteration glut.glutTimerFunc(1000 // FPS, event, value + 1) if model is not None: model.motion("vmd").step() fps_calc.show() glut.glutPostRedisplay()
def render(): # clear screen gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) # reset camera gl.glLoadIdentity() # perform camera movement if necessary if deltaMove: computePos(deltaMove) # set up camera glu.gluLookAt(cam_pos_x, cam_pos_y, cam_pos_z, cam_pos_x + look_at_x, cam_pos_y + look_at_y, cam_pos_z + look_at_z, 0.0, 1.0, 0.0) # draw all bodies for body in bodies: body.draw() # draw all springs for s in springs: s.draw() blanket.draw() # redraw glut.glutSwapBuffers() glut.glutPostRedisplay()
def display(): global args, mainfbo, texquad, signalgenerator with mainfbo: clear() t = reltime() effect.render(t) if args.emulate: gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glViewport(0, 0, int(screenWidth / 2), screenHeight) tree.render(reltime()) gl.glViewport(int(screenWidth / 2), 0, int(screenWidth / 2), screenHeight) texquad.render() else: gl.glClearColor(0, 0, 0, 0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) if args.preview: texquad.render() else: signalgenerator.render() glut.glutSwapBuffers() glut.glutPostRedisplay()
def mouseButton(self, button, mode, x, y): """Callback function for mouse button. http://stackoverflow.com/questions/14378/using-the-mouse-scrollwheel-in-glut """ #print button, mode if button == 3 and mode == 1: #scroll up #tZ = deltaY * self.scalingFactorTranslation tZ = 5 * self.scalingFactorTranslation self.translationMatrix.addTranslation(0, 0, -tZ) if button == 4 and mode == 1: #scroll down #tZ = deltaY * self.scalingFactorTranslation tZ = 5 * self.scalingFactorTranslation self.translationMatrix.addTranslation(0, 0, tZ) if mode == glut.GLUT_DOWN: self.mouseButtonPressed = button else: self.mouseButtonPressed = None self.oldMousePos[0], self.oldMousePos[1] = x, y glut.glutPostRedisplay()
def __onMouse(self, btn, state, x, y): zoom_factor = 1.1 if btn == glut.GLUT_LEFT_BUTTON: if state == glut.GLUT_DOWN: self.__am_rotating = True self.__x_origin = x self.__y_origin = y elif state == glut.GLUT_UP: self.__am_rotating = False elif btn == glut.GLUT_RIGHT_BUTTON: if state == glut.GLUT_DOWN: self.__am_translating = True self.__x_origin = x self.__y_origin = y elif state == glut.GLUT_UP: self.__am_translating = False elif btn == 3: # scroll up if state == glut.GLUT_DOWN: self.__camera = self.__center + (self.__camera - self.__center)/1.1 glut.glutPostRedisplay() elif btn == 4: # scroll down if state == glut.GLUT_DOWN: self.__camera = self.__center + (self.__camera - self.__center)*1.1 glut.glutPostRedisplay() else: print('mouse', btn, state)
def mouse_motion(self, x, y): glut.glutSetWindow(self.window_handle) old_x = self.mouse_x old_y = self.mouse_y dx = 2 * (x - old_x) / self.window_width dy = 2 * (old_y - y) / self.window_height aspect_ratio = self.window_width / self.window_height if self.mouse_button == glut.GLUT_LEFT_BUTTON: if self.mouse_state == glut.GLUT_DOWN: self.trans["x"] += dx * ZOOM_FACTOR * aspect_ratio self.trans["y"] += dy * ZOOM_FACTOR glut.glutPostRedisplay() if self.mouse_state == glut.GLUT_UP: pass if self.mouse_button == glut.GLUT_RIGHT_BUTTON: if self.mouse_state == glut.GLUT_DOWN: self.rot["y"] -= 90 * dx self.rot["x"] -= 90 * dy # self.rotate['y'] -= 5 * dx # self.rotate['x'] -= 5 * dy glut.glutPostRedisplay() if self.mouse_state == glut.GLUT_UP: pass if self.mouse_button == glut.GLUT_MIDDLE_BUTTON: if self.mouse_state == glut.GLUT_DOWN: pass if self.mouse_state == glut.GLUT_UP: pass self.mouse_x = x self.mouse_y = y
def motion(self, x, y): if self.lastMouse is not None: # If mouse pressed, increment X/Y rotation by amount of mouse motion curMouse = numpy.array([x,y]) diff = curMouse - self.lastMouse self.lastMouse = curMouse self.rot += diff glut.glutPostRedisplay()
def clique(button, state, a, b): global modificador if button == GLUT.GLUT_LEFT_BUTTON and state == GLUT.GLUT_DOWN: if modificador == 1: modificador = 0.5 else: modificador = 1 GLUT.glutPostRedisplay()
def rotate(self, cur_x, cur_y): if self.drag_start: drag_x, drag_y = self.drag_start diff_x, diff_y = cur_x - drag_x, cur_y - drag_y #before_rot_x, before_rot_y = rotation_before_drag self.rotations[-1] = diff_x, diff_y glut.glutPostRedisplay()
def rotate(self, cur_x, cur_y): if self.drag_start: drag_x, drag_y = self.drag_start diff_x, diff_y = cur_x - drag_x, cur_y - drag_y #before_rot_x, before_rot_y = rotation_before_drag self.rotations[-1] = diff_x, diff_y glut.glutPostRedisplay()
def on_idle(): global t, t0, frames t = glut.glutGet( glut.GLUT_ELAPSED_TIME ) frames = frames + 1 if t-t0 > 2500: print "FPS : %.2f (%d frames in %.2f second)" % (frames*1000.0/(t-t0), frames, (t-t0)/1000.0) t0, frames = t,0 glut.glutPostRedisplay()
def draw(self): '''Draw the window. This is a convenience method for forcing a redraw. The window must be the active one (see `switch_to`). ''' glut.glutPostRedisplay()
def keyboard(self, key, x, y): # Keyboard pressed if (key == '\x1b'): print 'ESC pressed, exiting.' sys.exit(0) elif key == ' ': self.rot = numpy.array([0,0]) glut.glutPostRedisplay()
def keyboard(self, key, x, y): """The new keyboard callback function.""" if ord(key) == 0o33: print('Exit', file=sys.stderr, flush=True) sys.exit() GLUT.glutPostRedisplay()
def wheel(self, direction): if direction == self.scroll_up_button: if self.zoom > 2: self.zoom -= self.zoom_increment elif direction == self.scroll_down_button: self.zoom += self.zoom_increment glut.glutPostRedisplay()
def on_passive_motion(x, y): global index _, _, _, h = gloo.get_parameter('viewport') data['a_position'][index] = x, h - y data['a_size'][index] = 5 data['a_fg_color'][index] = 0, 0, 0, 1 index = (index + 1) % 500 glut.glutPostRedisplay()
def _visibility(self, state): if state == glut.GLUT_VISIBLE: self._visible = True self.dispatch_event('on_show') glut.glutPostRedisplay() elif state == glut.GLUT_NOT_VISIBLE: self._visible = False self.dispatch_event('on_hide')
def on_passive_motion(x, y): global index _,_,_,h = gl.glGetIntegerv( gl.GL_VIEWPORT ) data['a_position'][index] = x,h-y data['a_size'][index] = 5 data['a_fg_color'][index] = 0,0,0,1 index = (index+1) % 500 glut.glutPostRedisplay()
def timer(fps): global clock clock += 0.005 * 1000.0/fps program['scale'] = (1+np.cos(clock))/2.0 program['m_transform'] = [ (np.cos(clock),np.sin(clock),0,0), (-np.sin(clock),np.cos(clock),0,0), (0,0,1,0), (0,0,0,1) ] print program['m_transform'] glut.glutTimerFunc(1000/fps, timer, fps) glut.glutPostRedisplay()
def update_mouse_position(self, x, y): """Handle mouse motion event.""" cur_pos = nds_coord(x, y, self.width, self.height) factor = np.exp((cur_pos[1] - self.first_mouse_position[1]) * -0.25) fovy = max(min(self.app.fovy * factor, 125), 25) self.app.update_projection(fovy, self.width, self.height) GLUT.glutPostRedisplay()
def on_passive_motion(x, y): global index _, _, _, h = gl.glGetIntegerv(gl.GL_VIEWPORT) data['a_position'][index] = x, h - y data['a_size'][index] = 5 data['a_fg_color'][index] = 0, 0, 0, 1 index = (index + 1) % 500 glut.glutPostRedisplay()
def show_event(self, ps): if not self.exitgl: if not self.is_initialized: self.initialize() self.set_particle_system(ps) glut.glutMainLoopEvent() glut.glutSetWindow(self.window_handle) glut.glutPostRedisplay()
def show_event(self, ps): if not self.exitgl: if not self.is_initialized: self.initialize() self.set_particle_system(ps) glut.glutMainLoopEvent() glut.glutSetWindow(self.window_handle) glut.glutPostRedisplay()
def mouse_button(self, button, mode, x_pos, y_pos): """Callback function for mouse button.""" if mode == GLUT.GLUT_DOWN: self.mouse_button_pressed = button else: self.mouse_button_pressed = None self.old_mouse_pos[0], self.old_mouse_pos[1] = x_pos, y_pos GLUT.glutPostRedisplay()
def keyboard(self, key, x, y): # Keyboard pressed if (key == '\x1b'): print 'ESC pressed, exiting.' sys.exit(0) elif key == ' ': self.rot = numpy.array([0, 0], dtype='float32') glut.glutPostRedisplay()
def motion(self, x, y): if self.lastMouse is not None: # If mouse pressed, increment X/Y rotation by amount of mouse motion curMouse = numpy.array([x, y]) diff = curMouse - self.lastMouse self.lastMouse = curMouse self.rot += diff glut.glutPostRedisplay()
def wheel(self, direction): if direction == self.scroll_up_button: if self.zoom > 2: self.zoom -= self.zoom_increment elif direction == self.scroll_down_button: self.zoom += self.zoom_increment glut.glutPostRedisplay()
def idle( self ): # get time current_time = time() delta = current_time - self.last_time self.last_time = current_time self.step( delta ) GLUT.glutPostRedisplay()
def __onReshape(self, width, height): self.__windowsize = (width, height) gl.glViewport(0,0,width,height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() glu.gluPerspective(40.,width/height,1.,1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() glut.glutPostRedisplay()
def Idle (): global fAngulo # Incrementamos el angulo fAngulo += 0.3 # Si es mayor que dos pi la decrementamos if fAngulo > 360: fAngulo -= 360 # Indicamos que es necesario repintar la pantalla glut.glutPostRedisplay()
def on_motion(x, y): global mouse, translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h - y dx, dy = x - mouse[0], y - mouse[1] translate = [translate[0] + dx, translate[1] + dy] mouse = x, y program['u_translate'] = translate glut.glutPostRedisplay()
def update(_): with use_program(gl_program): loc = gl.glGetUniformLocation(gl_program, 'time') gl.glUniform1f(loc, current_time_ms() - start) scale_width, scale_height = min(width, height)*1.0/width, min(width, height)*1.0/height loc = gl.glGetUniformLocation(gl_program, 'scale_to_square') gl.glUniform2f(loc, scale_width, scale_height) glut.glutPostRedisplay() glut.glutTimerFunc(0, update, 0)
def timer(fps): global theta, phi theta += 0.5 phi += 0.5 model = np.eye(4, dtype=np.float32) rotate(model, theta, 0, 0, 1) rotate(model, phi, 0, 1, 0) program["model"] = model glut.glutTimerFunc(1000 / fps, timer, fps) glut.glutPostRedisplay()
def on_motion( x, y ): global offset, mouse _,_,w,h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h-y dx,dy = x-mouse[0], y-mouse[1] offset += dx,dy mouse = x,y update() glut.glutPostRedisplay()
def on_scroll(dx, dy): global offset, zoom, mouse x,y = mouse z = min(max(0.5,zoom+.001*dy*zoom), 10) offset[0] = x-z*(x-offset[0])/zoom offset[1] = y-z*(y-offset[1])/zoom zoom = z update() glut.glutPostRedisplay()
def update_transforms(self, event): self.theta += .5 self.phi += .5 self.model = np.eye(4, dtype=np.float32) rotate(self.model, self.theta, 0,0,1) rotate(self.model, self.phi, 0,1,0) # Redraw and invoke new timer glut.glutTimerFunc(1000/fps, self.update_transforms, fps) glut.glutPostRedisplay()
def on_motion( x, y ): global offset, mouse _,_,w,h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h-y dx,dy = x-mouse[0], y-mouse[1] offset += dx,dy mouse = x,y collection.set_offset( offset ) grid._transform = offset[0],offset[1],int(zoom),0 glut.glutPostRedisplay()
def on_scroll(dx, dy): global mouse, translate, scale x, y = mouse s = min(max(0.25, scale + .001 * dy * scale), 200) translate[0] = x - s * (x - translate[0]) / scale translate[1] = y - s * (y - translate[1]) / scale translate = [translate[0], translate[1]] scale = s program['u_translate'] = translate program['u_scale'] = scale glut.glutPostRedisplay()