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