-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rust with wasm just isn't ready for primetime, mostly because async is so half baked, and the tooling isn't ready. I wanted to load an STL and render it with webgl. Unfortunately, because we're in a browser context, loading the STL requires the fetch API which means we need to use an async future to extract this data. The gymnastics required to do this mean that I'd have to change the entire program (running `main` from cargo run with a web target) as main can't be async. Then you're into learning how to use the wasm-bindgen tooling directly and all the documentation uses webpack, which is a red flag for feature creep to me. At this point, none of this is even fun any more, rather than solving problems, I'm researching types of glue.
- Loading branch information
1 parent
e5a91ba
commit 0bdf6c2
Showing
6 changed files
with
192 additions
and
92 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pub fn to_mat4(m: &na::Matrix4<f32>) -> [f32; 16] { | ||
return flatten(m.clone().into()) | ||
} | ||
|
||
// This is annoying | ||
fn flatten(a: [[f32; 4]; 4]) -> [f32; 16] { | ||
unsafe { std::mem::transmute(a) } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::convert::{to_mat4}; | ||
use std::fs::OpenOptions; | ||
use std::io::{Read, Seek}; | ||
|
||
pub trait WebGlTriangles { | ||
// Allows rendering with gl.TRIANGLES | ||
fn to_gl_triangles_vertices(&self) -> Vec<f32>; | ||
fn to_gl_triangles_indices(&self) -> Vec<u32>; | ||
fn to_model_view_mat4(&self) -> [f32; 16]; | ||
} | ||
|
||
|
||
pub struct Cube { | ||
xmin : f32, | ||
xmax: f32, | ||
ymin: f32, | ||
ymax: f32, | ||
zmin: f32, | ||
zmax: f32, | ||
model_view: na::Matrix4<f32> | ||
} | ||
|
||
impl Cube { | ||
pub fn new(xmin: f32, ymin: f32, zmin: f32, xmax: f32, ymax: f32, zmax: f32, model_view: na::Matrix4<f32>) -> Self { | ||
Cube {xmin, xmax, ymin, ymax, zmin, zmax, model_view} | ||
} | ||
} | ||
|
||
impl WebGlTriangles for Cube { | ||
fn to_gl_triangles_vertices(&self) -> Vec<f32> { | ||
return vec![ | ||
self.xmin, self.ymin, self.zmin, // BLB 0 | ||
self.xmin, self.ymin, self.zmax, // BLF 1 | ||
self.xmin, self.ymax, self.zmin, // TLB 2 | ||
self.xmin, self.ymax, self.zmax, // TLF 3 | ||
self.xmax, self.ymin, self.zmin, // BRB 4 | ||
self.xmax, self.ymin, self.zmax, // BRF 5 | ||
self.xmax, self.ymax, self.zmin, // TRB 6 | ||
self.xmax, self.ymax, self.zmax, // TRF 7 | ||
]; | ||
} | ||
|
||
fn to_gl_triangles_indices(&self) -> Vec<u32> { | ||
return vec! [ | ||
1, 3, 7, 1, 5, 7, // Front face | ||
0, 2, 6, 0, 4, 6, // Back face | ||
2, 3, 7, 2, 6, 7, // Top face | ||
0, 1, 5, 0, 4, 5, // Bottom face | ||
4, 5, 7, 4, 6, 7, // Right face | ||
0, 1, 3, 0, 2, 3 // Left face | ||
]; | ||
} | ||
|
||
fn to_model_view_mat4(&self) -> [f32; 16] { | ||
to_mat4(&self.model_view) | ||
} | ||
} | ||
|
||
pub struct Mesh { | ||
vertices: Vec<f32>, | ||
triangles: Vec<u32>, | ||
model_view: na::Matrix4<f32> | ||
} | ||
|
||
|
||
impl Mesh { | ||
pub fn from_stl<F: Read + Seek>(file: F, model_view: na::Matrix4<f32>) -> Self { | ||
let stl = stl_io::read_stl(&mut file).unwrap(); | ||
|
||
|
||
let vertices = Vec::new(); | ||
let triangles = Vec::new(); | ||
|
||
return Mesh { | ||
vertices, triangles, model_view | ||
} | ||
} | ||
} | ||
|
||
impl WebGlTriangles for Mesh { | ||
fn to_gl_triangles_vertices(&self) -> Vec<f32> { | ||
return self.vertices.clone(); | ||
} | ||
|
||
fn to_gl_triangles_indices(&self) -> Vec<u32> { | ||
return self.triangles.clone(); | ||
} | ||
|
||
fn to_model_view_mat4(&self) -> [f32; 16] { | ||
to_mat4(&self.model_view) | ||
} | ||
} |
Oops, something went wrong.