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
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>where
F: Facade,
P: AsRef<Path>,
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
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");
}
}
}
}
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.