Exemplo n.º 1
0
    def update(self, dt):
        self.elapsed_time += dt
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.begin_frame(
            int(mouse_x), int(mouse_y), buttons_states, 0, self.width, self.height
        )

        show_example_dialog()

        ImGuiExtra.end_frame()

        at = (c_float * 3)(*[0.0, 1.0, 0.0])
        eye = (c_float * 3)(*[0.0, 1.0, -2.5])
        up = (c_float * 3)(*[0.0, 1.0, 0.0])

        view = look_at(eye, at, up)
        projection = proj(60.0, self.width / self.height, 0.1, 100.0)

        bgfx.setViewTransform(0, as_void_ptr(view), as_void_ptr(projection))
        bgfx.setViewRect(0, 0, 0, self.width, self.height)

        bgfx.touch(0)

        mtx = rotate_xy(0, self.elapsed_time * 0.37)

        bgfx.setUniform(
            self.time_uniform,
            as_void_ptr((c_float * 4)(self.elapsed_time, 0.0, 0.0, 0.0)),
        )

        self.mesh.submit(0, self.main_program, mtx)

        bgfx.frame()
Exemplo n.º 2
0
    def update(self, dt):
        self.elapsed_time += dt
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.begin_frame(
            int(mouse_x), int(mouse_y), buttons_states, 0, self.width, self.height
        )

        show_example_dialog()
        self._create_imgui_cubes_selection_dialog()

        ImGuiExtra.end_frame()

        at = (c_float * 3)(*[0.0, 0.0, 0.0])
        eye = (c_float * 3)(*[0.0, 0.0, -35.0])
        up = (c_float * 3)(*[0.0, 1.0, 0.0])

        view = look_at(eye, at, up)
        projection = proj(60.0, self.width / self.height, 0.1, 100.0)

        bgfx.setViewTransform(0, as_void_ptr(view), as_void_ptr(projection))
        bgfx.setViewRect(0, 0, 0, self.width, self.height)

        bgfx.touch(0)

        for yy in range(0, 11):
            for xx in range(0, 11):
                mtx = rotate_xy(
                    self.elapsed_time + xx * 0.21, self.elapsed_time + yy * 0.37
                )
                mtx[3, 0] = -15.0 + xx * 3.0
                mtx[3, 1] = -15.0 + yy * 3.0
                mtx[3, 2] = 0.0
                bgfx.setTransform(as_void_ptr(mtx), 1)

                # Set vertex and index buffer.
                bgfx.setVertexBuffer(0, self.vertex_buffer, 0, num_vertices)
                bgfx.setIndexBuffer(
                    self.index_buffers[self.primitive_geometry.value],
                    0,
                    primitives[self.primitive_geometry.value].size,
                )

                bgfx.setState(
                    bgfx_states[self.primitive_geometry.value]
                    | (BGFX_STATE_WRITE_R if self.write_r.value else 0)
                    | (BGFX_STATE_WRITE_G if self.write_g.value else 0)
                    | (BGFX_STATE_WRITE_B if self.write_b.value else 0)
                    | (BGFX_STATE_WRITE_A if self.write_a.value else 0)
                    | BGFX_STATE_WRITE_Z
                    | BGFX_STATE_DEPTH_TEST_LESS
                    | BGFX_STATE_CULL_CW
                    | BGFX_STATE_MSAA,
                    0,
                )

                bgfx.submit(0, self.main_program, 0, False)

        bgfx.frame()
Exemplo n.º 3
0
    def add_particles(self, position: tuple, radius: float, strength: float):
        if self.simulate:
            self._init_compute_kernels()
            bgfx.setUniform(
                self.position_uniform,
                as_void_ptr((c_float * 2)(position[0], position[1])),
            )
            bgfx.setUniform(self.value_uniform,
                            as_void_ptr((c_float * 1)(strength)))
            bgfx.setUniform(self.radius_uniform,
                            as_void_ptr((c_float * 1)(radius)))

            bgfx.dispatch(0, self._add_particles_kernel, self._num_groups_x,
                          self._num_groups_x, 1)
            self._flip_buffer()
