texture_wrap/
texture_wrap.rs

1extern crate glium;
2extern crate glium_graphics;
3extern crate graphics;
4extern crate image as im;
5extern crate piston;
6
7use glium_graphics::{Flip, Glium2d, GliumWindow, OpenGL, Texture, TextureSettings, Wrap};
8use piston::event_loop::EventLoop;
9use piston::input::{Button, Key, PressEvent, RenderEvent};
10use piston::window::WindowSettings;
11
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}