From: Bernie Innocenti Date: Mon, 17 Apr 2017 02:18:05 +0000 (-0400) Subject: A pile of gross hacks, but works in a browser so it's all justified. X-Git-Tag: v0.6.0~10 X-Git-Url: https://codewiz.org/gitweb?p=mandelwow.git;a=commitdiff_plain;h=e523420da6df64609d595e1e6e902592c00f6a47 A pile of gross hacks, but works in a browser so it's all justified. --- diff --git a/Cargo.toml b/Cargo.toml index 82a174c..37d847a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,18 +5,29 @@ authors = ["Bernie Innocenti "] license = "GPL-3.0+" [profile.release] -lto = true -panic = 'abort' +#lto = true +#panic = 'abort' [dependencies] cgmath = "*" -glium = "*" +glium = "0.15.0" glutin = "*" genmesh = "0.4.1" image = "0.12.0" obj = { version = "0.5", features = ["usegenmesh"] } + +[target.'cfg(not(target_os = "emscripten"))'.dependencies] libxm = "1.0.0" sdl2 = "*" +backtrace = "0.2.2" +backtrace-sys = "0.1.10" + +[target.'cfg(target_os = "emscripten")'.dependencies] +glutin = "0.6.1" +backtrace = "0.2.3" + +[replace] +"backtrace:0.2.3" = { git = "https://github.com/badboy/backtrace-rs", branch = "emscripten-fix" } [[bin]] name = "mandelwow" diff --git a/debug.sh b/debug.sh new file mode 100755 index 0000000..27121aa --- /dev/null +++ b/debug.sh @@ -0,0 +1,4 @@ +set -e +cargo build --target asmjs-unknown-emscripten +cp target/asmjs-unknown-emscripten/debug/mandelwow.js . +emrun . diff --git a/index.html b/index.html new file mode 100644 index 0000000..00444fc --- /dev/null +++ b/index.html @@ -0,0 +1,190 @@ + + + + + + Emscripten-Generated Code + + + +
+
Downloading...
+ + + Resize canvas + Lock/hide mouse pointer     + + + + +
+ +
+ + +
+ +
+ + + + diff --git a/main.rs b/main.rs index 12a127b..2d68c82 100644 --- a/main.rs +++ b/main.rs @@ -1,11 +1,11 @@ -// Wow. Such fractal. - extern crate cgmath; #[macro_use(uniform,program,implement_vertex)] extern crate glium; extern crate glutin; extern crate image; +#[cfg(not(target_os = "emscripten"))] extern crate libxm; +#[cfg(not(target_os = "emscripten"))] extern crate sdl2; //use cgmath::prelude::*; @@ -15,10 +15,15 @@ use glium::{DisplayBuild, Surface}; use glutin::ElementState::Pressed; use glutin::Event::KeyboardInput; use glutin::VirtualKeyCode; +use std::os::raw::{c_int, c_void}; mod bounding_box; mod cube; mod mandelwow; +#[cfg(not(target_os = "emscripten"))] +mod sound; +#[cfg(target_os = "emscripten")] +#[path = "sound_emscripten.rs"] mod sound; mod support; @@ -30,12 +35,36 @@ fn screenshot(display : &glium::Display) { image.save(&mut output, image::ImageFormat::PNG).unwrap(); } +#[allow(non_camel_case_types)] +type em_callback_func = unsafe extern fn(); +extern { + fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int); +} + +thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> = + std::cell::RefCell::new(std::ptr::null_mut())); + +pub fn set_main_loop_callback(callback : F) where F : FnMut() { + MAIN_LOOP_CALLBACK.with(|log| { + *log.borrow_mut() = &callback as *const _ as *mut c_void; + }); + + unsafe { emscripten_set_main_loop(wrapper::, 0, 1); } + + unsafe extern "C" fn wrapper() where F : FnMut() { + MAIN_LOOP_CALLBACK.with(|z| { + let closure = *z.borrow_mut() as *mut F; + (*closure)(); + }); + } +} + fn main() { let _soundplayer = sound::start(); let display = glutin::WindowBuilder::new() - //.with_dimensions(1024, 768) - .with_fullscreen(glutin::get_primary_monitor()) + .with_dimensions(1024, 768) + //.with_fullscreen(glutin::get_primary_monitor()) .with_depth_buffer(24) .with_vsync() .with_title(format!("MandelWow")) @@ -61,7 +90,8 @@ fn main() { zmax: 1.1, }; - support::start_loop(|| { + //support::start_loop(|| { + set_main_loop_callback(|| { camera.update(); if !pause { @@ -106,7 +136,7 @@ fn main() { glutin::Event::Closed | KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) | KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => { - return support::Action::Stop + //return support::Action::Stop }, KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => { bounding_box_enabled ^= true; @@ -140,7 +170,6 @@ fn main() { } } - support::Action::Continue + //support::Action::Continue }); - } diff --git a/mandelwow.frag b/mandelwow.frag index 260e1bf..c2e4e2d 100644 --- a/mandelwow.frag +++ b/mandelwow.frag @@ -1,28 +1,25 @@ -#version 140 +#version 100 precision highp float; -in vec2 c; -in vec2 z; -out vec4 f_color; +varying vec2 c; +varying vec2 z; void main() { float zx = z.x; float zy = z.y; - int maxiter = 64; - int iter = maxiter; - while (iter > 0) { + const int maxiter = 64; + for (int iter = maxiter; iter > 0; iter--) { float zx2 = zx * zx; float zy2 = zy * zy; if (zx2 * zy2 > 4.0) { float index = float(iter) / float(maxiter); - f_color = vec4(index, 0.1, 1.0 - index / 2, 0.8 - index); + gl_FragColor = vec4(index, 0.1, 1.0 - index / 2.0, 0.8 - index); return; } zy = zx * zy * 2.0 + c.y; zx = zx2 - zy2 + c.x; - iter -= 1; } - f_color = vec4((sin(z.y) + 1.0) / 4, - (sin(z.x) + 1.0) / 4, - (sin(c.x) + 1.0) / 4, - 1.0); + gl_FragColor = vec4((sin(z.y) + 1.0) / 4.0, + (sin(z.x) + 1.0) / 4.0, + (sin(c.x) + 1.0) / 4.0, + 1.0); } diff --git a/mandelwow.rs b/mandelwow.rs index d3b86e6..8e759ef 100644 --- a/mandelwow.rs +++ b/mandelwow.rs @@ -30,7 +30,7 @@ fn mand(cx: f32, cy: f32) -> [f32; 3] { pub fn program(display: &glium::Display) -> glium::Program { return program!(display, - 140 => { + 100 => { vertex: include_str!("mandelwow.vert"), fragment: include_str!("mandelwow.frag"), }).unwrap(); diff --git a/mandelwow.vert b/mandelwow.vert index 9c3e5f8..0e67eda 100644 --- a/mandelwow.vert +++ b/mandelwow.vert @@ -1,11 +1,11 @@ -#version 140 +#version 100 uniform mat4 perspective; uniform mat4 view; uniform mat4 model; uniform vec2 z0; -in vec3 position; -out vec2 c; -out vec2 z; +attribute mediump vec3 position; +varying mediump vec2 c; +varying mediump vec2 z; void main() { mat4 modelview = view * model; diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..171d4f9 --- /dev/null +++ b/release.sh @@ -0,0 +1,4 @@ +set -e +cargo build --target asmjs-unknown-emscripten --release +cp target/asmjs-unknown-emscripten/release/mandelwow.js . +emrun . diff --git a/solid.frag b/solid.frag index 7d1a7c7..c7e5a51 100644 --- a/solid.frag +++ b/solid.frag @@ -1,7 +1,5 @@ -#version 140 - -out vec4 color; +#version 100 void main() { - color = vec4(1.0, 1.0, 1.0, 1.0); + gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); } diff --git a/solid.vert b/solid.vert index aaacaa9..e66723f 100644 --- a/solid.vert +++ b/solid.vert @@ -1,5 +1,5 @@ -#version 140 -in vec3 position; +#version 100 +attribute vec3 position; uniform mat4 perspective; uniform mat4 view; uniform mat4 model; diff --git a/sound_emscripten.rs b/sound_emscripten.rs new file mode 100644 index 0000000..2b3d2a3 --- /dev/null +++ b/sound_emscripten.rs @@ -0,0 +1,3 @@ +pub struct SoundPlayer {} + +pub fn start() -> SoundPlayer { SoundPlayer {} }