Exemplo n.º 4
0
    def add_velocity(self, position: tuple, velocity: tuple, radius: float):
        if self.simulate:
            self._init_compute_kernels()
            bgfx.setUniform(
                self.position_uniform,
                as_void_ptr((c_float * 2)(position[0], position[1])),
            )
            bgfx.setUniform(
                self.value_uniform,
                as_void_ptr((c_float * 2)(velocity[0], velocity[1])))
            bgfx.setUniform(self.radius_uniform,
                            as_void_ptr((c_float * 1)(radius)))

            bgfx.dispatch(0, self._add_velocity_kernel, self._num_groups_x,
                          self._num_groups_y, 1)
            self._flip_velocity_buffer()
Exemplo n.º 5
0
    def update(self, time_delta: float):
        self._init_compute_kernels()

        if self.simulate:
            bgfx.setUniform(self.dissipation_uniform,
                            as_void_ptr((c_float * 1)(self.dissipation)))
            bgfx.setUniform(self.elapsed_time_uniform,
                            as_void_ptr((c_float * 1)(time_delta)))
            bgfx.setUniform(self.speed_uniform,
                            as_void_ptr((c_float * 1)(self.speed)))

            bgfx.dispatch(
                0,
                self._advect_particles_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
            self._flip_buffer()
Exemplo n.º 6
0
    def init(self, platform_data):
        bgfx.setPlatformData(platform_data)
        bgfx.renderFrame()
        bgfx.init(self.init_conf)
        bgfx.reset(
            self.width, self.height, BGFX_RESET_VSYNC, self.init_conf.resolution.format,
        )

        bgfx.setDebug(BGFX_DEBUG_TEXT)
        bgfx.setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0, 0)

        self.vertex_layout = bgfx.VertexLayout()
        self.vertex_layout.begin().add(
            bgfx.Attrib.Position, 3, bgfx.AttribType.Float
        ).add(bgfx.Attrib.Color0, 4, bgfx.AttribType.Uint8, True).end()

        # Create static vertex buffer
        vb_memory = bgfx.copy(
            as_void_ptr(cube_vertices), sizeof(PosColorVertex) * num_vertices
        )
        self.vertex_buffer = bgfx.createVertexBuffer(vb_memory, self.vertex_layout)

        self.index_buffers = []

        for i in range(0, len(primitives)):
            ib_memory = bgfx.copy(as_void_ptr(primitives[i]), primitives[i].nbytes)
            self.index_buffers.append(bgfx.createIndexBuffer(ib_memory))

        # Create program from shaders.
        self.main_program = bgfx.createProgram(
            load_shader(
                "cubes.VertexShader.vert", ShaderType.VERTEX, root_path=root_path
            ),
            load_shader(
                "cubes.FragmentShader.frag", ShaderType.FRAGMENT, root_path=root_path
            ),
            True,
        )

        ImGuiExtra.create()
Exemplo n.º 7
0
    def _init_compute_kernels(self):
        bgfx.setUniform(
            self.particle_size_uniform,
            as_void_ptr((c_float * 2)(self._particle_size[0],
                                      self._particle_size[1])),
        )
        bgfx.setUniform(
            self.velocity_size_uniform,
            as_void_ptr((c_float * 2)(self._velocity_size[0],
                                      self._velocity_size[1])),
        )

        bgfx.setBuffer(
            TemplateConstants.PARTICLES_IN.value,
            self._particles_buffer[self.PARTICLES_IN],
            bgfx.Access.Write,
        )
        bgfx.setBuffer(
            TemplateConstants.PARTICLES_OUT.value,
            self._particles_buffer[self.PARTICLES_OUT],
            bgfx.Access.Write,
        )
