Exemple #1
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()
Exemple #2
0
    def __init__(self, gui=False, width=800, height=600, window_name='opengl'):
        self.width = width
        self.height = height

        if not glfw.init():
            raise RuntimeError('could not init glfw')

        hints = ((glfw.CONTEXT_VERSION_MAJOR, 4),
                 (glfw.CONTEXT_VERSION_MINOR, 5), (glfw.OPENGL_PROFILE,
                                                   glfw.OPENGL_CORE_PROFILE))
        for hint in hints:
            glfw.window_hint(*hint)

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

        def viewport_func(window, width, height):
            self.width = width
            self.height = height
            self.update_camera(update_proj=True)
            self.ctx.viewport = (0, 0, self.width, self.height)

        glfw.set_framebuffer_size_callback(self.window, viewport_func)

        if not self.window:
            glfw.terminate()
            raise RuntimeError('could not create glfw window')

        self.gui_impl = None
        if gui:
            self.gui_impl = GlfwRenderer(self.window)

        self.ctx = moderngl.create_context()
        self.ctx.viewport = (0, 0, self.width, self.height)
Exemple #3
0
    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)
Exemple #4
0
async def async_main(window, config):
    await asyncjob.start_worker()

    imgui_impl = GlfwRenderer(window)

    renderer = WorldRenderer()

    app = StarboundMap(renderer)
    frame_size = np.ones(2)
    while not glfw.window_should_close(window):
        glfw.poll_events()
        if G.minimized:  # do not render zero sized frame
            continue

        imgui_impl.process_inputs()
        frame_size = np.array(glfw.get_framebuffer_size(window))

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
        gl.glViewport(0, 0, frame_size[0], frame_size[1])

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

        app.gui()

        glfw.swap_buffers(window)

        await asyncio.sleep(0)

    config[CONFIG_WIDTH] = str(int(frame_size[0]))
    config[CONFIG_HEIGHT] = str(int(frame_size[1]))

    imgui_impl.shutdown()
    imgui.shutdown()
Exemple #5
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()
Exemple #6
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()
Exemple #7
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()
Exemple #8
0
    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
Exemple #9
0
    def makeGUI(self, gui):
        """ Create the GUI context and GlfwRenderer

            Args:
                gui (bool): if True create the GUI context and GlfwRenderer
        """
        if gui:
            imgui.create_context()
            self._renderer = GlfwRenderer(self.window(), True)
            self._opened = True
        else:
            self._renderer = None
            self._opened = False
Exemple #10
0
def init():
    global window, imgui_renderer, _ui_state
    glfw.set_error_callback(error_callback)
    imgui.create_context()
    if not glfw.init():
        print("GLFW Initialization fail!")
        return
    graphics_settings = configuration.get_graphics_settings()
    loader_settings = configuration.get_loader_settings()
    if graphics_settings is None:
        print("bla")
        return
    screen_utils.WIDTH = graphics_settings.getint("width")
    screen_utils.HEIGHT = graphics_settings.getint("height")
    screen_utils.MAX_FPS = graphics_settings.getint("max_fps")
    screen = None
    if graphics_settings.getboolean("full_screen"):
        screen = glfw.get_primary_monitor()
    hints = {
        glfw.DECORATED: glfw.TRUE,
        glfw.RESIZABLE: glfw.FALSE,
        glfw.CONTEXT_VERSION_MAJOR: 4,
        glfw.CONTEXT_VERSION_MINOR: 5,
        glfw.OPENGL_DEBUG_CONTEXT: glfw.TRUE,
        glfw.OPENGL_PROFILE: glfw.OPENGL_CORE_PROFILE,
        glfw.SAMPLES: 4,
    }
    window = _create_window(size=(screen_utils.WIDTH, screen_utils.HEIGHT),
                            pos="centered",
                            title="Tremor",
                            monitor=screen,
                            hints=hints,
                            screen_size=glfw.get_monitor_physical_size(
                                glfw.get_primary_monitor()))
    imgui_renderer = GlfwRenderer(window, attach_callbacks=False)
    glutil.log_capabilities()
    glEnable(GL_CULL_FACE)
    glCullFace(GL_BACK)
    glEnable(GL_DEPTH_TEST)
    glDepthMask(GL_TRUE)
    glDepthFunc(GL_LEQUAL)
    glDepthRange(0.0, 1.0)
    glEnable(GL_MULTISAMPLE)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    create_branched_programs()
    # create the uniforms
    _create_uniforms()
    # initialize all the uniforms for all the prpograms
    init_all_uniforms()
Exemple #11
0
 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
Exemple #12
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()
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()
Exemple #14
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()
def main():
    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    box1 = box2 = box3 = 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, 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("Scope Test")

        box1 = imgui.checkbox("Checkbox", box1)[1]

        with imgui.scope(2):
            imgui.new_line()
            imgui.text("Same name, different scope:")
            box2 = imgui.checkbox("Checkbox", box2)[1]

        imgui.new_line()
        imgui.text("Same name, same scope:")
        imgui.text("(This will not work right)")
        box3 = imgui.checkbox("Checkbox", box3)[1]

        imgui.end()

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

        imgui.render()
        glfw.swap_buffers(window)

    impl.shutdown()
    imgui.shutdown()
    glfw.terminate()
Exemple #16
0
def main():
    imgui.create_context()
    window = impl_glfw_init()

    impl = GlfwRenderer(window)

    io = imgui.get_io()
    jb = io.fonts.add_font_from_file_ttf(path_to_font, 30) if path_to_font is not None else None
    impl.refresh_font_texture()

    while not glfw.window_should_close(window):
        render_frame(impl, window, jb)

    impl.shutdown()
    glfw.terminate()
Exemple #17
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)
Exemple #18
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()
Exemple #19
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()
def init():
    if not glfw.init():
        return
    # Create window
    m = glfw.get_primary_monitor()
    mode = glfw.get_video_mode(m)
    width = mode.size.width
    height = mode.size.height

    window = glfw.create_window(width, height, "Fractals", None, None)
    if not window:
        glfw.terminate()
        exit(0)
    glfw.make_context_current(window)
    impl = GlfwRenderer(window)
    #compile shaders
    try:
        mandelbrot_shader = from_files_names("fractal_v.glsl",
                                             "mandelbrot_f.glsl")
        julia_shader = from_files_names("fractal_v.glsl", "julia_f.glsl")
        newton_shader = from_files_names("fractal_v.glsl", "newton_f.glsl")
    except ShaderCompilationError as e:
        print(e.logs)
        exit()
    #setup data used for window and shaders
    window_dict = {
        'width': width,
        'height': height,
        'n_iter': 100,
        'b': 2,
        'scalar': 4.5,
        'x_off': 0,
        'y_off': 0
    }
    window_dict = struct(window_dict)
    #setep callback so they can change local variables
    glfw.set_key_callback(window,
                          lambda *args: key_callback(window_dict, *args))
    glfw.set_window_size_callback(
        window, lambda *args: window_size_callback(window_dict, *args))
    #quad we are drawing to
    quad = pyglet.graphics.vertex_list(
        4, ('v2f', (-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0)))
    return window, window_dict, impl, quad, mandelbrot_shader, julia_shader, newton_shader
Exemple #21
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()
Exemple #22
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()
Exemple #23
0
def main():
    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.show_test_window()

        imgui.begin("Custom window", True)
        imgui.text("Bar")
        imgui.text_colored("Eggs", 0.2, 1., 0.)
        imgui.end()

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

        imgui.render()
        glfw.swap_buffers(window)

    impl.shutdown()
    imgui.shutdown()
    glfw.terminate()
Exemple #24
0
    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 _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)
Exemple #26
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()
Exemple #27
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()
    # Creating our shader program and telling OpenGL to use it
    pipeline = ModulationTransformShaderProgram()
    glUseProgram(pipeline.shaderProgram)

    # Setting up the clear screen color
    glClearColor(0.15, 0.15, 0.15, 1.0)

    # Creating shapes on GPU memory
    shapeQuad = bs.createRainbowQuad()
    gpuQuad = es.GPUShape().initBuffers()
    pipeline.setupVAO(gpuQuad)
    gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW)

    # initilize imgui context (see documentation)
    imgui.create_context()
    impl = GlfwRenderer(window)

    # Connecting the callback function 'on_key' to handle keyboard events
    # It is important to set the callback after the imgui setup
    glfw.set_key_callback(window, on_key)

    locationX = 0.0
    locationY = 0.0
    angle = 0.0
    color = (1.0, 1.0, 1.0)

    while not glfw.window_should_close(window):

        impl.process_inputs()
        # Using GLFW to check for input events
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()
Exemple #30
0
def render_snippet(
    source,
    file_path,
    title="",
    width=200,
    height=200,
    auto_layout=False,
    output_dir='.',
    click=None,
):
    _patch_imgui()

    # Little shim that filters out the new_frame and render commands
    # so we can use them in code examples. It's simply a hoax.
    lines = [
        line if all([
            "imgui.new_frame()" not in line, "imgui.render()" not in line,
            "imgui.end_frame()" not in line
        ]) else "" for line in source.split('\n')
    ]
    source = "\n".join(lines)

    code = compile(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)
    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] 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.DisplaySize.x * 0.5,
                                               implio.DisplaySize.y * 0.5,
                                               1,
                                               pivot_x=0.5,
                                               pivot_y=0.5)

            exec(code, locals(), globals())

        gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, offscreen_fb)

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

        imgui.render()

    # 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()