From ed1f331a8572243c4d590b49cae095831620fe43 Mon Sep 17 00:00:00 2001 From: beppe Date: Tue, 18 Jun 2024 16:27:33 +0200 Subject: [PATCH] pfff --- .gitignore | 5 +++++ beppe_online/Cargo.toml | 29 +++++++++++++++++++++++++++++ beppe_online/src/lib.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 beppe_online/Cargo.toml create mode 100644 beppe_online/src/lib.rs diff --git a/.gitignore b/.gitignore index b0619ab..3778ea2 100644 --- a/.gitignore +++ b/.gitignore @@ -138,3 +138,8 @@ Cargo.lock **/*.rs.bk *.pdb + + +# Added by cargo + +/target diff --git a/beppe_online/Cargo.toml b/beppe_online/Cargo.toml new file mode 100644 index 0000000..033df70 --- /dev/null +++ b/beppe_online/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "beppe_online" +version = "0.1.0" +edition = "2021" +authors = ["Beppe Vanrolleghem beppe.vanrolleghem@gmail.com"] +description = "Testing out wasm" + +[lib] +crate-type = ["cdylib"] + + + +[dependencies] +wasm-bindgen = "0.2" +js-sys = "0.3" + +[dependencies.web-sys] +version = "0.3" +features = [ + 'Document', + 'Window', + 'HtmlCanvasElement', + 'WebGlRenderingContext', + 'WebGl2RenderingContext', + 'WebGlProgram', + 'WebGlShader', + 'WebGlBuffer', + 'WebGlUniformLocation' +] diff --git a/beppe_online/src/lib.rs b/beppe_online/src/lib.rs new file mode 100644 index 0000000..390a11d --- /dev/null +++ b/beppe_online/src/lib.rs @@ -0,0 +1,27 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; +use web_sys::{WebGlRenderingContext, WebGlShader, WebGlProgram}; +extern crate js_sys; + +pub fn init_webgl_context(canvas_id: &str) -> Result { + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id(canvas_id).unwrap(); + let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; + let gl: WebGlRenderingContext = canvas.get_context("webgl")? + .unwrap() + .dyn_into::() + .unwrap(); + + gl.viewport( + 0, + 0, + canvas.width().try_into().unwrap(), + canvas.height().try_into().unwrap(), + ); + + Ok(gl); +} + + +// https://blog.logrocket.com/implement-webassembly-webgl-viewer-using-rust/#setup-environment +pub fn create_