Example #1
0
def app(render, width=1280, height=720, window_name="Maldives"):
    """

    :param render:
    :param width:
    :param height:
    :param window_name:
    :return:
    """
    imgui.create_context()
    window = impl_glfw_init(width, height, window_name)
    impl = GlfwRenderer(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        gl.glClearColor(0.3, 0.5, 0.4, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        impl.process_inputs()
        imgui.new_frame()
        render()
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)
    impl.shutdown()
    glfw.terminate()
Example #2
0
    def start(self):
        imgui.create_context()
        io = imgui.get_io()
        # io.fonts.add_font_default()
        io.fonts.add_font_from_file_ttf(
            "./res/font/Roboto-Medium.ttf", 14,
            io.fonts.get_glyph_ranges_chinese_full())
        io.fonts.add_font_from_file_ttf(
            "./res/font/FZY3JW.ttf", 13,
            io.fonts.get_glyph_ranges_chinese_full(), True)
        self.window = App._impl_glfw_init()
        impl = GlfwRenderer(self.window)

        while not glfw.window_should_close(self.window):
            glfw.poll_events()
            impl.process_inputs()
            imgui.new_frame()
            # imgui.show_test_window()

            self.update()

            gl.glClearColor(1., 1., 1., 1)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT)

            imgui.render()
            impl.render(imgui.get_draw_data())
            glfw.swap_buffers(self.window)

        self.main_view and self.main_view.on_close()
        impl.shutdown()
        glfw.terminate()
Example #3
0
def main(recfile):
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    slamgui = SLAMGUI(recfile)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_quit, _ = imgui.menu_item("Quit", 'Cmd+Q', False, True)
                if clicked_quit:
                    exit(0)
                imgui.end_menu()
            imgui.end_main_menu_bar()

        slamgui.render()
        gl.glClearColor(0, 0, 0, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #4
0
def main():
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    font_scaling_factor = fb_to_window_factor(window)

    io = impl.io

    # clear font atlas to avoid downscaled default font
    # on highdensity screens. First font added to font
    # atlas will become default font.
    io.fonts.clear()

    # set global font scaling
    io.font_global_scale = 1. / font_scaling_factor

    # dictionary of font objects from our font directory
    fonts = {
        os.path.split(font_path)[-1]: io.fonts.add_font_from_file_ttf(
            font_path, FONT_SIZE_IN_PIXELS * font_scaling_factor,
            io.fonts.get_glyph_ranges_latin())
        for font_path in FONTS_DIR
    }
    secondary_window_main_font = random.choice(list(fonts.values()))

    impl.refresh_font_texture()

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        imgui.begin("Window with multiple custom fonts", True)
        imgui.text("This example showcases font usage on text() widget")

        for font_name, font in fonts.items():
            imgui.separator()

            imgui.text("Font:{}".format(font_name))

            with imgui.font(font):
                imgui.text("This text uses '{}' font.".format(font_name))

        imgui.end()

        with imgui.font(secondary_window_main_font):
            imgui.begin("Window one main custom font", True)
            imgui.text("This window uses same custom font for all widgets")
            imgui.end()

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #5
0
class Application:
    def __init__(self, args):
        imgui.create_context()
        self._window = self._glfw_init()
        self._renderer = GlfwRenderer(self._window)
        self._scene = TinyRendererEditor()
        self.main_loop()

    def main_loop(self):
        while not glfw.window_should_close(self._window):
            glfw.poll_events()
            self._renderer.process_inputs()

            imgui.new_frame()

            gl.glClearColor(0.2, 0.2, 0.2, 1.0)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT)

            self._scene.update()

            imgui.render()
            draw_data = imgui.get_draw_data()
            if draw_data.valid:
                self._renderer.render(draw_data)
            glfw.swap_buffers(self._window)

        self._renderer.shutdown()
        glfw.terminate()

    def _glfw_init(self):
        """
        Initializes the glfw an OpenGL context and returns a window
        """
        width, height = 1600, 900
        window_name = "Computer Graphics Playground"

        if not glfw.init():
            print("Could not initialize OpenGL context")
            exit(1)

        # OS X supports only forward-compatible core profiles from 3.2
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

        # Create a windowed mode window and its OpenGL context
        window = glfw.create_window(int(width), int(height), window_name, None,
                                    None)
        glfw.make_context_current(window)

        if not window:
            glfw.terminate()
            print("Could not initialize Window")
            exit(1)

        return window
Example #6
0
    def __init__(self):
        """Initialise a program and start a rendering loop
        This creates a glfw window + an imgui context
        This also then handles an update and rendering loop

        Raises:
            EnvironmentError: if GLFW fails to initialize
            EnvironmentError: if GLFW fails to creat a window
        """
        if not glfw.init():
            raise EnvironmentError("Failed to initialize GLFW.")

        # Create window
        window = glfw.create_window(1600, 900, "Hello World!", None, None)
        if not window:
            glfw.terminate()
            raise EnvironmentError("Failed to create window.")

        # Add in an imgui context for debugging / UIs
        glfw.make_context_current(window)
        imgui.create_context()
        impl = ImGuiGlfwRenderer(window)

        # Build the scene and load resources
        self.buildScene()

        currentTime = glfw.get_time()
        # Window loop
        while not glfw.window_should_close(window):
            # Calculate delta time
            prevTime = currentTime
            currentTime = glfw.get_time()
            delta = currentTime - prevTime

            # Identify which keys are being pressed
            keyStates = {}
            for name, id in glfwKeyNames.items():
                keyStates[name] = glfw.get_key(window, id) == glfw.PRESS

            # Pass off to sub functions
            self.update(delta, keyStates)
            self.render(window)
            self.ui(window)

            # Render imgui stuff
            impl.render(imgui.get_draw_data())
            glfw.swap_buffers(window)
            glfw.poll_events()
            impl.process_inputs()

        # Exit gracefully once glfw wants to close the window
        glfw.terminate()
Example #7
0
def main():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    ip_address = "localhost:3333"
    #imgui.set_window_font_scale(1.0)
    comms = MouseSocket()
    click_guard = True

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()
        imgui.set_next_window_size(300, 120)
        imgui.set_next_window_position(10, 0)
        imgui.begin("", False, imgui.WINDOW_NO_RESIZE)
        changed, ip_address = imgui.input_text(label="IP:PORT",
                                               value=ip_address,
                                               buffer_length=30)
        clicked = imgui.button(label="CONNECT")
        if clicked:
            comms.connect(ip_address)

        max_x, max_y = glfw.get_window_size(window)
        x, y = glfw.get_cursor_pos(window)

        norm_x = x / max_x
        norm_y = y / max_y

        state = glfw.get_mouse_button(window, glfw.MOUSE_BUTTON_LEFT)
        if state == glfw.PRESS:
            click_guard = False
        elif state == glfw.RELEASE and not click_guard:
            click_guard = True
            comms.send_click(norm_x, norm_y)

        comms.send_mouse_coords(norm_x, norm_y)

        imgui.text("Mouse Coords: {:.2f}, {:.2f}".format(norm_x, norm_y))
        imgui.text("Connected: {}".format(comms.is_connected))
        imgui.end()

        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #8
0
def main():
    desktop = Desktop()
    imgui.create_context()
    window = core.impl_glfw_init()
    impl = GlfwRenderer(window)
    imgui.core.style_colors_light()

    app_list = list(get_app_list())

    # figure = plt.figure()
    # img = Image.open('./test.png')
    # texture, width, height = readImage(img)

    def update():
        imgui.new_frame()
        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("System", True):
                for app in app_list:
                    clicked, selected = imgui.menu_item(
                        app.__name__, '', False, True)
                    if clicked:
                        desktop.add(app())
                imgui.separator()
                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", '', False, True)

                if clicked_quit:
                    exit(1)

                imgui.end_menu()
            imgui.end_main_menu_bar()

        desktop.render()

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        update()

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        data = imgui.get_draw_data()
        impl.render(data)

        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #9
0
def main():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    _ = gl.glGenFramebuffers(1)

    videoWindows = []

    def dropFile(window, files):
        for file in files:
            videoWindows.append(VideoPlayer(file))

    glfw.set_drop_callback(window, dropFile)

    while not glfw.window_should_close(window):

        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        videoWindows = [x for x in videoWindows if x.open]
        if len(videoWindows) > 0:
            for videoWindow in videoWindows:
                videoWindow.render()
        else:
            imgui.core.set_next_window_position(10, 10)
            winw, winh = glfw.get_window_size(window)
            imgui.core.set_next_window_size(winw - 20, 10)

            imgui.begin("##Message",
                        True,
                        flags=imgui.WINDOW_NO_SCROLLBAR
                        | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_TITLE_BAR
                        | imgui.WINDOW_NO_MOVE)
            imgui.text("Drop one or more video files onto this window to play")
            imgui.end()

        gl.glClearColor(0., 0., 0., 1.)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    for videoWindow in videoWindows:
        videoWindow.terminate()
    impl.shutdown()
    glfw.terminate()
Example #10
0
def main():
    window = impl_glfw_init()
    imgui.create_context()
    impl = GlfwRenderer(window)

    plot_values = array('f', [sin(x * C) for x in range(L)])
    histogram_values = array('f', [random() for _ in range(20)])

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        imgui.begin("Plot example")
        imgui.plot_lines(
            "Sin(t)",
            plot_values,
            overlay_text="SIN() over time",
            # offset by one item every milisecond, plot values
            # buffer its end wraps around
            values_offset=int(time() * 100) % L,
            # 0=autoscale => (0, 50) = (autoscale width, 50px height)
            graph_size=(0, 50),
        )

        imgui.plot_histogram(
            "histogram(random())",
            histogram_values,
            overlay_text="random histogram",
            # offset by one item every milisecond, plot values
            # buffer its end wraps around
            graph_size=(0, 50),
        )

        imgui.end()

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #11
0
def main():
    ctx = imgui.create_context()
    implot.create_context()
    implot.set_imgui_context(ctx)

    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):

                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True)

                if clicked_quit:
                    exit(1)

                imgui.end_menu()
            imgui.end_main_menu_bar()

        imgui.begin("Custom window", True)
        imgui.text("Bar")
        imgui.text_ansi("B\033[31marA\033[mnsi ")
        imgui.text_ansi_colored("Eg\033[31mgAn\033[msi ", 0.2, 1., 0.)
        imgui.extra.text_ansi_colored("Eggs", 0.2, 1., 0.)
        imgui.end()

        # show_test_window()
        imgui.show_test_window()
        implot.show_demo_window()

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #12
0
class HelloTriangleImgui(OpenGLApp):
    def __init__(self):
        super().__init__("Hello Triangle with ImGui")
        self._vao = 0
        self._shader_program = None
        self._ui = None

    def initialize(self):
        vertices = np.array([-0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0], dtype=np.float32)

        self._vao = GL.glGenVertexArrays(1)
        GL.glBindVertexArray(self._vao)

        vbo = GL.glGenBuffers(1)
        GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo)
        GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices, GL.GL_STATIC_DRAW)

        GL.glVertexAttribPointer(0, 3, GL.GL_FLOAT, GL.GL_FALSE, 3 * ctypes.sizeof(ctypes.c_float), None)
        GL.glEnableVertexAttribArray(0)

        GL.glBindVertexArray(0)

        vertex_shader = Shader(GL.GL_VERTEX_SHADER, "shaders_src/hello_triangle_vertex.glsl")
        fragment_shader = Shader(GL.GL_FRAGMENT_SHADER, "shaders_src/hello_triangle_frag.glsl")
        self._shader_program = ShaderProgram(vertex_shader, fragment_shader)
        vertex_shader.delete()
        fragment_shader.delete()

        imgui.create_context()
        self._ui = GlfwRenderer(self._window, False)

    def render(self):
        self._shader_program.use()
        GL.glBindVertexArray(self._vao)
        GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3)

    def render_ui(self):
        imgui.new_frame()
        imgui.show_demo_window()
        imgui.render()
        self._ui.render(imgui.get_draw_data())
        self._ui.process_inputs()
