From a1dc4e124c549cf5cff731d07d6a845b47aa43e3 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 22 Apr 2020 00:15:40 -0400 Subject: [PATCH] shows the texture in an opengl-backed imgui renderer! --- Cargo.lock | 71 +++++++++++++++++++++++++ Cargo.toml | 3 ++ src/graphics_imgui.rs | 119 +++++++++++++++++++++++++++++++++++++++++- src/main.rs | 3 ++ 4 files changed, 195 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 306ec92f50..ac6470f20a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,6 +505,16 @@ dependencies = [ "gl_generator 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "gl_generator" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "khronos_api 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "gl_generator" version = "0.10.0" @@ -707,6 +717,24 @@ dependencies = [ "imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "imgui-opengl-renderer" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gl_generator 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "imgui-sdl2" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "imgui-sys" version = "0.3.0" @@ -778,6 +806,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "khronos_api" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "khronos_api" version = "3.1.0" @@ -1369,6 +1402,26 @@ name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sdl2" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2-sys 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sdl2-sys" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "semver" version = "0.9.0" @@ -1445,12 +1498,15 @@ dependencies = [ "image 0.23.2 (registry+https://github.com/rust-lang/crates.io-index)", "imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "imgui-glium-renderer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "imgui-opengl-renderer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "imgui-sdl2 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "imgui-winit-support 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "k4a-sys 0.2.0", "libc 0.2.68 (registry+https://github.com/rust-lang/crates.io-index)", "obj 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "opencv 0.33.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "sdl2 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "winit 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1718,6 +1774,14 @@ name = "xdg" version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "xml-rs" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "xml-rs" version = "0.8.1" @@ -1787,6 +1851,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum gl_generator 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a0ffaf173cf76c73a73e080366bf556b4776ece104b06961766ff11449f38604" "checksum gl_generator 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ca98bbde17256e02d17336a6bdb5a50f7d0ccacee502e191d3e3d0ec2f96f84a" "checksum gl_generator 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d" +"checksum gl_generator 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a795170cbd85b5a7baa58d6d7525cae6a03e486859860c220f7ebbbdd379d0a" "checksum glfw 0.37.0 (registry+https://github.com/rust-lang/crates.io-index)" = "36ed2a51f1e1c5722b7a3724fc146cbfe27297584a4fbb2797ce487f62bb0320" "checksum glfw-sys 3.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0b567b13b593ec58ea912b2658ee6230ffe20a069fa5b771800acc69bb3a157e" "checksum glium 0.26.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a46e88b2c64cfa63d54ed54dc2a0d25136470eb1721e10be49d857ab583f8663" @@ -1803,6 +1868,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum image 0.23.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9062b90712d25bc6bb165d110aa59c6b47c849246e341e7b86a98daff9d49f60" "checksum imgui 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8a2d28cee2982d4dab4dd954c23be339ab1f98be4ff5a13ecd8fbfb47cdfa23a" "checksum imgui-glium-renderer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d50773138db17dbedfa04feb3f49a2b220365938746882d349e06cb0a4c187e9" +"checksum imgui-opengl-renderer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fe0567f7e5fb8cbf74a8469040ae3cebed0d75823d6d991803dbdd81661a5d99" +"checksum imgui-sdl2 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e021b0ecd228c598f6ea39ba1aca7550e02cfa035c7e849ff393d082227bf70" "checksum imgui-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "21a8693e0f66ecd2ec10e2f0ed4dc4519071495f350e6d8ddaf3e70351229257" "checksum imgui-winit-support 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5685c7d17aee9555377f9c4f7b6fac96899a7015f6b41950c613a3df034aff08" "checksum inflate 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1cdb29978cc5797bd8dcc8e5bf7de604891df2a8dc576973d71a281e916db2ff" @@ -1811,6 +1878,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum jobserver 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2" "checksum jpeg-decoder 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0256f0aec7352539102a9efbcb75543227b7ab1117e0f95450023af730128451" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum khronos_api 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "037ab472c33f67b5fbd3e9163a2645319e5356fcd355efa6d4eb7fff4bbcb554" "checksum khronos_api 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f" @@ -1883,6 +1951,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" "checksum scoped_threadpool 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" "checksum scopeguard 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +"checksum sdl2 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1f74124048ea86b5cd50236b2443f6f57cf4625a8e8818009b4e50dbb8729a43" +"checksum sdl2-sys 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c2e1deb61ff274d29fb985017d4611d4004b113676eaa9c06754194caf82094e" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" "checksum shared_library 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5a9e7e0f2bfae24d8a5b5a66c5b257a83c7412304311512a0c054cd5e619da11" @@ -1924,4 +1994,5 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum x11-dl 2.18.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2bf981e3a5b3301209754218f962052d4d9ee97e478f4d26d4a6eced34c1fef8" "checksum xcb 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de" "checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" +"checksum xml-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2" "checksum xml-rs 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9afd061c1c7c8ef0a3d337fbf76fc8c098bd0dfefd4adaafef25a559352f2031" diff --git a/Cargo.toml b/Cargo.toml index 6adbc83fda..96bb6168c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,10 +21,13 @@ grr = "0.7.0" # purports to be low-level OpenGL image = "0.23.2" imgui = "0.3.0" # OpenGL windowing toolkit (experimental bindings) imgui-glium-renderer = "0.3.0" +imgui-opengl-renderer = "0.7.0" +imgui-sdl2 = "0.9.0" # for imgui-opengl-renderer imgui-winit-support = "0.3.1" k4a-sys = { path = "/home/bt/dev/3rd/k4a-sys" } # k4a-sys = "0.2.0" libc = "0.2.68" obj = "0.9.1" opencv = "0.33.1" rand = "0.7.3" +sdl2 = "0.33" # for imgui-opengl-renderer winit = "=0.21.0" # cross-platform window management, because imgui diff --git a/src/graphics_imgui.rs b/src/graphics_imgui.rs index 2bede5682c..11c8fb3255 100644 --- a/src/graphics_imgui.rs +++ b/src/graphics_imgui.rs @@ -18,6 +18,7 @@ use std::sync::Arc; use support; use std::path::Path; use std::ffi::c_void; +use std::time::Instant; fn load_texture(filename: &str) -> GLuint { let mut texture_id = 0; @@ -67,6 +68,122 @@ fn load_texture(filename: &str) -> GLuint { } pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4a_calibration_t) { + let sdl_context = sdl2::init().unwrap(); + let video = sdl_context.video().unwrap(); + + { + let gl_attr = video.gl_attr(); + gl_attr.set_context_profile(sdl2::video::GLProfile::Core); + gl_attr.set_context_version(3, 0); + } + + let window = video.window("rust-imgui-sdl2 demo", 1000, 1000) + .position_centered() + .resizable() + .opengl() + .allow_highdpi() + .build() + .unwrap(); + + let _gl_context = window.gl_create_context().expect("Couldn't create GL context"); + gl::load_with(|s| video.gl_get_proc_address(s) as _); + + + let mut imgui = imgui::Context::create(); + imgui.set_ini_filename(None); + + let gl_texture_id = load_texture("n64logo.png"); + let imgui_texture_id = TextureId::from(gl_texture_id as usize); + let mut visualizer = PointCloudVisualizer::new( + true, + calibration_data + ); + + + + let mut imgui_sdl2 = imgui_sdl2::ImguiSdl2::new(&mut imgui, &window); + + let renderer = imgui_opengl_renderer::Renderer::new(&mut imgui, |s| video.gl_get_proc_address(s) as _); + + let mut event_pump = sdl_context.event_pump().unwrap(); + + let mut last_frame = Instant::now(); + + + 'running: loop { + use sdl2::event::Event; + use sdl2::keyboard::Keycode; + + for event in event_pump.poll_iter() { + imgui_sdl2.handle_event(&mut imgui, &event); + if imgui_sdl2.ignore_event(&event) { continue; } + + match event { + Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { + break 'running + }, + _ => {} + } + } + + + imgui_sdl2.prepare_frame(imgui.io_mut(), &window, &event_pump.mouse_state()); + + let now = Instant::now(); + let delta = now - last_frame; + let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0; + last_frame = now; + imgui.io_mut().delta_time = delta_s; + + let ui = imgui.frame(); + ui.show_demo_window(&mut true); + + ui.separator(); + let mouse_pos = ui.io().mouse_pos; + ui.text(format!( + "Mouse Position: ({:.1},{:.1})", + mouse_pos[0], mouse_pos[1] + )); + ui.separator(); + + Image::new(imgui_texture_id, [100.0, 100.0]).build(&ui); + + unsafe { + gl::ClearColor(0.2, 0.2, 0.2, 1.0); + gl::Clear(gl::COLOR_BUFFER_BIT); + } + + imgui_sdl2.prepare_render(&ui, &window); + renderer.render(ui); + + window.gl_swap_window(); + + ::std::thread::sleep(::std::time::Duration::new(0, 1_000_000_000u32 / 60)); + } + + + + + /* + + let renderer = imgui_opengl_renderer::Renderer::new( + &mut imgui_context, + |s| video.gl_get_proc_address(s) as _ + ); + + let mut imgui_sdl2 = imgui_sdl2::ImguiSdl2::new(&mut imgui, &window); + + let gl_texture_id = load_texture("n64logo.png"); + let imgui_texture_id = TextureId::from(gl_texture_id as usize); + + */ + + + +} + + +fn asdf(capture_provider: Arc, calibration_data: k4a_sys::k4a_calibration_t) { let system = support::init(file!()); let context = system.display.get_context(); @@ -83,6 +200,7 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 let mut last_read_framebuffer_binding = 0; let mut last_current_program = 0; let mut last_active_texture = 0; + unsafe { gl::GetIntegerv(gl::TEXTURE_BINDING_2D, &mut last_texture_id); gl::GetIntegerv(gl::ARRAY_BUFFER_BINDING, &mut last_array_buffer_id); @@ -98,7 +216,6 @@ pub fn run(capture_provider: Arc, calibration_data: k4a_sys::k4 let gl_texture_id = load_texture("n64logo.png"); let imgui_texture_id = TextureId::from(gl_texture_id as usize); - let mut visualizer = PointCloudVisualizer::new( true, calibration_data diff --git a/src/main.rs b/src/main.rs index 992d65b6c1..f0511d723d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,12 +11,15 @@ extern crate glutin; extern crate grr; extern crate image; extern crate imgui_glium_renderer; +extern crate imgui_opengl_renderer; +extern crate imgui_sdl2; extern crate imgui_winit_support; extern crate k4a_sys; extern crate libc; extern crate obj; extern crate opencv; extern crate rand; +extern crate sdl2; extern crate winit; use std::borrow::BorrowMut;