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
impl Texture
Sourcepub fn new(texture: SrgbTexture2d) -> Texture
pub fn new(texture: SrgbTexture2d) -> Texture
Creates a new Texture.
Sourcepub fn empty<F>(factory: &mut F) -> Result<Self, TextureCreationError>where
F: Facade,
pub fn empty<F>(factory: &mut F) -> Result<Self, TextureCreationError>where
F: Facade,
Returns empty texture.
Sourcepub fn from_path<F, P>(
factory: &mut F,
path: P,
flip: Flip,
settings: &TextureSettings,
) -> Result<Self, String>
pub fn from_path<F, P>( factory: &mut F, path: P, flip: Flip, settings: &TextureSettings, ) -> Result<Self, String>
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
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}Sourcepub fn from_image<F>(
factory: &mut F,
img: &RgbaImage,
settings: &TextureSettings,
) -> Result<Self, TextureCreationError>where
F: Facade,
pub fn from_image<F>(
factory: &mut F,
img: &RgbaImage,
settings: &TextureSettings,
) -> Result<Self, TextureCreationError>where
F: Facade,
Creates a texture from image.
Sourcepub fn from_memory_alpha<F>(
factory: &mut F,
buffer: &[u8],
width: u32,
height: u32,
settings: &TextureSettings,
) -> Result<Self, TextureCreationError>where
F: Facade,
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.
Trait Implementations§
Source§impl<F> CreateTexture<F> for Texturewhere
F: Facade,
impl<F> CreateTexture<F> for Texturewhere
F: Facade,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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