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/src/app.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 WebInterface/wasm/asm-paint/src/app.rs (limited to 'WebInterface/wasm/asm-paint/src/app.rs') 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) { + } +} -- 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/app.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 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/app.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