首页 > 解决方案 > 如何将 gfx-rs (opengl) 场景渲染到 ImGui-rs 窗口中?

问题描述

我是 Rust 和 gfx-rs 的新手,我想渲染一个纹理并将该纹理用作 imgui 的图像,几个小时后:-) 我已经成功地使用 factory.view_texture_as_render_target 创建了一个纹理,但我没有知道如何在 imgui 中将其用作纹理;( ps :我曾经使用 c++ 作为https://gamedev.stackexchange.com/questions/140693/how-can-i-render-an-opengl-scene-into-an-imgui-window?rq=1 )

其实这就是我想做的,我试过了,但它不起作用。似乎纹理不被认为是同一类型(或者我做错了方式:因为我是生锈的初学者),如果看看下面的代码,我会很感激,也许你可以给我其他的建议。Ps:在 C++ 中它使用指针。但是在 rust 中,我不太习惯打字系统。

它基于这个例子:https ://github.com/WimbledonLabs/imgui_gfx_example 谢谢

pub fn main() {
    let mut events_loop = glutin::EventsLoop::new();
    //window && glcontext
    let windowbuilder = glutin::WindowBuilder::new()
        .with_title("Triangle Example".to_string())
        .with_dimensions(LogicalSize::from((512, 512)));

    let contextbuilder = glutin::ContextBuilder::new()
        .with_gl(GlRequest::Specific(OpenGl, (3, 2)))
        .with_vsync(true);
    let (window, mut device, mut factory, mut color_view, mut depth_view) =
        gfx_glutin::init::<ColorFormat, DepthFormat>(
            windowbuilder,
            contextbuilder,
            &events_loop,
        );


    let sampler = factory.create_sampler_linear();
    let texture = gfx_load_texture(&mut factory);

    let (vertex_buffer, slice) =
        factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
    let transform_buffer = factory.create_constant_buffer(1);






let viewer_size = (200, 200);
let (width, height) = viewer_size;

//let _depth = factory.create_depth_stencil_view_only(width as u16, height as u16).unwrap();

let levels = 1;
let kind = gfx::texture::Kind::D2(width as u16, height as u16, gfx::texture::AaMode::Single);
//providing the storage configuration and requesting the capabilities
let texture_buf = factory.create_texture::<gfx::format::R8_G8_B8_A8>(kind, levels,  gfx::memory::Bind::SHADER_RESOURCE |  gfx::memory::Bind::RENDER_TARGET,  gfx::memory::Usage::Data, None).unwrap();

// creating an RTV for level 0
let target_tex = factory.view_texture_as_render_target::<ColorFormat>(&texture_buf, 0, None).unwrap();      

// creating an SRV for the whole mipmap range
let mut resource_tex = factory.view_texture_as_shader_resource::<ColorFormat>(&texture_buf, (0, levels-1), gfx::format::Swizzle::new()).unwrap(); 






    let data = pipe::Data {
        vbuf: vertex_buffer,
        tex: (texture, sampler),
        transform: transform_buffer,
        //out: color_view.clone(),
        out: target_tex,
    };


let mut text_for_imgui = imgui::Textures::new();
let texture_for_imgui = text_for_imgui.insert(&texture_buf);
.
.
.
let ui = imgui.frame(frame_size, delta_s);
ui.image(texture_for_imgui , img_size);
.
.
.

标签: openglimgui

解决方案


推荐阅读