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 fn gl_info(display : &glium::Display) {
39 let version = *display.get_opengl_version();
40 let api = match version {
41 glium::Version(glium::Api::Gl, _, _) => "OpenGL",
42 glium::Version(glium::Api::GlEs, _, _) => "OpenGL ES"
44 println!("{} context verson: {}", api, display.get_opengl_version_string());
47 #[allow(non_camel_case_types)]
48 type em_callback_func = unsafe extern fn();
50 fn emscripten_set_main_loop(func : em_callback_func, fps : c_int, simulate_infinite_loop : c_int);
53 thread_local!(static MAIN_LOOP_CALLBACK: std::cell::RefCell<*mut c_void> =
54 std::cell::RefCell::new(std::ptr::null_mut()));
56 pub fn set_main_loop_callback<F>(callback : F) where F : FnMut() {
57 MAIN_LOOP_CALLBACK.with(|log| {
58 *log.borrow_mut() = &callback as *const _ as *mut c_void;
61 unsafe { emscripten_set_main_loop(wrapper::<F>, 0, 1); }
63 unsafe extern "C" fn wrapper<F>() where F : FnMut() {
64 MAIN_LOOP_CALLBACK.with(|z| {
65 let closure = *z.borrow_mut() as *mut F;
72 let _soundplayer = sound::start();
74 let display = glutin::WindowBuilder::new()
75 .with_dimensions(600, 600)
76 //.with_fullscreen(glutin::get_primary_monitor())
77 .with_depth_buffer(24)
79 .with_title(format!("MandelWow"))
85 let mandelwow_program = mandelwow::program(&display);
86 let bounding_box_program = bounding_box::solid_fill_program(&display);
88 let mut camera = support::camera::CameraState::new();
90 let mut pause = false;
91 let mut bounding_box_enabled = true;
92 let mut fullscreen = true;
94 // These are the bounds of the 3D Mandelwow section which we render in 3-space.
104 //support::start_loop(|| {
105 set_main_loop_callback(|| {
113 // Vary the wow factor to slice the Mandelwow along its 4th dimension.
116 let wsize = wmax - wmin;
117 let wow = (((t * 0.7).sin() + 1.0) / 2.0) * wsize + wmin;
119 //println!("t={} w={:?} camera={:?}", t, w, camera.get_pos());
121 let mut frame = display.draw();
122 frame.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
124 let rotation = cgmath::Matrix4::from(
125 Euler { x: Rad(t.sin() / 3.), y: Rad(t.sin() / 2.), z: Rad(t)});
126 let z_trans = -2.0; // Send the model back a little bit so it fits the screen.
128 Matrix4::from_translation(Vector3::unit_z() * z_trans) * rotation;
129 let model = cgmath::conv::array4x4(model2);
131 // Draw the bounding box before the fractal, when the Z-buffer is still clear,
132 // so the lines behind the semi-translucent areas will be drawn.
133 if bounding_box_enabled {
134 let uniforms = uniform! {
136 view: camera.get_view(),
137 perspective: camera.get_perspective(),
139 bounding_box::draw(&display, &mut frame, &bounding_box_program, &uniforms, &bounds);
142 mandelwow::draw(&display, &mut frame, &mandelwow_program, model, &camera, &bounds, wow);
143 frame.finish().unwrap();
145 for ev in display.poll_events() {
147 glutin::Event::Closed |
148 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Escape)) |
149 KeyboardInput(Pressed, _, Some(VirtualKeyCode::Q)) => {
150 //return support::Action::Stop
152 KeyboardInput(Pressed, _, Some(VirtualKeyCode::B)) => {
153 bounding_box_enabled ^= true;
155 KeyboardInput(Pressed, _, Some(VirtualKeyCode::P)) => {
158 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageUp)) => {
161 KeyboardInput(Pressed, _, Some(VirtualKeyCode::PageDown)) => {
164 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F10)) => {
165 screenshot(&display);
167 KeyboardInput(Pressed, _, Some(VirtualKeyCode::F11)) => {
170 // Not implemented on Linux
171 glutin::WindowBuilder::new()
172 .with_fullscreen(glutin::get_primary_monitor())
173 .with_depth_buffer(24)
174 .rebuild_glium(&display).unwrap();
176 //glutin::WindowBuilder::new()
177 // .rebuild_glium(&display).unwrap();
180 ev => camera.process_input(&ev),
184 //support::Action::Continue