From d727f6e769ae6f3210a57f32f6c198469efaa9b8 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 25 May 2019 22:44:04 +0200 Subject: Run with lighttpd and remove deploy.py --- WebInterface/wasm/asm-paint/src/lib.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 462a89d..b6a5840 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -8,20 +8,14 @@ macro_rules! console_log { extern "C" { #[wasm_bindgen(js_namespace = console)] fn log(s: &str); + + #[wasm_bindgen(js_namespace = document)] + fn write(s: &str); } #[wasm_bindgen(start)] pub fn entry() { - use web_sys; - console_log!("hello {} wasm", 42); - - let window = web_sys::window().unwrap(); - - let document = window.document().unwrap(); - - let body = document.body().unwrap(); - - //body.set_inner_html("

Hello from WASM

"); + console_log!("hello {} wasm", 44); - body.set_inner_html("oho"); + write("gooo"); } -- cgit v1.2.3-70-g09d2 From e2e4f0f698b95ca979e78ac12fa2395635321dd3 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sat, 25 May 2019 23:47:02 +0200 Subject: Add colored logging with fern --- WebInterface/wasm/asm-paint/Cargo.toml | 20 +++++++++++++- WebInterface/wasm/asm-paint/index.html | 1 + WebInterface/wasm/asm-paint/src/client_logger.rs | 34 ++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/lib.rs | 20 +++++--------- 4 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/client_logger.rs (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index c0a7c68..e8f5acb 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "asm-paint-rs" version = "0.1.0" -authors = ["Dennis Kobert "] +authors = [ + "natrixaeria", + "Dennis Kobert " +] edition = "2018" [lib] @@ -9,3 +12,18 @@ crate-type = ["cdylib"] [dependencies] wasm-bindgen = "0.2" +log = "0.4" +fern = "0.5" + +[dependencies.web-sys] +version = "0.3" +features = [ + 'Document', + 'Element', + 'HtmlCanvasElement', + 'WebGlBuffer', + 'WebGlRenderingContext', + 'WebGlProgram', + 'WebGlShader', + 'Window' +] diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 46be213..631122d 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -6,5 +6,6 @@ + diff --git a/WebInterface/wasm/asm-paint/src/client_logger.rs b/WebInterface/wasm/asm-paint/src/client_logger.rs new file mode 100644 index 0000000..a8765c6 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/client_logger.rs @@ -0,0 +1,34 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace=console, js_name=log)] + fn __console_log_colored2(f: &str, c1: &str, c2: &str); +} + +fn log(rec: &log::Record) { + __console_log_colored2(&format!("{}", rec.args()), + &format!("color: {}", match rec.level() { + log::Level::Trace => "violet", + log::Level::Debug => "blue", + log::Level::Info => "green", + log::Level::Warn => "orange", + log::Level::Error => "red" + }), ""); +} + +pub fn init_logger() { + fern::Dispatch::new().format(|out, message, record|{ + out.finish(format_args!( + "%c{}%c {} > {}", + record.level(), + record.target(), + message + ) + ) + }) + .level(log::LevelFilter::Debug) + .chain(fern::Output::call(log)) + .apply().unwrap(); +} + diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index b6a5840..fea9d0f 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,21 +1,13 @@ -use wasm_bindgen::prelude::*; - -macro_rules! console_log { - ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) -} +mod client_logger; -#[wasm_bindgen] -extern "C" { - #[wasm_bindgen(js_namespace = console)] - fn log(s: &str); +use wasm_bindgen::prelude::*; - #[wasm_bindgen(js_namespace = document)] - fn write(s: &str); -} +#[macro_use] +extern crate log; #[wasm_bindgen(start)] pub fn entry() { - console_log!("hello {} wasm", 44); + client_logger::init_logger(); - write("gooo"); + info!("{}", 42); } -- cgit v1.2.3-70-g09d2 From fb31751fab32c8b67affd75603084e4f143bc758 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 16:09:03 +0200 Subject: Add basic project structure --- WebInterface/wasm/asm-paint/Cargo.toml | 2 +- WebInterface/wasm/asm-paint/run | 2 +- WebInterface/wasm/asm-paint/src/app.rs | 16 ++++++++++++++++ WebInterface/wasm/asm-paint/src/lib.rs | 7 ++++++- WebInterface/wasm/asm-paint/src/site.rs | 18 ++++++++++++++++++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/app.rs create mode 100644 WebInterface/wasm/asm-paint/src/site.rs (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index e8f5acb..e2b1e17 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -3,7 +3,7 @@ name = "asm-paint-rs" version = "0.1.0" authors = [ "natrixaeria", - "Dennis Kobert " + "TrueDoctor" ] edition = "2018" diff --git a/WebInterface/wasm/asm-paint/run b/WebInterface/wasm/asm-paint/run index 61c1997..1da1e35 100755 --- a/WebInterface/wasm/asm-paint/run +++ b/WebInterface/wasm/asm-paint/run @@ -1,3 +1,3 @@ #!/bin/sh -wasm-pack build --target web +wasm-pack build --release --target web diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs new file mode 100644 index 0000000..d5d0e45 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -0,0 +1,16 @@ +use crate::site::Site; + +pub struct App { + site: Site, +} + +impl App { + pub fn new() -> Option { + Some(Self { + site: Site::from_current()?, + }) + } + + pub fn run(&self) { + } +} diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index fea9d0f..037e8e9 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,6 @@ mod client_logger; +mod site; +mod app; use wasm_bindgen::prelude::*; @@ -9,5 +11,8 @@ extern crate log; pub fn entry() { client_logger::init_logger(); - info!("{}", 42); + info!("begin running wasm application"); + + let app = app::App::new().unwrap(); + app.run(); } diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs new file mode 100644 index 0000000..740cb90 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -0,0 +1,18 @@ +use web_sys; + +pub struct Site { + window: web_sys::Window, + document: web_sys::Document, +} + +impl Site { + pub fn from_current() -> Option { + let window = web_sys::window() + .or_else(|| {error!("unable to query window"); None})?; + let document = window.document() + .or_else(|| {error!("unable to query document"); None})?; + Some(Self { + window, document + }) + } +} -- cgit v1.2.3-70-g09d2 From 12dcfc7320b7fd2b9e72f34e7411a6632cc3e4e0 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 18:04:40 +0200 Subject: Create a canvas --- WebInterface/wasm/asm-paint/deploy | 2 ++ WebInterface/wasm/asm-paint/index.html | 10 +++++++++ WebInterface/wasm/asm-paint/src/app.rs | 2 ++ WebInterface/wasm/asm-paint/src/canvas.rs | 27 ++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/client_logger.rs | 18 +++++++++++++--- WebInterface/wasm/asm-paint/src/lib.rs | 1 + WebInterface/wasm/asm-paint/src/site.rs | 9 ++++++++ 7 files changed, 66 insertions(+), 3 deletions(-) create mode 100755 WebInterface/wasm/asm-paint/deploy create mode 100644 WebInterface/wasm/asm-paint/src/canvas.rs (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/deploy b/WebInterface/wasm/asm-paint/deploy new file mode 100755 index 0000000..13d1d0a --- /dev/null +++ b/WebInterface/wasm/asm-paint/deploy @@ -0,0 +1,2 @@ +#!/bin/sh +lighttpd -f ./lighttpd.config diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 631122d..5a221e3 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -4,6 +4,16 @@ Scribblio + diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs index d5d0e45..003a2a1 100644 --- a/WebInterface/wasm/asm-paint/src/app.rs +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -12,5 +12,7 @@ impl App { } pub fn run(&self) { + let canvas = self.site.create_canvas().unwrap(); + canvas.render(); } } diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs new file mode 100644 index 0000000..a1ef415 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -0,0 +1,27 @@ +use web_sys; +use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +use wasm_bindgen::JsCast; + +pub struct Canvas { + element: web_sys::HtmlCanvasElement, + ctx: WebGlRenderingContext, +} + +impl Canvas { + pub fn new(element: web_sys::Element) -> Option { + let element: web_sys::HtmlCanvasElement = + element.dyn_into::() + .ok()?; + debug!("create webgl2 context"); + error!("'{:#?}'", element.get_context("webgl2").ok()??.dyn_into::()); + let ctx = element.get_context("webgl2").ok()?? + .dyn_into::().ok()?; + Some(Self { + element, ctx + }) + } + + pub fn render(&self) { + info!("rich kidd"); + } +} diff --git a/WebInterface/wasm/asm-paint/src/client_logger.rs b/WebInterface/wasm/asm-paint/src/client_logger.rs index a8765c6..f71918f 100644 --- a/WebInterface/wasm/asm-paint/src/client_logger.rs +++ b/WebInterface/wasm/asm-paint/src/client_logger.rs @@ -2,12 +2,24 @@ use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { - #[wasm_bindgen(js_namespace=console, js_name=log)] - fn __console_log_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=debug)] + fn __console_debug_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=info)] + fn __console_info_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=warn)] + fn __console_warn_colored2(f: &str, c1: &str, c2: &str); + #[wasm_bindgen(js_namespace=console, js_name=error)] + fn __console_error_colored2(f: &str, c1: &str, c2: &str); } fn log(rec: &log::Record) { - __console_log_colored2(&format!("{}", rec.args()), + let log_fn = match rec.level() { + log::Level::Trace | log::Level::Debug => __console_debug_colored2, + log::Level::Info => __console_info_colored2, + log::Level::Warn => __console_warn_colored2, + log::Level::Error => __console_error_colored2, + }; + log_fn(&format!("{}", rec.args()), &format!("color: {}", match rec.level() { log::Level::Trace => "violet", log::Level::Debug => "blue", diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 037e8e9..8b0ee8f 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,5 @@ mod client_logger; +mod canvas; mod site; mod app; diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs index 740cb90..47afd19 100644 --- a/WebInterface/wasm/asm-paint/src/site.rs +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -1,4 +1,5 @@ use web_sys; +use crate::canvas::Canvas; pub struct Site { window: web_sys::Window, @@ -15,4 +16,12 @@ impl Site { window, document }) } + + pub fn create_canvas(&self) -> Option { + debug!("gain canvas element"); + let element = self.document.get_element_by_id("canvas") + .or_else(|| {error!("could not gain canvas element"); None})?; + Canvas::new(element) + .or_else(|| {error!("could not create a webgl2 canvas"); None}) + } } -- cgit v1.2.3-70-g09d2 From 77a41c3065aea3269f0c48da9cc736c404a624c0 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 18:15:48 +0200 Subject: Fix WebGl -> WebGl2 bug --- WebInterface/wasm/asm-paint/Cargo.toml | 6 +- WebInterface/wasm/asm-paint/src/canvas.rs | 10 +-- WebInterface/wasm/asm-paint/src/lib.rs | 119 ++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 9 deletions(-) (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index e2b1e17..748bb8e 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -14,6 +14,7 @@ crate-type = ["cdylib"] wasm-bindgen = "0.2" log = "0.4" fern = "0.5" +js-sys = "0.3" [dependencies.web-sys] version = "0.3" @@ -21,9 +22,6 @@ features = [ 'Document', 'Element', 'HtmlCanvasElement', - 'WebGlBuffer', - 'WebGlRenderingContext', - 'WebGlProgram', - 'WebGlShader', + 'WebGl2RenderingContext', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index a1ef415..1956be0 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -1,10 +1,10 @@ use web_sys; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; +use web_sys::{WebGl2RenderingContext}; use wasm_bindgen::JsCast; pub struct Canvas { element: web_sys::HtmlCanvasElement, - ctx: WebGlRenderingContext, + ctx: WebGl2RenderingContext, } impl Canvas { @@ -13,15 +13,15 @@ impl Canvas { element.dyn_into::() .ok()?; debug!("create webgl2 context"); - error!("'{:#?}'", element.get_context("webgl2").ok()??.dyn_into::()); let ctx = element.get_context("webgl2").ok()?? - .dyn_into::().ok()?; + .dyn_into::().ok()?; + info!("created webgl2 context successfully"); Some(Self { element, ctx }) } pub fn render(&self) { - info!("rich kidd"); + info!("do a barrel roll"); } } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 8b0ee8f..432c370 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -17,3 +17,122 @@ pub fn entry() { let app = app::App::new().unwrap(); app.run(); } + +/* +use js_sys::WebAssembly; +use wasm_bindgen::prelude::*; +use wasm_bindgen::JsCast; +use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; + +#[wasm_bindgen(start)] +pub fn start() -> Result<(), JsValue> { + let document = web_sys::window().unwrap().document().unwrap(); + let canvas = document.get_element_by_id("canvas").unwrap(); + let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; + + let context = canvas + .get_context("webgl")? + .unwrap() + .dyn_into::()?; + + let vert_shader = compile_shader( + &context, + WebGlRenderingContext::VERTEX_SHADER, + r#" + attribute vec4 position; + void main() { + gl_Position = position; + } + "#, + )?; + let frag_shader = compile_shader( + &context, + WebGlRenderingContext::FRAGMENT_SHADER, + r#" + void main() { + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); + } + "#, + )?; + let program = link_program(&context, &vert_shader, &frag_shader)?; + context.use_program(Some(&program)); + + let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; + let memory_buffer = wasm_bindgen::memory() + .dyn_into::()? + .buffer(); + let vertices_location = vertices.as_ptr() as u32 / 4; + let vert_array = js_sys::Float32Array::new(&memory_buffer) + .subarray(vertices_location, vertices_location + vertices.len() as u32); + + let buffer = context.create_buffer().ok_or("failed to create buffer")?; + context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); + context.buffer_data_with_array_buffer_view( + WebGlRenderingContext::ARRAY_BUFFER, + &vert_array, + WebGlRenderingContext::STATIC_DRAW, + ); + context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); + context.enable_vertex_attrib_array(0); + + context.clear_color(0.0, 0.0, 0.0, 1.0); + context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); + + context.draw_arrays( + WebGlRenderingContext::TRIANGLES, + 0, + (vertices.len() / 3) as i32, + ); + Ok(()) +} + +pub fn compile_shader( + context: &WebGlRenderingContext, + shader_type: u32, + source: &str, +) -> Result { + let shader = context + .create_shader(shader_type) + .ok_or_else(|| String::from("Unable to create shader object"))?; + context.shader_source(&shader, source); + context.compile_shader(&shader); + + if context + .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) + .as_bool() + .unwrap_or(false) + { + Ok(shader) + } else { + Err(context + .get_shader_info_log(&shader) + .unwrap_or_else(|| String::from("Unknown error creating shader"))) + } +} + +pub fn link_program( + context: &WebGlRenderingContext, + vert_shader: &WebGlShader, + frag_shader: &WebGlShader, +) -> Result { + let program = context + .create_program() + .ok_or_else(|| String::from("Unable to create shader object"))?; + + context.attach_shader(&program, vert_shader); + context.attach_shader(&program, frag_shader); + context.link_program(&program); + + if context + .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) + .as_bool() + .unwrap_or(false) + { + Ok(program) + } else { + Err(context + .get_program_info_log(&program) + .unwrap_or_else(|| String::from("Unknown error creating program object"))) + } +} +*/ -- cgit v1.2.3-70-g09d2 From 9f0f87a772743ce67e37700516cae0ead17cced9 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 26 May 2019 19:25:38 +0200 Subject: Create shaders --- WebInterface/wasm/asm-paint/Cargo.toml | 2 + WebInterface/wasm/asm-paint/src/app.rs | 7 ++-- WebInterface/wasm/asm-paint/src/canvas.rs | 20 ++++++++-- WebInterface/wasm/asm-paint/src/lib.rs | 3 +- WebInterface/wasm/asm-paint/src/shader.rs | 62 +++++++++++++++++++++++++++++++ WebInterface/wasm/asm-paint/src/site.rs | 3 +- 6 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 WebInterface/wasm/asm-paint/src/shader.rs (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/Cargo.toml b/WebInterface/wasm/asm-paint/Cargo.toml index 748bb8e..269d77d 100644 --- a/WebInterface/wasm/asm-paint/Cargo.toml +++ b/WebInterface/wasm/asm-paint/Cargo.toml @@ -23,5 +23,7 @@ features = [ 'Element', 'HtmlCanvasElement', 'WebGl2RenderingContext', + 'WebGlProgram', + 'WebGlShader', 'Window' ] diff --git a/WebInterface/wasm/asm-paint/src/app.rs b/WebInterface/wasm/asm-paint/src/app.rs index 003a2a1..005764d 100644 --- a/WebInterface/wasm/asm-paint/src/app.rs +++ b/WebInterface/wasm/asm-paint/src/app.rs @@ -11,8 +11,9 @@ impl App { }) } - pub fn run(&self) { - let canvas = self.site.create_canvas().unwrap(); - canvas.render(); + pub fn run(&mut self) { + let mut canvas = self.site.create_canvas().unwrap(); + canvas.init().unwrap(); + info!("canvas initialisation was succuessfull"); } } diff --git a/WebInterface/wasm/asm-paint/src/canvas.rs b/WebInterface/wasm/asm-paint/src/canvas.rs index 1956be0..ddf6d51 100644 --- a/WebInterface/wasm/asm-paint/src/canvas.rs +++ b/WebInterface/wasm/asm-paint/src/canvas.rs @@ -1,10 +1,12 @@ use web_sys; use web_sys::{WebGl2RenderingContext}; use wasm_bindgen::JsCast; +use crate::shader::Shaders; pub struct Canvas { element: web_sys::HtmlCanvasElement, ctx: WebGl2RenderingContext, + shaders: Shaders, } impl Canvas { @@ -17,11 +19,23 @@ impl Canvas { .dyn_into::().ok()?; info!("created webgl2 context successfully"); Some(Self { - element, ctx + element, ctx, + shaders: Shaders::new(), }) } - pub fn render(&self) { - info!("do a barrel roll"); + pub fn init(&mut self) -> Result<(), ()> { + debug!("create program"); + self.shaders.create_program(&self.ctx) + .map_err(|e| error!("webgl2 create program: {}", e))?; + debug!("create vertex shader"); + self.shaders.create_vertex_shader(&self.ctx) + .map_err(|e| error!("webgl2 create vertex shader: {}", e))?; + debug!("create fragment shader"); + self.shaders.create_fragment_shader(&self.ctx) + .map_err(|e| error!("webgl2 create fragment shader: {}", e))?; + debug!("compile shader program"); + self.shaders.compile(&self.ctx) + .map_err(|e| error!("webgl2 shader: {}", e)) } } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 432c370..945f2b1 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -1,4 +1,5 @@ mod client_logger; +mod shader; mod canvas; mod site; mod app; @@ -14,7 +15,7 @@ pub fn entry() { info!("begin running wasm application"); - let app = app::App::new().unwrap(); + let mut app = app::App::new().unwrap(); app.run(); } diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs new file mode 100644 index 0000000..ac7c767 --- /dev/null +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -0,0 +1,62 @@ +use web_sys::{WebGlProgram, WebGl2RenderingContext}; + +const VERTEX_SHADER: &str = r#"#version 300 es +in vec4 pos; +void main() { + gl_Position = pos; +} +"#; + +const FRAGMENT_SHADER: &str = r#"#version 300 es +precision mediump float; +out vec4 color; + +void main() { + color = vec4(1, 0, 0, 1); +} +"#; + +pub struct Shaders { + program: Option, +} + +impl Shaders { + pub fn new() -> Self { + Self { + program: None, + } + } + + pub fn create_program(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.program = Some(ctx.create_program().ok_or("could not create program id")?); + Ok(()) + } + + fn create_shader(&mut self, ctx: &WebGl2RenderingContext, + shader_type: u32, source: &str) -> Result<(), String> { + let program = self.program.as_ref().ok_or("could not find created program")?; + let shader = ctx.create_shader(shader_type) + .ok_or("could not create shader")?; + ctx.shader_source(&shader, source); + ctx.compile_shader(&shader); + let status = ctx.get_shader_parameter(&shader, WebGl2RenderingContext::COMPILE_STATUS); + if status == wasm_bindgen::JsValue::TRUE { + ctx.attach_shader(program, &shader); + Ok(()) + } else { + Err(format!("\n{}", ctx.get_shader_info_log(&shader).unwrap_or_default())) + } + } + + pub fn create_vertex_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::VERTEX_SHADER, VERTEX_SHADER) + } + + pub fn create_fragment_shader(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + self.create_shader(ctx, WebGl2RenderingContext::FRAGMENT_SHADER, FRAGMENT_SHADER) + } + + pub fn compile(&mut self, ctx: &WebGl2RenderingContext) -> Result<(), String> { + Ok(()) + } +} diff --git a/WebInterface/wasm/asm-paint/src/site.rs b/WebInterface/wasm/asm-paint/src/site.rs index 47afd19..4ae0237 100644 --- a/WebInterface/wasm/asm-paint/src/site.rs +++ b/WebInterface/wasm/asm-paint/src/site.rs @@ -22,6 +22,7 @@ impl Site { let element = self.document.get_element_by_id("canvas") .or_else(|| {error!("could not gain canvas element"); None})?; Canvas::new(element) - .or_else(|| {error!("could not create a webgl2 canvas"); None}) + .or_else(|| {error!("could not create a webgl2 canvas. + Your browser doesn't seem to support webgl2"); None}) } } -- cgit v1.2.3-70-g09d2 From a6d2d62fae44b6d8c96e51055f7222bc679efc48 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Wed, 29 May 2019 21:00:49 +0200 Subject: Reforge code snippets --- WebInterface/wasm/asm-paint/index.html | 3 + WebInterface/wasm/asm-paint/src/lib.rs | 119 ------------------------------ WebInterface/wasm/asm-paint/src/shader.rs | 6 +- 3 files changed, 7 insertions(+), 121 deletions(-) (limited to 'WebInterface/wasm/asm-paint/src/lib.rs') diff --git a/WebInterface/wasm/asm-paint/index.html b/WebInterface/wasm/asm-paint/index.html index 5a221e3..c4f3734 100644 --- a/WebInterface/wasm/asm-paint/index.html +++ b/WebInterface/wasm/asm-paint/index.html @@ -13,6 +13,9 @@ width: 100%; height: 100%; } + img { + background: violet; + } diff --git a/WebInterface/wasm/asm-paint/src/lib.rs b/WebInterface/wasm/asm-paint/src/lib.rs index 945f2b1..6c773c5 100644 --- a/WebInterface/wasm/asm-paint/src/lib.rs +++ b/WebInterface/wasm/asm-paint/src/lib.rs @@ -18,122 +18,3 @@ pub fn entry() { let mut app = app::App::new().unwrap(); app.run(); } - -/* -use js_sys::WebAssembly; -use wasm_bindgen::prelude::*; -use wasm_bindgen::JsCast; -use web_sys::{WebGlProgram, WebGlRenderingContext, WebGlShader}; - -#[wasm_bindgen(start)] -pub fn start() -> Result<(), JsValue> { - let document = web_sys::window().unwrap().document().unwrap(); - let canvas = document.get_element_by_id("canvas").unwrap(); - let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::()?; - - let context = canvas - .get_context("webgl")? - .unwrap() - .dyn_into::()?; - - let vert_shader = compile_shader( - &context, - WebGlRenderingContext::VERTEX_SHADER, - r#" - attribute vec4 position; - void main() { - gl_Position = position; - } - "#, - )?; - let frag_shader = compile_shader( - &context, - WebGlRenderingContext::FRAGMENT_SHADER, - r#" - void main() { - gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); - } - "#, - )?; - let program = link_program(&context, &vert_shader, &frag_shader)?; - context.use_program(Some(&program)); - - let vertices: [f32; 9] = [-0.7, -0.7, 0.0, 0.7, -0.7, 0.0, 0.0, 0.7, 0.0]; - let memory_buffer = wasm_bindgen::memory() - .dyn_into::()? - .buffer(); - let vertices_location = vertices.as_ptr() as u32 / 4; - let vert_array = js_sys::Float32Array::new(&memory_buffer) - .subarray(vertices_location, vertices_location + vertices.len() as u32); - - let buffer = context.create_buffer().ok_or("failed to create buffer")?; - context.bind_buffer(WebGlRenderingContext::ARRAY_BUFFER, Some(&buffer)); - context.buffer_data_with_array_buffer_view( - WebGlRenderingContext::ARRAY_BUFFER, - &vert_array, - WebGlRenderingContext::STATIC_DRAW, - ); - context.vertex_attrib_pointer_with_i32(0, 3, WebGlRenderingContext::FLOAT, false, 0, 0); - context.enable_vertex_attrib_array(0); - - context.clear_color(0.0, 0.0, 0.0, 1.0); - context.clear(WebGlRenderingContext::COLOR_BUFFER_BIT); - - context.draw_arrays( - WebGlRenderingContext::TRIANGLES, - 0, - (vertices.len() / 3) as i32, - ); - Ok(()) -} - -pub fn compile_shader( - context: &WebGlRenderingContext, - shader_type: u32, - source: &str, -) -> Result { - let shader = context - .create_shader(shader_type) - .ok_or_else(|| String::from("Unable to create shader object"))?; - context.shader_source(&shader, source); - context.compile_shader(&shader); - - if context - .get_shader_parameter(&shader, WebGlRenderingContext::COMPILE_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(shader) - } else { - Err(context - .get_shader_info_log(&shader) - .unwrap_or_else(|| String::from("Unknown error creating shader"))) - } -} - -pub fn link_program( - context: &WebGlRenderingContext, - vert_shader: &WebGlShader, - frag_shader: &WebGlShader, -) -> Result { - let program = context - .create_program() - .ok_or_else(|| String::from("Unable to create shader object"))?; - - context.attach_shader(&program, vert_shader); - context.attach_shader(&program, frag_shader); - context.link_program(&program); - - if context - .get_program_parameter(&program, WebGlRenderingContext::LINK_STATUS) - .as_bool() - .unwrap_or(false) - { - Ok(program) - } else { - Err(context - .get_program_info_log(&program) - .unwrap_or_else(|| String::from("Unknown error creating program object"))) - } -} -*/ diff --git a/WebInterface/wasm/asm-paint/src/shader.rs b/WebInterface/wasm/asm-paint/src/shader.rs index 2823cd4..3352bcf 100644 --- a/WebInterface/wasm/asm-paint/src/shader.rs +++ b/WebInterface/wasm/asm-paint/src/shader.rs @@ -1,13 +1,15 @@ use web_sys::{WebGlProgram, WebGl2RenderingContext}; -const VERTEX_SHADER: &str = r#"#version 300 es +const VERTEX_SHADER: &str = +r#"#version 300 es in vec4 pos; void main() { gl_Position = pos; } "#; -const FRAGMENT_SHADER: &str = r#"#version 300 es +const FRAGMENT_SHADER: &str = +r#"#version 300 es precision mediump float; out vec4 color; -- cgit v1.2.3-70-g09d2