-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from BillyDM/glow
update to egui 0.19, use official glow painter
- Loading branch information
Showing
7 changed files
with
173 additions
and
841 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,83 @@ | ||
use baseview::Window; | ||
use egui::{Color32, Rgba}; | ||
use egui_glow::Painter; | ||
use raw_gl_context::GlContext; | ||
use std::sync::Arc; | ||
|
||
pub use raw_gl_context::GlConfig as RenderSettings; | ||
|
||
mod painter; | ||
use painter::Painter; | ||
|
||
pub struct Renderer { | ||
context: GlContext, | ||
glow_context: Arc<egui_glow::glow::Context>, | ||
painter: Painter, | ||
} | ||
|
||
impl Renderer { | ||
pub fn new(window: &Window, render_settings: RenderSettings, canvas_size: (u32, u32)) -> Self { | ||
pub fn new(window: &Window, render_settings: RenderSettings) -> Self { | ||
let context = GlContext::create(window, render_settings).unwrap(); | ||
|
||
context.make_current(); | ||
|
||
gl::load_with(|s| context.get_proc_address(s) as _); | ||
let glow_context = Arc::new(unsafe { | ||
egui_glow::glow::Context::from_loader_function(|s| context.get_proc_address(s)) | ||
}); | ||
|
||
let painter = Painter::new(canvas_size.0, canvas_size.1); | ||
let painter = egui_glow::Painter::new(Arc::clone(&glow_context), None, "") | ||
.map_err(|error| { | ||
eprintln!("error occurred in initializing painter:\n{}", error); | ||
}) | ||
.unwrap(); | ||
|
||
context.make_not_current(); | ||
|
||
Self { context, painter } | ||
Self { | ||
context, | ||
glow_context, | ||
painter, | ||
} | ||
} | ||
|
||
pub fn render( | ||
&mut self, | ||
bg_color: Rgba, | ||
clipped_meshes: Vec<egui::ClippedMesh>, | ||
egui_texture: &egui::Texture, | ||
bg_color: egui::Rgba, | ||
canvas_width: u32, | ||
canvas_height: u32, | ||
pixels_per_point: f32, | ||
egui_ctx: &mut egui::Context, | ||
shapes: &mut Vec<egui::epaint::ClippedShape>, | ||
textures_delta: &mut egui::TexturesDelta, | ||
) { | ||
let shapes = std::mem::take(shapes); | ||
let mut textures_delta = std::mem::take(textures_delta); | ||
|
||
self.context.make_current(); | ||
|
||
unsafe { | ||
use egui_glow::glow::HasContext as _; | ||
self.glow_context | ||
.clear_color(bg_color.r(), bg_color.g(), bg_color.b(), bg_color.a()); | ||
self.glow_context.clear(egui_glow::glow::COLOR_BUFFER_BIT); | ||
} | ||
|
||
for (id, image_delta) in textures_delta.set { | ||
self.painter.set_texture(id, &image_delta); | ||
} | ||
|
||
let clipped_primitives = egui_ctx.tessellate(shapes); | ||
let dimensions: [u32; 2] = [canvas_width, canvas_height]; | ||
|
||
self.painter | ||
.paint_meshes(bg_color, clipped_meshes, egui_texture, pixels_per_point); | ||
.paint_primitives(dimensions, pixels_per_point, &clipped_primitives); | ||
|
||
for id in textures_delta.free.drain(..) { | ||
self.painter.free_texture(id); | ||
} | ||
|
||
self.context.swap_buffers(); | ||
self.context.make_not_current(); | ||
} | ||
} | ||
|
||
pub fn new_user_texture( | ||
&mut self, | ||
size: (usize, usize), | ||
srgba_pixels: &[Color32], | ||
filtering: bool, | ||
) -> egui::TextureId { | ||
self.painter.new_user_texture(size, srgba_pixels, filtering) | ||
} | ||
|
||
pub fn update_user_texture_data(&mut self, texture_id: egui::TextureId, pixels: &[Color32]) { | ||
self.painter.update_user_texture_data(texture_id, pixels) | ||
} | ||
|
||
pub fn update_window_size(&mut self, width: u32, height: u32) { | ||
self.painter.set_canvas_size(width, height); | ||
impl Drop for Renderer { | ||
fn drop(&mut self) { | ||
self.painter.destroy() | ||
} | ||
} |
Oops, something went wrong.