1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

WIP: move 3d rendering example into egui_demo_lib

This commit is contained in:
Emil Ernerfeldt
2022-03-20 23:01:40 +01:00
parent 4922b1912b
commit 3ff55be4be
11 changed files with 278 additions and 58 deletions

View File

@@ -52,6 +52,7 @@ winit = ["egui-winit", "glutin"]
egui = { version = "0.17.0", path = "../egui", default-features = false, features = [
"convert_bytemuck",
] }
epi = { version = "0.17.0", path = "../epi" }
bytemuck = "1.7"
glow = "0.11"

View File

@@ -6,6 +6,7 @@ use egui::{
emath::Rect,
epaint::{Color32, Mesh, Primitive, Vertex},
};
use epi::Renderer as _;
use glow::HasContext;
use memoffset::offset_of;
@@ -19,6 +20,31 @@ pub use glow::Context;
const VERT_SRC: &str = include_str!("shader/vertex.glsl");
const FRAG_SRC: &str = include_str!("shader/fragment.glsl");
// ----------------------------------------------------------------------------
#[derive(Copy, Clone)]
pub enum TextureFilter {
Linear,
Nearest,
}
impl Default for TextureFilter {
fn default() -> Self {
TextureFilter::Linear
}
}
impl TextureFilter {
pub(crate) fn glow_code(&self) -> u32 {
match self {
TextureFilter::Linear => glow::LINEAR,
TextureFilter::Nearest => glow::NEAREST,
}
}
}
// ----------------------------------------------------------------------------
/// An OpenGL painter using [`glow`].
///
/// This is responsible for painting egui and managing egui textures.
@@ -46,7 +72,6 @@ pub struct Painter {
textures: HashMap<egui::TextureId, glow::Texture>,
#[cfg(feature = "epi")]
next_native_tex_id: u64, // TODO: 128-bit texture space?
/// Stores outdated OpenGL textures that are yet to be deleted
@@ -56,23 +81,28 @@ pub struct Painter {
destroyed: bool,
}
#[derive(Copy, Clone)]
pub enum TextureFilter {
Linear,
Nearest,
}
impl Default for TextureFilter {
fn default() -> Self {
TextureFilter::Linear
impl epi::Renderer for Painter {
/// Access the shared glow context.
fn gl(&self) -> &std::rc::Rc<glow::Context> {
&self.gl
}
}
impl TextureFilter {
pub(crate) fn glow_code(&self) -> u32 {
match self {
TextureFilter::Linear => glow::LINEAR,
TextureFilter::Nearest => glow::NEAREST,
/// Get the [`glow::Texture`] bound to a [`egui::TextureId`].
fn texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
self.textures.get(&texture_id).copied()
}
fn register_native_texture(&mut self, native: glow::Texture) -> egui::TextureId {
self.assert_not_destroyed();
let id = egui::TextureId::User(self.next_native_tex_id);
self.next_native_tex_id += 1;
self.textures.insert(id, native);
id
}
fn replace_native_texture(&mut self, id: egui::TextureId, replacing: glow::Texture) {
if let Some(old_tex) = self.textures.insert(id, replacing) {
self.textures_to_destroy.push(old_tex);
}
}
}
@@ -226,7 +256,6 @@ impl Painter {
vertex_buffer,
element_array_buffer,
textures: Default::default(),
#[cfg(feature = "epi")]
next_native_tex_id: 1 << 32,
textures_to_destroy: Vec::new(),
destroyed: false,
@@ -234,11 +263,6 @@ impl Painter {
}
}
/// Access the shared glow context.
pub fn gl(&self) -> &std::rc::Rc<glow::Context> {
&self.gl
}
pub fn max_texture_side(&self) -> usize {
self.max_texture_side
}
@@ -403,7 +427,7 @@ impl Painter {
#[inline(never)] // Easier profiling
fn paint_mesh(&mut self, mesh: &Mesh) {
debug_assert!(mesh.is_valid());
if let Some(texture) = self.get_texture(mesh.texture_id) {
if let Some(texture) = self.texture(mesh.texture_id) {
unsafe {
self.gl
.bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer));
@@ -579,11 +603,6 @@ impl Painter {
}
}
/// Get the [`glow::Texture`] bound to a [`egui::TextureId`].
pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
self.textures.get(&texture_id).copied()
}
unsafe fn destroy_gl(&self) {
self.gl.delete_program(self.program);
for tex in self.textures.values() {
@@ -642,25 +661,6 @@ impl Drop for Painter {
}
}
#[cfg(feature = "epi")]
impl epi::NativeTexture for Painter {
type Texture = glow::Texture;
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
self.assert_not_destroyed();
let id = egui::TextureId::User(self.next_native_tex_id);
self.next_native_tex_id += 1;
self.textures.insert(id, native);
id
}
fn replace_native_texture(&mut self, id: egui::TextureId, replacing: Self::Texture) {
if let Some(old_tex) = self.textures.insert(id, replacing) {
self.textures_to_destroy.push(old_tex);
}
}
}
fn set_clip_rect(
gl: &glow::Context,
size_in_pixels: (u32, u32),