Struct glium_graphics::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)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
fn main() {
    let opengl = OpenGL::V3_2;
    let (w, h) = (300, 300);
    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: image_test", [w, h])
        .exit_on_esc(true)
        .graphics_api(opengl)
        .build()
        .unwrap();

    let rust_logo = Texture::from_path(
        window,
        "assets/rust.png",
        Flip::None,
        &TextureSettings::new(),
    )
    .unwrap();

    let mut g2d = Glium2d::new(opengl, window);
    window.set_lazy(true);
    while let Some(e) = window.next() {
        use graphics::*;

        if let Some(args) = e.render_args() {
            let mut target = window.draw();
            g2d.draw(&mut target, args.viewport(), |c, g| {
                clear(color::WHITE, g);
                rectangle(
                    [1.0, 0.0, 0.0, 1.0],
                    [0.0, 0.0, 100.0, 100.0],
                    c.transform,
                    g,
                );
                rectangle(
                    [0.0, 1.0, 0.0, 0.3],
                    [50.0, 50.0, 100.0, 100.0],
                    c.transform,
                    g,
                );
                image(&rust_logo, c.transform.trans(100.0, 100.0), g);
            });
            target.finish().unwrap();
        }
    }
}
More examples
Hide additional examples
examples/colored_image_test.rs (lines 21-26)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
fn main() {
    let opengl = OpenGL::V3_2;
    let (w, h) = (300, 300);
    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: colored_image_test", [w, h])
        .exit_on_esc(true)
        .graphics_api(opengl)
        .build()
        .unwrap();

    let rust_logo = Texture::from_path(
        window,
        "assets/rust-white.png",
        Flip::None,
        &TextureSettings::new(),
    )
    .unwrap();

    let mut g2d = Glium2d::new(opengl, window);
    window.set_lazy(true);
    while let Some(e) = window.next() {
        use graphics::*;

        if let Some(args) = e.render_args() {
            let mut target = window.draw();
            g2d.draw(&mut target, args.viewport(), |c, g| {
                use graphics::triangulation::{tx, ty};

                let transform = c.transform.trans(0.0, 0.0);

                let tr = |p: [f64; 2]| [tx(transform, p[0], p[1]), ty(transform, p[0], p[1])];

                clear(color::WHITE, g);
                Rectangle::new([1.0, 0.0, 0.0, 1.0])
                    .draw([0.0, 0.0, 100.0, 100.0], &c.draw_state, c.transform, g);
                Rectangle::new([0.0, 1.0, 0.0, 0.3])
                    .draw([50.0, 50.0, 100.0, 100.0], &c.draw_state, c.transform, g);
                g.tri_list_uv_c(&c.draw_state, &rust_logo, |f| {
                    (f)(
                        &[
                            tr([0.0, 0.0]),
                            tr([300.0, 0.0]),
                            tr([0.0, 300.0]),

                            tr([300.0, 0.0]),
                            tr([0.0, 300.0]),
                            tr([300.0, 300.0]),
                        ],
                        &[
                            [0.0, 0.0],
                            [1.0, 0.0],
                            [0.0, 1.0],

                            [1.0, 0.0],
                            [0.0, 1.0],
                            [1.0, 1.0],
                        ],
                        &[
                            [1.0, 0.0, 0.0, 1.0],
                            [0.0, 1.0, 0.0, 1.0],
                            [0.0, 0.0, 1.0, 1.0],

                            [0.0, 00.0, 0.0, 1.0],
                            [0.0, 00.0, 0.0, 1.0],
                            [0.0, 00.0, 0.0, 1.0],
                        ]
                    )
                });
            });
            target.finish().unwrap();
        }
    }
}
examples/texture_wrap.rs (lines 37-42)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
fn main() {
    println!("Press U to change the texture wrap mode for the u coordinate");
    println!("Press V to change the texture wrap mode for the v coordinate");

    let opengl = OpenGL::V3_2;
    let (w, h) = (600, 600);
    let mut window: GliumWindow = WindowSettings::new("glium_graphics: texture_wrap", [w, h])
        .exit_on_esc(true)
        .graphics_api(opengl)
        .build()
        .unwrap();
    window.set_lazy(true);

    // Set up wrap modes
    let wrap_modes = [
        Wrap::ClampToEdge,
        Wrap::ClampToBorder,
        Wrap::Repeat,
        Wrap::MirroredRepeat,
    ];
    let mut ix_u = 0;
    let mut ix_v = 0;
    let mut texture_settings = TextureSettings::new();
    texture_settings.set_border_color([0.0, 0.0, 0.0, 1.0]);

    let mut rust_logo = Texture::from_path(
        &mut window,
        "assets/rust.png",
        Flip::None,
        &texture_settings,
    )
    .unwrap();

    let mut g2d = Glium2d::new(opengl, &mut window);
    while let Some(e) = window.next() {
        if let Some(args) = e.render_args() {
            use graphics::*;
            let mut target = window.draw();
            g2d.draw(&mut target, args.viewport(), |_c, g| {
                clear([1.0; 4], g);
                let points = [[0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5], [0.5, -0.5]];
                // (0, 1, 2) and (0, 2, 3)
                let uvs = [
                    [4.0, 0.0],
                    [0.0, 0.0],
                    [0.0, 4.0],
                    [4.0, 0.0],
                    [0.0, 4.0],
                    [4.0, 4.0],
                ];
                let mut verts = [[0.0, 0.0]; 6];
                let indices_points: [usize; 6] = [0, 1, 2, 0, 2, 3];
                for (ixv, &ixp) in (0..6).zip((&indices_points).into_iter()) {
                    verts[ixv] = points[ixp];
                }
                g.tri_list_uv(&DrawState::new_alpha(), &[1.0; 4], &rust_logo, |f| {
                    f(&verts, &uvs)
                });
            });
            target.finish().unwrap();
        }

        if let Some(Button::Keyboard(Key::U)) = e.press_args() {
            ix_u = (ix_u + 1) % wrap_modes.len();
            texture_settings.set_wrap_u(wrap_modes[ix_u]);
            rust_logo = Texture::from_path(
                &mut window,
                "assets/rust.png",
                Flip::None,
                &texture_settings,
            )
            .unwrap();
            println!(
                "Changed texture wrap mode for u coordinate to: {:?}",
                wrap_modes[ix_u]
            );
        }

        if let Some(Button::Keyboard(Key::V)) = e.press_args() {
            ix_v = (ix_v + 1) % wrap_modes.len();
            texture_settings.set_wrap_v(wrap_modes[ix_v]);
            rust_logo = Texture::from_path(
                &mut window,
                "assets/rust.png",
                Flip::None,
                &texture_settings,
            )
            .unwrap();
            println!(
                "Changed texture wrap mode for v coordinate to: {:?}",
                wrap_modes[ix_v]
            );
        }
    }
}
examples/draw_state.rs (lines 25-30)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
fn main() {
    println!("Press A to change blending");
    println!("Press S to change clip inside/out");

    let opengl = OpenGL::V3_2;
    let (w, h) = (640, 480);
    let ref mut window: GliumWindow = WindowSettings::new("glium_graphics: image_test", [w, h])
        .exit_on_esc(true)
        .graphics_api(opengl)
        .build()
        .unwrap();

    let mut blend = Blend::Alpha;
    let mut clip_inside = true;
    let rust_logo = Texture::from_path(
        window,
        "assets/rust.png",
        Flip::None,
        &TextureSettings::new(),
    )
    .unwrap();

    let mut g2d = Glium2d::new(opengl, window);
    window.set_lazy(true);
    while let Some(e) = window.next() {
        if let Some(args) = e.render_args() {
            use graphics::*;

            let mut target = window.draw();
            g2d.draw(&mut target, args.viewport(), |c, g| {
                clear([0.8, 0.8, 0.8, 1.0], g);
                g.clear_stencil(0);
                Rectangle::new([1.0, 0.0, 0.0, 1.0]).draw(
                    [0.0, 0.0, 100.0, 100.0],
                    &c.draw_state,
                    c.transform,
                    g,
                );

                let draw_state = c.draw_state.blend(blend);
                Rectangle::new([0.5, 1.0, 0.0, 0.3]).draw(
                    [50.0, 50.0, 100.0, 100.0],
                    &draw_state,
                    c.transform,
                    g,
                );

                let transform = c.transform.trans(100.0, 100.0);
                // Compute clip rectangle from upper left corner.
                let (clip_x, clip_y, clip_w, clip_h) = (100, 100, 100, 100);
                let (clip_x, clip_y, clip_w, clip_h) = (
                    clip_x,
                    c.viewport.unwrap().draw_size[1] - clip_y - clip_h,
                    clip_w,
                    clip_h,
                );
                let clipped = c.draw_state.scissor([clip_x, clip_y, clip_w, clip_h]);
                Image::new().draw(&rust_logo, &clipped, transform, g);

                let transform = c.transform.trans(200.0, 200.0);
                Ellipse::new([1.0, 0.0, 0.0, 1.0]).draw(
                    [0.0, 0.0, 50.0, 50.0],
                    &DrawState::new_clip(),
                    transform,
                    g,
                );
                Image::new().draw(
                    &rust_logo,
                    &(if clip_inside {
                        DrawState::new_inside()
                    } else {
                        DrawState::new_outside()
                    }),
                    transform,
                    g,
                );
            });

            target.finish().unwrap();
        }

        if let Some(Button::Keyboard(Key::A)) = e.press_args() {
            blend = match blend {
                Blend::Alpha => Blend::Add,
                Blend::Add => Blend::Multiply,
                Blend::Multiply => Blend::Invert,
                Blend::Invert => Blend::Lighter,
                Blend::Lighter => Blend::Alpha,
            };
            println!("Changed blending to {:?}", blend);
        }

        if let Some(Button::Keyboard(Key::S)) = e.press_args() {
            clip_inside = !clip_inside;
            if clip_inside {
                println!("Changed to clip inside");
            } else {
                println!("Changed to clip outside");
            }
        }
    }
}
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 Texturewhere 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

§

type Error = TextureCreationError

The error when performing an operation.
source§

impl<F> UpdateTexture<F> for Texturewhere 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 !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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.