Beispiel #1
0
    def update(self, dt):
        self.elapsed_time += dt
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.imgui_begin_frame(
            int(mouse_x), int(mouse_y), buttons_states, 0, self.width, self.height
        )

        show_example_dialog()

        ImGuiExtra.imgui_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.set_view_transform(0, as_void_ptr(view), as_void_ptr(projection))
        bgfx.set_view_rect(0, 0, 0, self.width, self.height)

        bgfx.touch(0)

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

        bgfx.set_uniform(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()
Beispiel #2
0
 def shutdown(self):
     ImGuiExtra.imgui_destroy()
     for index_buffer in self.index_buffers:
         bgfx.destroy(index_buffer)
     bgfx.destroy(self.vertex_buffer)
     bgfx.destroy(self.main_program)
     bgfx.shutdown()
Beispiel #3
0
    def init(self, platform_data):
        self.init_conf.platform_data = platform_data
        bgfx.render_frame()
        bgfx.init(self.init_conf)
        bgfx.reset(
            self.width, self.height, BGFX_RESET_VSYNC, self.init_conf.resolution.format,
        )

        bgfx.set_debug(BGFX_DEBUG_TEXT)
        bgfx.set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0, 0)

        # Create time uniform
        self.time_uniform = bgfx.create_uniform("u_time", bgfx.UniformType.VEC4)

        # Load Bunny mesh
        self.mesh = Mesh(Path(__file__).parent.parent / "assets" / "meshes" / "bunny.bin")

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

        ImGuiExtra.imgui_create()
Beispiel #4
0
    def init(self, platform_data):
        self.init_conf.platform_data = platform_data
        bgfx.render_frame()
        bgfx.init(self.init_conf)
        bgfx.reset(
            self.width,
            self.height,
            BGFX_RESET_VSYNC,
            self.init_conf.resolution.format,
        )

        bgfx.set_debug(BGFX_DEBUG_TEXT)
        bgfx.set_view_clear(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).add(bgfx.Attrib.TEXCOORD0, 2,
                                                 bgfx.AttribType.FLOAT).end()

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

        # Create index buffer
        ib_memory = bgfx.copy(as_void_ptr(cube_indices), cube_indices.nbytes)
        self.index_buffer = bgfx.create_index_buffer(ib_memory)

        # Create texture uniform
        self.texture_uniform = bgfx.create_uniform("s_tex",
                                                   bgfx.UniformType.SAMPLER)

        # Load the image using PIL and make the texture
        logo = Image.open(
            Path(__file__).parent.parent / "assets" / "textures" /
            "python_logo.png")
        logo_memory = bgfx.copy(as_void_ptr(logo.tobytes()),
                                len(logo.tobytes()))
        self.logo_texture = bgfx.create_texture2d(logo.width, logo.height,
                                                  False, 1,
                                                  bgfx.TextureFormat.RGBA8,
                                                  BGFX_TEXTURE_RT, logo_memory)

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

        ImGuiExtra.imgui_create()