Example #13
0
def replay(fname):
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    replaygui = ReplayGUI(fname)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_quit, _ = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True)
                if clicked_quit:
                    exit(0)
                imgui.end_menu()
            if imgui.begin_menu("View", True):
                _, replaygui.show_frontview = imgui.menu_item(
                    "Front view", selected=replaygui.show_frontview)
                imgui.end_menu()
            if imgui.begin_menu("Tools", True):
                clicked, _ = imgui.menu_item("Learn control params")
                if clicked:
                    replaygui.learn_controls = True
                clicked, _ = imgui.menu_item("Lap timer")
                if clicked:
                    replaygui.lap_timer = True
                imgui.end_menu()
            imgui.end_main_menu_bar()

        replaygui.render()
        gl.glClearColor(0, 0, 0, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #14
0
class Gui:

    def __init__(self, window):
        imgui.create_context()
        self.impl = GlfwRenderer(window)
        self.color = 0.45, 0.55, 0.60, 1.00


    def NewFrame(self):
        self.impl.process_inputs()
        imgui.new_frame()
    
    def EndFrame(self):
        imgui.end_frame()
        imgui.render()
        self.impl.render(imgui.get_draw_data())


    def endGui(self):
        self.impl.shutdown()
Example #15
0
class Gui(object, IRuntimeModule):
    def __init__(self):
        super(Gui, self).__init__()
        self.impl = None  # type: Optional[GlfwRenderer]
        self.on_gui = None
        self.io = None
        self.on_gui_editor = None

    # ---------------------------------------------------
    # IRuntimeModule
    # ---------------------------------------------------
    @logger
    def initialize(self):
        imgui.create_context()
        self.io = imgui.get_io()
        # TODO 这些枚举搜不到,需要式再说
        # 		io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;           // Enable Docking
        # 		io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;         // Enable Multi-Viewport / Platform Windows
        # self.io.config_flags |= imgui.CONFIG_NAV_ENABLE_KEYBOARD  # Enable Keyboard Controls TODO cause error
        window = G.appm.window_handle
        self.impl = GlfwRenderer(
            window, attach_callbacks=False
        )  # NOTE otherwise imgui will register glfw callbacks
        imgui.style_colors_dark()
        self.on_gui = G.appm.on_gui
        self.on_gui_editor = G.editorm.on_gui

    @logger
    def finalize(self):
        self.impl.shutdown()
        imgui.destroy_context()

    def update(self):
        self.impl.process_inputs()
        imgui.new_frame()

        self.on_gui and self.on_gui()
        self.on_gui_editor and self.on_gui_editor()

        imgui.render()
        self.impl.render(imgui.get_draw_data())
Example #16
0
def sim():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    sg = SimGUI()
    t0 = time.time()
    play = True
    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_quit, _ = imgui.menu_item("Quit", 'Cmd+Q', False, True)
                if clicked_quit:
                    exit(0)
                imgui.end_menu()
            imgui.end_main_menu_bar()

        t = time.time()
        if play:
            sg.step(min(t - t0, 0.03))
        else:
            sg.idle()
        t0 = t
        play = sg.render(play)

        gl.glClearColor(0, 0, 0, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #17
0
def main_glfw():
    def glfw_init():
        width, height = 1280, 720
        window_name = "minimal ImGui/GLFW3 example"
        if not glfw.init():
            print("Could not initialize OpenGL context")
            exit(1)
        # OS X supports only forward-compatible core profiles from 3.2
        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
        # Create a windowed mode window and its OpenGL context
        window = glfw.create_window(
            int(width), int(height), window_name, None, None
        )
        glfw.make_context_current(window)
        if not window:
            glfw.terminate()
            print("Could not initialize Window")
            exit(1)
        return window
    window = glfw_init()
    impl = GlfwRenderer(window)
    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()
        on_frame()
        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)
    impl.shutdown()
    glfw.terminate()
Example #18
0
def main():
    init()

    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        draw(imgui)

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #19
0
def main():
    global width_library
    global width_shematic
    global width_context
    global height_window

    global previous_key_callback
    global selected_link
    global selected_node
    global iggraph
    global node_library
    global debug_is_mouse_dragging
    
    global show_debug_window

    # states -------------------------
    
    scrolling = imgui.Vec2(0, 0)

    iggraph = IGGraph(node_library)

    # iggraph.reset()

    node_hovered_in_scene = -1
    parameter_link_start = None
    selected_parameter = None

    io_hovered = None 
    io_anchors_width_not_hovered = 10
    io_anchors_width_hovered = 15 
    right_splitter_is_active = False
    left_splitter_is_active = False
    
    image_width = 0
    image_height = 0
    image_texture = None

    # states -------------------------

    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)
    io = imgui.get_io()
    previous_key_callback = glfw.set_key_callback(window,key_event)
    init_textures()

    assign_parameter_colors(node_library)

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()
        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_new, selected_new = imgui.menu_item(
                    "New", 'Cmd+N', False, True
                )
                if clicked_new:
                    iggraph = IGGraph(node_library)
                clicked_load, selected_load = imgui.menu_item(
                    "Load", 'Cmd+L', False, True
                )
                if clicked_load:
                    root = tk.Tk()
                    root.withdraw()
                    filename = filedialog.askopenfilename()
                    if filename :
                        f=open(filename)
                        iggraph.from_json(json.load(f))
                        f.close()
                clicked_save, selected_save = imgui.menu_item(
                    "Save", 'Cmd+S', False, True
                )
                if clicked_save:
                    iggraph.reset()
                    graph_json = iggraph.to_json()
                    text2save = json.dumps(graph_json, indent=4, sort_keys=True)
                    root = tk.Tk()
                    root.withdraw()
                    f = filedialog.asksaveasfile(mode='w', defaultextension=".json")
                    if f is None: # asksaveasfile return `None` if dialog closed with "cancel".
                        return
                    f.write(text2save)
                    f.close()
                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True
                )
                if clicked_quit:
                    exit(0)
                imgui.end_menu()
            if imgui.begin_menu("Debug", True):
                show_debug_window_clicked,  show_debug_window_selected = imgui.menu_item(
                    "Show Debug window", 'Cmd+D', show_debug_window, True
                )
                if show_debug_window_clicked:
                    show_debug_window = not show_debug_window
                catch_exceptions_clicked,  catch_exceptions_selected = imgui.menu_item(
                    "Catch Exceptions", '', iggraph.catch_exceptions, True
                )
                if catch_exceptions_clicked:
                    iggraph.catch_exceptions = not iggraph.catch_exceptions
                imgui.separator()
                imgui.menu_item(
                    "Examples", '', False, False
                )
                show_example_mosaic_clicked,  show_example_mosaic_selected = imgui.menu_item(
                    "Mosaic", '', False, True
                )
                if show_example_mosaic_clicked:
                    example_mosaic(iggraph)
                imgui.end_menu()
            imgui.end_main_menu_bar()

        height_window = io.display_size.y - 18  # imgui.get_cursor_pos_y()

        imgui.push_style_var(imgui.STYLE_ITEM_SPACING, imgui.Vec2(0,0))
        imgui.set_next_window_size(io.display_size.x, height_window)
        imgui.set_next_window_position(0, 18)
        imgui.push_style_var(imgui.STYLE_WINDOW_ROUNDING, 0)
        imgui.begin("Splitter test", False, imgui.WINDOW_NO_TITLE_BAR | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_BRING_TO_FRONT_ON_FOCUS)
        imgui.pop_style_var()
        imgui.pop_style_var()

        width_shematic = io.display_size.x - separator_width  - width_context - separator_width - width_library

        # ==============================================================================
        # Library
        # ==============================================================================

        imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0)
        imgui.begin_child("Library", width_library, 0, True)
        for node_name in node_library.nodes:
            if imgui.button(node_name, width_library):
                iggraph.create_node(node_name)
        imgui.end_child()
        imgui.pop_style_var()

        imgui.same_line()
        imgui.button("left_splitter", separator_width, height_window - 20)
        left_splitter_is_active = imgui.is_item_active()
        if (left_splitter_is_active):
            width_library += io.mouse_delta.x
            scrolling = imgui.Vec2(scrolling.x - io.mouse_delta.x, scrolling.y)
        if (imgui.is_item_hovered()):
            imgui.set_mouse_cursor(imgui.MOUSE_CURSOR_RESIZE_EW)
        
        imgui.same_line()

        # ==============================================================================
        # Shematic
        # ==============================================================================
        imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0)
        imgui.begin_child("shematic", width_shematic, 0, True)

        if show_inputs_popup(iggraph):
            imgui.open_popup("Outputs")
        show_outputs_popup(iggraph)

        # create our child canvas
        if iggraph.get_state() == iggraph.STATE_IDLE:
            imgui.text("status: edit | ")
        elif iggraph.get_state() == iggraph.STATE_RUNNING:
            imgui.text("status: run  | ")
        imgui.same_line()
        if iggraph.get_state() == iggraph.STATE_IDLE:
            if imgui.button("run"):
                #TODO if not running
                iggraph.set_state(iggraph.STATE_RUNNING)
                iggraph.prepare_to_run()
                imgui.open_popup("User Input")
            imgui.same_line()
            if imgui.button("run one step"):
                iggraph.set_state(iggraph.STATE_RUNNING)
                iggraph.prepare_to_run()
                iggraph.run_one_step()
        elif iggraph.get_state() == iggraph.STATE_RUNNING:
            if imgui.button("stop running"):
                iggraph.reset()
                iggraph.set_state(iggraph.STATE_IDLE)
            imgui.same_line()
            if imgui.button("run one step"):
                iggraph.set_state(iggraph.STATE_RUNNING)
                iggraph.run_one_step()
        # imgui.same_line(imgui.get_window_width() - 100)
        imgui.push_style_var(imgui.STYLE_FRAME_PADDING, imgui.Vec2(1, 1))
        imgui.push_style_var(imgui.STYLE_WINDOW_PADDING, imgui.Vec2(0, 0))
        imgui.begin_child("scrolling_region", 0, 0, True, imgui.WINDOW_NO_SCROLLBAR | imgui.WINDOW_NO_MOVE)
        imgui.pop_style_var()
        imgui.pop_style_var()
        imgui.push_item_width(120.0)

        offset = add(imgui.get_cursor_screen_pos(), scrolling)
        draw_list = imgui.get_window_draw_list()

        # Display links
        draw_list.channels_split(2)
        draw_list.channels_set_current(0)
        for link in iggraph.links:
            draw_link_param_to_param(draw_list, offset, link.output_parameter, link.input_parameter, selected_link == link)

        # Display nodes
        parameter_link_end = None
        one_parameter_hovered = False
        one_node_moving_active = False
        for node in iggraph.nodes:
            imgui.push_id(str(node.id))
            node_rect_min = add(offset, node.pos)
            draw_list.channels_set_current(1) # foreground
            old_any_active = imgui.is_any_item_active()

            #display node content first
            # todo
            test = add(node_rect_min, NODE_WINDOW_PADDING)
            imgui.set_cursor_screen_position(add(node_rect_min, NODE_WINDOW_PADDING))
            imgui.begin_group()
            imgui.text("")
            imgui.text(node.name)
            imgui.text("")
            imgui.end_group()

            # save size
            node_widgets_active = False # (not old_any_active and imgui.is_any_item_active())
            node.size = add( add( imgui.get_item_rect_size(), NODE_WINDOW_PADDING) , NODE_WINDOW_PADDING)
            node_rect_max = add(node.size, node_rect_min) 
            
            #display node box
            draw_list.channels_set_current(0) # background
            imgui.set_cursor_screen_position(node_rect_min)
            imgui.invisible_button(str(node.id), node.size.x, node.size.y)
            if imgui.is_item_hovered():
                node_hovered_in_scene = node.id
            else:
                node_hovered_in_scene = None
            node_moving_active = imgui.is_item_active()
            use_hovered_color = node_hovered_in_scene or selected_node == node
            draw_list.add_rect_filled(node_rect_min.x, node_rect_min.y, node_rect_max.x, node_rect_max.y, get_node_color(node, iggraph, use_hovered_color), 5)
            if node_hovered_in_scene and iggraph.is_error(node):
                imgui.begin_tooltip()
                imgui.text(iggraph.error_nodes[node])
                imgui.end_tooltip()

            # input parameters
            for parameter_name in node.inputs:
                parameter = node.inputs[parameter_name]
                center = node.get_intput_slot_pos(parameter)
                center_with_offset = add(offset, center)
                if io_hovered == parameter:
                    io_anchors_width = io_anchors_width_hovered
                else:
                    io_anchors_width = io_anchors_width_not_hovered
                imgui.set_cursor_pos(imgui.Vec2(center.x-io_anchors_width/2, center.y-io_anchors_width/2))
                imgui.push_id(str(str(node.id) + "input" + parameter.id))
                if (imgui.invisible_button("input", io_anchors_width, io_anchors_width)):
                    selected_parameter = parameter
                # imgui.is_item_hovered() does not work when dragging
                is_hovering = ((io.mouse_pos.x-offset.x>center.x-io_anchors_width/2) and 
                               (io.mouse_pos.x-offset.x<center.x+io_anchors_width/2) and
                               (io.mouse_pos.y-offset.y>center.y-io_anchors_width/2) and
                               (io.mouse_pos.y-offset.y<center.y+io_anchors_width/2))
                if is_hovering:
                    io_hovered = parameter
                    one_parameter_hovered = True
                    imgui.begin_tooltip()
                    imgui.text(parameter_name)
                    imgui.end_tooltip()
                if is_hovering and imgui.is_mouse_released(0):
                    parameter_link_end = parameter
                imgui.pop_id()
                draw_list.add_circle_filled(center_with_offset.x, center_with_offset.y, io_anchors_width/2, get_parameter_color(parameter))

            # output parameters
            for parameter_name in node.outputs:
                parameter = node.outputs[parameter_name]
                center = node.get_output_slot_pos(parameter)
                center_with_offset = add(offset, center)
                if io_hovered == parameter:
                    io_anchors_width = io_anchors_width_hovered
                else:
                    io_anchors_width = io_anchors_width_not_hovered
                imgui.set_cursor_pos(imgui.Vec2(center.x-io_anchors_width/2, center.y-io_anchors_width/2))
                imgui.push_id(str(str(node.id) + "output" + parameter.id))
                if (imgui.invisible_button("output", io_anchors_width, io_anchors_width)):
                    selected_parameter = parameter
                is_hovering = ((io.mouse_pos.x-offset.x>center.x-io_anchors_width/2) and 
                    (io.mouse_pos.x-offset.x<center.x+io_anchors_width/2) and
                    (io.mouse_pos.y-offset.y>center.y-io_anchors_width/2) and
                    (io.mouse_pos.y-offset.y<center.y+io_anchors_width/2))
                if is_hovering:
                    io_hovered = parameter
                    one_parameter_hovered = True
                    imgui.begin_tooltip()
                    imgui.text(parameter_name)
                    imgui.end_tooltip()
                draw_list.add_circle_filled(center_with_offset.x, center_with_offset.y, io_anchors_width/2, get_parameter_color(parameter))
                imgui.pop_id()
                # cannot use imgui.is_item_active, seems buggy with the scroll
                if is_hovering and imgui.is_mouse_down(0):
                    parameter_link_start = parameter

            if node_widgets_active or node_moving_active:
                selected_node = node
                one_node_moving_active = True
            if node_moving_active and imgui.is_mouse_dragging(0) and node.id==selected_node.id:
               node.pos = add(node.pos, io.mouse_delta)

            debug_is_mouse_dragging = imgui.is_mouse_dragging(0)

            imgui.pop_id()
        draw_list.channels_merge()
        if not one_parameter_hovered:
            io_hovered = None

        # scrolling
        mouse_is_in_schematic = (io.mouse_pos.x > width_library) and\
                                (io.mouse_pos.x < (width_library + width_shematic)) and\
                                (io.mouse_pos.y < height_window) and\
                                (io.mouse_pos.y > 18)
        if not one_node_moving_active and\
           not parameter_link_start and\
           imgui.is_mouse_dragging(0) and\
           mouse_is_in_schematic and\
           not left_splitter_is_active and\
           not right_splitter_is_active and\
           imgui.is_window_focused():
            scroll_offset = imgui.Vec2(io.mouse_delta.x, io.mouse_delta.y)
            scrolling = add(scrolling, scroll_offset)
        # link creation
        if parameter_link_start and parameter_link_end:
            iggraph.add_link(parameter_link_start, parameter_link_end)
        # creating link
        elif parameter_link_start and imgui.is_mouse_dragging(0):
            draw_link_param_to_point(draw_list, offset, parameter_link_start, io.mouse_pos.x, io.mouse_pos.y, True)
        # mouse release
        if imgui.is_mouse_released(0):
            parameter_link_start = None

        imgui.pop_item_width()
        imgui.end_child()

        # mouse click on the scene
        if imgui.is_mouse_clicked(0):
            mouse_pos = imgui.get_mouse_pos()
            local_mouse_pos = imgui.Vec2(mouse_pos.x - offset.x, mouse_pos.y - offset.y)
            selected_link = None
            for link in iggraph.links:
                start_node = link.output_parameter.owner
                start_pos = start_node.get_output_slot_pos(link.output_parameter)
                end_node = link.input_parameter.owner
                end_pos = end_node.get_intput_slot_pos(link.input_parameter)
                distance_mouse_start = math.sqrt(((local_mouse_pos.x-start_pos.x)**2) + ((local_mouse_pos.y-start_pos.y)**2))
                distance_mouse_end = math.sqrt(((local_mouse_pos.x-end_pos.x)**2) + ((local_mouse_pos.y-end_pos.y)**2))
                distance_start_end = math.sqrt(((start_pos.x-end_pos.x)**2) + ((start_pos.y-end_pos.y)**2))
                if ((distance_mouse_start + distance_mouse_end) - distance_start_end) < 0.1:
                    selected_link = link
                
        imgui.end_child()
        imgui.pop_style_var()

        imgui.same_line()
        imgui.button("right_splitter", separator_width, height_window - 20)
        right_splitter_is_active = imgui.is_item_active()
        if (right_splitter_is_active):
            width_context -= io.mouse_delta.x
        if (imgui.is_item_hovered()):
            imgui.set_mouse_cursor(imgui.MOUSE_CURSOR_RESIZE_EW)

        # ==============================================================================
        # Context
        # ==============================================================================

        imgui.same_line()
        imgui.push_style_var(imgui.STYLE_CHILD_BORDERSIZE, 0)
        imgui.begin_child("child3", width_context, 0, True);
        if selected_node:
            if selected_node.handle_dynamic_parameters():
                if imgui.button("add parameter"):
                    selected_node.add_dynamic_parameter()
            if imgui.tree_node("Inputs"):
                for parameter_name in selected_node.inputs:
                    parameter = selected_node.inputs[parameter_name]
                    if imgui.tree_node(parameter.id):
                        display_parameter(parameter, True)
                        imgui.tree_pop()
                imgui.tree_pop()
            if imgui.tree_node("Output"):
                for parameter_name in selected_node.outputs:
                    parameter = selected_node.outputs[parameter_name]
                    if imgui.tree_node(parameter.id):
                        display_parameter(parameter, False)
                        imgui.tree_pop()
                imgui.tree_pop()
        imgui.end_child()
        imgui.pop_style_var()

        # 
        imgui.end()

        # ==============================================================================
        # Debug Window
        # ==============================================================================
        if show_debug_window:
            debug_window_expanded, show_debug_window = imgui.begin("Debug", True)
            if parameter_link_start:
                imgui.text("parameter_link_start: " + parameter_link_start.id)
            else:
                imgui.text("parameter_link_start: " + "None")
            if selected_parameter:
                imgui.text("selected_parameter: " + selected_parameter.id)
            else:
                imgui.text("selected_parameter: " + "None")
            imgui.text("is_mouse_dragging: " + str(debug_is_mouse_dragging))
            imgui.text("mouse: (" + str(io.mouse_pos.x) + ", " + str(io.mouse_pos.y) + ")")
            imgui.end()

        gl.glClearColor(1., 1., 1., 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #20
0
class App:
    def __init__(self, width=640, height=480, title="Hello world"):
        imgui.create_context()

        if not glfw.init():
            return

        self.window = glfw.create_window(width, height, title, None, None)
        if not self.window:
            glfw.terminate()
            return

        glfw.make_context_current(self.window)
        self.ctx = moderngl.create_context(require=460)

        self.impl = ImguiRenderer(self.window, attach_callbacks=False)

        glfw.set_key_callback(self.window, self._on_key)
        glfw.set_cursor_pos_callback(self.window, self._on_mouse_move)
        glfw.set_mouse_button_callback(self.window, self._on_mouse_button)
        glfw.set_window_size_callback(self.window, self._on_resize)
        glfw.set_char_callback(self.window, self._on_char)
        glfw.set_scroll_callback(self.window, self._on_scroll)

        self.init()

    def main_loop(self):
        previous_time = glfw.get_time()

        # Loop until the user closes the window
        while not glfw.window_should_close(self.window):
            glfw.poll_events()
            self.impl.process_inputs()

            current_time = glfw.get_time()
            delta_time = current_time - previous_time
            previous_time = current_time
            self.update(current_time, delta_time)
            self.render()

            imgui.new_frame()
            self.ui()
            imgui.render()
            self.impl.render(imgui.get_draw_data())

            glfw.swap_buffers(self.window)

        self.impl.shutdown()
        glfw.terminate()

    def should_close(self):
        glfw.set_window_should_close(self.window, True)

    def mouse_pos(self):
        return glfw.get_cursor_pos(self.window)

    def size(self):
        return glfw.get_window_size(self.window)

    def init(self):
        pass

    def update(self, time):
        pass

    def render(self):
        pass

    def ui(self):
        pass

    def _on_key(self, window, key, scancode, action, mods):
        self.impl.keyboard_callback(window, key, scancode, action, mods)
        self.on_key(key, scancode, action, mods)

    def on_key(self, key, scancode, action, mods):
        pass

    def _on_char(self, window, codepoint):
        self.impl.char_callback(window, codepoint)
        self.on_char(codepoint)

    def on_char(self, codepoint):
        pass

    def _on_mouse_move(self, window, x, y):
        self.impl.mouse_callback(window, x, y)
        self.on_mouse_move(x, y)

    def on_mouse_move(self, x, y):
        pass

    def _on_mouse_button(self, window, button, action, mods):
        if not imgui.get_io().want_capture_mouse:
            self.on_mouse_button(button, action, mods)

    def on_mouse_button(self, button, action, mods):
        pass

    def _on_scroll(self, window, xoffset, yoffset):
        self.impl.scroll_callback(window, xoffset, yoffset)
        self.on_scroll(xoffset, yoffset)

    def on_scroll(self, xoffset, yoffset):
        pass

    def _on_resize(self, window, width, height):
        self.impl.resize_callback(window, width, height)
        self.on_resize(width, height)

    def on_resize(self, width, height):
        pass
        glClear(GL_COLOR_BUFFER_BIT)

        # imgui function
        impl.process_inputs()

        locationX, locationY, angle, color = \
            transformGuiOverlay(locationX, locationY, angle, color)

        # Setting uniforms and drawing the Quad
        glUniformMatrix4fv(
            glGetUniformLocation(pipeline.shaderProgram, "transform"), 1,
            GL_TRUE,
            np.matmul(tr.translate(locationX, locationY, 0.0),
                      tr.rotationZ(angle)))
        glUniform3f(
            glGetUniformLocation(pipeline.shaderProgram, "modulationColor"),
            color[0], color[1], color[2])
        pipeline.drawCall(gpuQuad)

        # Drawing the imgui texture over our drawing
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        impl.render(imgui.get_draw_data())

        # Once the render is done, buffers are swapped, showing only the complete scene.
        glfw.swap_buffers(window)

    # freeing GPU memory
    gpuQuad.clear()

    impl.shutdown()
    glfw.terminate()
Example #22
0
def main():
    width, height = Settings.WindowWidth, Settings.WindowHeight

    # initialize glfw
    if not glfw.init():
        return

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.SAMPLES, 4)
    glfw.window_hint(glfw.DEPTH_BITS, 32)

    window = glfw.create_window(width, height, "OpenGL Window", None, None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)
    glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
    # glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)

    # imgui stuff
    imgui.create_context()
    impl = GlfwRenderer(window)

    # Check OpenGL version
    print(glGetString(GL_VERSION))

    glfw.swap_interval(0)

    load = GLTFLoader("resources/gltf/trailer/scene.gltf")

    scene = load.get_scene()

    glEnable(GL_DEPTH_TEST)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glEnable(GL_MULTISAMPLE)

    Global.ProjMat = glm.perspective(glm.radians(35), width / height, 1.0,
                                     100000.0)

    scene.setup_scene()

    grid = GUI.Grid(10, 50)

    renderer = Renderer()

    translation = glm.vec3(0.0, 0.0, 0.0)

    grid = GUI.Grid(10, 50)
    rotation = 0.0
    increment = 2.0

    view = glm.mat4(1.0)

    # Camera
    cam = GUI.Flycam(glm.vec3(0.0, 150.0, 1000))

    # Time & FPS counter
    tt = GUI.TimeTracker()

    # Input poll
    inpt = GUI.Input()

    # glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)
    glfw.set_mouse_button_callback(window, mouse_button_callback)
    glfw.set_drop_callback(window, drop_callback)

    scene.nodelist[0].transformation = glm.rotate(
        scene.nodelist[0].transformation, glm.radians(90), vec3(1.0, 0.0, 0.0))

    scene.nodelist[0].transformation = glm.rotate(
        scene.nodelist[0].transformation, glm.radians(180),
        vec3(0.0, 1.0, 0.0))

    while not glfw.window_should_close(window):
        # Time & FPS counter
        tt.update()

        # Input
        process_input(window)
        inpt.poll_mouse_pos(window)
        glfw.poll_events()

        # Camera
        cam.update(window)

        renderer.clear()
        glClear(GL_DEPTH_BUFFER_BIT)

        # --------Imgui----------
        impl.process_inputs()
        imgui.new_frame()
        GUI.draw_imgui(scene)

        imgui.show_demo_window()

        # Camera Stuff
        Global.ViewMat = cam.get_view()

        grid.draw_grid(Global.ProjMat, Global.ViewMat)

        scene.draw_scene()

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()
Example #23
0
def run_reloadable_imgui_app(app_module, no_reload=()):

    no_reload = no_reload + (__name__, )
    settings = getattr(app_module, '__settings__', {})

    window_title = settings.get('window_title', DEFAULT_WINDOW_TITLE)
    initial_window_size = settings.get('initital_window_size',
                                       DEFAULT_WINDOW_SIZE)
    target_framerate = settings.get('target_framerate',
                                    DEFAULT_TARGET_FRAMERATE)
    num_active_frames_after_input = 2 * target_framerate  # arbitrary

    window = impl_glfw_init(window_title=window_title,
                            window_size=initial_window_size)
    renderer = GlfwRenderer(window)
    io = imgui.get_io()

    max_frame_dur = 1 / target_framerate

    frame_start = 0.
    frame_end = 0.
    frame_dur = 0.
    wait_dur = 0.

    prev_mouse_pos = (0, 0)
    mouse_pos = (0, 0)
    prev_mouse_down_0 = False
    prev_frame_click_0_finished = False

    frames_since_last_input = 0

    # TODO:
    def got_input() -> bool:
        """
		Checks if the user sent any left-mouse mouse inputs, like moving/clicking the mouse
		"""
        nonlocal mouse_pos
        nonlocal prev_mouse_pos
        nonlocal prev_mouse_down_0
        nonlocal prev_frame_click_0_finished

        mouse_pos = io.mouse_pos
        mouse_moved = (mouse_pos != prev_mouse_pos)
        mouse_changed = io.mouse_down[0] != prev_mouse_down_0
        click_0_finished = prev_mouse_down_0 and (
            not io.mouse_down[0])  # mouse was down previous frame, now it's up
        # key_clicked = any(io.keys_down)
        result = mouse_moved or mouse_changed or io.mouse_down[
            0] or prev_frame_click_0_finished  # or key_clicked

        prev_mouse_pos = mouse_pos
        prev_mouse_down_0 = io.mouse_down[0]
        prev_frame_click_0_finished = click_0_finished
        return result

    m_request = None
    setattr(app_module, '__was_reloaded__', False)
    # reload loop
    while True:
        # This isn't the render loop yet.
        # This one is to enable reloading,
        # in which case we need to run init
        # and start the render loop again.

        # ========================
        if hasattr(app_module, '__app_init__'):
            app_module.__app_init__()
        # ========================

        # render loop
        while True:

            frame_start = glfw.get_time()  # seconds
            # debug_log("frame_start_ms", frame_start*1000) # miliseconds

            glfw.poll_events()
            renderer.process_inputs()

            if got_input():
                frames_since_last_input = 0
            else:
                frames_since_last_input += 1

            if frames_since_last_input <= num_active_frames_after_input:

                imgui.new_frame()

                # =========================
                if hasattr(
                        app_module, '__pre_frame__'
                ):  # TODO: should this run before imgui.new_frame(?)
                    app_module.__pre_frame__()

                app_module.__draw__()

                if hasattr(
                        app_module, '__post_frame__'
                ):  # TODO: should this run before imgui.end_frame(?)
                    m_request = app_module.__post_frame__()
                    # the app's request is processed at the end of the loop
                # =========================

                gl.glClearColor(1., 1., 1., 1)
                gl.glClear(gl.GL_COLOR_BUFFER_BIT)

                imgui.render()
                renderer.render(imgui.get_draw_data())
                glfw.swap_buffers(window)

            frame_end = glfw.get_time()  # seconds
            frame_dur = frame_end - frame_start
            wait_dur = max_frame_dur - frame_dur
            if wait_dur > 0:
                sleep(wait_dur)

            if m_request in ('reload',
                             'shutdown') or glfw.window_should_close(window):
                break  # stop the render loop

        # end render loop

        # if control flow got to this point, it means that
        # the render loop finished. possible reasons:
        #	- the app requested a reload
        #	- the app requested a shutdown
        #	- glfw.window_should_close(window) is true

        if glfw.window_should_close(window) or m_request == 'shutdown':
            break  # end the reload loop, run user shutdown, close
        elif m_request == 'reload':
            rlu.recursive_reload(app_module,
                                 dir=rlu.module_dirpath(app_module),
                                 excluded=no_reload)
            setattr(app_module, '__was_reloaded__', True)
            continue

    # end reload loop

    # =========================
    if hasattr(app_module, '__app_shutdown__'):
        app_module.__app_shutdown__()
    # =========================

    renderer.shutdown()
    glfw.terminate()
