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,7 +16,6 @@ use geometry::{Camera, DirectLight, Polyeder, Primitives};
16 use transform::{TMatrix}; 16 use transform::{TMatrix};
17 17
18 use std::fmt::{Display, Formatter, Result}; 18 use std::fmt::{Display, Formatter, Result};
19 -use std::ptr;  
20 use std::sync::mpsc; 19 use std::sync::mpsc;
21 use std::time::Instant; 20 use std::time::Instant;
22 use wasm_bindgen::prelude::*; 21 use wasm_bindgen::prelude::*;
@@ -35,7 +34,8 @@ pub struct Color(u8, u8, u8, u8); @@ -35,7 +34,8 @@ pub struct Color(u8, u8, u8, u8);
35 pub struct View3d { width :u16 34 pub struct View3d { width :u16
36 , height :u16 35 , height :u16
37 , size :usize 36 , size :usize
38 - , start :Instant 37 + , degree :i32
  38 + //, start :Instant
39 , tetrahedron :Polyeder<f64> 39 , tetrahedron :Polyeder<f64>
40 , cube :Polyeder<f64> 40 , cube :Polyeder<f64>
41 , camera :Option<Camera<f64>> 41 , camera :Option<Camera<f64>>
@@ -47,31 +47,40 @@ pub struct View3d { width :u16 @@ -47,31 +47,40 @@ pub struct View3d { width :u16
47 #[wasm_bindgen] 47 #[wasm_bindgen]
48 impl View3d { 48 impl View3d {
49 pub fn new(width :u16, height :u16) -> Self { 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 let light_vector = Vector(0.0, 0.0, 1.0); 51 let light_vector = Vector(0.0, 0.0, 1.0);
52 52
53 let mut view3d = Self { width: width 53 let mut view3d = Self { width: width
54 , height: height 54 , height: height
55 , size: size 55 , size: size
56 - , start: Instant::now() 56 + , degree: 0
  57 + //, start: Instant::now()
57 , tetrahedron: Polyeder::tetrahedron(100.0) 58 , tetrahedron: Polyeder::tetrahedron(100.0)
58 , cube: Polyeder::cube(56.25) 59 , cube: Polyeder::cube(56.25)
59 , camera: None 60 , camera: None
60 , light: DirectLight::new(light_vector) 61 , light: DirectLight::new(light_vector)
61 , zbuf: vec!(0.0; size) 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 view3d.camera = Some(Camera::<f64>::new(&view3d, 45)); 66 view3d.camera = Some(Camera::<f64>::new(&view3d, 45));
65 view3d 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 let t = TMatrix::translate(Vector(0.0, 0.0, 150.0)); 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 let rot1 = TMatrix::combine(vec!(rz, rx, t)); 85 let rot1 = TMatrix::combine(vec!(rz, rx, t));
77 let rot2 = TMatrix::combine(vec!(rz, ry, t)); 86 let rot2 = TMatrix::combine(vec!(rz, ry, t));
@@ -79,6 +88,8 @@ impl View3d { @@ -79,6 +88,8 @@ impl View3d {
79 let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00) 88 let objects = vec!( (self.tetrahedron.transform(&rot1), 0xFFFF00)
80 , ( self.cube.transform(&rot2), 0x0000FF) ); 89 , ( self.cube.transform(&rot2), 0x0000FF) );
81 90
  91 + self.degree = (self.degree + 1) % 360;
  92 +
82 self.clear(); 93 self.clear();
83 94
84 match self.camera { 95 match self.camera {
@@ -86,7 +97,7 @@ impl View3d { @@ -86,7 +97,7 @@ impl View3d {
86 Some(camera) => { 97 Some(camera) => {
87 for (o, color) in objects { 98 for (o, color) in objects {
88 for (pg, c) in o.project(&camera, &self.light, color) { 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,11 +119,8 @@ impl Canvas<f64> for View3d {
108 } 119 }
109 120
110 fn clear(&mut self) { 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 fn set_pixel(&mut self, c :Coordinate<f64>, color :u32) { 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,17 +4,30 @@ import { memory } from "wasm-game-of-life/wasm_game_of_life_bg";
4 // 3D canvas stuff 4 // 3D canvas stuff
5 const view3d = View3d.new(151, 151); 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 const view3d_renderLoop = () => { 12 const view3d_renderLoop = () => {
14 view3d.update(); 13 view3d.update();
  14 + drawView3d();
  15 +
15 requestAnimationFrame(view3d_renderLoop); 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 // game of life stuff 31 // game of life stuff
19 const CELL_SIZE = 5; // px 32 const CELL_SIZE = 5; // px
20 const GRID_COLOR = "#CCCCCC"; 33 const GRID_COLOR = "#CCCCCC";
@@ -90,7 +103,10 @@ const drawCells = () => { @@ -90,7 +103,10 @@ const drawCells = () => {
90 }; 103 };
91 104
92 // start everything ... 105 // start everything ...
93 -drawGrid();  
94 -drawCells();  
95 -requestAnimationFrame(renderLoop); 106 +//drawGrid();
  107 +//drawCells();
  108 +//requestAnimationFrame(renderLoop);
  109 +
  110 +view3d.update();
  111 +drawView3d();
96 requestAnimationFrame(view3d_renderLoop); 112 requestAnimationFrame(view3d_renderLoop);
Please register or login to post a comment