Beispiel #5
0
    def update(self, dt):
        self.elapsed_time += dt
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.imgui_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.imgui_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.set_view_transform(0, as_void_ptr(view), as_void_ptr(projection))
        bgfx.set_view_rect(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.set_transform(as_void_ptr(mtx), 1)

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

                bgfx.set_state(
                    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()
Beispiel #6
0
    def init(self, platform_data):
        self.init_conf.platform_data = platform_data
        bgfx.render_frame()
        bgfx.init(self.init_conf)

        bgfx.set_debug(BGFX_DEBUG_TEXT)
        bgfx.set_view_clear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF,
                            1.0, 0)
        ImGuiExtra.imgui_create()
Beispiel #7
0
    def update(self, dt):
        self.elapsed_time += dt
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.imgui_begin_frame(int(mouse_x), int(mouse_y),
                                     buttons_states, 0, self.width,
                                     self.height)

        show_example_dialog()

        ImGuiExtra.imgui_end_frame()

        at = (c_float * 3)(*[0.0, 0.0, 0.0])
        eye = (c_float * 3)(*[0.0, 0.0, -15.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.set_view_transform(0, as_void_ptr(view), as_void_ptr(projection))
        bgfx.set_view_rect(0, 0, 0, self.width, self.height)

        bgfx.touch(0)

        for yy in range(-2, 2):
            for xx in range(-2, 2):
                mtx = rotate_xy(self.elapsed_time + xx * 0.51,
                                self.elapsed_time + yy * 0.27)
                mtx[3, 0] = 4 + xx * 3.5
                mtx[3, 1] = 2 + yy * 3.5
                mtx[3, 2] = 0
                bgfx.set_transform(as_void_ptr(mtx), 1)

                # Set vertex and index buffer.
                bgfx.set_vertex_buffer(0, self.vertex_buffer, 0, num_vertices)
                bgfx.set_index_buffer(self.index_buffer, 0, cube_indices.size)

                # Set the texture
                bgfx.set_texture(0, self.texture_uniform, self.logo_texture)

                bgfx.set_state(
                    0
                    | BGFX_STATE_WRITE_RGB
                    | BGFX_STATE_WRITE_A
                    | BGFX_STATE_WRITE_Z
                    | BGFX_STATE_DEPTH_TEST_LESS
                    | BGFX_STATE_MSAA,
                    0,
                )

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

        bgfx.frame()
Beispiel #8
0
    def update(self, dt):
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.imgui_begin_frame(int(mouse_x), int(mouse_y),
                                     buttons_states, 0, self.width,
                                     self.height)

        show_example_dialog()

        ImGuiExtra.imgui_end_frame()

        bgfx.set_view_rect(0, 0, 0, self.width, self.height)
        bgfx.touch(0)
        bgfx.dbg_text_clear(0, False)
        bgfx.dbg_text_image(
            int(max(self.width / 2 / 8, 20)) - 20,
            int(max(self.height / 2 / 16, 6)) - 6,
            40,
            12,
            python_image.logo,
            160,
        )

        stats = bgfx.get_stats()

        bgfx.dbg_text_printf(
            1,
            1,
            0x0F,
            "Color can be changed with ANSI \x1b[9;me\x1b[10;ms\x1b[11;mc\x1b[12;ma\x1b[13;mp\x1b[14;me\x1b[0m code too.",
        )
        bgfx.dbg_text_printf(
            80,
            1,
            0x0F,
            "\x1b[;0m    \x1b[;1m    \x1b[; 2m    \x1b[; 3m    \x1b[; 4m    \x1b[; 5m    \x1b[; 6m    \x1b[; 7m    \x1b[0m",
        )
        bgfx.dbg_text_printf(
            80,
            2,
            0x0F,
            "\x1b[;8m    \x1b[;9m    \x1b[;10m    \x1b[;11m    \x1b[;12m    \x1b[;13m    \x1b[;14m    \x1b[;15m    \x1b[0m",
        )
        bgfx.dbg_text_printf(
            1,
            2,
            0x0F,
            f"Backbuffer {stats.width}W x {stats.height}H in pixels, debug text {stats.text_width}W x {stats.text_height}H in characters.",
        )

        bgfx.frame()
Beispiel #9
0
    def shutdown(self):
        self.fluid_simulator.destroy()
        self.particle_area.destroy()

        ImGuiExtra.imguiDestroy()

        bgfx.destroy(self.index_buffer)
        bgfx.destroy(self.vertex_buffer)
        bgfx.destroy(self.output_texture)
        bgfx.destroy(self.cs_program)
        bgfx.destroy(self.texture_uniform)
        bgfx.destroy(self.main_program)

        bgfx.shutdown()
Beispiel #10
0
    def update(self, dt):
        mouse_x, mouse_y, buttons_states = self.get_mouse_state()
        ImGuiExtra.imguiBeginFrame(
            int(mouse_x), int(mouse_y), buttons_states, 0, self.fb_width, self.fb_height
        )
        show_properties_dialog(self.fluid_simulator, self.particle_area, self.hidpi)

        ImGuiExtra.imguiEndFrame()

        vel_y = random.uniform(-0.08, 0.08)
        vel_x = random.uniform(-0.01, 0.1)
        strength = random.uniform(0.01, 0.09)

        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_velocity((0.23, 0.5), (vel_x, vel_y), 34.0)
        self.fluid_simulator.add_circle_obstacle((0.5, 0.7), 30.0)
        self.fluid_simulator.add_triangle_obstacle(
            (0.65, 0.5), (0.42, 0.5), (0.42, 0.39)
        )
        self.fluid_simulator.add_triangle_obstacle(
            (0.65, 0.06), (0.65, 0.39), (0.42, 0.39)
        )
        self.fluid_simulator.update(dt)

        self.particle_area.add_particles((0.2, 0.5), 220.0, strength)
        self.particle_area.update(dt)

        bgfx.dispatch(0, self.cs_program, self.fb_width // 16, self.fb_height // 16)
        bgfx.setTexture(0, self.texture_uniform, self.output_texture)
        bgfx.setState(BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A)
        bgfx.submit(0, self.main_program, 0, False)
        bgfx.frame()
Beispiel #11
0
    def init(self, platform_data):
        self.init_conf.platform_data = platform_data
        bgfx.render_frame()
        bgfx.init(self.init_conf)
        bgfx.reset(
            self.width,
            self.height,
            BGFX_RESET_VSYNC,
            self.init_conf.resolution.format,
        )

        bgfx.set_debug(BGFX_DEBUG_TEXT)
        bgfx.set_view_clear(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.create_vertex_buffer(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.create_index_buffer(ib_memory))

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

        ImGuiExtra.imgui_create()
Beispiel #12
0
 def shutdown(self):
     ImGuiExtra.imgui_destroy()
     self.mesh.destroy()
     bgfx.destroy(self.main_program)
     bgfx.shutdown()
Beispiel #13
0
 def shutdown(self):
     ImGuiExtra.imgui_destroy()
     bgfx.shutdown()
Beispiel #14
0
    def init(self, platform_data):
        self.init_conf.platformData = 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, 0x443355FF, 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.0
        self.fluid_simulator.iterations = 100

        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
        )

        # Create program from shaders.
        self.main_program = bgfx.createProgram(
            load_shader(
                "demo.VertexShader.vert", ShaderType.VERTEX, root_path=root_path
            ),
            load_shader(
                "demo.FragmentShader.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.imguiCreate(36.0)