egui support
- New Resource, EguiQueue: Utility to queue egui ui builders - New Rendering System, EguiRenderer: Utilizes a modified egui_glium library to render egui UI - Adjusted RenderingSystem trait and RenderingContext structure to provide event_loop and egui_queue. - Various minor adjusments
This commit is contained in:
parent
e88ce258ce
commit
ce24354f3b
18 changed files with 806 additions and 204 deletions
|
|
@ -9,6 +9,8 @@ use raidillon_assets::include_shader;
|
|||
pub use raidillon_platform::Camera;
|
||||
use glam::Vec3;
|
||||
use glium::uniforms::{MagnifySamplerFilter, MinifySamplerFilter, SamplerWrapFunction};
|
||||
use winit::event::Event;
|
||||
use winit::event_loop::EventLoop;
|
||||
use raidillon_ecs::{Transform, ModelID};
|
||||
use raidillon_ecs::components::ModelHandle;
|
||||
use crate::model::Model;
|
||||
|
|
@ -21,7 +23,7 @@ pub struct BasicMeshRenderingSystem {
|
|||
}
|
||||
|
||||
impl RenderingSystem for BasicMeshRenderingSystem {
|
||||
fn initialize(display: &Display<WindowSurface>, _window: Arc<Mutex<glium::winit::window::Window>>) -> Self {
|
||||
fn initialize(display: &Display<WindowSurface>, _window: Arc<Mutex<glium::winit::window::Window>>, event_loop: &EventLoop<()>) -> Self {
|
||||
const VERT_SRC: &str = include_shader!("gl_textured.vert");
|
||||
const FRAG_SRC: &str = include_shader!("gl_textured.frag");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
use glium::Display;
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use imgui::{Context as ImguiContext};
|
||||
use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
||||
use imgui_glium_renderer::Renderer as ImguiGliumRenderer;
|
||||
use winit::window::Window;
|
||||
use winit::event::Event;
|
||||
use glium::Frame;
|
||||
use crate::RenderingSystem;
|
||||
use crate::system::RenderingContext;
|
||||
|
||||
pub struct ImguiBridge {
|
||||
imgui: ImguiContext,
|
||||
platform: WinitPlatform,
|
||||
renderer: ImguiGliumRenderer,
|
||||
last_frame: Instant,
|
||||
rendered_this_frame: bool,
|
||||
}
|
||||
|
||||
impl RenderingSystem for ImguiBridge {
|
||||
fn handle_event(&mut self, window: Arc<Mutex<Window>>, event: Event<()>) {
|
||||
let window = window.lock().unwrap();
|
||||
self.platform.handle_event(self.imgui.io_mut(), &*window, &event);
|
||||
}
|
||||
|
||||
fn prepare_frame(&mut self, window: Arc<Mutex<Window>>) {
|
||||
self.rendered_this_frame = false;
|
||||
let now = Instant::now();
|
||||
self.imgui.io_mut().update_delta_time(now - self.last_frame);
|
||||
self.last_frame = now;
|
||||
let window = window.lock().unwrap();
|
||||
self.platform
|
||||
.prepare_frame(self.imgui.io_mut(), &*window)
|
||||
.expect("Failed to prepare frame");
|
||||
}
|
||||
|
||||
fn render(&mut self, ctx: &mut RenderingContext) {
|
||||
if self.rendered_this_frame { return; }
|
||||
self.rendered_this_frame = true;
|
||||
|
||||
let ui = self.imgui.frame();
|
||||
ctx.debug_ui_buffer.write_buffer(&ui);
|
||||
|
||||
{
|
||||
let window = ctx.window.lock().unwrap();
|
||||
self.platform.prepare_render(&ui, &*window);
|
||||
}
|
||||
let draw_data = self.imgui.render();
|
||||
if draw_data.total_vtx_count == 0 && draw_data.total_idx_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.renderer.render(ctx.target, draw_data).expect("imgui rendering failed");
|
||||
}
|
||||
|
||||
fn initialize(display: &Display<WindowSurface>, window: Arc<Mutex<Window>>) -> Self {
|
||||
let mut imgui = ImguiContext::create();
|
||||
imgui.set_ini_filename(None);
|
||||
let mut platform = WinitPlatform::new(&mut imgui);
|
||||
let window = window.lock().unwrap();
|
||||
platform.attach_window(imgui.io_mut(), &*window, HiDpiMode::Default);
|
||||
imgui.fonts().add_font(&[imgui::FontSource::DefaultFontData { config: None }]);
|
||||
let renderer = ImguiGliumRenderer::new(&mut imgui, display).unwrap();
|
||||
|
||||
Self {
|
||||
imgui,
|
||||
platform,
|
||||
renderer,
|
||||
last_frame: Instant::now(),
|
||||
rendered_this_frame: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
46
glium_platform/src/render/egui.rs
Normal file
46
glium_platform/src/render/egui.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
use egui::ViewportId;
|
||||
use glium::{Display, Frame};
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use winit::window::Window;
|
||||
use crate::RenderingSystem;
|
||||
use crate::system::RenderingContext;
|
||||
use egui_glium::EguiGlium;
|
||||
use winit::event::{Event, WindowEvent};
|
||||
use winit::event_loop::EventLoop;
|
||||
|
||||
pub struct EguiRenderer {
|
||||
egui_glium: EguiGlium,
|
||||
}
|
||||
|
||||
impl RenderingSystem for EguiRenderer {
|
||||
fn initialize(display: &Display<WindowSurface>, window: Arc<Mutex<Window>>, event_loop: &EventLoop<()>) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let window = window.lock().unwrap();
|
||||
let egui_glium = EguiGlium::new(ViewportId::ROOT, &display, &window, &event_loop);
|
||||
|
||||
Self { egui_glium }
|
||||
}
|
||||
|
||||
fn render(&mut self, ctx: &mut RenderingContext) {
|
||||
let window = ctx.window.lock().unwrap();
|
||||
|
||||
self.egui_glium.run(&window, |egui_ctx| {
|
||||
ctx.egui_queue.borrow_mut().run(egui_ctx);
|
||||
});
|
||||
|
||||
self.egui_glium.paint(ctx.display, ctx.target);
|
||||
}
|
||||
|
||||
fn handle_event(&mut self, window: Arc<Mutex<Window>>, event: Event<()>) {
|
||||
let window = window.lock().unwrap();
|
||||
match event {
|
||||
Event::WindowEvent { event, .. } => {
|
||||
let _ = self.egui_glium.on_event(&window, &event);
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
mod basic;
|
||||
pub mod debug_ui;
|
||||
mod skybox;
|
||||
mod egui;
|
||||
|
||||
pub use basic::BasicMeshRenderingSystem;
|
||||
pub use skybox::SkyboxRenderingSystem;
|
||||
pub use egui::EguiRenderer;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use glium::index::PrimitiveType;
|
|||
use glium::texture::{RawImage2d, SrgbTexture2d, Texture2d};
|
||||
use glium::uniform;
|
||||
use glam::{Mat4, Vec2, Vec3};
|
||||
use winit::event_loop::EventLoop;
|
||||
use raidillon_assets::include_shader;
|
||||
use crate::system::RenderingContext;
|
||||
use crate::RenderingSystem;
|
||||
|
|
@ -109,7 +110,7 @@ impl SkyboxRenderingSystem {
|
|||
}
|
||||
|
||||
impl RenderingSystem for SkyboxRenderingSystem {
|
||||
fn initialize(display: &Display<WindowSurface>, _window: Arc<Mutex<glium::winit::window::Window>>) -> Self {
|
||||
fn initialize(display: &Display<WindowSurface>, _window: Arc<Mutex<glium::winit::window::Window>>, event_loop: &EventLoop<()>) -> Self {
|
||||
const VERT_SRC: &str = include_shader!("skybox.vert");
|
||||
const FRAG_SRC: &str = include_shader!("skybox.frag");
|
||||
let program = Program::from_source(display, VERT_SRC, FRAG_SRC, None).unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue