michaelfairley / rust-imgui-opengl-renderer

OpenGL (3+) rendering for imgui-rs
37 stars 32 forks source link

Undesired window transparency #18

Closed subtle-byte closed 1 year ago

subtle-byte commented 3 years ago

The problem demo: image

vscode is visible through the window when imgui's window is semi-transparent.

The #17 fixes this problem.

michaelfairley commented 3 years ago

I don't think this is this library's fault. Are you using 1.0 for the alpha in your ClearColor?

This code is based off of the official Dear ImGui OpenGL backend, and I'm hesitant to depart from what they're doing without a clear understanding that it's the wrong thing.

subtle-byte commented 3 years ago

I just tested your example app and there is no problem.

So maybe the problem is when glutin is used (also I run it with wayland), or there is a problem in my code:

My code (Yeah, I use `1.0` for the alpha in `ClearColor`) ```rust use imgui::im_str; pub struct System { pub event_loop: glutin::event_loop::EventLoop<()>, pub windowed_context: glutin::WindowedContext, pub imgui_context: imgui::Context, pub winit_platform: imgui_winit_support::WinitPlatform, pub renderer: imgui_opengl_renderer::Renderer, } mod gl { include!(concat!(env!("OUT_DIR"), "/bindings.rs")); } fn init(title: &str) -> System { let event_loop = glutin::event_loop::EventLoop::new(); let windowed_context = { let window_builder = glutin::window::WindowBuilder::new() .with_title(title.to_owned()); let context_builder = glutin::ContextBuilder::new() .with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 0))) // .with_gl_profile(glutin::GlProfile::Compatibility) .with_vsync(true); context_builder.build_windowed(window_builder, &event_loop).unwrap() }; let windowed_context = unsafe { windowed_context.make_current().unwrap() }; let mut imgui_context = imgui::Context::create(); let mut winit_platform = imgui_winit_support::WinitPlatform::init(&mut imgui_context); winit_platform.attach_window( imgui_context.io_mut(), windowed_context.window(), imgui_winit_support::HiDpiMode::Rounded ); let load_gl_function = |symbol| windowed_context.get_proc_address(symbol) as *const _; gl::load_with(load_gl_function); let renderer = imgui_opengl_renderer::Renderer::new(&mut imgui_context, load_gl_function); System {event_loop, windowed_context, imgui_context, winit_platform, renderer} } impl System { pub fn main_loop(self, mut run_ui: F) { use glutin::event::Event; let System { event_loop, windowed_context, mut imgui_context, mut winit_platform, renderer, } = self; event_loop.run(move |event, _, control_flow| match event { Event::MainEventsCleared => { windowed_context.window().request_redraw(); } Event::RedrawRequested(_) => { winit_platform .prepare_frame(imgui_context.io_mut(), windowed_context.window()) .unwrap(); let mut ui = imgui_context.frame(); let mut run = true; run_ui(&mut run, &mut ui); if !run { *control_flow = glutin::event_loop::ControlFlow::Exit; } winit_platform.prepare_render(&ui, windowed_context.window()); renderer.render(ui); windowed_context.swap_buffers().unwrap(); } Event::WindowEvent { event: glutin::event::WindowEvent::Resized(physical_size), .. } => { winit_platform.handle_event(imgui_context.io_mut(), windowed_context.window(), &event); windowed_context.resize(physical_size); } Event::WindowEvent { event: glutin::event::WindowEvent::CloseRequested, .. } => *control_flow = glutin::event_loop::ControlFlow::Exit, event => { winit_platform.handle_event(imgui_context.io_mut(), windowed_context.window(), &event); } }) } } fn main() { let system = init(file!()); unsafe { gl::BlendFuncSeparate(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA, gl::ZERO, gl::ONE); gl::Enable(gl::BLEND); } let mut a = 0.5; system.main_loop(move |_, ui| { unsafe { gl::ClearColor(0.2, 0.2, 0.2, 1.0); gl::Clear(gl::COLOR_BUFFER_BIT); gl::Begin(gl::TRIANGLES); gl::Color4f(1.0, 0.0, 0.0, 0.5); gl::Vertex3f(0.0, 0.0, 0.0); gl::Vertex3f(1.0, 0.0, 0.0); gl::Vertex3f(0.0, 1.0, 0.0); gl::Color4f(0.0, 1.0, 0.0, a); gl::Vertex3f(0.0, -0.5, 0.0); gl::Vertex3f(1.0, -0.5, 0.0); gl::Vertex3f(0.0, 0.5, 0.0); gl::End(); } imgui::Window::new(im_str!("Hello world")) .size([300.0, 110.0], imgui::Condition::FirstUseEver) .build(ui, || { ui.text(im_str!("Hello world!")); ui.text(im_str!("こんにちは世界!")); ui.text(im_str!("This...is...imgui-rs!")); ui.separator(); let mouse_pos = ui.io().mouse_pos; ui.text(format!( "Mouse Position: ({:.1},{:.1})", mouse_pos[0], mouse_pos[1] )); imgui::Slider::new(im_str!("dlfjklkj")) .range(0.0..=1.0) .build(ui, &mut a); }); }); } ```
subtle-byte commented 3 years ago

I tested imgui-rs's example (imgui-glium-renderer + glium + imgui-winit-support + winit) - there is the same problem on Wayland, and no on X11.

Also I've met the same transparency in https://github.com/imgui-rs/imgui-rs/issues/246 's screenshot.