mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-11 21:48:36 +00:00
Merge pull request #341 from cosmicchipsocket/glium-change-texture-filter
imgui-glium-renderer: User-selectable texture filters per-texture
This commit is contained in:
commit
7d95b2d686
@ -6,10 +6,12 @@ use std::rc::Rc;
|
||||
use glium::{
|
||||
backend::Facade,
|
||||
texture::{ClientFormat, RawImage2d},
|
||||
uniforms::{MagnifySamplerFilter, MinifySamplerFilter, SamplerBehavior},
|
||||
Texture2d,
|
||||
};
|
||||
use image::{jpeg::JpegDecoder, ImageDecoder};
|
||||
use imgui::*;
|
||||
use imgui_glium_renderer::Texture;
|
||||
|
||||
mod support;
|
||||
|
||||
@ -28,7 +30,7 @@ impl CustomTexturesApp {
|
||||
fn register_textures<F>(
|
||||
&mut self,
|
||||
gl_ctx: &F,
|
||||
textures: &mut Textures<Rc<Texture2d>>,
|
||||
textures: &mut Textures<Texture>,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
where
|
||||
F: Facade,
|
||||
@ -55,7 +57,15 @@ impl CustomTexturesApp {
|
||||
format: ClientFormat::U8U8U8,
|
||||
};
|
||||
let gl_texture = Texture2d::new(gl_ctx, raw)?;
|
||||
let texture_id = textures.insert(Rc::new(gl_texture));
|
||||
let texture = Texture {
|
||||
texture: Rc::new(gl_texture),
|
||||
sampler: SamplerBehavior {
|
||||
magnify_filter: MagnifySamplerFilter::Linear,
|
||||
minify_filter: MinifySamplerFilter::Linear,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
let texture_id = textures.insert(texture);
|
||||
|
||||
self.my_texture_id = Some(texture_id);
|
||||
}
|
||||
@ -86,7 +96,7 @@ impl CustomTexturesApp {
|
||||
}
|
||||
|
||||
impl Lenna {
|
||||
fn new<F>(gl_ctx: &F, textures: &mut Textures<Rc<Texture2d>>) -> Result<Self, Box<dyn Error>>
|
||||
fn new<F>(gl_ctx: &F, textures: &mut Textures<Texture>) -> Result<Self, Box<dyn Error>>
|
||||
where
|
||||
F: Facade,
|
||||
{
|
||||
@ -104,7 +114,15 @@ impl Lenna {
|
||||
format: ClientFormat::U8U8U8,
|
||||
};
|
||||
let gl_texture = Texture2d::new(gl_ctx, raw)?;
|
||||
let texture_id = textures.insert(Rc::new(gl_texture));
|
||||
let texture = Texture {
|
||||
texture: Rc::new(gl_texture),
|
||||
sampler: SamplerBehavior {
|
||||
magnify_filter: MagnifySamplerFilter::Linear,
|
||||
minify_filter: MinifySamplerFilter::Linear,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
let texture_id = textures.insert(texture);
|
||||
Ok(Lenna {
|
||||
texture_id,
|
||||
size: [width as f32, height as f32],
|
||||
|
||||
@ -5,7 +5,9 @@ use glium::backend::{Context, Facade};
|
||||
use glium::index::{self, PrimitiveType};
|
||||
use glium::program::ProgramChooserCreationError;
|
||||
use glium::texture::{ClientFormat, MipmapsOption, RawImage2d, TextureCreationError};
|
||||
use glium::uniforms::{MagnifySamplerFilter, MinifySamplerFilter, SamplerWrapFunction};
|
||||
use glium::uniforms::{
|
||||
MagnifySamplerFilter, MinifySamplerFilter, Sampler, SamplerBehavior, SamplerWrapFunction,
|
||||
};
|
||||
use glium::{
|
||||
program, uniform, vertex, Blend, DrawError, DrawParameters, IndexBuffer, Program, Rect,
|
||||
Surface, Texture2d, VertexBuffer,
|
||||
@ -86,11 +88,16 @@ impl From<DrawError> for RendererError {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Texture {
|
||||
pub texture: Rc<Texture2d>,
|
||||
pub sampler: SamplerBehavior,
|
||||
}
|
||||
|
||||
pub struct Renderer {
|
||||
ctx: Rc<Context>,
|
||||
program: Program,
|
||||
font_texture: Texture2d,
|
||||
textures: Textures<Rc<Texture2d>>,
|
||||
font_texture: Texture,
|
||||
textures: Textures<Texture>,
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
@ -115,10 +122,10 @@ impl Renderer {
|
||||
self.font_texture = upload_font_texture(ctx.fonts(), &self.ctx)?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn textures(&mut self) -> &mut Textures<Rc<Texture2d>> {
|
||||
pub fn textures(&mut self) -> &mut Textures<Texture> {
|
||||
&mut self.textures
|
||||
}
|
||||
fn lookup_texture(&self, texture_id: TextureId) -> Result<&Texture2d, RendererError> {
|
||||
fn lookup_texture(&self, texture_id: TextureId) -> Result<&Texture, RendererError> {
|
||||
if texture_id.id() == usize::MAX {
|
||||
Ok(&self.font_texture)
|
||||
} else if let Some(texture) = self.textures.get(texture_id) {
|
||||
@ -187,6 +194,8 @@ impl Renderer {
|
||||
&& clip_rect[2] >= 0.0
|
||||
&& clip_rect[3] >= 0.0
|
||||
{
|
||||
let texture = self.lookup_texture(texture_id)?;
|
||||
|
||||
target.draw(
|
||||
&vtx_buffer,
|
||||
&idx_buffer
|
||||
@ -195,10 +204,7 @@ impl Renderer {
|
||||
&self.program,
|
||||
&uniform! {
|
||||
matrix: matrix,
|
||||
tex: self.lookup_texture(texture_id)?.sampled()
|
||||
.minify_filter(MinifySamplerFilter::Linear)
|
||||
.magnify_filter(MagnifySamplerFilter::Linear)
|
||||
.wrap_function(SamplerWrapFunction::BorderClamp)
|
||||
tex: Sampler(texture.texture.as_ref(), texture.sampler)
|
||||
},
|
||||
&DrawParameters {
|
||||
blend: Blend::alpha_blending(),
|
||||
@ -230,7 +236,7 @@ impl Renderer {
|
||||
fn upload_font_texture(
|
||||
mut fonts: imgui::FontAtlasRefMut,
|
||||
ctx: &Rc<Context>,
|
||||
) -> Result<Texture2d, RendererError> {
|
||||
) -> Result<Texture, RendererError> {
|
||||
let texture = fonts.build_rgba32_texture();
|
||||
let data = RawImage2d {
|
||||
data: Cow::Borrowed(texture.data),
|
||||
@ -240,7 +246,19 @@ fn upload_font_texture(
|
||||
};
|
||||
let font_texture = Texture2d::with_mipmaps(ctx, data, MipmapsOption::NoMipmap)?;
|
||||
fonts.tex_id = TextureId::from(usize::MAX);
|
||||
Ok(font_texture)
|
||||
Ok(Texture {
|
||||
texture: Rc::new(font_texture),
|
||||
sampler: SamplerBehavior {
|
||||
minify_filter: MinifySamplerFilter::Linear,
|
||||
magnify_filter: MagnifySamplerFilter::Linear,
|
||||
wrap_function: (
|
||||
SamplerWrapFunction::BorderClamp,
|
||||
SamplerWrapFunction::BorderClamp,
|
||||
SamplerWrapFunction::BorderClamp,
|
||||
),
|
||||
..Default::default()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn compile_default_program<F: Facade>(facade: &F) -> Result<Program, ProgramChooserCreationError> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user