|
|
@ -16,7 +16,6 @@ use geometry::{Camera, DirectLight, Polyeder, Primitives}; |
|
|
use transform::{TMatrix};
|
|
|
use transform::{TMatrix};
|
|
|
|
|
|
|
|
|
use std::fmt::{Display, Formatter, Result};
|
|
|
use std::fmt::{Display, Formatter, Result};
|
|
|
use std::ptr;
|
|
|
|
|
|
use std::sync::mpsc;
|
|
|
use std::sync::mpsc;
|
|
|
use std::time::Instant;
|
|
|
use std::time::Instant;
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
@ -35,7 +34,8 @@ pub struct Color(u8, u8, u8, u8); |
|
|
pub struct View3d { width :u16 |
|
|
pub struct View3d { width :u16 |
|
|
, height :u16 |
|
|
, height :u16 |
|
|
, size :usize |
|
|
, size :usize |
|
|
, start :Instant
|
|
|
|
|
|
|
|
|
, degree :i32 |
|
|
|
|
|
//, start :Instant
|
|
|
, tetrahedron :Polyeder<f64>
|
|
|
, tetrahedron :Polyeder<f64>
|
|
|
, cube :Polyeder<f64>
|
|
|
, cube :Polyeder<f64>
|
|
|
, camera :Option<Camera<f64>>
|
|
|
, camera :Option<Camera<f64>>
|
|
|
@ -47,31 +47,40 @@ pub struct View3d { width :u16 |
|
|
#[wasm_bindgen]
|
|
|
#[wasm_bindgen]
|
|
|
impl View3d {
|
|
|
impl View3d {
|
|
|
pub fn new(width :u16, height :u16) -> Self {
|
|
|
pub fn new(width :u16, height :u16) -> Self {
|
|
|
let size = width as usize * height as usize;
|
|
|
|
|
|
|
|
|
let size = width as usize * height as usize;
|
|
|
let light_vector = Vector(0.0, 0.0, 1.0);
|
|
|
let light_vector = Vector(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
|
let mut view3d = Self { width: width
|
|
|
let mut view3d = Self { width: width
|
|
|
, height: height
|
|
|
, height: height
|
|
|
, size: size
|
|
|
, size: size
|
|
|
, start: Instant::now()
|
|
|
|
|
|
|
|
|
, degree: 0
|
|
|
|
|
|
//, start: Instant::now()
|
|
|
, tetrahedron: Polyeder::tetrahedron(100.0)
|
|
|
, tetrahedron: Polyeder::tetrahedron(100.0)
|
|
|
, cube: Polyeder::cube(56.25)
|
|
|
, cube: Polyeder::cube(56.25)
|
|
|
, camera: None |
|
|
, camera: None |
|
|
, light: DirectLight::new(light_vector)
|
|
|
, light: DirectLight::new(light_vector)
|
|
|
, zbuf: vec!(0.0; size)
|
|
|
, zbuf: vec!(0.0; size)
|
|
|
, image: vec!(Color(0, 0, 0, 0); size) };
|
|
|
|
|
|
|
|
|
, image: vec!(Color(0, 0, 0, 0xFF); size)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
|
|
|
view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
|
|
|
view3d
|
|
|
view3d
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn update(mut self) {
|
|
|
|
|
|
let deg = ((self.start.elapsed() / 25).as_millis() % 360) as i32;
|
|
|
|
|
|
|
|
|
pub fn width(&self) -> u16 {
|
|
|
|
|
|
self.width
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn height(&self) -> u16 {
|
|
|
|
|
|
self.height
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn update(&mut self) {
|
|
|
|
|
|
//let deg = ((self.start.elapsed() / 25).as_millis() % 360) as i32;
|
|
|
let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
|
|
|
let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
|
|
|
let rz = TMatrix::rotate_z(deg);
|
|
|
|
|
|
let rx = TMatrix::rotate_x(-deg*2);
|
|
|
|
|
|
let ry = TMatrix::rotate_y(-deg*2);
|
|
|
|
|
|
|
|
|
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 rot1 = TMatrix::combine(vec!(rz, rx, t));
|
|
|
let rot2 = TMatrix::combine(vec!(rz, ry, t));
|
|
|
let rot2 = TMatrix::combine(vec!(rz, ry, t));
|
|
|
@ -79,6 +88,8 @@ impl View3d { |
|
|
let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
|
|
|
let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
|
|
|
, ( self.cube.transform(&rot2), 0x0000FF) );
|
|
|
, ( self.cube.transform(&rot2), 0x0000FF) );
|
|
|
|
|
|
|
|
|
|
|
|
self.degree = (self.degree + 1) % 360;
|
|
|
|
|
|
|
|
|
self.clear();
|
|
|
self.clear();
|
|
|
|
|
|
|
|
|
match self.camera {
|
|
|
match self.camera {
|
|
|
@ -86,7 +97,7 @@ impl View3d { |
|
|
Some(camera) => {
|
|
|
Some(camera) => {
|
|
|
for (o, color) in objects {
|
|
|
for (o, color) in objects {
|
|
|
for (pg, c) in o.project(&camera, &self.light, color) {
|
|
|
for (pg, c) in o.project(&camera, &self.light, color) {
|
|
|
(&pg).fill(&mut self, c);
|
|
|
|
|
|
|
|
|
(&pg).fill(self, c);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
@ -108,11 +119,8 @@ impl Canvas<f64> for View3d { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn clear(&mut self) {
|
|
|
fn clear(&mut self) {
|
|
|
self.zbuf = vec!(0.0; self.size);
|
|
|
|
|
|
unsafe {
|
|
|
|
|
|
let ptr = self.image.as_mut_ptr();
|
|
|
|
|
|
ptr::write_bytes(ptr, 0, self.size);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
self.zbuf = vec!(0.0; self.size);
|
|
|
|
|
|
self.image = vec!(Color(0, 0, 0, 0xFF); self.size);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn set_pixel(&mut self, c :Coordinate<f64>, color :u32) {
|
|
|
fn set_pixel(&mut self, c :Coordinate<f64>, color :u32) {
|
|
|
|