def process(dt): # Poll for and process events glfw.glfwPollEvents() for window in __windows__: window.activate() window.imguiRenderer.process_inputs() # Dispatch the main draw event window.dispatch_event('on_draw', dt) # Dispatch the idle event window.dispatch_event('on_idle', dt) imgui_draw_data = imgui.get_draw_data() if imgui_draw_data is not None: window.imguiRenderer.render(imgui_draw_data) # Swap buffers window.swap() for window in __windows_to_remove__: window.destroy() __windows_to_remove__.remove(window) return len(__windows__)
def showWindow(self): GLFW.glfwMakeContextCurrent(self.__window) self.__initialize() while not GLFW.glfwWindowShouldClose(self.__window): self.__loop(self) GLFW.glfwTerminate()
def close(self): glfw.glfwSetWindowShouldClose(self._native_window, True) __windows__.remove(self) __windows_to_remove__.append(self) for i in range(len(self._timer_stack)): handler, interval = self._timer_stack[i] self._clock.unschedule(handler) self.dispatch_event('on_close')
def update(self, timeout=0.0): glfw.glfwWaitEventsTimeout(timeout) self.update_gui() gl_utils.glFlush() glfw.glfwSwapBuffers(self._window) if self.hdpi_changed(): # calling resize will handle hdpi changes and resize the UI accordingly self.manual_resize()
def windowHint(self): GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, True) GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE) GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 2) GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 1)
def animation(self, shader_program, speed=1.0): looplen = self.keyframes[-1].time - self.keyframes[0].time current_time = (glfw.glfwGetTime() / speed) % looplen while (self.keyframes[self.prev_iter].time > current_time) or ( self.keyframes[self.prev_iter + 1].time < current_time): self.prev_iter = (self.prev_iter + 1) % (len(self.keyframes) - 1) duration = self.keyframes[self.prev_iter + 1].time - self.keyframes[self.prev_iter].time frame_progress = (current_time - self.keyframes[self.prev_iter].time) / (duration) pre_frame = self.keyframes[self.prev_iter] next_frame = self.keyframes[self.prev_iter + 1] self.interpolation_joint = dict() # frame_progress = 0.5 # print("progress: ", frame_progress, "looplen ", looplen, self.prev_iter, self.prev_iter + 1) for key, value in pre_frame.joint_transform.items(): t_m = self.interpolating_translation( value[0], next_frame.joint_transform.get(key)[0], frame_progress) r_m = self.interpolating_rotation( value[1], next_frame.joint_transform.get(key)[1], frame_progress) matrix = np.matmul(t_m, r_m) self.interpolation_joint[key] = matrix self.load_animation_matrices(self.root_joint, np.identity(4)) self.render(shader_program)
def on_framebuffer_resize(self, window, w, h): """Updates windows/UI sizes and redraws the UI with correct HDPI scaling.""" self.framebuffer_size = w, h if self.is_minimized(): return # NOTE: macOS and some Linux versions with the wayland display server have an # upscaled framebuffer when using a high-DPI monitor. This means that screen # coordinates != pixel coordinates. We need to know if this is the case in order # to transform UI input into the right coordinates, so we check for this case # here. window_size = glfw.glfwGetWindowSize(window) self.has_scaled_framebuffer = window_size != self.framebuffer_size # Always clear buffers on resize to make sure that the black stripes left/right # are black and not polluted from previous frames. Make sure this is applied on # the whole window and not within glViewport! gl_utils.glClear(gl_utils.GL_COLOR_BUFFER_BIT) gl_utils.glClearColor(0, 0, 0, 1) self.hdpi_factor = glfw.glfwGetWindowContentScale(window)[0] self.gui.scale = self.gui_user_scale * self.hdpi_factor with self.use_content_area() as (x, y, content_w, content_h): # update GUI window to full window self.gui.update_window(w, h) # update content container to content square # NOTE: since this is part of the UI, it will be affected by the gui # scaling, but we actually need real coordinates, so we need to convert it # back. self.cont.outline = ui.FitBox( ui.Vec2(x // self.hdpi_factor, y // self.hdpi_factor), ui.Vec2(content_w // self.hdpi_factor, content_h // self.hdpi_factor), ) self.draw_texture() self.gui.collect_menus() self.update_gui() gl_utils.glFlush() glfw.glfwSwapBuffers(self._window)
def process_unconsumed_user_input(self, user_input): if self.is_minimized(): return x, y = glfw.glfwGetCursorPos(self._window) # x, y are in screen coordinates. pyglui expects pixel coordinates. pos = self.screen_to_pixel(x, y) pos = normalize(pos, self.framebuffer_size) # Position in img pixels pos = denormalize(pos, self.texture.shape[:2]) for button, action, mods in user_input.buttons: self.on_click(pos, button, action)
def set_fullscreen(self, fullscreen, screen=None): screen = 0 if screen is None else screen mode = glfw.glfwGetVideoMode(glfw.glfwGetMonitors()[screen]) if fullscreen: glfw.glfwSetWindowMonitor(self._native_window, screen, 0, 0, mode[0], mode[1], mode[-1]) else: glfw.glfwSetWindowMonitor(self._native_window, screen, 0, 0, 256, 256, mode[-1])
def process(dt): # Poll for and process events glfw.glfwPollEvents() for window in __windows__: # Make window active window.activate() # Dispatch the main draw event window.dispatch_event('on_draw', dt) # Dispatch the idle event window.dispatch_event('on_idle', dt) # Swap buffers window.swap() for window in __windows_to_remove__: window.destroy() __windows_to_remove__.remove(window) return len(__windows__)
def on_mouse_button(win, button, action, mods): x, y = glfw.glfwGetCursorPos(win) if self._hidpi: x, y = 2 * x, 2 * y button = __mouse_map__.get(button, window.mouse.UNKNOWN) if action == glfw.GLFW_RELEASE: self._button = window.mouse.NONE self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_release', x, y, button) elif action == glfw.GLFW_PRESS: self._button = button self._mouse_x = x self._mouse_y = y self.dispatch_event('on_mouse_press', x, y, button)
def __init__(self): if not GLFW.glfwInit(): raise EnvironmentError("Could not initialize the library.") GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, 8) self.__window = GLFW.glfwCreateWindow(self.width, self.height, "Path", None, None) if not self.__window: GLFW.glfwTerminate() raise RuntimeError("Could create a window.")
def close(self): if not self._window: return glfw.glfwRestoreWindow(self._window) del self.gui[:] self.gui.terminate() glfw.glfwDestroyWindow(self._window) glfw.glfwTerminate() del self.gui self._window = None
def render(window): global program, vao, indices, cube_positions GLFW.glfwPollEvents() GL.glViewport(0, 0, window.width, window.height) cameraPos = GLM.vec3(0.0, 0.0, 3.0) cameraTarget = GLM.vec3(0.0, 0.0, 0.0) cameraDirection = GLM.normalize(cameraPos - cameraTarget) up = GLM.vec3(0.0, 1.0, 0.0) cameraRight = GLM.normalize(GLM.cross(up, cameraDirection)) cameraUp = GLM.cross(cameraDirection, cameraRight) view = GLM.lookAt(GLM.vec3(0.0, 0.0, 3.0), GLM.vec3(0.0, 0.0, 0.0), GLM.vec3(0.0, 1.0, 0.0)) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) program.useProgram() view = GLM.mat4(1.0) view = GLM.translate(view, GLM.vec3(0.0, 0.0, -3.0)) projection = GLM.perspective(GLM.radians(45.0), window.width / window.height, 0.1, 100.0) program.setMat4("projection", projection) for w in range(10): radius = 10.0 camX = MATH.sin(GLFW.glfwGetTime()) * radius camZ = MATH.cos(GLFW.glfwGetTime()) * radius view = GLM.lookAt(GLM.vec3(camX, 0.0, camZ), GLM.vec3(0.0, 0.0, 0.0), GLM.vec3(0.0, 1.0, 0.0)) model = GLM.mat4(1.0) model = GLM.translate(model, cube_positions[w]) model = GLM.rotate(model, GLM.radians(20.0 * w), GLM.vec3(1.0, 0.3, 0.5)) program.setMat4("model", model) program.setMat4("view", view) vao.useVAO(program) GL.glDrawArrays(GL.GL_TRIANGLES, 0, 36) vao.unBind() GLFW.glfwSwapBuffers(window.getWindow())
def destroy(self): glfw.glfwDestroyWindow(self._native_window)
def get_screen(self): return glfw.glfwGetWindowMonitor(self._native_window)
def set_title(self, title): glfw.glfwSetWindowTitle(self._native_window, title) self._title = title
def open(self, size=(800, 800), pos=(50, 50), gui_scale=1.0, ui_config=None): if self._window: return glfw.glfwInit() glfw.glfwWindowHint(glfw.GLFW_SCALE_TO_MONITOR, glfw.GLFW_TRUE) # Window name needs to be equal to `StartupWMClass` field in Linux .desktop file # else the icon will not show correctly on Linux! self._window = glfw.glfwCreateWindow(*size, "Pupil Invisible Monitor", monitor=None, share=None) glfw.glfwSetWindowSizeLimits(self._window, 200, 200, glfw.GLFW_DONT_CARE, glfw.GLFW_DONT_CARE) glfw.glfwSetWindowPos(self._window, *pos) glfw.glfwMakeContextCurrent(self._window) cygl.utils.init() self.gui = ui.UI() self.gui_user_scale = gui_scale # Adding an intermediate container fixes a pylgui display bug self.cont = ui.Container((0, 0), (0, 0), (0, 0)) self.quickbar = ui.Horizontally_Stretching_Menu( "Quick Bar", (0.0, -120.0), (0.0, 0.0)) self.cont.append(self.quickbar) self.gui.append(self.cont) # Register callbacks main_window glfw.glfwSetFramebufferSizeCallback(self._window, self.on_framebuffer_resize) glfw.glfwSetKeyCallback(self._window, self.on_window_key) glfw.glfwSetCharCallback(self._window, self.on_window_char) glfw.glfwSetMouseButtonCallback(self._window, self.on_window_mouse_button) glfw.glfwSetCursorPosCallback(self._window, self.on_pos) glfw.glfwSetScrollCallback(self._window, self.on_scroll) self.gui.configuration = ui_config or {} gl_utils.basic_gl_setup() # Perform an initial window size setup self.manual_resize()
def should_draw(self): return self._window and not glfw.glfwWindowShouldClose(self._window)
def get_position(self): self._x, self._y = glfw.glfwGetWindowPos(self._native_window) return self._x, self._y
def loopCallback(window): renderer.render(window) sync(GLFW.glfwGetTime())
def set_size(self, width, height): glfw.glfwSetWindowSize(self._native_window, width, height) self._width, self._height = glfw.glfwGetFramebufferSize( self._native_window)
def swap(self): glfw.glfwSwapBuffers(self._native_window)
def initialize(self, engine): GLFW.glfwSetErrorCallback(engine.errorCallback) GLFW.glfwSetWindowSizeCallback(self.__window, self.sizeCallback) self.windowHint() self.__loop = engine.loopCallback self.__initialize = engine.initialize
def get_size(self): # self._width, self._height = glfw.glfwGetWindowSize(self._native_window) self._width, self._height = glfw.glfwGetFramebufferSize( self._native_window) return self._width, self._height
def activate(self): glfw.glfwMakeContextCurrent(self._native_window)
def hdpi_changed(self): return self.hdpi_factor != glfw.glfwGetWindowContentScale( self._window)[0]
def manual_resize(self): self.on_framebuffer_resize(self._window, *glfw.glfwGetFramebufferSize(self._window))
def sync(start): slot = 1 / 30.0 end = start + slot while GLFW.glfwGetTime() < end: time.sleep(1 / 1000.0)
def set_position(self, x, y): glfw.glfwSetWindowPos(self._native_window, x, y) self._x, self._y = glfw.glfwGetWindowPos(self._native_window)