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()
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()
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()
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()
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()
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
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()
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(): 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(): 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()
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()
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 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()
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()
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()
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()
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())
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()
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()
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()
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()
# 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 # Poll and handle events (inputs, window resize, etc.) # You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. # - When io.want_capture_mouse is true, do not dispatch mouse input data to your main application. # - When io.want_capture_keyboard is true, do not dispatch keyboard input data to your main application. # Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. # io = imgui.get_io() #print(io.want_capture_mouse, io.want_capture_keyboard) glfw.poll_events() # Filling or not the shapes depending on the controller state if (controller.fillPolygon): glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) else:
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()
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")
def run(self): """Run the main game loop logic""" # Shorten the variables world = self.world rendering_system = self.rendering_system window = self.window impl = ImGuiGlfwRenderer(window) # set the variables from the world object world.terrain = Terrain() world.terrain.load("data/track_01_128.png", rendering_system) world.racer = Racer() world.racer.load("data/racer_02.obj", world.terrain) current_time = glfw.get_time() prev_mouse_x, prev_mouse_y = glfw.get_cursor_pos(window) while not glfw.window_should_close(window): prev_time = current_time current_time = glfw.get_time() dt = current_time - prev_time key_state_map = {} for item_name, item_id in GLFW_KEYMAP.items(): key_state_map[item_name] = glfw.get_key(window, item_id) == glfw.PRESS for item_name, item_id in GLFW_MOUSE_MAP.items(): key_state_map[item_name] = glfw.get_mouse_button( window, item_id) == glfw.PRESS imgui.new_frame() imgui.set_next_window_size(430.0, 450.0, imgui.FIRST_USE_EVER) imgui.begin("Tweak variables") mouse_x, mouse_y = glfw.get_cursor_pos(window) mouse_delta = [mouse_x - prev_mouse_x, mouse_y - prev_mouse_y] prev_mouse_x, prev_mouse_y = mouse_x, mouse_y # Update 'world logic' im_io = imgui.get_io() if im_io.want_capture_mouse: mouse_delta = [0, 0] update(world, rendering_system, dt, key_state_map, mouse_delta) width, height = glfw.get_framebuffer_size(window) renderFrame(world, rendering_system, width, height) # mgui.show_test_window() imgui.end() imgui.render() # Swap front and back buffers glfw.swap_buffers(window) # Poll for and process events glfw.poll_events() impl.process_inputs() # This is the end of the game. Do some cleanup glfw.destroy_window(window) glfw.terminate()
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)
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()
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 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()
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)