Exemplo n.º 8
0
    def add_circle_obstacle(self,
                            position: tuple,
                            radius: float,
                            static=False):
        if self.simulate:
            self._init_compute_kernels()
            bgfx.setUniform(
                self.position_uniform,
                as_void_ptr((c_float * 2)(position[0], position[1])),
            )
            bgfx.setUniform(self.radius_uniform,
                            as_void_ptr((c_float * 1)(radius)))
            bgfx.setUniform(self.static_uniform,
                            as_void_ptr((c_float * 1)(1.0 if static else 0.0)))

            bgfx.dispatch(
                0,
                self._add_circle_obstacle_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
Exemplo n.º 9
0
    def add_triangle_obstacle(self,
                              p1: tuple,
                              p2: tuple,
                              p3: tuple,
                              static=False):
        if self.simulate:
            self._init_compute_kernels()
            bgfx.setUniform(self.p1_uniform,
                            as_void_ptr((c_float * 2)(p1[0], p1[1])))
            bgfx.setUniform(self.p2_uniform,
                            as_void_ptr((c_float * 2)(p2[0], p2[1])))
            bgfx.setUniform(self.p3_uniform,
                            as_void_ptr((c_float * 2)(p3[0], p3[1])))
            bgfx.setUniform(self.static_uniform,
                            as_void_ptr((c_float * 1)(1.0 if static else 0.0)))

            bgfx.dispatch(
                0,
                self._add_triangle_obstacle_kernel,
                self._num_groups_x,
                self._num_groups_y,
                1,
            )
Exemplo n.º 10
0
    def _init_compute_kernels(self):
        bgfx.setUniform(self.size_uniform,
                        as_void_ptr((c_float * 2)(self._width, self._height)))

        bgfx.setBuffer(1, self._velocity_buffer[self.VELOCITY_READ],
                       bgfx.Access.Read)
        bgfx.setBuffer(2, self._velocity_buffer[self.VELOCITY_WRITE],
                       bgfx.Access.Write)

        bgfx.setBuffer(3, self._pressure_buffer[self.PRESSURE_READ],
                       bgfx.Access.Read)
        bgfx.setBuffer(4, self._pressure_buffer[self.PRESSURE_WRITE],
                       bgfx.Access.Write)

        bgfx.setBuffer(5, self._divergence_buffer, bgfx.Access.ReadWrite)
        bgfx.setBuffer(6, self._vorticity_buffer, bgfx.Access.ReadWrite)
        bgfx.setBuffer(7, self._obstacles_buffer, bgfx.Access.ReadWrite)
Exemplo n.º 11
0
    def _update_params(self, time_delta: float):
        bgfx.setUniform(self.elapsed_time_uniform,
                        as_void_ptr((c_float * 1)(time_delta)))
        bgfx.setUniform(self.speed_uniform,
                        as_void_ptr((c_float * 1)(self.speed)))
        bgfx.setUniform(self.dissipation_uniform,
                        as_void_ptr((c_float * 1)(self.dissipation)))
        bgfx.setUniform(self.vorticity_scale_uniform,
                        as_void_ptr((c_float * 1)(self.vorticity)))

        if self._viscosity > 0.0:
            centre_factor = 1.0 / self.viscosity
            stencil_factor = 1.0 / (4.0 + centre_factor)

            bgfx.setUniform(self.alpha_uniform,
                            as_void_ptr((c_float * 1)(centre_factor)))
            bgfx.setUniform(self.rbeta_uniform,
                            as_void_ptr((c_float * 1)(stencil_factor)))
Exemplo n.º 12
0
def _load_mem(content: bytes) -> bgfx.Memory:
    size = len(content)
    memory = bgfx.copy(as_void_ptr(content), size)
    return memory
Exemplo n.º 13
0
    def init(self, platform_data):
        bgfx.renderFrame()
        bgfx.setPlatformData(platform_data)
        bgfx.init(self.init_conf)
        bgfx.reset(
            self.fb_width,
            self.fb_height,
            BGFX_RESET_VSYNC | BGFX_RESET_HIDPI,
            self.init_conf.resolution.format,
        )

        bgfx.setDebug(BGFX_DEBUG_TEXT)
        # bgfx.setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0xFFFFFFFF, 1.0, 0)
        bgfx.setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x1a0427FF,
                          1.0, 0)

        self.vertex_layout = bgfx.VertexLayout()
        self.vertex_layout.begin().add(bgfx.Attrib.Position, 3,
                                       bgfx.AttribType.Float).add(
                                           bgfx.Attrib.TexCoord0, 3,
                                           bgfx.AttribType.Float).end()

        self.fluid_simulator = FluidSimulator(self.width // 2,
                                              self.height // 2,
                                              self.vertex_layout)
        self.fluid_simulator.vorticity = 1.0
        self.fluid_simulator.viscosity = 0.5
        self.fluid_simulator.iterations = 50

        self.particle_area = SmoothParticlesArea(self.fb_width, self.fb_height,
                                                 self.fluid_simulator,
                                                 self.vertex_layout)
        self.particle_area.dissipation = 0.980

        # Create static vertex buffer
        vb_memory = bgfx.copy(as_void_ptr(cube_vertices),
                              sizeof(PosColorVertex) * 4)
        self.vertex_buffer = bgfx.createVertexBuffer(vb_memory,
                                                     self.vertex_layout)

        ib_memory = bgfx.copy(as_void_ptr(cube_indices), cube_indices.nbytes)
        self.index_buffer = bgfx.createIndexBuffer(ib_memory)

        self.output_texture = bgfx.createTexture2D(
            self.fb_width,
            self.fb_height,
            False,
            1,
            bgfx.TextureFormat.RGBA8,
            BGFX_TEXTURE_COMPUTE_WRITE,
        )

        self.texture_uniform = bgfx.createUniform("InputTexture",
                                                  bgfx.UniformType.Sampler)
        self.window_size_uniform = bgfx.createUniform("WindowSize",
                                                      bgfx.UniformType.Vec4)
        self.velocity_size_uniform = bgfx.createUniform(
            "VelocitySize", bgfx.UniformType.Vec4)
        self.arrow_tile_size_uniform = bgfx.createUniform(
            "ArrowTileSize", bgfx.UniformType.Vec4)

        # Create program from shaders.
        self.main_program = bgfx.createProgram(
            load_shader("demo.VertexShader.vert",
                        ShaderType.VERTEX,
                        root_path=root_path),
            load_shader(
                "demo.FieldFragmentShader.frag",
                ShaderType.FRAGMENT,
                root_path=root_path,
            ),
            True,
        )
        self.quiver_program = bgfx.createProgram(
            load_shader("demo.VertexShader.vert",
                        ShaderType.VERTEX,
                        root_path=root_path),
            load_shader(
                "demo.QuiverFragmentShader.frag",
                ShaderType.FRAGMENT,
                root_path=root_path,
            ),
            True,
        )
        self.cs_program = bgfx.createProgram(
            load_shader("demo.ComputeShader.comp",
                        ShaderType.COMPUTE,
                        root_path=root_path),
            True,
        )

        ImGuiExtra.create(36.0)
