pub fn set_camera(camera: &dyn Camera)Expand description
Set active 2D or 3D camera.
Examples found in repository?
examples/tree.rs (line 37)
30async fn main() {
31 let camera = Camera2D {
32 zoom: vec2(1., 1.),
33 target: vec2(0.0, 0.5),
34 ..Default::default()
35 };
36
37 set_camera(&camera);
38 loop {
39 clear_background(LIGHTGRAY);
40
41 draw_circle(0., 0., 0.03, DARKGRAY);
42 tree(unsafe { get_internal_gl().quad_gl }, get_time(), 0, 1., 0.3);
43
44 next_frame().await
45 }
46}More examples
examples/camera.rs (lines 10-13)
4async fn main() {
5 loop {
6 clear_background(LIGHTGRAY);
7
8 // Render some primitives in camera space
9
10 set_camera(&Camera2D {
11 zoom: vec2(1., screen_width() / screen_height()),
12 ..Default::default()
13 });
14 draw_line(-0.4, 0.4, -0.8, 0.9, 0.05, BLUE);
15 draw_rectangle(-0.3, 0.3, 0.2, 0.2, GREEN);
16 draw_circle(0., 0., 0.1, YELLOW);
17
18 // Back to screen space, render some text
19
20 set_default_camera();
21 draw_text("HELLO", 30.0, 200.0, 30.0, BLACK);
22
23 next_frame().await
24 }
25}examples/3d.rs (lines 13-18)
4async fn main() {
5 let rust_logo = load_texture("examples/rust.png").await.unwrap();
6 let ferris = load_texture("examples/ferris.png").await.unwrap();
7
8 loop {
9 clear_background(LIGHTGRAY);
10
11 // Going 3d!
12
13 set_camera(&Camera3D {
14 position: vec3(-20., 15., 0.),
15 up: vec3(0., 1., 0.),
16 target: vec3(0., 0., 0.),
17 ..Default::default()
18 });
19
20 draw_grid(20, 1., BLACK, GRAY);
21
22 draw_cube_wires(vec3(0., 1., -6.), vec3(2., 2., 2.), DARKGREEN);
23 draw_cube_wires(vec3(0., 1., 6.), vec3(2., 2., 2.), DARKBLUE);
24 draw_cube_wires(vec3(2., 1., 2.), vec3(2., 2., 2.), YELLOW);
25
26 draw_plane(vec3(-8., 0., -8.), vec2(5., 5.), Some(&ferris), WHITE);
27
28 draw_cube(
29 vec3(-5., 1., -2.),
30 vec3(2., 2., 2.),
31 Some(&rust_logo),
32 WHITE,
33 );
34 draw_cube(vec3(-5., 1., 2.), vec3(2., 2., 2.), Some(&ferris), WHITE);
35 draw_cube(vec3(2., 0., -2.), vec3(0.4, 0.4, 0.4), None, BLACK);
36
37 draw_sphere(vec3(-8., 0., 0.), 1., None, BLUE);
38
39 // Back to screen space, render some text
40
41 set_default_camera();
42 draw_text("WELCOME TO 3D WORLD", 10.0, 20.0, 30.0, BLACK);
43
44 next_frame().await
45 }
46}examples/post_processing.rs (lines 21-26)
4async fn main() {
5 let render_target = render_target(320, 150);
6 render_target.texture.set_filter(FilterMode::Nearest);
7
8 let material = load_material(
9 ShaderSource::Glsl {
10 vertex: CRT_VERTEX_SHADER,
11 fragment: CRT_FRAGMENT_SHADER,
12 },
13 Default::default(),
14 )
15 .unwrap();
16
17 loop {
18 // drawing to the texture
19
20 // 0..100, 0..100 camera
21 set_camera(&Camera2D {
22 zoom: vec2(0.01, 0.01),
23 target: vec2(0.0, 0.0),
24 render_target: Some(render_target.clone()),
25 ..Default::default()
26 });
27
28 clear_background(LIGHTGRAY);
29 draw_line(-30.0, 45.0, 30.0, 45.0, 3.0, BLUE);
30 draw_circle(-45.0, -35.0, 20.0, YELLOW);
31 draw_circle(45.0, -35.0, 20.0, GREEN);
32
33 // drawing to the screen
34
35 set_default_camera();
36
37 clear_background(WHITE);
38 gl_use_material(&material);
39 draw_texture_ex(
40 &render_target.texture,
41 0.,
42 0.,
43 WHITE,
44 DrawTextureParams {
45 dest_size: Some(vec2(screen_width(), screen_height())),
46 ..Default::default()
47 },
48 );
49 gl_use_default_material();
50
51 next_frame().await;
52 }
53}examples/raw_miniquad.rs (lines 18-21)
4async fn main() {
5 let stage = {
6 let InternalGlContext {
7 quad_context: ctx, ..
8 } = unsafe { get_internal_gl() };
9
10 raw_miniquad::Stage::new(ctx)
11 };
12
13 loop {
14 clear_background(LIGHTGRAY);
15
16 // Render some primitives in camera space
17
18 set_camera(&Camera2D {
19 zoom: vec2(1., screen_width() / screen_height()),
20 ..Default::default()
21 });
22 draw_line(-0.4, 0.4, -0.8, 0.9, 0.05, BLUE);
23 draw_rectangle(-0.3, 0.3, 0.2, 0.2, GREEN);
24 draw_circle(0., 0., 0.1, YELLOW);
25
26 {
27 let mut gl = unsafe { get_internal_gl() };
28
29 // Ensure that macroquad's shapes are not going to be lost
30 gl.flush();
31
32 let t = get_time();
33
34 gl.quad_context.apply_pipeline(&stage.pipeline);
35
36 gl.quad_context
37 .begin_default_pass(miniquad::PassAction::Nothing);
38 gl.quad_context.apply_bindings(&stage.bindings);
39
40 for i in 0..10 {
41 let t = t + i as f64 * 0.3;
42
43 gl.quad_context
44 .apply_uniforms(miniquad::UniformsSource::table(
45 &raw_miniquad::shader::Uniforms {
46 offset: (t.sin() as f32 * 0.5, (t * 3.).cos() as f32 * 0.5),
47 },
48 ));
49 gl.quad_context.draw(0, 6, 1);
50 }
51 gl.quad_context.end_render_pass();
52 }
53
54 // Back to screen space, render some text
55
56 set_default_camera();
57 draw_text("HELLO", 30.0, 200.0, 30.0, BLACK);
58
59 next_frame().await
60 }
61}examples/letterbox.rs (line 33)
7async fn main() {
8 // Setup 'render_target', used to hold the rendering result so we can resize it
9 let render_target = render_target(VIRTUAL_WIDTH as u32, VIRTUAL_HEIGHT as u32);
10 render_target.texture.set_filter(FilterMode::Linear);
11
12 // Setup camera for the virtual screen, that will render to 'render_target'
13 let mut render_target_cam =
14 Camera2D::from_display_rect(Rect::new(0., 0., VIRTUAL_WIDTH, VIRTUAL_HEIGHT));
15 render_target_cam.render_target = Some(render_target.clone());
16
17 loop {
18 // Get required scaling value
19 let scale: f32 = f32::min(
20 screen_width() / VIRTUAL_WIDTH,
21 screen_height() / VIRTUAL_HEIGHT,
22 );
23
24 // Mouse position in the virtual screen
25 let virtual_mouse_pos = Vec2 {
26 x: (mouse_position().0 - (screen_width() - (VIRTUAL_WIDTH * scale)) * 0.5) / scale,
27 y: (mouse_position().1 - (screen_height() - (VIRTUAL_HEIGHT * scale)) * 0.5) / scale,
28 };
29
30 // ------------------------------------------------------------------------
31 // Begin drawing the virtual screen to 'render_target'
32 // ------------------------------------------------------------------------
33 set_camera(&render_target_cam);
34
35 clear_background(LIGHTGRAY);
36
37 draw_text("Hello Letterbox", 20.0, 20.0, 30.0, DARKGRAY);
38 draw_circle(VIRTUAL_WIDTH / 2.0 - 65.0, VIRTUAL_HEIGHT / 2.0, 35.0, RED);
39 draw_circle(VIRTUAL_WIDTH / 2.0 + 65.0, VIRTUAL_HEIGHT / 2.0, 35.0, BLUE);
40 draw_circle(
41 VIRTUAL_WIDTH / 2.0,
42 VIRTUAL_HEIGHT / 2.0 - 65.0,
43 35.0,
44 YELLOW,
45 );
46
47 draw_circle(virtual_mouse_pos.x, virtual_mouse_pos.y, 15.0, BLACK);
48
49 // ------------------------------------------------------------------------
50 // Begin drawing the window screen
51 // ------------------------------------------------------------------------
52 set_default_camera();
53
54 clear_background(BLACK); // Will be the letterbox color
55
56 // Draw 'render_target' to window screen, porperly scaled and letterboxed
57 draw_texture_ex(
58 &render_target.texture,
59 (screen_width() - (VIRTUAL_WIDTH * scale)) * 0.5,
60 (screen_height() - (VIRTUAL_HEIGHT * scale)) * 0.5,
61 WHITE,
62 DrawTextureParams {
63 dest_size: Some(vec2(VIRTUAL_WIDTH * scale, VIRTUAL_HEIGHT * scale)),
64 flip_y: true, // Must flip y otherwise 'render_target' will be upside down
65 ..Default::default()
66 },
67 );
68
69 next_frame().await;
70 }
71}Additional examples can be found in: