Texture

Struct Texture 

Source
pub struct Texture(pub SrgbTexture2d, pub [SamplerWrapFunction; 2]);
Expand description

Wrapper for 2D texture.

Tuple Fields§

§0: SrgbTexture2d§1: [SamplerWrapFunction; 2]

Implementations§

Source§

impl Texture

Source

pub fn new(texture: SrgbTexture2d) -> Texture

Creates a new Texture.

Source

pub fn empty<F>(factory: &mut F) -> Result<Self, TextureCreationError>
where F: Facade,

Returns empty texture.

Source

pub fn from_path<F, P>( factory: &mut F, path: P, flip: Flip, settings: &TextureSettings, ) -> Result<Self, String>
where F: Facade, P: AsRef<Path>,

Creates a texture from path.

Examples found in repository?
examples/image_test.rs (lines 21-26)
12fn main() {
13    let opengl = OpenGL::V3_2;
14    let (w, h) = (300, 300);
15    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: image_test", [w, h])
16        .exit_on_esc(true)
17        .graphics_api(opengl)
18        .build()
19        .unwrap();
20
21    let rust_logo = Texture::from_path(
22        window,
23        "assets/rust.png",
24        Flip::None,
25        &TextureSettings::new(),
26    )
27    .unwrap();
28
29    let mut g2d = Glium2d::new(opengl, window);
30    window.set_lazy(true);
31    while let Some(e) = window.next() {
32        use graphics::*;
33
34        if let Some(args) = e.render_args() {
35            let mut target = window.draw();
36            g2d.draw(&mut target, args.viewport(), |c, g| {
37                clear(color::WHITE, g);
38                rectangle(
39                    [1.0, 0.0, 0.0, 1.0],
40                    [0.0, 0.0, 100.0, 100.0],
41                    c.transform,
42                    g,
43                );
44                rectangle(
45                    [0.0, 1.0, 0.0, 0.3],
46                    [50.0, 50.0, 100.0, 100.0],
47                    c.transform,
48                    g,
49                );
50                image(&rust_logo, c.transform.trans(100.0, 100.0), g);
51            });
52            target.finish().unwrap();
53        }
54    }
55}
More examples
Hide additional examples
examples/colored_image_test.rs (lines 21-26)
12fn main() {
13    let opengl = OpenGL::V3_2;
14    let (w, h) = (300, 300);
15    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: colored_image_test", [w, h])
16        .exit_on_esc(true)
17        .graphics_api(opengl)
18        .build()
19        .unwrap();
20
21    let rust_logo = Texture::from_path(
22        window,
23        "assets/rust-white.png",
24        Flip::None,
25        &TextureSettings::new(),
26    )
27    .unwrap();
28
29    let mut g2d = Glium2d::new(opengl, window);
30    window.set_lazy(true);
31    while let Some(e) = window.next() {
32        use graphics::*;
33
34        if let Some(args) = e.render_args() {
35            let mut target = window.draw();
36            g2d.draw(&mut target, args.viewport(), |c, g| {
37                use graphics::triangulation::{tx, ty};
38
39                let transform = c.transform.trans(0.0, 0.0);
40
41                let tr = |p: [f64; 2]| [tx(transform, p[0], p[1]), ty(transform, p[0], p[1])];
42
43                clear(color::WHITE, g);
44                Rectangle::new([1.0, 0.0, 0.0, 1.0])
45                    .draw([0.0, 0.0, 100.0, 100.0], &c.draw_state, c.transform, g);
46                Rectangle::new([0.0, 1.0, 0.0, 0.3])
47                    .draw([50.0, 50.0, 100.0, 100.0], &c.draw_state, c.transform, g);
48                g.tri_list_uv_c(&c.draw_state, &rust_logo, |f| {
49                    (f)(
50                        &[
51                            tr([0.0, 0.0]),
52                            tr([300.0, 0.0]),
53                            tr([0.0, 300.0]),
54
55                            tr([300.0, 0.0]),
56                            tr([0.0, 300.0]),
57                            tr([300.0, 300.0]),
58                        ],
59                        &[
60                            [0.0, 0.0],
61                            [1.0, 0.0],
62                            [0.0, 1.0],
63
64                            [1.0, 0.0],
65                            [0.0, 1.0],
66                            [1.0, 1.0],
67                        ],
68                        &[
69                            [1.0, 0.0, 0.0, 1.0],
70                            [0.0, 1.0, 0.0, 1.0],
71                            [0.0, 0.0, 1.0, 1.0],
72
73                            [0.0, 00.0, 0.0, 1.0],
74                            [0.0, 00.0, 0.0, 1.0],
75                            [0.0, 00.0, 0.0, 1.0],
76                        ]
77                    )
78                });
79            });
80            target.finish().unwrap();
81        }
82    }
83}
examples/texture_wrap.rs (lines 37-42)
12fn main() {
13    println!("Press U to change the texture wrap mode for the u coordinate");
14    println!("Press V to change the texture wrap mode for the v coordinate");
15
16    let opengl = OpenGL::V3_2;
17    let (w, h) = (600, 600);
18    let mut window: GliumWindow = WindowSettings::new("glium_graphics: texture_wrap", [w, h])
19        .exit_on_esc(true)
20        .graphics_api(opengl)
21        .build()
22        .unwrap();
23    window.set_lazy(true);
24
25    // Set up wrap modes
26    let wrap_modes = [
27        Wrap::ClampToEdge,
28        Wrap::ClampToBorder,
29        Wrap::Repeat,
30        Wrap::MirroredRepeat,
31    ];
32    let mut ix_u = 0;
33    let mut ix_v = 0;
34    let mut texture_settings = TextureSettings::new();
35    texture_settings.set_border_color([0.0, 0.0, 0.0, 1.0]);
36
37    let mut rust_logo = Texture::from_path(
38        &mut window,
39        "assets/rust.png",
40        Flip::None,
41        &texture_settings,
42    )
43    .unwrap();
44
45    let mut g2d = Glium2d::new(opengl, &mut window);
46    while let Some(e) = window.next() {
47        if let Some(args) = e.render_args() {
48            use graphics::*;
49            let mut target = window.draw();
50            g2d.draw(&mut target, args.viewport(), |_c, g| {
51                clear([1.0; 4], g);
52                let points = [[0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5], [0.5, -0.5]];
53                // (0, 1, 2) and (0, 2, 3)
54                let uvs = [
55                    [4.0, 0.0],
56                    [0.0, 0.0],
57                    [0.0, 4.0],
58                    [4.0, 0.0],
59                    [0.0, 4.0],
60                    [4.0, 4.0],
61                ];
62                let mut verts = [[0.0, 0.0]; 6];
63                let indices_points: [usize; 6] = [0, 1, 2, 0, 2, 3];
64                for (ixv, &ixp) in (0..6).zip((&indices_points).into_iter()) {
65                    verts[ixv] = points[ixp];
66                }
67                g.tri_list_uv(&DrawState::new_alpha(), &[1.0; 4], &rust_logo, |f| {
68                    f(&verts, &uvs)
69                });
70            });
71            target.finish().unwrap();
72        }
73
74        if let Some(Button::Keyboard(Key::U)) = e.press_args() {
75            ix_u = (ix_u + 1) % wrap_modes.len();
76            texture_settings.set_wrap_u(wrap_modes[ix_u]);
77            rust_logo = Texture::from_path(
78                &mut window,
79                "assets/rust.png",
80                Flip::None,
81                &texture_settings,
82            )
83            .unwrap();
84            println!(
85                "Changed texture wrap mode for u coordinate to: {:?}",
86                wrap_modes[ix_u]
87            );
88        }
89
90        if let Some(Button::Keyboard(Key::V)) = e.press_args() {
91            ix_v = (ix_v + 1) % wrap_modes.len();
92            texture_settings.set_wrap_v(wrap_modes[ix_v]);
93            rust_logo = Texture::from_path(
94                &mut window,
95                "assets/rust.png",
96                Flip::None,
97                &texture_settings,
98            )
99            .unwrap();
100            println!(
101                "Changed texture wrap mode for v coordinate to: {:?}",
102                wrap_modes[ix_v]
103            );
104        }
105    }
106}
examples/draw_state.rs (lines 25-30)
11fn main() {
12    println!("Press A to change blending");
13    println!("Press S to change clip inside/out");
14
15    let opengl = OpenGL::V3_2;
16    let (w, h) = (640, 480);
17    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: image_test", [w, h])
18        .exit_on_esc(true)
19        .graphics_api(opengl)
20        .build()
21        .unwrap();
22
23    let mut blend = Blend::Alpha;
24    let mut clip_inside = true;
25    let rust_logo = Texture::from_path(
26        window,
27        "assets/rust.png",
28        Flip::None,
29        &TextureSettings::new(),
30    )
31    .unwrap();
32
33    let mut g2d = Glium2d::new(opengl, window);
34    window.set_lazy(true);
35    while let Some(e) = window.next() {
36        if let Some(args) = e.render_args() {
37            use graphics::*;
38
39            let mut target = window.draw();
40            g2d.draw(&mut target, args.viewport(), |c, g| {
41                clear([0.8, 0.8, 0.8, 1.0], g);
42                g.clear_stencil(0);
43                Rectangle::new([1.0, 0.0, 0.0, 1.0]).draw(
44                    [0.0, 0.0, 100.0, 100.0],
45                    &c.draw_state,
46                    c.transform,
47                    g,
48                );
49
50                let draw_state = c.draw_state.blend(blend);
51                Rectangle::new([0.5, 1.0, 0.0, 0.3]).draw(
52                    [50.0, 50.0, 100.0, 100.0],
53                    &draw_state,
54                    c.transform,
55                    g,
56                );
57
58                let transform = c.transform.trans(100.0, 100.0);
59                // Compute clip rectangle from upper left corner.
60                let (clip_x, clip_y, clip_w, clip_h) = (100, 100, 100, 100);
61                let (clip_x, clip_y, clip_w, clip_h) = (
62                    clip_x,
63                    c.viewport.unwrap().draw_size[1] - clip_y - clip_h,
64                    clip_w,
65                    clip_h,
66                );
67                let clipped = c.draw_state.scissor([clip_x, clip_y, clip_w, clip_h]);
68                Image::new().draw(&rust_logo, &clipped, transform, g);
69
70                let transform = c.transform.trans(200.0, 200.0);
71                Ellipse::new([1.0, 0.0, 0.0, 1.0]).draw(
72                    [0.0, 0.0, 50.0, 50.0],
73                    &DrawState::new_clip(),
74                    transform,
75                    g,
76                );
77                Image::new().draw(
78                    &rust_logo,
79                    &(if clip_inside {
80                        DrawState::new_inside()
81                    } else {
82                        DrawState::new_outside()
83                    }),
84                    transform,
85                    g,
86                );
87            });
88
89            target.finish().unwrap();
90        }
91
92        if let Some(Button::Keyboard(Key::A)) = e.press_args() {
93            blend = match blend {
94                Blend::Alpha => Blend::Add,
95                Blend::Add => Blend::Multiply,
96                Blend::Multiply => Blend::Invert,
97                Blend::Invert => Blend::Lighter,
98                Blend::Lighter => Blend::Alpha,
99            };
100            println!("Changed blending to {:?}", blend);
101        }
102
103        if let Some(Button::Keyboard(Key::S)) = e.press_args() {
104            clip_inside = !clip_inside;
105            if clip_inside {
106                println!("Changed to clip inside");
107            } else {
108                println!("Changed to clip outside");
109            }
110        }
111    }
112}
Source

