2 #[macro_use(uniform,program,implement_vertex)]
6 #[cfg(not(target_os = "emscripten"))]
8 #[cfg(not(target_os = "emscripten"))]
11 //use cgmath::prelude::*;
12 use cgmath::{Euler, Matrix4, Rad, Vector3};
14 use glium::{DisplayBuild, Surface};
15 use glutin::ElementState::Pressed;
16 use glutin::Event::KeyboardInput;
17 use glutin::VirtualKeyCode;
18 use std::os::raw::{c_int, c_void};
23 #[cfg(not(target_os = "emscripten"))]
25 #[cfg(target_os = "emscripten")]
26 #[path = "sound_emscripten.rs"]
30 fn screenshot(display : &glium::Display) {
31 let image: glium::texture::RawImage2d<u8> = display.read_front_buffer();
32 let image = image::ImageBuffer::from_raw(image.width, image.height, image.data.into_owned()).unwrap();
33 let image = image::DynamicImage::ImageRgba8(image).flipv();
34 let mut output = std::fs::File::create(&std::path::Path::new("screenshot.png")).unwrap();
35 image.save(&mut output, image::ImageFormat::PNG).unwrap();
38 #[allow(non_camel_case_types)]
39 type em_callback_func = unsafe extern fn();
41 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
44 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
45 std::cell::RefCell::new(std::ptr::null_mut()));
47 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() {
48 MAIN_LOOP_CALLBACK.with(|log| {
49 *log.borrow_mut() = &callback as *const _ as *mut c_void;
52 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
54 unsafe extern "C" fn wrapper<F>() where F : FnMut() {
55 MAIN_LOOP_CALLBACK.with(|z| {
56 let closure = *z.borrow_mut() as *mut F;
63 let _soundplayer = sound::start();
65 let display = glutin::WindowBuilder::new()
66 .with_dimensions(1024, 768)
67 //.with_fullscreen(glutin::get_primary_monitor())
68 .with_depth_buffer(24)
70 .with_title(format!("MandelWow"))
74 let mandelwow_program = mandelwow::program(&display);
75 let bounding_box_program = bounding_box::solid_fill_program(&display);
77 let mut camera = support::camera::CameraState::new();
79 let mut pause = false;
80 let mut bounding_box_enabled = true;
81 let mut fullscreen = true;
83 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
93 //support::start_loop(|| {
94 set_main_loop_callback(|| {
102 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
105 let wsize = wmax - wmin;
106 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
108 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
110 let mut frame = display.draw();
111 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
113 let rotation = cgmath::Matrix4::from(
114 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t)});
115 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
117 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation;
118 let model = cgmath::conv::array4x4(model2);
120 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
121 // so the lines behind the semi-translucent areas will be drawn.
122 if bounding_box_enabled {
123 let uniforms = uniform! {
125 view: camera.get_view(),
126 perspective: camera.get_perspective(),
128 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
131 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
132 frame.finish().unwrap();
134 for ev in display.poll_events() {
136 glutin::Event::Closed |
137 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
138 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
139 //return support::Action::Stop
141 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
142 bounding_box_enabled ^= true;
144 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
147 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
150 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
153 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
154 screenshot(&display);
156 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
159 // Not implemented on Linux
160 glutin::WindowBuilder::new()
161 .with_fullscreen(glutin::get_primary_monitor())
162 .with_depth_buffer(24)
163 .rebuild_glium(&display).unwrap();
165 //glutin::WindowBuilder::new()
166 // .rebuild_glium(&display).unwrap();
169 ev => camera.process_input(&ev),
173 //support::Action::Continue