如何将gfx-rs(opengl)场景渲染到ImGui-rs窗口中?

时间:2018-12-23 16:05:29

标签: opengl imgui

我是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 ++中,它使用指针。但是在生锈的情况下,我不太习惯打字系统。

它基于以下示例: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);
.
.
.

0 个答案:

没有答案