def hide_window(self): ''' Hides renderer window :return: None ''' self.is_window_hidden = True glfw.hide_window(self.__window) glfw.poll_events()
def _monitor_event(self, monitor, event): name = glfw.get_monitor_name(monitor) if event == glfw.DISCONNECTED: logger.info("Unplugged monitor {}".format(name)) else: logger.info("Plugged monitor {}".format(name)) self.pano_renderer.enableOutput(False) if self.output_window: glfw.hide_window(self.output_window) async .delay(SWITCH_DELAY, self.open_output_window)
def _vispy_set_visible(self, visible): # Show or hide the window or widget if self._id is None: return if visible: glfw.show_window(self._id) # this ensures that the show takes effect self._vispy_update() else: glfw.hide_window(self._id)
def __init__(self, resolution=(800, 600), hidden=True): if self._run_init: self._run_init = False else: return if not glfw.init(): raise RuntimeError("Failed to initialize GLFW.") self.hidden = hidden self.resolution = resolution self.vao_id = 0 self.vert_vbo_id = 0 self.color_vbo_id = 0 self.vertex_buffer = None self.color_buffer = None self.frame_buffer = None self.result_texture = None self.n_verts = 0 glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.RESIZABLE, False) self.window = glfw.create_window(resolution[0], resolution[1], "Renderer", None, None) if not self.window: glfw.terminate() raise RuntimeError("Failed to create GLFW context.") glfw.make_context_current(self.window) if hidden: glfw.hide_window(self.window) self._init_alpha() self._init_frame_buffer_objects() with open("shaders/vertex.glsl") as vs: vertex_shader = shaders.compileShader(vs.read(), GL_VERTEX_SHADER) with open("shaders/fragment.glsl") as fs: fragment_shader = shaders.compileShader(fs.read(), GL_FRAGMENT_SHADER) self.program = shaders.compileProgram(vertex_shader, fragment_shader) with self.program: uloc = glGetUniformLocation(self.program, 'screen_to_standard_transform') T = self._screen_to_clip() glUniformMatrix3fv(uloc, 1, True, T) self.vao_id = self._init_vao()
def __close_window(self, window): """ if not joined, glfw function can be called where there is no glfw context anymore. Downside of joining is that window destruction will only occur when draw waiting is over - meaning destruction only takes place after a frame this can cause 'noticable stall' when fps is very low to detour this problem, window is hidden before actually being destroyed :param window: :return: """ with self.__context.glfw as window: glfw.hide_window(window) # <- glfw window still alive here self._render_thread.join() self.__context.context_master.remove_window(self) glfw.destroy_window(window) # <- window destoied here
def open_output_window(self): self._pause_event_loop() # Select monitor self._select_monitor() # Open window if self.monitor: # Fullscreen self._select_video_mode() x, y = glfw.get_monitor_pos(self.monitor) logger.info("Output selected : {} on {} at {},{}".format( str(self.video_mode), glfw.get_monitor_name(self.monitor), x, y)) w, h = self.video_mode[0] self.pano_renderer.setViewport(w, h) self.pano_renderer.setRefreshRate( self._convert_rate(30), self._convert_rate(self.video_mode[2])) if self.output_window: glfw.set_window_monitor(self.output_window, self.monitor, x, y, w, h, self.video_mode[2]) glfw.show_window(self.output_window) self.pano_renderer.enableOutput(True) else: self._open_output_window(w, h, self.monitor, self.video_mode) else: # No monitor available or windowed w = self.width / 4 h = self.height / 4 self.pano_renderer.setViewport(w, h) monitor = glfw.get_primary_monitor() if monitor: rate = glfw.get_video_mode(monitor)[2] self.pano_renderer.setRefreshRate(self._convert_rate(30), self._convert_rate(rate)) if self.output_window: self.pano_renderer.enableOutput(True) glfw.show_window(self.output_window) else: self._open_output_window(w, h) if not SETTINGS.display in ["window", "windowed"]: # No monitor available glfw.hide_window(self.output_window) self._unpause_event_loop()
def __init__(self, name, size, show=True): self.size = size # window setup if not glfw.init(): raise RuntimeError('Failed to initialize GLFW') self.window = glfw.create_window(size[0], size[1], name, None, None) if not show: glfw.hide_window(self.window) # hide window if not self.window: glfw.terminate() raise RuntimeError('Failed to create window') # set context glfw.make_context_current(self.window) # init Syhon self.server = syphonpy.SyphonServer(name) # OpenGL init glMatrixMode(GL_PROJECTION) glOrtho(0, size[0], size[1], 0, 1, -1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glDisable(GL_DEPTH_TEST) glClearColor(0.0, 0.0, 0.0, 0.0) glEnable(GL_TEXTURE_2D) # create texture id for use with Syphon self.TextureID = glGenTextures(1) # initalise our sender texture glBindTexture(GL_TEXTURE_2D, self.TextureID) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glBindTexture(GL_TEXTURE_2D, 0)
def _on_close(self, *args): print("closing") all_glfw_canvases.discard(self) glfw.hide_window(self._window)
def _on_close(self, *args): all_glfw_canvases.discard(self) glfw.hide_window(self._window)
def draw(self, return_mat=False): if self.client: img = self.client.new_frame_image() if img and img.texture_size().width and img.texture_size().height: if self.first_frame_flg: # set window size and show option glfw.set_window_size(self.window, int(img.texture_size().width), int(img.texture_size().height)) if not self.show: glfw.hide_window(self.window) # hide window # OpenGL init glMatrixMode(GL_PROJECTION) glOrtho(0, int(img.texture_size().width), int(img.texture_size().height), 0, 1, -1) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glDisable(GL_DEPTH_TEST) glClearColor(0.0, 0.0, 0.0, 0.0) glEnable(GL_TEXTURE_RECTANGLE) self.first_frame_flg = False print(f"server: \"{self.server.app_name}/{self.server.name}\", window: \"{self.window_name}\", size: \"{img.texture_size().width} × {img.texture_size().height}\"") glfw.make_context_current(self.window) # Clear screen glClearColor(0.0, 0.0, 0.0, 0.0) glClear(GL_COLOR_BUFFER_BIT) # Bind the texture glEnable(GL_TEXTURE_RECTANGLE) glActiveTexture(GL_TEXTURE0) glBindTexture(GL_TEXTURE_RECTANGLE, img.texture_name()) # Configure texturing as we want it glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glColor4f(1.0, 1.0, 1.0, 1.0) texCoords = np.array([ 0.0, img.texture_size().height, img.texture_size().width, img.texture_size().height, img.texture_size().width, 0.0, 0.0, 0.0], dtype=GLfloat) verts = np.array([ 0.0, 0.0, img.texture_size().width, 0.0, img.texture_size().width, img.texture_size().height, 0.0, img.texture_size().height], dtype=GLfloat) glEnableClientState( GL_TEXTURE_COORD_ARRAY ) glTexCoordPointer(2, GL_FLOAT, 0, texCoords) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, 0, verts) glDrawArrays( GL_TRIANGLE_FAN, 0, 4 ) if return_mat: x_scale, y_scale = glfw.get_window_content_scale(self.window) frame = np.zeros((int(img.texture_size().height * x_scale), int(img.texture_size().width * y_scale), 4), np.uint8) glPixelStorei(GL_PACK_ALIGNMENT, 1) glPixelStorei(GL_PACK_ROW_LENGTH, (int(img.texture_size().width * x_scale))) glReadPixels(0, 0, int(img.texture_size().width * x_scale), int(img.texture_size().height * y_scale), GL_BGRA, GL_UNSIGNED_BYTE, frame.data) frame = cv2.resize(frame, (int(img.texture_size().width), int(img.texture_size().height))) frame = cv2.flip(frame, 0) return frame glfw.swap_buffers(self.window) glfw.poll_events() # unbind our sender texture glBindTexture(GL_TEXTURE_RECTANGLE, 0)