Ejemplo n.º 1
0
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__)
Ejemplo n.º 2
0
    def showWindow(self):
        GLFW.glfwMakeContextCurrent(self.__window)
        self.__initialize()

        while not GLFW.glfwWindowShouldClose(self.__window):
            self.__loop(self)

        GLFW.glfwTerminate()
Ejemplo n.º 3
0
 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')
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
    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])
Ejemplo n.º 10
0
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__)
Ejemplo n.º 11
0
        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)
Ejemplo n.º 12
0
    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.")
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
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())
Ejemplo n.º 15
0
 def destroy(self):
     glfw.glfwDestroyWindow(self._native_window)
Ejemplo n.º 16
0
 def get_screen(self):
     return glfw.glfwGetWindowMonitor(self._native_window)
Ejemplo n.º 17
0
 def set_title(self, title):
     glfw.glfwSetWindowTitle(self._native_window, title)
     self._title = title
Ejemplo n.º 18
0
    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()
Ejemplo n.º 19
0
 def should_draw(self):
     return self._window and not glfw.glfwWindowShouldClose(self._window)
Ejemplo n.º 20
0
 def get_position(self):
     self._x, self._y = glfw.glfwGetWindowPos(self._native_window)
     return self._x, self._y
Ejemplo n.º 21
0
def loopCallback(window):
    renderer.render(window)
    sync(GLFW.glfwGetTime())
Ejemplo n.º 22
0
 def set_size(self, width, height):
     glfw.glfwSetWindowSize(self._native_window, width, height)
     self._width, self._height = glfw.glfwGetFramebufferSize(
         self._native_window)
Ejemplo n.º 23
0
 def swap(self):
     glfw.glfwSwapBuffers(self._native_window)
Ejemplo n.º 24
0
 def initialize(self, engine):
     GLFW.glfwSetErrorCallback(engine.errorCallback)
     GLFW.glfwSetWindowSizeCallback(self.__window, self.sizeCallback)
     self.windowHint()
     self.__loop = engine.loopCallback
     self.__initialize = engine.initialize
Ejemplo n.º 25
0
 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
Ejemplo n.º 26
0
 def activate(self):
     glfw.glfwMakeContextCurrent(self._native_window)
Ejemplo n.º 27
0
 def hdpi_changed(self):
     return self.hdpi_factor != glfw.glfwGetWindowContentScale(
         self._window)[0]
Ejemplo n.º 28
0
 def manual_resize(self):
     self.on_framebuffer_resize(self._window,
                                *glfw.glfwGetFramebufferSize(self._window))
Ejemplo n.º 29
0
def sync(start):
    slot = 1 / 30.0
    end = start + slot

    while GLFW.glfwGetTime() < end:
        time.sleep(1 / 1000.0)
Ejemplo n.º 30
0
 def set_position(self, x, y):
     glfw.glfwSetWindowPos(self._native_window, x, y)
     self._x, self._y = glfw.glfwGetWindowPos(self._native_window)