Example #24
0
class ImGuiPlayer:

    # ---------------------------------------------------------------------------
    # --------------------------- class variables -------------------------------
    # ---------------------------------------------------------------------------

    window = None
    impl: GlfwRenderer = None

    app: ImGuiPlayerApp = None

    quit_requested: bool = None

    def __init__(self, app: ImGuiPlayerApp):

        self.app = app

        #initialize window and GUI
        imgui.create_context()

        self.window = self.impl_glfw_init()
        self.impl = GlfwRenderer(self.window)

        self.quit_requested = False

    def impl_glfw_init(self):
        width, height = 1200, 1200
        window_name = "RefocusApp"

        if not glfw.init():
            print("Could not initialize OpenGL context")
            exit(1)

        glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
        glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)

        # the OPENGL_COMPAT_PROFILE enables the mix between pyimgui and glumpy
        glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_COMPAT_PROFILE)
        glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

        window = glfw.create_window(int(width), int(height), window_name, None,
                                    None)
        glfw.make_context_current(window)

        if not window:
            glfw.terminate()
            print("Could not initialize Window")
            sys.exit(1)

        self.app.window_size_callback(window, 1200, 1200)

        return window

    # ---------------------------------------------------------------------------
    # --------------------------- class methods - gui and menu   ----------------
    # ---------------------------------------------------------------------------

    def key_callback(self, window, key, scancode, action, mods):

        io = imgui.get_io()
        self.impl.keyboard_callback(window, key, scancode, action, mods)

        if io.want_capture_keyboard:
            print("imgui handles")
        else:
            #print("processed by app: key pressed")

            if key == glfw.KEY_ESCAPE and action == glfw.RELEASE:
                self.quit_requested = True

            if key == glfw.KEY_Q and np.bitwise_and(mods, glfw.MOD_CONTROL):
                self.quit_requested = True

            self.app.key_callback(window, key, scancode, action, mods)

    def mouse_cursor_pos_callback(self, window, xpos, ypos):
        io = imgui.get_io()

        if not io.want_capture_mouse:
            self.app.mouse_cursor_pos_callback(window, xpos, ypos)

    def mouse_button_callback(self, window, button, action, mods):
        io = imgui.get_io()

        if io.want_capture_mouse:
            print("imgui handles")
        else:
            self.app.mouse_button_callback(window, button, action, mods)

    def mouse_scroll_callback(self, window, xoffset, yoffset):
        io = imgui.get_io()

        if io.want_capture_mouse:
            print("imgui handles")
        else:
            #print( "processed by app: scroll: {},{}".format( xoffset, yoffset ));

            self.app.mouse_scroll_callback(window, xoffset, yoffset)

    def window_size_callback(self, window, width, height):
        self.app.window_size_callback(window, width, height)

    def run(self):

        glfw.set_key_callback(self.window, self.key_callback)
        glfw.set_cursor_pos_callback(self.window,
                                     self.mouse_cursor_pos_callback)
        glfw.set_mouse_button_callback(self.window, self.mouse_button_callback)
        glfw.set_scroll_callback(self.window, self.mouse_scroll_callback)
        glfw.set_window_size_callback(self.window, self.window_size_callback)

        while not glfw.window_should_close(
                self.window) and not self.quit_requested:
            self.app.on_draw()

            glfw.poll_events()
            self.impl.process_inputs()

            self.app.menu()

            gl.glClearColor(0., 0., 0.2, 1)
            gl.glClear(gl.GL_COLOR_BUFFER_BIT)

            self.app.on_draw()

            imgui.render()
            self.impl.render(imgui.get_draw_data())
            glfw.swap_buffers(self.window)

        self.impl.shutdown()
        glfw.terminate()