pub fn from_image<F>( factory: &mut F, img: &RgbaImage, settings: &TextureSettings, ) -> Result<Self, TextureCreationError>
where F: Facade,

Creates a texture from image.

Source

pub fn from_memory_alpha<F>( factory: &mut F, buffer: &[u8], width: u32, height: u32, settings: &TextureSettings, ) -> Result<Self, TextureCreationError>
where F: Facade,

Creates texture from memory alpha.

Source

pub fn update<F>( &mut self, factory: &mut F, img: &RgbaImage, ) -> Result<(), TextureCreationError>
where F: Facade,

Updates texture with an image.

Trait Implementations§

Source§

impl<F> CreateTexture<F> for Texture
where F: Facade,

Source§

fn create<S: Into<[u32; 2]>>( factory: &mut F, _format: Format, memory: &[u8], size: S, settings: &TextureSettings, ) -> Result<Self, Self::Error>

Create texture from memory.
Source§

impl ImageSize for Texture

Source§

fn get_size(&self) -> (u32, u32)

Get the image size.
Source§

fn get_width(&self) -> u32

Gets the image width.
Source§

fn get_height(&self) -> u32

Gets the image height.
Source§

impl<F> TextureOp<F> for Texture

Source§

type Error = TextureCreationError

The error when performing an operation.
Source§

impl<F> UpdateTexture<F> for Texture
where F: Facade,

Source§

fn update<O: Into<[u32; 2]>, S: Into<[u32; 2]>>( &mut self, factory: &mut F, _format: Format, memory: &[u8], offset: O, size: S, ) -> Result<(), Self::Error>

Update the texture. Read more

Auto Trait Implementations§

§

impl !Freeze for Texture

§

impl !RefUnwindSafe for Texture

§

impl !Send for Texture

§

impl !Sync for Texture

§

impl Unpin for Texture

§

impl !UnwindSafe for Texture

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more