Exemplo n.º 14
0
    def update(self, dt):
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()

        ImGuiExtra.begin_frame(int(mouse_x), int(mouse_y), buttons_states, 0,
                               self.fb_width, self.fb_height)
        show_properties_dialog(self.fb_width, self.fb_height, self.hidpi)
        self._create_imgui_config_dialog()

        ImGuiExtra.end_frame()

        at = (c_float * 3)(*[0.0, 0.0, 0.0])
        eye = (c_float * 3)(*[0.0, 0.0, 10.0])
        up = (c_float * 3)(*[0.0, 1.0, 0.0])

        view = look_at(eye, at, up)
        projection = proj(11.4, 1, 0.1, 100.0)

        bgfx.setViewRect(0, 0, 0, self.fb_width, self.fb_height)

        bgfx.setViewTransform(0, as_void_ptr(view), as_void_ptr(projection))

        bgfx.setVertexBuffer(0, self.vertex_buffer, 0, 4)
        bgfx.setIndexBuffer(self.index_buffer, 0, cube_indices.size)

        bgfx.setState(BGFX_STATE_DEFAULT)
        bgfx.setImage(0, self.output_texture, 0, bgfx.Access.Write)

        self.fluid_simulator.add_circle_obstacle((0.5, 0.5), 40.0)

        self.fluid_simulator.update(dt)
        self.particle_area.update(dt)

        if glfw.get_mouse_button(self.window, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS\
                and not ImGui.GetIO().WantCaptureMouse:
            n_mouse_x, n_mouse_y = self._get_normalized_mouse_coords(
                mouse_x, mouse_y)
            n_old_mouse_x, n_old_mouse_y = self._get_normalized_mouse_coords(
                self.old_mouse_x, self.old_mouse_y)
            vel_x = n_mouse_x - n_old_mouse_x
            vel_y = n_mouse_y - n_old_mouse_y
            # if vel_x != 0 and vel_y != 0:
            self.fluid_simulator.add_velocity((n_mouse_x, n_mouse_y),
                                              (vel_x * 10, vel_y * 10), 32.0)
            self.particle_area.add_particles((n_mouse_x, n_mouse_y),
                                             self.particles_diameter,
                                             self.particles_strength)

        self.old_mouse_x = mouse_x
        self.old_mouse_y = mouse_y

        bgfx.setState(0 | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A
                      | BGFX_STATE_BLEND_ALPHA)

        bgfx.dispatch(0, self.cs_program, self.fb_width // 16,
                      self.fb_height // 16)
        bgfx.setTexture(0, self.texture_uniform, self.output_texture)
        bgfx.submit(0, self.main_program, 0, False)

        if self.show_quiver_plot_overlay.value:
            bgfx.setBuffer(1, self.fluid_simulator.get_velocity_buffer(),
                           bgfx.Access.Read)
            bgfx.setUniform(
                self.window_size_uniform,
                as_void_ptr((c_float * 2)(self.fb_width, self.fb_height)),
            )
            bgfx.setUniform(
                self.velocity_size_uniform,
                as_void_ptr((c_float * 2)(self.fluid_simulator.width,
                                          self.fluid_simulator.height)),
            )
            bgfx.setUniform(
                self.arrow_tile_size_uniform,
                as_void_ptr((c_float * 1)(self.quiver_plot_resolutions[
                    self.quiver_plot_resolution.value])),
            )
            bgfx.submit(0, self.quiver_program, 0, False)

        bgfx.frame()
Exemplo n.º 15
0
    def run(self):
        glfw_native.glfwCreateWindow.argtypes = [
            ctypes.c_int,
            ctypes.c_int,
            ctypes.c_char_p,
        ]
        glfw_native.glfwCreateWindow.restype = ctypes.POINTER(glfw._GLFWwindow)
        glfw_native.glfwMakeContextCurrent.argtypes = [
            ctypes.POINTER(glfw._GLFWwindow)
        ]
        glfw_native.glfwWindowShouldClose.argtypes = [
            ctypes.POINTER(glfw._GLFWwindow)
        ]
        glfw_native.glfwWindowShouldClose.restype = ctypes.c_int

        glfw.init()

        glfw.window_hint(glfw.CLIENT_API, glfw.NO_API)
        glfw.window_hint(glfw.COCOA_RETINA_FRAMEBUFFER, glfw.TRUE)

        self.window = glfw.create_window(self.width, self.height, self.title,
                                         None, None)

        self.fb_width, self.fb_height = glfw.get_framebuffer_size(self.window)

        self.hidpi = self.fb_width != self.width or self.fb_height != self.height

        glfw.set_window_size_callback(self.window, self._handle_window_resize)

        handle, display = None, None

        if sys.platform == "darwin":
            handle = glfw_native.glfwGetCocoaWindow(self.window)
        elif sys.platform == "win32":
            handle = glfw_native.glfwGetWin32Window(self.window)
        elif sys.platform == "linux" and "WAYLAND_DISPLAY" not in os.environ:
            handle = glfw_native.glfwGetX11Window(self.window)
            display = glfw_native.glfwGetX11Display()
        elif sys.platform == "linux" and "WAYLAND_DISPLAY" in os.environ:
            handle = glfw_native.glfwGetWaylandWindow(self.window)
            display = glfw_native.glfwGetWaylandDisplay()

        data = bgfx.PlatformData()
        data.ndt = as_void_ptr(display) if display else cppyy.nullptr
        data.nwh = as_void_ptr(handle)
        data.context = cppyy.nullptr
        data.backBuffer = cppyy.nullptr
        data.backBufferDS = cppyy.nullptr

        self.init(data)

        last_time = None

        while not glfw.window_should_close(self.window):
            glfw.poll_events()

            now = time.perf_counter()
            if not last_time:
                last_time = now

            frame_time = now - last_time
            last_time = now

            self.update(frame_time)

        self.shutdown()
        glfw.terminate()