Struct glium_graphics::Glium2d
source · pub struct Glium2d { /* private fields */ }
Expand description
The resources needed for rendering 2D.
Implementations§
source§impl Glium2d
impl Glium2d
sourcepub fn new<W>(opengl: OpenGL, window: &W) -> Glium2dwhere
W: Facade,
pub fn new<W>(opengl: OpenGL, window: &W) -> Glium2dwhere W: Facade,
Creates a new Glium2d
.
Examples found in repository?
examples/text_test.rs (line 27)
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
fn main() {
let opengl = OpenGL::V3_2;
let size = [500, 300];
let ref mut window: GliumWindow = WindowSettings::new("gfx_graphics: text_test", size)
.exit_on_esc(true)
.graphics_api(opengl)
.build()
.unwrap();
let mut glyph_cache = GlyphCache::new(
Path::new("assets/FiraSans-Regular.ttf"),
window.clone(),
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() {
let mut target = window.draw();
g2d.draw(&mut target, args.viewport(), |c, g| {
use graphics::*;
clear([1.0; 4], g);
text::Text::new_color([0.0, 0.5, 0.0, 1.0], 32)
.draw(
"Hello glium_graphics!",
&mut glyph_cache,
&DrawState::default(),
c.transform.trans(10.0, 100.0),
g,
)
.unwrap();
});
target.finish().unwrap();
}
}
}
More examples
examples/image_test.rs (line 29)
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();
}
}
}
examples/colored_image_test.rs (line 29)
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 (line 45)
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 (line 33)
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 draw<F, U>(&mut self, target: &mut Frame, viewport: Viewport, f: F) -> Uwhere
F: FnOnce(Context, &mut GliumGraphics<'_, '_, Frame>) -> U,
pub fn draw<F, U>(&mut self, target: &mut Frame, viewport: Viewport, f: F) -> Uwhere F: FnOnce(Context, &mut GliumGraphics<'_, '_, Frame>) -> U,
Renders 2D graphics.
Examples found in repository?
examples/text_test.rs (lines 32-45)
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
fn main() {
let opengl = OpenGL::V3_2;
let size = [500, 300];
let ref mut window: GliumWindow = WindowSettings::new("gfx_graphics: text_test", size)
.exit_on_esc(true)
.graphics_api(opengl)
.build()
.unwrap();
let mut glyph_cache = GlyphCache::new(
Path::new("assets/FiraSans-Regular.ttf"),
window.clone(),
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() {
let mut target = window.draw();
g2d.draw(&mut target, args.viewport(), |c, g| {
use graphics::*;
clear([1.0; 4], g);
text::Text::new_color([0.0, 0.5, 0.0, 1.0], 32)
.draw(
"Hello glium_graphics!",
&mut glyph_cache,
&DrawState::default(),
c.transform.trans(10.0, 100.0),
g,
)
.unwrap();
});
target.finish().unwrap();
}
}
}
More examples
examples/image_test.rs (lines 36-51)
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();
}
}
}
examples/colored_image_test.rs (lines 36-79)
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 50-70)
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 40-87)
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");
}
}
}
}