def main():

    useLiveCamera = True

    #gc.disable()

    # # transform to convert the image to tensor
    # transform = transforms.Compose([
    #     transforms.ToTensor()
    # ])
    # # initialize the model
    # model = torchvision.models.detection.keypointrcnn_resnet50_fpn(pretrained=True,
    #                                                             num_keypoints=17)
    # # set the computation device
    # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    # # load the modle on to the computation device and set to eval mode
    # model.to(device).eval()

    # initialize glfw
    if not glfw.init():
        return
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    #creating the window
    window = glfw.create_window(1600, 900, "PyGLFusion", None, None)
    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    imgui.create_context()
    impl = GlfwRenderer(window)

    # rendering
    glClearColor(0.2, 0.3, 0.2, 1.0)

    #           positions        texture coords
    quad = [
        -1.0, -1.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0,
        1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0
    ]

    quad = np.array(quad, dtype=np.float32)

    indices = [0, 1, 2, 2, 3, 0]

    indices = np.array(indices, dtype=np.uint32)

    screenVertex_shader = (Path(__file__).parent /
                           'shaders/ScreenQuad.vert').read_text()

    screenFragment_shader = (Path(__file__).parent /
                             'shaders/ScreenQuad.frag').read_text()

    renderShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(screenVertex_shader, GL_VERTEX_SHADER),
        OpenGL.GL.shaders.compileShader(screenFragment_shader,
                                        GL_FRAGMENT_SHADER))

    # set up VAO and VBO for full screen quad drawing calls
    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, 80, quad, GL_STATIC_DRAW)

    EBO = glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24, indices, GL_STATIC_DRAW)

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
    glEnableVertexAttribArray(0)
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
    glEnableVertexAttribArray(1)

    # shaders

    bilateralFilter_shader = (Path(__file__).parent /
                              'shaders/bilateralFilter.comp').read_text()
    bilateralFilterShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(bilateralFilter_shader,
                                        GL_COMPUTE_SHADER))

    alignDepthColor_shader = (Path(__file__).parent /
                              'shaders/alignDepthColor.comp').read_text()
    alignDepthColorShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(alignDepthColor_shader,
                                        GL_COMPUTE_SHADER))

    depthToVertex_shader = (Path(__file__).parent /
                            'shaders/depthToVertex.comp').read_text()
    depthToVertexShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(depthToVertex_shader,
                                        GL_COMPUTE_SHADER))

    vertexToNormal_shader = (Path(__file__).parent /
                             'shaders/vertexToNormal.comp').read_text()
    vertexToNormalShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(vertexToNormal_shader,
                                        GL_COMPUTE_SHADER))

    raycast_shader = (Path(__file__).parent /
                      'shaders/raycast.comp').read_text()
    raycastShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(raycast_shader, GL_COMPUTE_SHADER))

    integrate_shader = (Path(__file__).parent /
                        'shaders/integrate.comp').read_text()
    integrateShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(integrate_shader, GL_COMPUTE_SHADER))

    trackP2P_shader = (Path(__file__).parent /
                       'shaders/p2pTrack.comp').read_text()
    trackP2PShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(trackP2P_shader, GL_COMPUTE_SHADER))

    reduceP2P_shader = (Path(__file__).parent /
                        'shaders/p2pReduce.comp').read_text()
    reduceP2PShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(reduceP2P_shader, GL_COMPUTE_SHADER))

    trackP2V_shader = (Path(__file__).parent /
                       'shaders/p2vTrack.comp').read_text()
    trackP2VShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(trackP2V_shader, GL_COMPUTE_SHADER))

    reduceP2V_shader = (Path(__file__).parent /
                        'shaders/p2vReduce.comp').read_text()
    reduceP2VShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(reduceP2V_shader, GL_COMPUTE_SHADER))

    LDLT_shader = (Path(__file__).parent / 'shaders/LDLT.comp').read_text()
    LDLTShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(LDLT_shader, GL_COMPUTE_SHADER))

    # Splatter
    globalMapUpdate_shader = (Path(__file__).parent /
                              'shaders/GlobalMapUpdate.comp').read_text()
    globalMapUpdateShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(globalMapUpdate_shader,
                                        GL_COMPUTE_SHADER))

    indexMapGenVert_shader = (Path(__file__).parent /
                              'shaders/IndexMapGeneration.vert').read_text()

    indexMapGenFrag_shader = (Path(__file__).parent /
                              'shaders/IndexMapGeneration.frag').read_text()

    IndexMapGenerationShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(indexMapGenVert_shader,
                                        GL_VERTEX_SHADER),
        OpenGL.GL.shaders.compileShader(indexMapGenFrag_shader,
                                        GL_FRAGMENT_SHADER))

    SurfaceSplattingVert_shader = (
        Path(__file__).parent / 'shaders/SurfaceSplatting.vert').read_text()

    SurfaceSplattingFrag_shader = (
        Path(__file__).parent / 'shaders/SurfaceSplatting.frag').read_text()

    SurfaceSplattingShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(SurfaceSplattingVert_shader,
                                        GL_VERTEX_SHADER),
        OpenGL.GL.shaders.compileShader(SurfaceSplattingFrag_shader,
                                        GL_FRAGMENT_SHADER))

    UnnecessaryPointRemoval_shader = (
        Path(__file__).parent /
        'shaders/UnnecessaryPointRemoval.comp').read_text()
    UnnecessaryPointRemovalShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(UnnecessaryPointRemoval_shader,
                                        GL_COMPUTE_SHADER))

    # P2V
    expm_shader = (Path(__file__).parent / 'shaders/expm.comp').read_text()
    expmShader = OpenGL.GL.shaders.compileProgram(
        OpenGL.GL.shaders.compileShader(expm_shader, GL_COMPUTE_SHADER))

    d2c, c2d, K, invK, colK = camera.start(useLiveCamera)

    shaderDict = {
        'renderShader': renderShader,
        'bilateralFilterShader': bilateralFilterShader,
        'alignDepthColorShader': alignDepthColorShader,
        'depthToVertexShader': depthToVertexShader,
        'vertexToNormalShader': vertexToNormalShader,
        'raycastVolumeShader': raycastShader,
        'integrateVolumeShader': integrateShader,
        'trackP2PShader': trackP2PShader,
        'reduceP2PShader': reduceP2PShader,
        'trackP2VShader': trackP2VShader,
        'reduceP2VShader': reduceP2VShader,
        'LDLTShader': LDLTShader,
        'globalMapUpdate': globalMapUpdateShader,
        'indexMapGeneration': IndexMapGenerationShader,
        'surfaceSplatting': SurfaceSplattingShader,
        'unnecessaryPointRemoval': UnnecessaryPointRemovalShader,
        'expm': expmShader
    }

    bufferDict = {
        'p2pReduction': -1,
        'p2pRedOut': -1,
        'p2vReduction': -1,
        'p2vRedOut': -1,
        'test': -1,
        'outBuf': -1,
        'poseBuffer': -1,
        'globalMap0': -1,
        'globalMap1': -1,
        'atomic0': -1,
        'atomic1': -1
    }

    textureDict = {
        'rawColor': -1,
        'lastColor': -1,
        'nextColor': -1,
        'rawDepth': -1,
        'filteredDepth': -1,
        'lastDepth': -1,
        'nextDepth': -1,
        'refVertex': -1,
        'refNormal': -1,
        'virtualVertex': -1,
        'virtualNormal': -1,
        'virtualDepth': -1,
        'virtualColor': -1,
        'mappingC2D': -1,
        'mappingD2C': -1,
        'xyLUT': -1,
        'tracking': -1,
        'volume': -1,
        'indexMap': -1
    }

    fboDict = {'indexMap': -1, 'virtualFrame': -1}
    #        'iters' : (2, 5, 10),

    fusionConfig = {
        'volSize': (128, 128, 128),
        'volDim': (1.0, 1.0, 1.0),
        'iters': (2, 2, 2),
        'initOffset': (0, 0, 0),
        'maxWeight': 100.0,
        'distThresh': 0.05,
        'normThresh': 0.9,
        'nearPlane': 0.1,
        'farPlane': 4.0,
        'maxMapSize': 5000000,
        'c_stable': 10.0,
        'sigma': 0.6
    }

    cameraConfig = {
        'depthWidth': 640,
        'depthHeight': 576,
        'colorWidth': 1920,
        'colorHeight': 1080,
        'd2c': d2c,
        'c2d': c2d,
        'depthScale': 0.001,
        'K': K,
        'invK': invK,
        'colK': colK
    }

    textureDict = frame.generateTextures(textureDict, cameraConfig,
                                         fusionConfig)
    bufferDict = frame.generateBuffers(bufferDict, cameraConfig, fusionConfig)

    colorMat = np.zeros(
        (cameraConfig['colorHeight'], cameraConfig['colorWidth'], 3),
        dtype="uint8")
    useColorMat = False
    integrateFlag = True
    resetFlag = True
    initPose = glm.mat4()
    initPose[3, 0] = fusionConfig['volDim'][0] / 2.0
    initPose[3, 1] = fusionConfig['volDim'][1] / 2.0
    initPose[3, 2] = 0

    blankResult = np.array([0, 0, 0, 0, 0, 0], dtype='float32')

    glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferDict['poseBuffer'])
    glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 16 * 4,
                    glm.value_ptr(initPose))
    glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4, 16 * 4,
                    glm.value_ptr(glm.inverse(initPose)))
    glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 2, 16 * 4,
                    glm.value_ptr(glm.mat4(1.0)))
    glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 3, 16 * 4,
                    glm.value_ptr(glm.mat4(1.0)))
    glBufferSubData(GL_SHADER_STORAGE_BUFFER, 16 * 4 * 4, 6 * 4, blankResult)
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0)

    mouseX, mouseY = 0, 0
    clickedPoint3D = glm.vec4(fusionConfig['volDim'][0] / 2.0,
                              fusionConfig['volDim'][1] / 2.0, 0, 0)
    sliderDim = fusionConfig['volDim'][0]

    #[32 64 128 256 512]
    currentSize = math.log2(fusionConfig['volSize'][0]) - 5
    volumeStatsChanged = False

    currPose = initPose

    # splatter stuff
    frameCount = 0
    fboDict = frame.generateFrameBuffers(fboDict, textureDict, cameraConfig)

    initAtomicCount = np.array([0], dtype='uint32')
    mapSize = np.array([0], dtype='uint32')

    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, bufferDict['atomic0'])
    glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, 4, initAtomicCount)
    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0)

    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, bufferDict['atomic1'])
    glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, 4, initAtomicCount)
    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0)

    # aa = torch.tensor([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=torch.float32, device=torch.device('cuda'))
    # bb = torch.tensor([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=torch.float32, device=torch.device('cuda'))

    # #setup pycuda gl interop needs to be after openGL is init
    # import pycuda.gl.autoinit
    # import pycuda.gl
    # cuda_gl = pycuda.gl
    # cuda_driver = pycuda.driver
    # from pycuda.compiler import SourceModule
    # import pycuda

    # pycuda_source_ssbo = cuda_gl.RegisteredBuffer(int(bufferDict['test']), cuda_gl.graphics_map_flags.NONE)

    # sm = SourceModule("""
    #     __global__ void simpleCopy(float *inputArray, float *outputArray) {
    #             unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;

    #             outputArray[x] = inputArray[x];
    #             inputArray[x] = 8008.135f;
    #     }
    # """)

    # cuda_function = sm.get_function("simpleCopy")

    # mappingObj = pycuda_source_ssbo.map()
    # data, size = mappingObj.device_ptr_and_size()

    # cuda_function(np.intp(aa.data_ptr()), np.intp(data), block=(8, 1, 1))

    # mappingObj.unmap()

    # glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferDict['test'])
    # tee = glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 32)
    # glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0)
    # teeData = np.frombuffer(tee, dtype=np.float32)
    # print(teeData)

    # modTensor = aa.cpu().data.numpy()
    # print(modTensor)

    #fusionConfig['initOffset'] = (initPose[3,0], initPose[3,1], initPose[3,2])

    # LUTs
    #createXYLUT(k4a, textureDict, cameraConfig) <-- bug in this

    person.init()

    while not glfw.window_should_close(window):

        glfw.poll_events()
        impl.process_inputs()
        imgui.new_frame()

        sTime = time.perf_counter()

        try:
            capture = camera.getFrames(useLiveCamera)

            if capture.color is not None:
                #if useLiveCamera == False:
                #if k4a.configuration["color_format"] == ImageFormat.COLOR_MJPG:
                #    colorMat = cv2.imdecode(capture.color, cv2.IMREAD_COLOR)
                #    useColorMat = True
                glActiveTexture(GL_TEXTURE0)
                glBindTexture(GL_TEXTURE_2D, textureDict['rawColor'])
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
                                int(cameraConfig['colorWidth']),
                                int(cameraConfig['colorHeight']),
                                (GL_RGB, GL_RGBA)[useLiveCamera],
                                GL_UNSIGNED_BYTE,
                                (capture.color, colorMat)[useColorMat])

            if capture.depth is not None:
                glActiveTexture(GL_TEXTURE1)
                glBindTexture(GL_TEXTURE_2D, textureDict['rawDepth'])
                glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
                                int(cameraConfig['depthWidth']),
                                int(cameraConfig['depthHeight']), GL_RED,
                                GL_UNSIGNED_SHORT, capture.depth)

        except EOFError:
            break

        # #smallMat = cv2.pyrDown(colorMat)
        # start_time = time.time()
        # rotMat = cv2.flip(colorMat, 0)

        # pil_image = Image.fromarray(rotMat).convert('RGB')
        # image = transform(pil_image)

        # image = image.unsqueeze(0).to(device)
        # end_time = time.time()
        # print((end_time - start_time) * 1000.0)

        # with torch.no_grad():
        #     outputs = model(image)

        # output_image = utils.draw_keypoints(outputs, rotMat)
        # cv2.imshow('Face detection frame', output_image)
        # if cv2.waitKey(1) & 0xFF == ord('q'):
        #     break

        person.getPose(textureDict, cameraConfig, capture.color)

        frame.bilateralFilter(shaderDict, textureDict, cameraConfig)
        frame.depthToVertex(shaderDict, textureDict, cameraConfig,
                            fusionConfig)
        frame.alignDepthColor(shaderDict, textureDict, cameraConfig,
                              fusionConfig)
        frame.vertexToNormal(shaderDict, textureDict, cameraConfig)

        frame.mipmapTextures(textureDict)

        #currPose = track.runP2P(shaderDict, textureDict, bufferDict, cameraConfig, fusionConfig, currPose, integrateFlag, resetFlag)
        currPose = track.runP2V(shaderDict, textureDict, bufferDict,
                                cameraConfig, fusionConfig, currPose,
                                integrateFlag, resetFlag)

        #mapSize = track.runSplatter(shaderDict, textureDict, bufferDict, fboDict, cameraConfig, fusionConfig, mapSize, frameCount, integrateFlag, resetFlag)
        frameCount += 1

        if resetFlag == True:
            resetFlag = False
            integrateFlag = True

        imgui.begin("Menu", True)
        if imgui.button("Reset"):
            fusionConfig['volSize'] = (1 << (currentSize + 5), 1 <<
                                       (currentSize + 5), 1 <<
                                       (currentSize + 5))
            fusionConfig['volDim'] = (sliderDim, sliderDim, sliderDim)
            currPose, integrateFlag, resetFlag = track.reset(
                textureDict, bufferDict, cameraConfig, fusionConfig,
                clickedPoint3D)
            volumeStatsChanged = False

        if imgui.button("Integrate"):
            integrateFlag = not integrateFlag
        imgui.same_line()
        imgui.checkbox("", integrateFlag)

        changedDim, sliderDim = imgui.slider_float("dim",
                                                   sliderDim,
                                                   min_value=0.01,
                                                   max_value=5.0)

        clickedSize, currentSize = imgui.combo(
            "size", currentSize, ["32", "64", "128", "256", "512"])

        if imgui.is_mouse_clicked():
            if not imgui.is_any_item_active():
                mouseX, mouseY = imgui.get_mouse_pos()
                w, h = glfw.get_framebuffer_size(window)
                xPos = ((mouseX % int(w / 3)) / (w / 3) *
                        cameraConfig['depthWidth'])
                yPos = (mouseY / (h)) * cameraConfig['depthHeight']
                clickedDepth = capture.depth[
                    int(yPos + 0.5),
                    int(xPos + 0.5)] * cameraConfig['depthScale']
                clickedPoint3D = clickedDepth * (
                    cameraConfig['invK'] * glm.vec4(xPos, yPos, 1.0, 0.0))
                volumeStatsChanged = True

        if changedDim or clickedSize:
            volumeStatsChanged = True

        imgui.end()

        graphics.render(VAO, window, shaderDict, textureDict)

        imgui.render()

        impl.render(imgui.get_draw_data())

        eTime = time.perf_counter()

        #print((eTime-sTime) * 1000, mapSize[0])

        glfw.swap_buffers(window)

    glfw.terminate()
    if useLiveCamera == True:
        camera.stop()
