Commit 50db7017989e770f9e9d48c525121e4d97cef79f

Authored by Georg Hopp
1 parent a0c638c5

Show nice 3D instead of boring game

... ... @@ -16,7 +16,6 @@ use geometry::{Camera, DirectLight, Polyeder, Primitives};
16 16 use transform::{TMatrix};
17 17
18 18 use std::fmt::{Display, Formatter, Result};
19   -use std::ptr;
20 19 use std::sync::mpsc;
21 20 use std::time::Instant;
22 21 use wasm_bindgen::prelude::*;
... ... @@ -35,7 +34,8 @@ pub struct Color(u8, u8, u8, u8);
35 34 pub struct View3d { width :u16
36 35 , height :u16
37 36 , size :usize
38   - , start :Instant
  37 + , degree :i32
  38 + //, start :Instant
39 39 , tetrahedron :Polyeder<f64>
40 40 , cube :Polyeder<f64>
41 41 , camera :Option<Camera<f64>>
... ... @@ -47,31 +47,40 @@ pub struct View3d { width :u16
47 47 #[wasm_bindgen]
48 48 impl View3d {
49 49 pub fn new(width :u16, height :u16) -> Self {
50   - let size = width as usize * height as usize;
  50 + let size = width as usize * height as usize;
51 51 let light_vector = Vector(0.0, 0.0, 1.0);
52 52
53 53 let mut view3d = Self { width: width
54 54 , height: height
55 55 , size: size
56   - , start: Instant::now()
  56 + , degree: 0
  57 + //, start: Instant::now()
57 58 , tetrahedron: Polyeder::tetrahedron(100.0)
58 59 , cube: Polyeder::cube(56.25)
59 60 , camera: None
60 61 , light: DirectLight::new(light_vector)
61 62 , zbuf: vec!(0.0; size)
62   - , image: vec!(Color(0, 0, 0, 0); size) };
  63 + , image: vec!(Color(0, 0, 0, 0xFF); size)
  64 + };
63 65
64 66 view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
65 67 view3d
66 68 }
67 69
68   - pub fn update(mut self) {
69   - let deg = ((self.start.elapsed() / 25).as_millis() % 360) as i32;
  70 + pub fn width(&self) -> u16 {
  71 + self.width
  72 + }
  73 +
  74 + pub fn height(&self) -> u16 {
  75 + self.height
  76 + }
70 77
  78 + pub fn update(&mut self) {
  79 + //let deg = ((self.start.elapsed() / 25).as_millis() % 360) as i32;
71 80 let t = TMatrix::translate(Vector(0.0, 0.0, 150.0));
72   - let rz = TMatrix::rotate_z(deg);
73   - let rx = TMatrix::rotate_x(-deg*2);
74   - let ry = TMatrix::rotate_y(-deg*2);
  81 + let rz = TMatrix::rotate_z(self.degree);
  82 + let rx = TMatrix::rotate_x(-self.degree*2);
  83 + let ry = TMatrix::rotate_y(-self.degree*2);
75 84
76 85 let rot1 = TMatrix::combine(vec!(rz, rx, t));
77 86 let rot2 = TMatrix::combine(vec!(rz, ry, t));
... ... @@ -79,6 +88,8 @@ impl View3d {
79 88 let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
80 89 , ( self.cube.transform(&rot2), 0x0000FF) );
81 90
  91 + self.degree = (self.degree + 1) % 360;
  92 +
82 93 self.clear();
83 94
84 95 match self.camera {
... ... @@ -86,7 +97,7 @@ impl View3d {
86 97 Some(camera) => {
87 98 for (o, color) in objects {
88 99 for (pg, c) in o.project(&camera, &self.light, color) {
89   - (&pg).fill(&mut self, c);
  100 + (&pg).fill(self, c);
90 101 }
91 102 }
92 103 },
... ... @@ -108,11 +119,8 @@ impl Canvas<f64> for View3d {
108 119 }
109 120
110 121 fn clear(&mut self) {
111   - self.zbuf = vec!(0.0; self.size);
112   - unsafe {
113   - let ptr = self.image.as_mut_ptr();
114   - ptr::write_bytes(ptr, 0, self.size);
115   - }
  122 + self.zbuf = vec!(0.0; self.size);
  123 + self.image = vec!(Color(0, 0, 0, 0xFF); self.size);
116 124 }
117 125
118 126 fn set_pixel(&mut self, c :Coordinate<f64>, color :u32) {
... ...
... ... @@ -4,17 +4,30 @@ import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";
4 4 // 3D canvas stuff
5 5 const view3d = View3d.new(151, 151);
6 6
7   -const view3d_canvas = document.getElementById("view3d");
8   -view3d_canvas.width = view3d.width();
9   -view3d_canvas.width = view3d.height();
10   -
11   -const view3d_ctx = view3d_canvas.getContext('2d');
  7 +const view3d_canvas = document.getElementById("view3d");
  8 +view3d_canvas.width = view3d.width();
  9 +view3d_canvas.height = view3d.height();
  10 +const view3d_ctx = view3d_canvas.getContext('2d');
12 11
13 12 const view3d_renderLoop = () => {
14 13 view3d.update();
  14 + drawView3d();
  15 +
15 16 requestAnimationFrame(view3d_renderLoop);
16 17 }
17 18
  19 +const drawView3d = () => {
  20 + const view3d_imagePtr = view3d.image();
  21 + const view3d_image = new ImageData(
  22 + new Uint8ClampedArray( memory.buffer
  23 + , view3d.image()
  24 + , view3d.width() * view3d.height() * 4 )
  25 + , view3d.width()
  26 + , view3d.height() );
  27 +
  28 + view3d_ctx.putImageData(view3d_image, 0, 0);
  29 +}
  30 +
18 31 // game of life stuff
19 32 const CELL_SIZE = 5; // px
20 33 const GRID_COLOR = "#CCCCCC";
... ... @@ -90,7 +103,10 @@ const drawCells = () => {
90 103 };
91 104
92 105 // start everything ...
93   -drawGrid();
94   -drawCells();
95   -requestAnimationFrame(renderLoop);
  106 +//drawGrid();
  107 +//drawCells();
  108 +//requestAnimationFrame(renderLoop);
  109 +
  110 +view3d.update();
  111 +drawView3d();
96 112 requestAnimationFrame(view3d_renderLoop);
... ...
Please register or login to post a comment