def recent_events(self, events): frame = events.get("frame") if not frame: return if self.drag_offset is not None: pos = glfw.get_cursor_pos(glfw.get_current_context()) pos = normalize(pos, glfw.get_window_size(glfw.get_current_context())) pos = denormalize( pos, (frame.img.shape[1], frame.img.shape[0]) ) # Position in img pixels self.pos[0] = pos[0] + self.drag_offset[0] self.pos[1] = pos[1] + self.drag_offset[1] if self.watermark is not None: # keep in image bounds, do this even when not dragging because the image sizes could change. self.pos[1] = max( 0, min(frame.img.shape[0] - self.watermark.shape[0], max(self.pos[1], 0)), ) self.pos[0] = max( 0, min(frame.img.shape[1] - self.watermark.shape[1], max(self.pos[0], 0)), ) pos = int(self.pos[0]), int(self.pos[1]) img = frame.img roi = ( slice(pos[1], pos[1] + self.watermark.shape[0]), slice(pos[0], pos[0] + self.watermark.shape[1]), ) w_roi = slice(0, img.shape[0] - pos[1]), slice(0, img.shape[1] - pos[0]) img[roi] = self.watermark[w_roi] * self.alpha_mask[w_roi] + img[roi] * ( 1 - self.alpha_mask[w_roi] )
def open_window(self): if not self.window: self.input = {"button": None, "mouse": (0, 0)} # get glfw started if self.run_independently: glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) self.window = glfw.create_window(self.window_size[0], self.window_size[1], self.name, None, None) else: self.window = glfw.create_window( self.window_size[0], self.window_size[1], self.name, None, glfw.get_current_context(), ) self.other_window = glfw.get_current_context() glfw.make_context_current(self.window) glfw.swap_interval(0) glfw.set_window_pos(self.window, window_position_default[0], window_position_default[1]) # Register callbacks window glfw.set_framebuffer_size_callback(self.window, self.on_resize) glfw.set_window_iconify_callback(self.window, self.on_iconify) glfw.set_key_callback(self.window, self.on_window_key) glfw.set_char_callback(self.window, self.on_window_char) glfw.set_mouse_button_callback(self.window, self.on_window_mouse_button) glfw.set_cursor_pos_callback(self.window, self.on_pos) glfw.set_scroll_callback(self.window, self.on_scroll) # get glfw started if self.run_independently: glutils.init() self.basic_gl_setup() self.sphere = glutils.Sphere(20) self.glfont = fs.Context() self.glfont.add_font("opensans", get_opensans_font_path()) self.glfont.set_size(18) self.glfont.set_color_float((0.2, 0.5, 0.9, 1.0)) self.on_resize(self.window, *glfw.get_framebuffer_size(self.window)) glfw.make_context_current(self.other_window)
def open_window(self): if not self._window: monitor = None # open with same aspect ratio as surface surface_aspect_ratio = (self.surface.real_world_size["x"] / self.surface.real_world_size["y"]) win_h = 640 win_w = int(win_h / surface_aspect_ratio) self._window = glfw.create_window( win_h, win_w, "Reference Surface: " + self.surface.name, monitor, glfw.get_current_context(), ) glfw.set_window_pos( self._window, self.window_position_default[0], self.window_position_default[1], ) self.trackball = gl_utils.trackball.Trackball() self.input = {"down": False, "mouse": (0, 0)} # Register callbacks glfw.set_framebuffer_size_callback(self._window, self.on_resize) glfw.set_key_callback(self._window, self.on_window_key) glfw.set_window_close_callback(self._window, self.on_close) glfw.set_mouse_button_callback(self._window, self.on_window_mouse_button) glfw.set_cursor_pos_callback(self._window, self.on_pos) glfw.set_scroll_callback(self._window, self.on_scroll) self.on_resize(self._window, *glfw.get_framebuffer_size(self._window)) # gl_state settings active_window = glfw.get_current_context() glfw.make_context_current(self._window) gl_utils.basic_gl_setup() gl_utils.make_coord_system_norm_based() # refresh speed settings glfw.swap_interval(0) glfw.make_context_current(active_window)
def current_context(window): prev_context = glfw.get_current_context() glfw.make_context_current(window) try: yield finally: glfw.make_context_current(prev_context)
def _switch_to_current_context(self): previous_context = glfw.get_current_context() glfw.make_context_current(self.__gl_handle) try: yield finally: glfw.make_context_current(previous_context)
def gl_display_in_window(self): active_window = glfw.get_current_context() glfw.make_context_current(self._window) clear_gl_screen() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() p_window_size = glfw.get_window_size(self._window) r = p_window_size[0] / 15.0 # compensate for radius of marker gl.glOrtho(-r, p_window_size[0] + r, p_window_size[1] + r, -r, -1, 1) # Switch back to Model View Matrix gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # hacky way of scaling and fitting in different window rations/sizes grid = _make_grid() * min((p_window_size[0], p_window_size[1] * 5.5 / 4.0)) # center the pattern grid -= np.mean(grid) grid += (p_window_size[0] / 2 - r, p_window_size[1] / 2 + r) draw_points(grid, size=r, color=RGBA(0.0, 0.0, 0.0, 1), sharpness=0.95) if self.clicks_to_close < 5: self.glfont.set_size(int(p_window_size[0] / 30.0)) self.glfont.draw_text( p_window_size[0] / 2.0, p_window_size[1] / 4.0, "Touch {} more times to close window.".format(self.clicks_to_close), ) glfw.swap_buffers(self._window) glfw.make_context_current(active_window)
def _gl_state_settings(window): active_window = glfw.get_current_context() glfw.make_context_current(window) gl_utils.basic_gl_setup() gl_utils.make_coord_system_norm_based() glfw.swap_interval(0) glfw.make_context_current(active_window)
def _platform_free(self): """Frees resources associated with this context.""" if self._context: if glfw.get_current_context() == self._context: glfw.make_context_current(None) self._destroy_window(self._context) self._context = None
def current_mouse_pos(window, camera_render_size, frame_size): content_scale = gl_utils.get_content_scale(window) x, y = glfw.get_cursor_pos(glfw.get_current_context()) pos = x * content_scale, y * content_scale pos = normalize(pos, camera_render_size) # Position in img pixels pos = denormalize(pos, frame_size) return (int(pos[0]), int(pos[1]))
def alarm_handler(signal_number, stack_frame): """ Close the current GLFW window when called due to a SIGALRM. """ assert signal_number == signal.SIGALRM window = glfw.get_current_context() assert window is not None glfw.set_window_should_close(window, True)
def on_resize(self, window, w, h): active_window = glfw.get_current_context() glfw.make_context_current(window) # norm_size = normalize((w,h),glfwGetWindowSize(window)) # fb_size = denormalize(norm_size,glfwGetFramebufferSize(window)) self.adjust_gl_view(w, h, window) glfw.make_context_current(active_window) self.width, self.height = w, h
def begin_update_window(self): if self.window: if glfw.window_should_close(self.window): self.close_window() return self.other_window = glfw.get_current_context() glfw.make_context_current(self.window)
def on_resize(self, window, w, h): active_window = glfw.get_current_context() glfw.make_context_current(window) # norm_size = normalize((w,h),glfwGetWindowSize(window)) # fb_size = denormalize(norm_size,glfwGetFramebufferSize(window)) self.adjust_gl_view(w, h, window) glfw.make_context_current(active_window) self.width, self.height = w, h
def alarm_handler(signal_number, stack_frame): """ Close the current GLFW window when called due to a SIGALRM. """ assert signal_number == signal.SIGALRM window = glfw.get_current_context() assert window is not None glfw.set_window_should_close(window, True)
def __init__(self, model, position, rotation, scale): Thing.__init__(self, model, position, rotation[0], rotation[1], rotation[2], scale) self.window = glfw.get_current_context() self.current_speed = 0 self.current_turn_speed = 0 self.up_speed = 0 self.isGrounded = True
def on_resize(self, window, w, h): h = max(h, 1) w = max(w, 1) self.window_size = (w, h) active_window = glfw.get_current_context() glfw.make_context_current(window) self.adjust_gl_view(w, h) glfw.make_context_current(active_window)
def open_window(self): if not self._window: if self.fullscreen: try: monitor = glfw.get_monitors()[self.monitor_idx] except Exception: logger.warning( "Monitor at index %s no longer availalbe using default" % idx) self.monitor_idx = 0 monitor = glfw.get_monitors()[self.monitor_idx] mode = glfw.get_video_mode(monitor) height, width = mode.size.height, mode.size.width else: monitor = None height, width = 640, 480 self._window = glfw.create_window( height, width, "Calibration", monitor, glfw.get_current_context(), ) if not self.fullscreen: # move to y = 31 for windows os glfw.set_window_pos(self._window, 200, 31) # Register callbacks glfw.set_framebuffer_size_callback(self._window, on_resize) glfw.set_key_callback(self._window, self.on_window_key) glfw.set_window_close_callback(self._window, self.on_close) glfw.set_mouse_button_callback(self._window, self.on_window_mouse_button) on_resize(self._window, *glfw.get_framebuffer_size(self._window)) # gl_state settings active_window = glfw.get_current_context() glfw.make_context_current(self._window) basic_gl_setup() glfw.make_context_current(active_window) self.clicks_to_close = 5
def _glfw_init(self): glfw.init() glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) window = glfw.create_window( 640, 480, "Head Pose Tracker Visualizer", None, glfw.get_current_context(), ) return window
def activate(self, width, height): """Called when entering the `make_current` context manager. Args: width: Integer specifying the new framebuffer width in pixels. height: Integer specifying the new framebuffer height in pixels. """ self._previous_context = glfw.get_current_context() glfw.make_context_current(self._context) if (width, height) != glfw.get_window_size(self._context): glfw.set_window_size(self._context, width, height)
def on_resize(window, w, h): h = max(h, 1) w = max(w, 1) hdpi_factor = (glfw.get_framebuffer_size(window)[0] / glfw.get_window_size(window)[0]) w, h = w * hdpi_factor, h * hdpi_factor gui.update_window(w, h) active_window = glfw.get_current_context() glfw.make_context_current(active_window) # norm_size = normalize((w,h),glfw.get_window_size(window)) # fb_size = denormalize(norm_size,glfw.get_framebuffer_size(window)) adjust_gl_view(w, h, window) glfw.make_context_current(active_window)
def init_ui(self): self.glfont = fontstash.Context() self.glfont.add_font("opensans", get_opensans_font_path()) self.glfont.set_size(32) self.glfont.set_color_float((0.2, 0.5, 0.9, 1.0)) self.glfont.set_align_string(v_align="center", h_align="middle") self.window_size = glfw.get_framebuffer_size(glfw.get_current_context()) self.tex = Render_Target(*self.window_size) self._socket = zmq_tools.Msg_Receiver( self.g_pool.zmq_ctx, self.g_pool.ipc_sub_url, self.subscriptions )
def _on_gl_display(self): if not self._window: return active_window = glfw.get_current_context() glfw.make_context_current(self._window) self._init_3d_window() self._trackball.push() self._render() self._trackball.pop() glfw.swap_buffers(self._window) glfw.make_context_current(active_window) glfw.poll_events()
def on_resize(window, w, h): nonlocal window_size nonlocal content_scale is_minimized = bool(glfw.get_window_attrib(window, glfw.ICONIFIED)) if is_minimized: return # Always clear buffers on resize to make sure that there are no overlapping # artifacts from previous frames. gl_utils.glClear(GL_COLOR_BUFFER_BIT) gl_utils.glClearColor(0, 0, 0, 1) active_window = glfw.get_current_context() glfw.make_context_current(window) content_scale = gl_utils.get_content_scale(window) framebuffer_scale = gl_utils.get_framebuffer_scale(window) g_pool.gui.scale = content_scale window_size = w, h g_pool.camera_render_size = w - int( icon_bar_width * g_pool.gui.scale), h g_pool.gui.update_window(w, h) g_pool.gui.collect_menus() for g in g_pool.graphs: g.scale = content_scale g.adjust_window_size(w, h) adjust_gl_view(w, h) glfw.make_context_current(active_window) # Minimum window size required, otherwise parts of the UI can cause openGL # issues with permanent effects. Depends on the content scale, which can # potentially be dynamically modified, so we re-adjust the size limits every # time here. min_size = int(2 * icon_bar_width * g_pool.gui.scale / framebuffer_scale) glfw.set_window_size_limits( window, min_size, min_size, glfw.DONT_CARE, glfw.DONT_CARE, ) # Needed, to update the window buffer while resizing consume_events_and_render_buffer()
def gl_display_in_window(self, world_tex): """ here we map a selected surface onto a separate window. """ if self._window and self.surface.detected: active_window = glfw.get_current_context() glfw.make_context_current(self._window) gl_utils.clear_gl_screen() # cv uses 3x3 gl uses 4x4 transformation matrices width, height = self.tracker.camera_model.resolution img_corners = np.array( [(0, height), (width, height), (width, 0), (0, 0)], dtype=np.float32 ) denorm_trans = _get_norm_to_points_trans(img_corners) trans_mat = self.surface.dist_img_to_surf_trans @ denorm_trans trans_mat = gl_utils.cvmat_to_glmat(trans_mat) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glOrtho(0, 1, 0, 1, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPushMatrix() # apply trans_mat to our quad - this will stretch the quad such that the # surface will span the window extends gl.glLoadMatrixf(trans_mat) world_tex.draw() gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW) gl.glPopMatrix() self.draw_recent_pupil_positions() glfw.swap_buffers(self._window) glfw.make_context_current(active_window)
def _on_set_frame_buffer_size(self, window, w, h): self._trackball.set_window_size(w, h) active_window = glfw.get_current_context() glfw.make_context_current(window) gl_utils.adjust_gl_view(w, h) glfw.make_context_current(active_window)
def on_resize(self, window, w, h): self.trackball.set_window_size(w, h) active_window = glfw.get_current_context() glfw.make_context_current(window) gl_utils.adjust_gl_view(w, h) glfw.make_context_current(active_window)
def open( self, gui_monitor: GUIMonitor, title: str, is_fullscreen: bool = False, size: T.Tuple[int, int] = None, position: T.Tuple[int, int] = None, ): if self.is_open: # TODO: Warn that the window is already open return if not gui_monitor.is_available: raise ValueError(f"Window requires an available monitor.") has_fixed_size = ((size is not None) and (len(size) == 2) and (size[0] > 0) and (size[1] > 0)) if is_fullscreen and has_fixed_size: raise ValueError( f"Fullscreen is mutually exclusive to having a fixed size.") if position is None: if platform.system() == "Windows": position = (8, 90) else: position = (0, 0) if is_fullscreen: size = gui_monitor.size # NOTE: Always creating windowed window here, even if in fullscreen mode. On # windows you might experience a black screen for up to 1 sec when creating # a blank window directly in fullscreen mode. By creating it windowed and # then switching to fullscreen it will stay white the entire time. self.__gl_handle = glfw.create_window(*size, title, None, glfw.get_current_context()) if not is_fullscreen: glfw.set_window_pos(self.__gl_handle, *position) # Register callbacks glfw.set_framebuffer_size_callback(self.__gl_handle, self.on_resize) glfw.set_key_callback(self.__gl_handle, self.on_key) glfw.set_mouse_button_callback(self.__gl_handle, self.on_mouse_button) self.on_resize(self.__gl_handle, *glfw.get_framebuffer_size(self.__gl_handle)) # gl_state settings with self._switch_to_current_context(): gl_utils.basic_gl_setup() glfw.swap_interval(0) if is_fullscreen: # Switch to full screen here. See NOTE above at glfw.create_window(). glfw.set_window_monitor( self.__gl_handle, gui_monitor.unsafe_handle, 0, 0, *gui_monitor.size, gui_monitor.refresh_rate, )
def setup(self): glfw.set_key_callback(glfw.get_current_context(), self.key_callback)
def _before_make_current(self, width, height): previous_context = glfw.get_current_context() glfw.make_context_current(self._context) if (width, height) != glfw.get_window_size(self._context): glfw.set_window_size(self._context, width, height) return previous_context
def on_resize(window, w, h): active_window = glfw.get_current_context() glfw.make_context_current(window) adjust_gl_view(w, h) glfw.make_context_current(active_window)