Example #26
0
class Application:
    class UniformHolder:
        def __init__(self, program: Program):
            self.resolution = program.get_uniform("uResolution")
            self.mousePos = program.get_uniform("uMousePos")
            self.zoom = program.get_uniform("uZoom")

    def _draw(self):
        glDrawArrays(GL_QUADS, 0, 4)

    def _on_resize(self, window, w, h):
        self.width = w
        self.height = h

        self.uniHolder.resolution.set_update(ivec2(w, h))

        glViewport(0, 0, w, h)

    def _on_mouse_move(self, window, x, y):
        mpos = vec2(x, self.height - y)
        diff = mpos - self.prev_mpos
        self.prev_mpos = mpos

        if glfw.get_mouse_button(
                window, glfw.MOUSE_BUTTON_LEFT
        ) == glfw.PRESS and not imgui.get_io().want_capture_mouse:
            self.mvel += 3.0 * diff / self.uniHolder.zoom.value

    def _on_scroll(self, window, x, y):
        self.zm_vel = y * 3.0

    def _init_window(self, width, height, fullscreen):
        if not glfw.init():
            raise Exception("glfw can not be initialized!")

        monitor = None
        if fullscreen:
            monitor = glfw.get_primary_monitor()

        self.window = glfw.create_window(width, height, "Toy", monitor, None)

        if not self.window:
            raise Exception("glfw window can not be created!")

        glfw.set_window_pos(self.window, 50, 50)
        glfw.make_context_current(self.window)
        glfw.swap_interval(1)

        glfw.set_window_size_callback(self.window, self._on_resize)
        glfw.set_cursor_pos_callback(self.window, self._on_mouse_move)
        glfw.set_scroll_callback(self.window, self._on_scroll)

        imgui.create_context()
        self.impl = GlfwRenderer(self.window, False)

    def __init__(self, fs_path: str, width, height, fullscreen=False):
        self._init_window(width, height, fullscreen)

        self.program = Program.from_files("res/base.vs.glsl", fs_path)

        self.prev_mpos = vec2(0, 0)
        self.u_mpos = vec2(0, 0)

        self.prev_time = glfw.get_time()
        self.mvel = vec2(0, 0)

        self.zoom = 0.0
        self.zm_vel = 0.0

        self.uniHolder = Application.UniformHolder(self.program)

        self._on_resize(self.window, width, height)

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.program.delete()
        glfw.terminate()

    def __enter__(self):
        glClearColor(0, 1, 0, 0)

        while not glfw.window_should_close(self.window):
            cur_time = glfw.get_time()
            elapsed = cur_time - self.prev_time
            self.prev_time = cur_time

            glfw.poll_events()
            self.impl.process_inputs()

            self._process(elapsed)

            imgui.new_frame()

            imgui.set_next_window_position(0, 0)
            imgui.set_next_window_size(280, 180)

            imgui.begin("Uniforms", False, imgui.WINDOW_NO_RESIZE)

            imgui.push_item_width(-45)

            for cont in self.program.controls:
                cont.update()

            imgui.end()

            glClear(GL_COLOR_BUFFER_BIT)
            self._draw()

            imgui.render()
            self.impl.render(imgui.get_draw_data())

            glfw.swap_buffers(self.window)

    def _process(self, delta):
        self.u_mpos += self.mvel * delta
        self.uniHolder.mousePos.set_update(self.u_mpos)

        self.uniHolder.zoom.value += self.zm_vel * delta
        self.uniHolder.zoom.update()

        x = pow(0.002, delta)
        self.mvel *= x
        self.zm_vel *= x
