lib.rs
2.55 KB
1
2
3
4
5
6
7
8
9
10
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
extern crate easel3d;
mod easel3d_wasm;
use easel3d_wasm::{WasmCanvas, Color};
use easel3d::easel::canvas::Canvas;
use easel3d::easel::fillable::Fillable;
use easel3d::math::transform::TMatrix;
use easel3d::math::vector::Vector;
use easel3d::space::camera::Camera;
use easel3d::space::light::DirectLight;
use easel3d::space::polyeder::Polyeder;
use easel3d::space::primitives::Primitives;
use wasm_bindgen::prelude::*;
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
pub struct View3d { canvas :WasmCanvas
, degree :i32
, tetrahedron :Polyeder<f64>
, cube :Polyeder<f64>
, camera :Camera<f64>
, light :DirectLight<f64> }
#[wasm_bindgen]
impl View3d {
pub fn new(width :u16, height :u16) -> Self {
let light_vector = Vector(0.0, 0.0, 1.0);
let canvas = WasmCanvas::new(width, height);
let camera = Camera::<f64>::new(&canvas, 45);
Self { canvas: canvas
, degree: 0
, tetrahedron: Polyeder::tetrahedron(100.0)
, cube: Polyeder::cube(56.25)
, camera: camera
, light: DirectLight::new(light_vector) }
}
pub fn width(&self) -> u16 {
self.canvas.width()
}
pub fn height(&self) -> u16 {
self.canvas.height()
}
pub fn update(&mut self) {
let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
let rz = TMatrix::rotate_z(self.degree);
let rx = TMatrix::rotate_x(-self.degree*2);
let ry = TMatrix::rotate_y(-self.degree*2);
let rot1 = TMatrix::combine(vec!(rz, rx, t));
let rot2 = TMatrix::combine(vec!(rz, ry, t));
let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
, ( self.cube.transform(&rot2), 0x0000FF) );
let rlx = TMatrix::rotate_x(-self.degree/4);
let rly = TMatrix::rotate_y(-self.degree/1);
let light = self.light.transform(&TMatrix::combine(vec!(rlx, rly)));
self.degree = (self.degree + 1) % (4*360);
self.canvas.clear();
for (o, color) in objects {
for (pg, c) in o.project(&self.camera, &light, color) {
(&pg).fill(&mut self.canvas, c);
}
}
}
pub fn image(&self) -> *const Color {
self.canvas.image()
}
}