Esempio n. 1
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()
Esempio n. 2
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()
Esempio n. 3
0
    def __init__(self,
                 simulator,
                 snapshot,
                 nlines,
                 window_name,
                 width,
                 height,
                 font_path="microsim/opencl/fonts/RobotoMono.ttf"):
        """Create the window, imgui renderer, and all background renderers.

        Args:
            nplaces: Number of places being simulated.
            npeople: Number of people being simulated.
            nlines: Number of connection lines to draw per person (recommend low, must be < nslots).
            window_name: The name to display on the application window.
            width: Initial width of the window in screen coordinates.
            height: Initial height of the window in screen coordinates.
            font_path: Path the the .ttf file to use for text in imgui.
        """
        nplaces = simulator.nplaces
        npeople = simulator.npeople
        device = simulator.device_name()
        platform = simulator.platform_name()

        if not glfw.init():
            raise OSError("Could not initialize window")

        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, glfw.TRUE)

        window = glfw.create_window(width, height, window_name, None, None)
        if not window:
            glfw.terminate()
            raise OSError("Could not initialize window")

        glfw.make_context_current(window)
        imgui.create_context()
        impl = GlfwRenderer(window)

        glfw.set_framebuffer_size_callback(window, self.resize_callback)
        glfw.set_key_callback(window, self.key_callback)

        font = imgui.get_io().fonts.add_font_from_file_ttf(font_path, 56)
        impl.refresh_font_texture()

        # vertices representing corners of the screen
        quad_vertices = np.array([
            -1.0,
            -1.0,
            1.0,
            1.0,
            -1.0,
            1.0,
            -1.0,
            -1.0,
            1.0,
            1.0,
            1.0,
            -1.0,
        ],
                                 dtype=np.float32)

        # Create vertex buffers on the GPU
        quad_vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, quad_vbo)
        glBufferData(GL_ARRAY_BUFFER, 4 * 2 * 6, quad_vertices, GL_STATIC_DRAW)

        locations_vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, locations_vbo)
        glBufferData(GL_ARRAY_BUFFER, 4 * 2 * nplaces, None, GL_STATIC_DRAW)

        hazards_vbo = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, hazards_vbo)
        glBufferData(GL_ARRAY_BUFFER, 4 * nplaces, None, GL_DYNAMIC_DRAW)

        links_ebo = glGenBuffers(1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, links_ebo)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * 2 * npeople * nlines, None,
                     GL_STATIC_DRAW)

        # Set up the vao for the point shader
        point_vao = glGenVertexArrays(1)
        glBindVertexArray(point_vao)
        glBindBuffer(GL_ARRAY_BUFFER, locations_vbo)
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * 4, None)
        glEnableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, hazards_vbo)
        glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, 4, None)
        glEnableVertexAttribArray(1)

        # Set up the vao for the line shader
        line_vao = glGenVertexArrays(1)
        glBindVertexArray(line_vao)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, links_ebo)
        glBindBuffer(GL_ARRAY_BUFFER, locations_vbo)
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * 4, None)
        glEnableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, hazards_vbo)
        glVertexAttribIPointer(1, 1, GL_UNSIGNED_INT, 4, None)
        glEnableVertexAttribArray(1)

        # Set up the vao for the quad
        quad_vao = glGenVertexArrays(1)
        glBindVertexArray(quad_vao)
        glBindBuffer(GL_ARRAY_BUFFER, quad_vbo)
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * 4, None)
        glEnableVertexAttribArray(0)

        glBindVertexArray(0)

        # Load and compile shaders
        places_program = load_shader("places")
        grid_program = load_shader("grid")

        # Enable OpenGL features
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        # Initialise Camera position
        position = np.array([0.0, 0.0, 0.05], dtype=np.float32)

        # Imgui styling
        style = imgui.get_style()
        set_styles(style)

        # Make a guess on font size
        font_scale = 0.5
        self.update_font_scale(font_scale)

        # Initialise viewport based on framebuffer
        width, height = glfw.get_framebuffer_size(window)
        glViewport(0, 0, width, height)

        self.simulator = simulator
        self.snapshot = snapshot
        self.initial_state_snapshot = copy.deepcopy(snapshot)
        self.params = Params.fromarray(snapshot.buffers.params)
        self.nplaces = nplaces
        self.npeople = npeople
        self.nlines = nlines
        self.width = width
        self.height = height
        self.platform = platform
        self.device = device
        self.window = window
        self.first = True
        self.impl = impl
        self.font = font
        self.font_scale = font_scale

        self.simulation_active = False
        self.do_lockdown = False
        self.point_size = 2.0
        self.show_grid = True
        self.show_points = True
        self.show_lines = False
        self.show_parameters = False
        self.show_saveas = False
        self.spacing = 40.0
        self.move_sensitivity = 10.0
        self.zoom_multiplier = 1.01
        self.position = position
        self.snapshot_dir = "microsim/opencl/snapshots"
        self.snapshots = [
            f for f in os.listdir(self.snapshot_dir) if f.endswith(".npz")
        ]
        self.current_snapshot = self.snapshots.index(f"{snapshot.name}.npz")
        self.selected_snapshot = self.current_snapshot
        self.saveas_file = self.snapshots[self.current_snapshot]
        self.summary = Summary(snapshot, store_detailed_counts=False)

        self.quad_vbo = quad_vbo
        self.locations_vbo = locations_vbo
        self.hazards_vbo = hazards_vbo
        self.links_ebo = links_ebo
        self.point_vao = point_vao
        self.line_vao = line_vao
        self.quad_vao = quad_vao
        self.places_program = places_program
        self.grid_program = grid_program

        self.upload_hazards(self.snapshot.buffers.place_hazards)
        self.upload_locations(self.snapshot.buffers.place_coords)
        self.upload_links(self.snapshot.buffers.people_place_ids)