Example #27
0
def render_snippet(
    source,
    file_path,
    title="",
    width=200,
    height=200,
    auto_layout=False,
    output_dir='.',
    click=None,
):
    _patch_imgui()
    source = find_fonts(source)

    init_source, frame_source = split_sources(source)

    init_code = compile(init_source, '<str>', 'exec')
    frame_code = compile(frame_source, '<str>', 'exec')
    window_name = "minimal ImGui/GLFW3 example"

    if not glfw.init():
        print("Could not initialize OpenGL context")
        exit(1)

    # OS X supports only forward-compatible core profiles from 3.2
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)
    # note: creating context without window is tricky so made window invisible
    glfw.window_hint(glfw.VISIBLE, False)

    window = glfw.create_window(int(width), int(height), window_name, None,
                                None)

    glfw.make_context_current(window)

    if not window:
        glfw.terminate()
        print("Could not initialize Window")
        exit(1)

    impl = GlfwRenderer(window)

    exec_ns = _ns(globals(), locals())

    if init_source:
        exec(init_code, exec_ns)

    glfw.poll_events()

    # render target for framebuffer
    texture = gl.glGenTextures(1)
    gl.glBindTexture(gl.GL_TEXTURE_2D, texture)
    gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, width, height, 0,
                    gl.GL_RGB, gl.GL_UNSIGNED_BYTE, None)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER,
                       gl.GL_NEAREST)
    gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER,
                       gl.GL_NEAREST)

    # create new framebuffer
    offscreen_fb = gl.glGenFramebuffers(1)
    gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, offscreen_fb)
    # attach texture to framebuffer
    gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0,
                              gl.GL_TEXTURE_2D, texture, 0)

    # note: Clicking simulation is hacky as f**k and it
    #       requires at least three frames to be rendered:
    #       * 1st with mouse in position but without button pressed.
    #       * 2nd in roughly same posiotion of mouse to turn on hover
    #         mouse button starts to press but still does not trigger click.
    #       * 3rd in the same position with button pressed still to finally
    #         trigger the "clicked" state.
    # note: If clicking simulation is not required we draw only one frame.
    for m_state in ([None, None, None] if not click else [False, True, True]):

        # note: Mouse click MUST be simulated before new_frame call!
        if click:
            impl.io.mouse_draw_cursor = True
            simulate_click(click[0], click[1], m_state)
        else:
            # just make sure mouse state is clear
            _clear_mouse()

        impl.process_inputs()
        imgui.new_frame()

        with imgui.styled(imgui.STYLE_ALPHA, 1):
            imgui.core.set_next_window_size(0, 0)

            if auto_layout:
                imgui.set_next_window_size(width - 10, height - 10)
                imgui.set_next_window_position(impl.io.display_size.x * 0.5,
                                               impl.io.display_size.y * 0.5,
                                               pivot_x=0.5,
                                               pivot_y=0.5)
            exec(frame_code, exec_ns)

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, offscreen_fb)

        gl.glClearColor(1, 1, 1, 0)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())

    # retrieve pixels from framebuffer and write to file
    pixels = gl.glReadPixels(0, 0, width, height, gl.GL_RGBA,
                             gl.GL_UNSIGNED_BYTE)

    image = Image.frombytes('RGBA', (width, height), pixels)
    # note: glReadPixels returns lines "bottom to top" but PIL reads bytes
    #       top to bottom
    image = image.transpose(Image.FLIP_TOP_BOTTOM)
    image.save(os.path.join(output_dir, file_path))

    glfw.terminate()
Example #28
0
class Application(object):
    def __init__(self):
        self.camera = TrackballCamera(radius=5.0)

    def init(self, viewer):
        self.viewer = viewer

        window = Window(width=1200, height=675, name='contact modes')
        window.set_on_init(self.init_win)
        window.set_on_draw(self.render)
        window.set_on_key_press(self.on_key_press)
        window.set_on_key_release(self.on_key_release)
        window.set_on_mouse_press(self.on_mouse_press)
        window.set_on_mouse_drag(self.on_mouse_drag)
        window.set_on_resize(self.on_resize)

        viewer.add_window(window)

        self.window = window
        self.imgui_impl = GlfwRenderer(window.window, False)

    def init_win(self):
        glEnable(GL_LIGHTING)
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_LIGHT0)
        # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)

        # Enable anti-aliasing and circular points.
        glEnable(GL_LINE_SMOOTH)
        glEnable(GL_POINT_SMOOTH)
        glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
        glHint(GL_POINT_SMOOTH_HINT, GL_NICEST)

        #
        self.grid = Grid(0.25, 5)
        self.grid.get_tf_world().set_translation(np.array([0, 0, -0.5]))

    def render(self):
        # Clear frame.
        glClearColor(0.2, 0.3, 0.3, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        # Draw.
        # self.draw_grid()

        # Menu.
        self.imgui_impl.process_inputs()
        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):
                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True)

                if clicked_quit:
                    exit(1)

                imgui.end_menu()
            imgui.end_main_menu_bar()

        imgui.render()
        self.imgui_impl.render(imgui.get_draw_data())

    def draw_grid(self, shader):
        self.grid.draw(shader)

    def on_resize(self, width, height):
        glViewport(0, 0, width, height)

    def on_key_press(self, win, key, scancode, action, mods):
        pass

    def on_key_release(self, win, key, scancode, action, mods):
        pass

    def on_mouse_press(self, x, y, button, modifiers):
        if imgui.get_io().want_capture_mouse:
            return
        x = 2.0 * (x / self.window.width) - 1.0
        y = 2.0 * (y / self.window.height) - 1.0
        if button == 1:  # left click
            self.camera.mouse_roll(x, y, False)
        if button == 4:  # right click
            self.camera.mouse_zoom(x, y, False)

    def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
        if imgui.get_io().want_capture_mouse:
            return
        x = 2.0 * (x / self.window.width) - 1.0
        y = 2.0 * (y / self.window.height) - 1.0
        if button == 1:  # left click
            self.camera.mouse_roll(x, y)
        if button == 4:  # right click
            self.camera.mouse_zoom(x, y)
