Skip to content

Commit

Permalink
Abandoning this attempt for now.
Browse files Browse the repository at this point in the history
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
peterbraden committed Apr 26, 2022
1 parent e5a91ba commit 0bdf6c2
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 92 deletions.
63 changes: 51 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ nalgebra = "0.30"
console_error_panic_hook = "0.1"
wasm-bindgen = "0.2.79"
js-sys = "0.3"
stl_io = "0.6.0"
wasm-bindgen-futures = "0.4.30"

[dependencies.web-sys]
version = "0.3"
Expand All @@ -27,4 +29,5 @@ features = [
'WebGlProgram',
'WebGlShader',
'Window',
'Response',
]
9 changes: 9 additions & 0 deletions src/convert.rs
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) }
}

33 changes: 32 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#![allow(unused_imports)]
#![allow(dead_code)]


mod webgl;
mod mesh;
mod convert;
extern crate nalgebra as na;
use na::{Vector3, Rotation3};
use crate::webgl::{RenderContext};

use std::task::Poll;
use wasm_bindgen_futures::JsFuture;
use std::future::Future;
use std::pin::Pin;

/*
use web_sys::console;
fn console_log(s: String){
Expand All @@ -14,6 +22,22 @@ fn console_log(s: String){
*/


fn fetch_model() -> web_sys::Response {
let window = web_sys::window().unwrap();
let resp_future = JsFuture::from(window.fetch_with_str(""));
let result;
loop {
match Pin::new(&mut resp_future).poll() {
Poll::Pending => { /* do nothing */ },
Poll::Ready(value) => {
result = value;
break;
}
}
}
return result
}

fn main() {
// When building for WASM, print panics to the browser console
#[cfg(target_arch = "wasm32")]
Expand All @@ -26,12 +50,19 @@ fn main() {
let view = na::Isometry3::look_at_rh(&eye, &target, &Vector3::y());
let projection: na::Perspective3<f32> = na::Perspective3::new(width as f32/height as f32, 0.4, 0.1, 10000.);

let stl = fetch_model();

let mut rc = RenderContext::new(width, height, projection.as_matrix() * view.to_homogeneous());

rc.add_object(
/*
Box::new(
webgl::Cube::new(-0.3, -0.3, -0.3, 0.6, 0.8, 0.8, na::Matrix4::identity())
mesh::Cube::new(-0.3, -0.3, -0.3, 0.6, 0.8, 0.8, na::Matrix4::identity())
)
*/
Box::new(
mesh::Mesh::from_stl(stl, na::Matrix4::identity())
)
);
rc.start();
}
Expand Down
92 changes: 92 additions & 0 deletions src/mesh.rs
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)
}
}

0 comments on commit 0bdf6c2

Please sign in to comment.