rotating_cube/
rotating_cube.rs

1// Oliver Berzs
2// https://github.com/oberzs/duku
3
4// This example draws a cube in the center of the window,
5// rotating and coloring it based on the time that has passed.
6
7use duku::Camera;
8use duku::Duku;
9use duku::Hsb;
10use duku::Light;
11use duku::Result;
12use std::time::Instant;
13
14fn main() -> Result<()> {
15    // create duku context and window
16    let (mut duku, window) = Duku::windowed(500, 500)?;
17
18    // create 3D camera with 90 fov
19    let camera = Camera::perspective(90);
20
21    // create directional light
22    let light = Light::directional("#ffffff", [-1.0, -1.0, 1.0]);
23
24    // start timer for rotation and color
25    let timer = Instant::now();
26
27    // start window loop
28    window.while_open(move |_| {
29        // start drawing on window
30        duku.draw(Some(&camera), |t| {
31            // setup scene
32            t.background("#ababab");
33            t.light(light);
34
35            // get elapsed time since start
36            let elapsed = timer.elapsed().as_secs_f32();
37
38            // transform scene
39            let angle = elapsed * 45.0;
40            t.rotate_x(angle);
41            t.rotate_y(angle);
42            t.translate_z(2.0);
43
44            // draw cube
45            let hue = (elapsed * 60.0) as u16;
46            t.tint(Hsb::new(hue, 70, 80));
47            t.cube([1.0, 1.0, 1.0]);
48        });
49    });
50
51    Ok(())
52}