Example #29
0
class MainWindow():
    def __init__(self, gui=True, pipe=None, img=None):
        self.use_imgui = gui
        self.use_occt = not gui
        self.please_stop = False
        self.rqq = rqQueue()
        self.texture_updated = False
        self.prev_pos = [0, 0]
        self.offscreen_view_size = [0, 0]

        if self.use_imgui:
            print("Creating IMGUI context")
            imgui.create_context()
        else:
            print("IMGUI disabled")
        # Create a windowed mode window and its OpenGL context

        if self.use_imgui:
            self.canva = glfwViewer3d()
            self.canva.set_pipe(pipe)
            self.canva.set_img(img)
            self.canva.init_driver()
            self.impl = GlfwRenderer(self.canva.window)
        else:
            self.canva = offscreenViewer3d()
            self.canva.set_pipe(pipe)
            self.canva.set_img(img)

    def parse_reply(self):
        rp = self.canva.pipe.recv()
        jdata = json.loads(rp)
        if jdata['rp'] == replies[RP_IMAGE_DATA]:
            if (self.offscreen_view_size[0] == self.canva.view_size[0]) and (
                    self.offscreen_view_size[1] == self.canva.view_size[1]):
                self.canva.set_image(self.canva.view_size[0],
                                     self.canva.view_size[1],
                                     data=self.canva.img.buf)
            else:
                self.canva.set_image_black()
            self.canva.reply_received()
        elif jdata['rp'] == replies[RP_ACK]:
            if (self.offscreen_view_size[0] == self.canva.view_size[0]) and (
                    self.offscreen_view_size[1] == self.canva.view_size[1]):
                self.canva.set_image(self.canva.view_size[0],
                                     self.canva.view_size[1],
                                     data=self.canva.img.buf)
            else:
                self.canva.set_image_black()
            self.canva.reply_received()
        elif jdata['rp'] == replies[RP_ACK_SET_SIZE]:
            self.offscreen_view_size = [jdata['args'][0], jdata['args'][1]]
            if (self.offscreen_view_size[0] == self.canva.view_size[0]) and (
                    self.offscreen_view_size[1] == self.canva.view_size[1]):
                self.canva.set_image(self.canva.view_size[0],
                                     self.canva.view_size[1],
                                     data=self.canva.img.buf)
            else:
                self.canva.set_image_black()
            self.canva.reply_received()

    def mainloop(self):
        if self.use_imgui:
            self.canva.init_shader()
            self.canva.create_objects()
            self.rqq.rq_set_size(self.canva.view_size[0],
                                 self.canva.view_size[1])
            print("Waiting set size reply")
            self.rqq.process(self.canva)
            self.parse_reply()
            self.rqq.rq_load_image()
            while (not self.canva.should_close() and (not self.please_stop)):
                self.canva.proc()
                if self.canva.get_need_resize():
                    self.canva.set_image_black()
                    self.rqq.rq_set_size(self.canva.view_size[0],
                                         self.canva.view_size[1])
                    self.canva.start_frame()
                    self.canva.swap_buffers()
                    self.canva.poll_events()
                    continue
                self.rqq.process(self.canva)
                if self.canva.pipe.poll() == True:
                    self.parse_reply()
                self.impl.process_inputs()
                imgui.new_frame()

                if imgui.begin_main_menu_bar():
                    if imgui.begin_menu("File", True):
                        clicked_quit, selected_quit = imgui.menu_item(
                            "Quit", 'Ctrl+Q', False, True)
                        if clicked_quit:
                            self.please_stop = True
                        imgui.end_menu()
                    if imgui.begin_menu("Render", True):
                        imgui.menu_item("Save", None, False, True)
                        imgui.end_menu()
                    imgui.end_main_menu_bar()

                # right button rotation
                if imgui.is_mouse_down(1):
                    if self.canva.drag_start == None:
                        pos = imgui.get_io().mouse_pos
                        self.canva.drag_start = [pos[0], pos[1]]
                        self.rqq.rq_start_rotation(self.canva.drag_start[0],
                                                   self.canva.drag_start[1])
                    else:
                        pos = imgui.get_io().mouse_pos
                        self.pt = [pos[0], pos[1]]
                        self.rqq.rq_rotate(self.pt[0], self.pt[1])
                # left button panning
                elif imgui.is_mouse_down(0):
                    if self.canva.drag_start == None:
                        pos = imgui.get_io().mouse_pos
                        self.canva.drag_start = [pos[0], pos[1]]
                        self.prev_pos = pos
                    else:
                        pos = imgui.get_io().mouse_pos
                        if not self.prev_pos == pos:
                            self.rqq.rq_pan(pos[0] - self.prev_pos[0],
                                            pos[1] - self.prev_pos[1])
                            self.prev_pos = pos
                # wheel button scrolling
                else:
                    self.canva.drag_start = None
                    mw = imgui.get_io().mouse_wheel
                    pos = imgui.get_io().mouse_pos
                    if mw != 0:
                        self.rqq.rq_scroll(mw)
                    else:
                        if not self.prev_pos == pos:
                            self.rqq.rq_move(pos[0], pos[1])
                            self.prev_pos = pos

                self.canva.start_frame()
                imgui.render()
                draw_data = imgui.get_draw_data()
                self.impl.render(draw_data)
                self.canva.swap_buffers()
                self.canva.poll_events()
            self.canva.rq_stop()
            self.canva.pipe.close()
            print("GUI stopped")
        else:
            while True:
                rq = self.canva.pipe.recv()
                jdata = json.loads(rq)
                #print("rq:", rq, "jdata:", type(jdata))
                if jdata['rq'] == requests[RQ_LOAD_IMAGE]:
                    tex = self.canva.call_load_image()
                elif jdata['rq'] == requests[RQ_START_ROTATION]:
                    self.canva.call_start_rotation(jdata['args'][0],
                                                   jdata['args'][1])
                elif jdata['rq'] == requests[RQ_ROTATE]:
                    self.canva.call_rotate(jdata['args'][0], jdata['args'][1])
                elif jdata['rq'] == requests[RQ_SET_SIZE]:
                    self.canva.call_set_size(jdata['args'][0],
                                             jdata['args'][1])
                elif jdata['rq'] == requests[RQ_SCROLL]:
                    self.canva.call_scroll(jdata['args'])
                elif jdata['rq'] == requests[RQ_MOVE]:
                    self.canva.call_move(jdata['args'][0], jdata['args'][1])
                elif jdata['rq'] == requests[RQ_PAN]:
                    self.canva.call_pan(jdata['args'][0], jdata['args'][1])
                elif jdata['rq'] == requests[RQ_STOP]:
                    break
            self.canva.img.unlink()
            self.canva.pipe.close()
            print("OCCT stopped")
class Window:
    def __init__(self):
        if not glfw.init():
            raise RuntimeError("Unable to initialize glfw.")

        w_window, h_window = 800, 600

        self.window = glfw.create_window(w_window, h_window,
                                         "Voxel Visualizer", None, None)

        if not self.window:
            glfw.terminate()
            raise RuntimeError("Unable to create window.")

        monitor = glfw.get_primary_monitor()
        mode = glfw.get_video_mode(monitor)
        w_mon, h_mon = mode.size
        # center window.
        glfw.set_window_pos(self.window, int(0.5 * w_mon - 0.5 * w_window),
                            int(0.5 * h_mon - 0.5 * h_window))

        # Make the window's context current
        glfw.make_context_current(self.window)

        glfw.set_framebuffer_size_callback(self.window, self.on_resize)
        # add mouse handlers.
        glfw.set_input_mode(self.window, glfw.STICKY_MOUSE_BUTTONS, glfw.TRUE)
        glfw.set_mouse_button_callback(self.window, self.on_mouse_btn)
        glfw.set_cursor_pos_callback(self.window, self.on_mouse_move)
        glfw.set_window_size_callback(self.window, self.on_resize)

        glfw.set_key_callback(self.window, self.keyboard_callback)
        glfw.set_char_callback(self.window, self.char_callback)
        glfw.set_scroll_callback(self.window, self.scroll_callback)

        self.voxel_dims = glow.ivec3(256, 256, 32)

        # read config file.
        CFG = yaml.safe_load(open("config/semantic-kitti.yaml", 'r'))
        color_dict = CFG["color_map"]
        self.label_colors = glow.GlTextureRectangle(1024,
                                                    1,
                                                    internalFormat=GL_RGB,
                                                    format=GL_RGB)

        cols = np.zeros((1024 * 3), dtype=np.uint8)
        for label_id, color in color_dict.items():
            cols[3 * label_id + 0] = color[2]
            cols[3 * label_id + 1] = color[1]
            cols[3 * label_id + 2] = color[0]

        self.label_colors.assign(cols)

        self.initializeGL()

        # initialize imgui
        imgui.create_context()
        self.impl = GlfwRenderer(self.window, attach_callbacks=False)

        self.on_resize(self.window, w_window, h_window)

        self.data = []
        self.isDrag = False
        self.buttonPressed = None
        self.cam = Camera()
        self.cam.lookAt(25.0, 25.0, 25.0, 0.0, 0.0, 0.0)

        self.currentTimestep = 0
        self.sliderValue = 0
        self.showLabels = True

    def initializeGL(self):
        """ initialize GL related stuff. """

        self.num_instances = np.prod(self.voxel_dims)

        # see https://stackoverflow.com/questions/28375338/cube-using-single-gl-triangle-strip, but the normals are a problem.
        # verts = np.array([
        #     -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1, -1,
        #     -1, 1, -1, -1, -1, 1, -1, 1, 1, -1
        # ],
        #                  dtype=np.float32)

        # yapf: disable

        p1 = [1, 0, 0]
        p2 = [0, 0, 0]
        p3 = [1, 1, 0]
        p4 = [0, 1, 0]
        p5 = [1, 0, 1]
        p6 = [0, 0, 1]
        p7 = [0, 1, 1]
        p8 = [1, 1, 1]

        verts = np.array([
            # first face
            p4, p3, p7, p3, p7, p8,
            # second face
            p7, p8, p5, p7, p6, p5,
            # third face
            p8, p5, p3, p5, p3, p1,
            # fourth face
            p3, p1, p4, p1, p4, p2,
            # fifth face
            p4, p2, p7, p2, p7, p6,
            # sixth face
            p6, p5, p2, p5, p2, p1
        ], dtype=np.float32).reshape(-1)

        normals = np.array([[0, 1, 0] * 6,
                            [0, 0, 1] * 6,
                            [1, 0, 0] * 6,
                            [0, 0, -1] * 6,
                            [-1, 0, 0] * 6,
                            [0, -1, 0] * 6
                            ], dtype=np.float32).reshape(-1)

        # yapf: enable
        glow.WARN_INVALID_UNIFORMS = True

        self.labels = np.array([], dtype=np.float32)

        self.cube_verts = glow.GlBuffer()
        self.cube_verts.assign(verts)
        self.cube_normals = glow.GlBuffer()
        self.cube_normals.assign(normals)
        self.label_vbo = glow.GlBuffer()

        glPointSize(5.0)

        self.vao = glGenVertexArrays(1)
        glBindVertexArray(self.vao)

        SIZEOF_FLOAT = 4

        self.cube_verts.bind()
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * SIZEOF_FLOAT,
                              GLvoidp(0))
        glEnableVertexAttribArray(0)
        self.cube_verts.release()

        self.cube_normals.bind()
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * SIZEOF_FLOAT,
                              GLvoidp(0))
        glEnableVertexAttribArray(1)
        self.cube_normals.release()

        glEnableVertexAttribArray(2)
        self.label_vbo.bind()
        # Note: GL_UNSINGED_INT did not work as expected! I could not figure out what was wrong there!
        glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, SIZEOF_FLOAT,
                              GLvoidp(0))
        self.label_vbo.release()
        glVertexAttribDivisor(2, 1)

        glBindVertexArray(0)

        self.program = glow.GlProgram()
        self.program.attach(
            glow.GlShader.fromFile(GL_VERTEX_SHADER,
                                   "auxiliary/shaders/draw_voxels.vert"))
        self.program.attach(
            glow.GlShader.fromFile(GL_FRAGMENT_SHADER,
                                   "auxiliary/shaders/draw_voxels.frag"))
        self.program.link()

        self.prgDrawPose = glow.GlProgram()
        self.prgDrawPose.attach(
            glow.GlShader.fromFile(GL_VERTEX_SHADER,
                                   "auxiliary/shaders/empty.vert"))
        self.prgDrawPose.attach(
            glow.GlShader.fromFile(GL_GEOMETRY_SHADER,
                                   "auxiliary/shaders/draw_pose.geom"))
        self.prgDrawPose.attach(
            glow.GlShader.fromFile(GL_FRAGMENT_SHADER,
                                   "auxiliary/shaders/passthrough.frag"))
        self.prgDrawPose.link()

        self.prgTestUniform = glow.GlProgram()
        self.prgTestUniform.attach(
            glow.GlShader.fromFile(GL_VERTEX_SHADER,
                                   "auxiliary/shaders/check_uniforms.vert"))
        self.prgTestUniform.attach(
            glow.GlShader.fromFile(GL_FRAGMENT_SHADER,
                                   "auxiliary/shaders/passthrough.frag"))
        self.prgTestUniform.link()

        # general parameters
        glClearColor(1.0, 1.0, 1.0, 1.0)
        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LEQUAL)
        glEnable(GL_LINE_SMOOTH)

        # x = forward, y = left, z = up to x = right, y = up, z = backward
        self.conversion_ = np.array(
            [0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, 0, 0, 0, 1],
            dtype=np.float32).reshape(4, 4)

        self.program.bind()

        self.program["voxel_size"] = 0.5
        self.program["voxel_dims"] = self.voxel_dims
        self.program["label_colors"] = 0

        self.program.release()

        self.vao_no_points = glGenVertexArrays(1)

    def open_directory(self, directory):
        """ open given sequences directory and get filenames of relevant files. """
        self.subdirs = [
            subdir for subdir in ["voxels", "predictions"]
            if os.path.exists(os.path.join(directory, subdir))
        ]

        if len(self.subdirs) == 0:
            raise RuntimeError("Neither 'voxels' nor 'predictions' found in " +
                               directory)

        self.availableData = {}
        self.data = {}

        for subdir in self.subdirs:
            self.availableData[subdir] = []
            self.data[subdir] = {}
            complete_path = os.path.join(directory, subdir)
            files = os.listdir(complete_path)

            data = sorted([
                os.path.join(complete_path, f) for f in files
                if f.endswith(".bin")
            ])
            if len(data) > 0:
                self.availableData[subdir].append("input")
                self.data[subdir]["input"] = data
                self.num_scans = len(data)

            data = sorted([
                os.path.join(complete_path, f) for f in files
                if f.endswith(".label")
            ])
            if len(data) > 0:
                self.availableData[subdir].append("labels")
                self.data[subdir]["labels"] = data
                self.num_scans = len(data)

            data = sorted([
                os.path.join(complete_path, f) for f in files
                if f.endswith(".invalid")
            ])
            if len(data) > 0:
                self.availableData[subdir].append("invalid")
                self.data[subdir]["invalid"] = data
                self.num_scans = len(data)

            data = sorted([
                os.path.join(complete_path, f) for f in files
                if f.endswith(".occluded")
            ])
            if len(data) > 0:
                self.availableData[subdir].append("occluded")
                self.data[subdir]["occluded"] = data
                self.num_scans = len(data)

        self.current_subdir = 0
        self.current_data = self.availableData[self.subdirs[
            self.current_subdir]][0]

        self.currentTimestep = 0
        self.sliderValue = 0

        self.lastChange = None
        self.lastUpdate = time.time()
        self.button_backward_hold = False
        self.button_forward_hold = False

        # todo: modify based on available stuff.
        self.showLabels = (self.current_data == "labels")
        self.showInput = (self.current_data == "input")
        self.showInvalid = (self.current_data == "invalid")
        self.showOccluded = (self.current_data == "occludded")

    def setCurrentBufferData(self, data_name, t):
        # update buffer content with given data identified by data_name.
        subdir = self.subdirs[self.current_subdir]

        if len(self.data[subdir][data_name]) < t: return False

        # Note: uint with np.uint32 did not work as expected! (with instances and uint32 this causes problems!)
        if data_name == "labels":
            buffer_data = np.fromfile(self.data[subdir][data_name][t],
                                      dtype=np.uint16).astype(np.float32)
        else:
            buffer_data = unpack(
                np.fromfile(self.data[subdir][data_name][t],
                            dtype=np.uint8)).astype(np.float32)

        self.label_vbo.assign(buffer_data)

        return True

    def on_resize(self, window, w, h):
        # set projection matrix
        fov = math.radians(45.0)
        aspect = w / h

        self.projection_ = glPerspective(fov, aspect, 0.1, 2000.0)

        self.impl.resize_callback(window, w, h)

    def on_mouse_btn(self, window, button, action, mods):
        x, y = glfw.get_cursor_pos(self.window)

        imgui.get_io().mouse_pos = (x, y)
        if imgui.get_io().want_capture_mouse:

            return

        if action == glfw.PRESS:
            self.buttonPressed = button
            self.isDrag = True
            self.cam.mousePressed(x, y, self.buttonPressed, None)
        else:
            self.buttonPressed = None
            self.isDrag = False
            self.cam.mouseReleased(x, y, self.buttonPressed, None)

    def on_mouse_move(self, window, x, y):
        if self.isDrag:
            self.cam.mouseMoved(x, y, self.buttonPressed, None)

    def keyboard_callback(self, window, key, scancode, action, mods):
        self.impl.keyboard_callback(window, key, scancode, action, mods)

        if not imgui.get_io().want_capture_keyboard:
            if key == glfw.KEY_B or key == glfw.KEY_LEFT:
                self.currentTimestep = self.sliderValue = max(
                    0, self.currentTimestep - 1)

            if key == glfw.KEY_N or key == glfw.KEY_RIGHT:
                self.currentTimestep = self.sliderValue = min(
                    self.num_scans - 1, self.currentTimestep + 1)

            if key == glfw.KEY_Q or key == glfw.KEY_ESCAPE:
                exit(0)

    def char_callback(self, window, char):
        self.impl.char_callback(window, char)

    def scroll_callback(self, window, x_offset, y_offset):
        self.impl.scroll_callback(window, x_offset, y_offset)

    def run(self):
        # Loop until the user closes the window
        while not glfw.window_should_close(self.window):
            # Poll for and process events
            glfw.poll_events()

            # build gui.
            self.impl.process_inputs()

            w, h = glfw.get_window_size(self.window)
            glViewport(0, 0, w, h)

            imgui.new_frame()

            timeline_height = 35
            imgui.push_style_var(imgui.STYLE_WINDOW_ROUNDING, 0)
            imgui.push_style_var(imgui.STYLE_FRAME_ROUNDING, 0)

            imgui.set_next_window_position(0, h - timeline_height - 10)
            imgui.set_next_window_size(w, timeline_height)

            imgui.begin(
                "Timeline", False, imgui.WINDOW_NO_TITLE_BAR
                | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_SCROLLBAR)
            imgui.columns(1)
            imgui.same_line(0, 0)
            imgui.push_item_width(-50)
            changed, value = imgui.slider_int("", self.sliderValue, 0,
                                              self.num_scans - 1)
            if changed: self.sliderValue = value
            if self.sliderValue != self.currentTimestep:
                self.currentTimestep = self.sliderValue

            imgui.push_style_var(imgui.STYLE_FRAME_ROUNDING, 3)

            play_delay = 1
            refresh_rate = 0.05

            current_time = time.time()

            imgui.same_line(spacing=5)
            changed = imgui.button("<", 20)
            if self.currentTimestep > 0:
                # just a click
                if changed:
                    self.currentTimestep = self.sliderValue = self.currentTimestep - 1
                    self.lastUpdate = current_time

                # button pressed.
                if imgui.is_item_active() and not self.button_backward_hold:
                    self.hold_start = current_time
                    self.button_backward_hold = True

                if not imgui.is_item_active() and self.button_backward_hold:
                    self.button_backward_hold = False

                # start playback when button pressed long enough
                if self.button_backward_hold and (
                    (current_time - self.hold_start) > play_delay):
                    if (current_time - self.lastUpdate) > refresh_rate:
                        self.currentTimestep = self.sliderValue = self.currentTimestep - 1
                        self.lastUpdate = current_time

            imgui.same_line(spacing=2)
            changed = imgui.button(">", 20)

            if self.currentTimestep < self.num_scans - 1:
                # just a click
                if changed:
                    self.currentTimestep = self.sliderValue = self.currentTimestep + 1
                    self.lastUpdate = current_time

                # button pressed.
                if imgui.is_item_active() and not self.button_forward_hold:
                    self.hold_start = current_time
                    self.button_forward_hold = True

                if not imgui.is_item_active() and self.button_forward_hold:
                    self.button_forward_hold = False

                # start playback when button pressed long enough
                if self.button_forward_hold and (
                    (current_time - self.hold_start) > play_delay):
                    if (current_time - self.lastUpdate) > refresh_rate:
                        self.currentTimestep = self.sliderValue = self.currentTimestep + 1
                        self.lastUpdate = current_time

            imgui.pop_style_var(3)
            imgui.end()

            imgui.set_next_window_position(20, 20, imgui.FIRST_USE_EVER)
            imgui.set_next_window_size(200, 150, imgui.FIRST_USE_EVER)
            imgui.begin("Show Data")

            if len(self.subdirs) > 1:
                for i, subdir in enumerate(self.subdirs):
                    changed, value = imgui.checkbox(subdir,
                                                    self.current_subdir == i)
                    if i < len(self.subdirs) - 1: imgui.same_line()
                    if changed and value: self.current_subdir = i

            subdir = self.subdirs[self.current_subdir]

            data_available = "input" in self.availableData[subdir]
            if data_available:
                imgui.push_style_var(imgui.STYLE_ALPHA, 1.0)
            else:
                imgui.push_style_var(imgui.STYLE_ALPHA, 0.3)

            changed, value = imgui.checkbox("input", self.showInput)
            if changed and value and data_available:
                self.showInput = True
                self.showLabels = False

            imgui.pop_style_var()

            data_available = "labels" in self.availableData[subdir]
            if data_available:
                imgui.push_style_var(imgui.STYLE_ALPHA, 1.0)
            else:
                imgui.push_style_var(imgui.STYLE_ALPHA, 0.3)

            changed, value = imgui.checkbox("labels", self.showLabels)
            if changed and value and data_available:
                self.showInput = False
                self.showLabels = True

            imgui.pop_style_var()

            data_available = "invalid" in self.availableData[subdir]
            if data_available:
                imgui.push_style_var(imgui.STYLE_ALPHA, 1.0)
            else:
                imgui.push_style_var(imgui.STYLE_ALPHA, 0.3)

            changed, value = imgui.checkbox("invalid", self.showInvalid)
            if changed and data_available: self.showInvalid = value

            imgui.pop_style_var()

            data_available = "occluded" in self.availableData[subdir]
            if data_available:
                imgui.push_style_var(imgui.STYLE_ALPHA, 1.0)
            else:
                imgui.push_style_var(imgui.STYLE_ALPHA, 0.3)

            changed, value = imgui.checkbox("occluded", self.showOccluded)
            if changed and data_available: self.showOccluded = value

            imgui.pop_style_var()

            imgui.end()

            # imgui.show_demo_window()

            showData = []
            if self.showInput: showData.append("input")
            if self.showOccluded: showData.append("occluded")
            if self.showInvalid: showData.append("invalid")

            mvp = self.projection_ @ self.cam.matrix @ self.conversion_

            glClearColor(1.0, 1.0, 1.0, 1.0)

            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            glBindVertexArray(self.vao)

            self.program.bind()
            self.program["mvp"] = mvp.transpose()
            self.program["view_mat"] = (
                self.cam.matrix @ self.conversion_).transpose()
            self.program["lightPos"] = glow.vec3(10, 10, 10)
            self.program["voxel_scale"] = 0.8
            self.program["voxel_alpha"] = 1.0
            self.program["use_label_colors"] = True

            self.label_colors.bind(0)

            if self.showLabels:
                self.setCurrentBufferData("labels", self.currentTimestep)
                glDrawArraysInstanced(GL_TRIANGLES, 0, 36, self.num_instances)

            self.program["use_label_colors"] = False
            self.program["voxel_color"] = glow.vec3(0.3, 0.3, 0.3)

            self.program["voxel_alpha"] = 0.5

            for data_name in showData:
                self.program["voxel_scale"] = 0.5
                if data_name == "input": self.program["voxel_scale"] = 0.8

                self.setCurrentBufferData(data_name, self.currentTimestep)
                glDrawArraysInstanced(GL_TRIANGLES, 0, 36, self.num_instances)

            self.program.release()
            self.label_colors.release(0)

            glBindVertexArray(self.vao_no_points)

            self.prgDrawPose.bind()
            self.prgDrawPose["mvp"] = mvp.transpose()
            self.prgDrawPose["pose"] = np.identity(4, dtype=np.float32)
            self.prgDrawPose["size"] = 1.0

            glDrawArrays(GL_POINTS, 0, 1)
            self.prgDrawPose.release()

            glBindVertexArray(0)

            # draw gui ontop.
            imgui.render()
            self.impl.render(imgui.get_draw_data())

            # Swap front and back buffers
            glfw.swap_buffers(self.window)